[Cython] Bug in print statement

Vitja Makarov vitja.makarov at gmail.com
Wed May 9 18:57:14 CEST 2012


2012/5/9 Stefan Behnel <stefan_ml at behnel.de>:
> 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.
>

I've moved printing function to Utility/ and fixed refcount bug, if
jenkins is ok I'm gonna push this commit to master

https://github.com/vitek/cython/commit/83eceb31b4ed9afc0fd6d24c9eda5e52d9420535


-- 
vitja.


More information about the cython-devel mailing list