Bläddra i källkod

implemented python_import() and get_reference_count()

Fabian Peter Hammerle 9 år sedan
förälder
incheckning
9c4164c99d
1 ändrade filer med 31 tillägg och 0 borttagningar
  1. 31 0
      jack.c

+ 31 - 0
jack.c

@@ -28,6 +28,37 @@ static PyTypeObject port_type = {
     PyObject_HEAD_INIT(NULL)
     };
 
+static PyObject* python_import(const char* name)
+{
+    PyObject* python_name = PyString_FromString(name);
+    PyObject* object = PyImport_Import(python_name);
+    Py_DECREF(python_name);
+    return object;
+}
+
+long get_reference_count(PyObject* object)
+{
+    PyObject* sys = python_import("sys");
+    PyObject* getrefcount = PyObject_GetAttrString(sys, "getrefcount");
+    // Return the reference count of the object.
+    // The count returned is generally one higher than you might expect,
+    // because it includes the (temporary) reference as an argument to getrefcount().
+    PyObject* argument_list = PyTuple_Pack(1, object);
+    PyObject* result = PyObject_CallObject(getrefcount, argument_list);
+    Py_DECREF(argument_list);
+    long count;
+    if(!result) {
+        PyErr_PrintEx(0);
+        count = -2;
+    } else {
+        count = PyInt_AsLong(result);
+        Py_DECREF(result);
+    }
+    Py_DECREF(getrefcount);
+    Py_DECREF(sys);
+    return count;
+}
+
 static void jack_registration_callback(jack_port_id_t port_id, int registered, void* arg)
 {
     // register: non-zero if the port is being registered, zero if the port is being unregistered