[Cython] segfault due to using DECREF instead of XDECREF

Syam Gadde syam.gadde at duke.edu
Tue Jan 21 23:00:06 CET 2014

(apologies to the moderator for multiple postings -- I'm trying to figure out which of my email addresses is actually being exported to the public!)


It seems that cython's static code analysis is failing on the following
code.  It thinks that a particular variable (myobject2) must not be
NULL, but in fact it is, so the __Pyx_DECREF_SET segfaults.  This came
from third-party code, so I could fix it with an explicit initialization
to None, but I'd love to have Cython deal with it directly.  Thanks for
your help!

#<<<BEGIN tmpnone.py>>>
# When compiled with Cython 0.19.2 or 0.20dev, the following code
# segfaults on import

import random

def myfunc():
      ## uncommenting the following fixes things
      #myobject2 = None

      myfalse = random.random() > 2

      if myfalse:
          myobject = None
          myobject2 = None

      if not myfalse:
          # here Cython uses __Pyx_XDECREF_SET
          myobject = None

      print "Made it past myobject assignment"

      if not myfalse or myobject2 is None:
          # here Cython uses Pyx_DECREF_SET because it has determined
          # that __pyx_v_myobject2 can't be null, but it really, really can!
          # (if no one assigned myobject2 yet)
          myobject2 = None

      print "Made it past myobject2 assignment"

#<<<END tmpnone.py>>>

#<<BEGIN setup.py>>>
#<from distutils.core import setup
from Cython.Build import cythonize

      ext_modules = cythonize("tmpnone.py")
#<<END setup.py>>>

[prompt]$ python setup.py build_ext --inplace
[prompt]$ ls tmpnone.*
tmpnone.c  tmpnone.py  tmpnone.so
[prompt]$ python
Python 2.6.6 (r266:84292, Nov 21 2013, 12:39:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
  >>> import tmpnone
Made it past myobject assignment
Segmentation fault

More information about the cython-devel mailing list