Author tzickel
Recipients benjamin.peterson, brett.cannon, gregory.p.smith, meador.inge, tzickel
Date 2015-09-18.16:41:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1442594476.99.0.72643074534.issue25083@psf.upfronthosting.co.za>
In-reply-to
Content
Not sure why nobody has responded yet, but I have gone up and made a patch for the problem for 2.7 HEAD. Would be great if someone with more understanding of python's source could say if this is the optimal place to do the ferror test.

I am able to see that this patch fixes the issue (now the import fails on EOF with an error instead of producing an empty valid AST and an empty code object .pyc file)

Unfortunately testing that the patch fixes the issue, currently involves LD_PRELOADing an dynamic library which hooks __srget (because thats what the getc macro in Py_UniversalNewlineFgets uses in posix systems) to return EOF, and ferror to return a non zero result:

If the code for the hooking dynamic library is needed, please tell me (I can't figure out how to make an automated test for it).

-----

shell$ cat a.py
print 'hi'

Before fixing python:

shell$ ls a.py*
a.py
shell$ DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=libblah.dylib python
Python 2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['BORK_IO_TEST'] = 'a' # this activates the EOF / ferror
>>> import a # this should print 'hi' or fail but does not...
>>> a
<module 'a' from 'a.py'>
>>> exit()
shell$ ls a.py*
a.py   a.pyc

You can see that it accepted a.py as an empty file and a bad a.pyc was created.

After the patch:
shell$ ls a.py*
a.py

shell$ DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=libblah.dylib ./python.exe
Python 2.7.10+ (2.7:f6125114b55f+, Sep 18 2015, 19:18:34)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['BORK_IO_TEST'] = 'a'
>>> import a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "a.py", line 1

    ^
SyntaxError: unexpected EOF while parsing
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
>>> exit()
shell$ ls a.py*
a.py

Now the import failed, and of course no empty code .pyc file was created.
History
Date User Action Args
2015-09-18 16:41:17tzickelsetrecipients: + tzickel, brett.cannon, gregory.p.smith, benjamin.peterson, meador.inge
2015-09-18 16:41:16tzickelsetmessageid: <1442594476.99.0.72643074534.issue25083@psf.upfronthosting.co.za>
2015-09-18 16:41:16tzickellinkissue25083 messages
2015-09-18 16:41:16tzickelcreate