1. Python CFFI
  2. Untitled project
  3. cffi
  4. Issues
Issue #160 resolved

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;
+    }
     return x;

Comments (4)

  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. Armin Rigo
    • changed status to open

    Reopened: I realized it might still be helpful if we can dump a Python stack trace and abort, instead of blindly segfaulting. (But we must abort, not raise a Python exception, imho.)

  4. Log in to comment