[Cython] Bug in print statement

Vitja Makarov vitja.makarov at gmail.com
Wed May 9 18:31:51 CEST 2012


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.

-- 
vitja.


More information about the cython-devel mailing list