Issue #160 invalid

from_handle checks python object's validity

Xiaoqiang Wang
created an issue

Does it make sense to check wether the underneath python object is still valid?

diff -r 0157531d9a16 c/_cffi_backend.c
--- a/c/_cffi_backend.c Tue May 06 11:05:50 2014 +0200
+++ b/c/_cffi_backend.c Mon Jun 02 16:08:58 2014 +0200
@@ -5012,6 +5012,10 @@
         return NULL;
     }
     x = (PyObject *)(raw + 42);
+    if (Py_REFCNT(x) == 0) {
+        PyErr_SetString(PyExc_RuntimeError, "underneath Python object has been garbage collected");
+        return NULL;
+    }
     Py_INCREF(x);
     return x;
 }

Comments (2)

  1. Armin Rigo

    At least, not with an error message that isn't saying explicitly "oups, argh! you're lucky you didn't segfault, but I happened to be able to check that the pointer is pointing to random garbage."

  2. Armin Rigo

    Closing as "not a good idea to check": people might rely on getting this error, while in reality they would be lucky to get this error instead of a segmentation fault.

  3. Log in to comment