Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange syntax error in python, version 2.6 and 3.1

this may not be an earth-shattering deficiency of python, but i still wonder about the rationale behind the following behavior: when i run

source = """
print( 'helo' )
if __name__ == '__main__':
  print( 'yeah!' )

#"""

print( compile( source, '<whatever>', 'exec' ) )

i get ::

  File "<whatever>", line 6
    #
    ^
SyntaxError: invalid syntax

i can avoid this exception by (1) deleting the trailing #; (2) deleting or outcommenting the if __name__ == '__main__':\n print( 'yeah!' ) lines; (3) add a newline to very end of the source.

moreover, if i have the source end without a trailing newline right behind the print( 'yeah!' ), the source will also compile without error.

i could also reproduce this behavior with python 2.6, so it’s not new to the 3k series.

i find this error to be highly irritating, all the more since when i put above source inside a file and execute it directly or have it imported, no error will occur—which is the expected behavior.

a # (hash) outside a string literal should always represent the start of a (possibly empty) comment in a python source; moreover, the presence or absence of a if __name__ == '__main__' clause should not change the interpretation of a soure on a syntactical level.

can anyone reproduce the above problem, and/or comment on the phenomenon?

cheers

like image 317
flow Avatar asked Jun 04 '10 18:06

flow


1 Answers

update

turns out this is indeed a bug as pointed out by http://groups.google.com/group/comp.lang.python/msg/b4842cc7abd75fe9; the bug report is at http://bugs.python.org/issue1184112; it appears to be fixed in 2.7 and 3.2.

solution

once recognized, this bug is extremely simple to fix: since a valid python source should stay both syntactically valid and semantically unchanged when a newline is added to the source text, just mechanically do just that to any source text. this reminds me of the ; semicolon you mechanically put in between source texts when assembling a multi-file javascript source for efficient gzipped delivery to the remote client.

like image 100
flow Avatar answered Oct 18 '22 10:10

flow