[Cython] Bug in NULL handling introduced 0.14.1-1

Chris Colbert sccolbert at gmail.com
Sun Feb 13 21:09:39 CET 2011

I have cython file which is using PyObject_GenericSetAttr which is defined
as follows in my pyx file:

cdef extern from "Python.h":
    int PyObject_GenericSetAttr(object, object, object) except -1

Now in my script I am using that function to generically delete an attribute
by passing a NULL as the last value (this is proper way to trigger a generic
delattr in the Python c-api) with the following line:

PyObject_GenericSetAttr(self, name, <object>NULL)  # need to cast to object
or Cython won't compile

In Cython 0.14.1-1 this generates the following code:

  __pyx_t_3 = __pyx_v_self;
  __pyx_t_2 = __pyx_v_name;
  __pyx_t_4 = ((PyObject *)NULL);
  __pyx_t_5 = PyObject_GenericSetAttr(__pyx_t_3, __pyx_t_2, __pyx_t_4); if
(unlikely(__pyx_t_5 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno =
124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;
  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;

This causes a segfault because the NULL is getting increfed via Py_INCREF
instead of Py_XINCREF.

This wasnt a problem in Cython 0.13-1 which generated the following code:

  __pyx_t_3 = PyObject_GenericSetAttr(__pyx_v_self, __pyx_v_name, ((PyObject
*)NULL)); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[0];
__pyx_lineno = 124; __pyx_clineno = __LINE__; goto __pyx_L1_error;}

and doesn't attempt to INCREF the NULL.

Is there a workaround for my current situation?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20110213/410bb352/attachment.html>

More information about the cython-devel mailing list