classification
Title: 'compile' built-in function failures when missing EOL
Type: Stage:
Components: Interpreter Core Versions: Python 3.0, Python 2.7, Python 2.6, Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, oripel, terry.reedy, zseil
Priority: normal Keywords:

Created on 2006-04-29 22:00 by oripel, last changed 2009-11-12 23:58 by benjamin.peterson. This issue is now closed.

Files
File name Uploaded Description Edit
compile_comment_test.patch oripel, 2006-04-29 22:00 Added a test in Lib/test/test_compile.py
Messages (5)
msg28384 - (view) Author: Ori Peleg (oripel) Date: 2006-04-29 22:00
The 'compile' built-in function sometimes fails when
given a source string that doesn't end in an EOL.

The following example crashes Python 2.3, 2.4, and 2.5a1:

prompt> ./python2.5 -c "compile('def foo(x):\n
pass\n#abc', 'blah.py', 'exec')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "blah.py", line 3
    #abc
      ^
SyntaxError: invalid syntax

Attached is a patch to Lib/test/test_compile.py with a
test for this.
msg28385 - (view) Author: Ori Peleg (oripel) Date: 2006-04-29 22:07
Logged In: YES 
user_id=1131251

The case that led me to this was calling
'trace.find_executable_linenos' on source files with this
condition.

See also
https://opensvn.csie.org/traccgi/testoob/trac.cgi/ticket/206
msg28386 - (view) Author: Ziga Seilnacht (zseil) * (Python committer) Date: 2006-05-19 20:26
Logged In: YES 
user_id=1326842

This is a documented limitation of compile. The docs
say "the input must be terminated by at least one
newline character". See:
http://docs.python.org/lib/built-in-funcs.html#l2h-16

I would say that this is a bug in the trace module
(compare trace.find_executable_module and
py_compile.compile).
msg74713 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-10-13 23:19
As near as I can tell, for 2.5.2 and 3.0c1, the limitation on compile
only applies when the last line only contains a comment.

>>> print (compile("def f():\n  pass #haha",'','exec'))
<code object <module> at 00AADAD0, file "", line 1>

>>> print (compile("def f():\n  pass\n#haha",'','exec')) # or
>>> print (compile("def f():\n  pass\n  #haha",'','exec'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 3
    #haha

I would prefer more consistent behavior.  I have opened a separate doc
issue that includes the documentation of this issue.
http://bugs.python.org/issue4118
msg95165 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-11-12 23:58
Fixed in r76231.
History
Date User Action Args
2009-11-12 23:58:18benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg95165

resolution: fixed
2008-10-13 23:19:16terry.reedysetnosy: + terry.reedy
messages: + msg74713
versions: + Python 2.6, Python 3.0, Python 2.7
2006-04-29 22:00:38oripelcreate