Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r2203:e12558a3ce6b
Date: 2015-07-04 19:57 +0200
http://bitbucket.org/cffi/cffi/changeset/e12558a3ce6b/

Log:    Use the logic in cgc.c to implement ffi.gc() also for the pure
        Python in-line version of FFI

diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c
--- a/c/_cffi_backend.c
+++ b/c/_cffi_backend.c
@@ -5512,6 +5512,9 @@
                            (PyObject *)&CTypeDescr_Type);
 }
 
+/* forward: implemented in ffi_obj.c */
+static PyObject *b_gcp(PyObject *self, PyObject *args);
+
 /************************************************************/
 
 static char _testfunc0(char a, char b)
@@ -5817,6 +5820,7 @@
     {"newp_handle", b_newp_handle, METH_VARARGS},
     {"from_handle", b_from_handle, METH_O},
     {"from_buffer", b_from_buffer, METH_VARARGS},
+    {"gcp", b_gcp, METH_VARARGS},
 #ifdef MS_WIN32
     {"getwinerror", (PyCFunction)b_getwinerror, METH_VARARGS | METH_KEYWORDS},
 #endif
diff --git a/c/ffi_obj.c b/c/ffi_obj.c
--- a/c/ffi_obj.c
+++ b/c/ffi_obj.c
@@ -680,6 +680,19 @@
     return gc_weakrefs_build(self, cd, destructor);
 }
 
+static PyObject *b_gcp(PyObject *self, PyObject *args)
+{
+    /* for in-line mode */
+    static FFIObject *ffi1 = NULL;
+
+    if (ffi1 == NULL) {
+        ffi1 = ffi_internal_new(&FFI_Type, NULL);
+        if (ffi1 == NULL)
+            return NULL;
+    }
+    return ffi_gc(ffi1, args, NULL);
+}
+
 PyDoc_STRVAR(ffi_callback_doc,
 "Return a callback object or a decorator making such a callback object.\n"
 "'cdecl' must name a C function pointer type.  The callback invokes the\n"
diff --git a/cffi/api.py b/cffi/api.py
--- a/cffi/api.py
+++ b/cffi/api.py
@@ -327,6 +327,13 @@
         data.  Later, when this new cdata object is garbage-collected,
         'destructor(old_cdata_object)' will be called.
         """
+        try:
+            gcp = self._backend.gcp
+        except AttributeError:
+            pass
+        else:
+            return gcp(cdata, destructor)
+        #
         with self._lock:
             try:
                 gc_weakrefs = self.gc_weakrefs
diff --git a/testing/cffi0/backend_tests.py b/testing/cffi0/backend_tests.py
--- a/testing/cffi0/backend_tests.py
+++ b/testing/cffi0/backend_tests.py
@@ -1503,16 +1503,19 @@
 
     def test_gc_finite_list(self):
         ffi = FFI(backend=self.Backend())
+        public = not hasattr(ffi._backend, 'gcp')
         p = ffi.new("int *", 123)
         keepalive = []
         for i in range(10):
             keepalive.append(ffi.gc(p, lambda p: None))
-            assert len(ffi.gc_weakrefs.data) == i + 1  #should be a private 
attr
+            if public:
+                assert len(ffi.gc_weakrefs.data) == i + 1
         del keepalive[:]
         import gc; gc.collect(); gc.collect()
         for i in range(10):
             keepalive.append(ffi.gc(p, lambda p: None))
-        assert len(ffi.gc_weakrefs.data) == 10
+        if public:
+            assert len(ffi.gc_weakrefs.data) == 10
 
     def test_CData_CType(self):
         ffi = FFI(backend=self.Backend())
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to