[Python-Dev] Bug 1184112 still valid

Nick Coghlan ncoghlan at gmail.com
Fri Mar 17 18:19:35 CET 2006


Terry Reedy wrote:
> "Fredrik Lundh" <fredrik at pythonware.com> wrote in message 
> news:dveini$ntv$1 at sea.gmane.org...
>>> http://sourceforge.net/tracker/index.php?func=detail&aid=1184112&group_id=5470&atid=105470
>>    "When compiling multi-line statements, two caveats apply: line
>>    endings must be represented by a single newline character ('\n'),
>>    and the input must be terminated by at least one newline character."
> 
> Relevant part of quote added to bug report.

One quirk though, is that omitting the newline actually works for some other 
statements (e.g. exec "if 1:\n  print 'Hello world!'" works just fine)

However, I don't think the quote is as relevant as one might think, because in 
the code that fails the multi-line statement is not only terminated by a 
newline, it's actually followed by a blank line as well. The line missing the 
newline is only a single-line statement (specifically, a line with only a 
comment).

Compare:

Py> exec """
... if 1:
...   print 'This works!'
...
... pass # Do nothing at all"""
This works!

With:

Py> exec """
... if 1:
...   print 'This breaks!'
...
... # Do nothing at all"""
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "<string>", line 5
     # Do nothing at all
                      ^
SyntaxError: invalid syntax

There's an easy workaround (adding the missing newline), but I think there's 
definitely something going wrong in the tokenizer.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list