compile() and comments

Terry Reedy tjreedy at udel.edu
Mon Oct 13 19:20:32 EDT 2008


Ed Leafe wrote:
> On Oct 13, 2008, at 8:35 AM, Fuzzyman wrote:
> 
>> It is certainly an odd restriction, but the docs for compile [1] do
>> explicitly state that the input must be newline terminated.
> 
> 
>     Understood; what I found odd was that if the last 
> non-newline-terminated statement was *not* a comment, no error was thrown.

It kind of lulls one to sleep, then WHAM.

As near as I can tell, 2.5.2 and 3.0c1 require the comment to be on a 
separate line to raise an exception.

 >>> 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

http://bugs.python.org/issue1479099
'compile' built-in function failures when missing EOL

Ideally, doc should match behavior.  Consistent rejection would be 
better.  Other implementations might do this.

I also included this issue in doc issue
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

Terry Jan Reedy




More information about the Python-list mailing list