[Cython] Bug in print statement

Stefan Behnel stefan_ml at behnel.de
Wed May 9 18:44:11 CEST 2012


Vitja Makarov, 09.05.2012 18:31:
> Del statement inference enabled pyregr.test_descr testcase and it SIGSEGVs.
> Here is minimal example:
> 
> import unittest
> import sys
> 
> class Foo(unittest.TestCase):
>     def test_file_fault(self):
>         # Testing sys.stdout is changed in getattr...
>         test_stdout = sys.stdout
>         class StdoutGuard:
>             def __getattr__(self, attr):
>                 test_stdout.write('%d\n' % sys.getrefcount(self))
>                 sys.stdout =  test_stdout #sys.__stdout__
>                 test_stdout.write('%d\n' % sys.getrefcount(self))
>                 test_stdout.write('getattr: %r\n' % attr)
>                 test_stdout.flush()
>                 raise RuntimeError("Premature access to sys.stdout.%s" % attr)
>         sys.stdout = StdoutGuard()
>         try:
>             print "Oops!"
>         except RuntimeError:
>             pass
>         finally:
>             sys.stdout = test_stdout
> 
>     def test_getattr_hooks(self):
>         pass
> 
> from test import test_support
> test_support.run_unittest(Foo)
> 
> It works in python and sigsegvs in cython.
> It seems to me that the problem is StdoutGuard() is still used when
> its reference counter is zero since Python interpreter does
> Py_XINCREF() for file object and __Pyx_Print() doesn't.

Makes sense to change that, IMHO. An additional INCREF during something as
involved as a print() will not hurt anyone.

IIRC, I had the same problem with PyPy - guess I should have fixed it back
then instead of taking the lazy escape towards using the print() function.

Stefan


More information about the cython-devel mailing list