9

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

6
  • I was able to reproduce it. Bizarre - I'm interested in the answer too. Commented Jun 4, 2010 at 18:39
  • Reproduced in 2.6.1. It does seem to be a bug to me too, but it's a pretty extreme edge case. Commented Jun 4, 2010 at 18:50
  • 3
    You could put your "update" as the answer and accept that. Commented Jun 4, 2010 at 18:52
  • you may call it an edge-case but it does have the power to become a show-stopper until intense head-scratching and fumbling-araound lets you go on with your daily work; insofar fixing it is worth-while. the problem here is that it is so obscure and hard to google. Commented Jun 4, 2010 at 18:52
  • 1
    You found the solution! Good for you, but the question is 'stuck' as unanswered. Perhaps you could post the solution as an answer to your own question, and accept it later on? :) Commented Jun 4, 2010 at 20:02

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.