Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > strange syntax error

Reply
Thread Tools

strange syntax error

 
 
_wolf
Guest
Posts: n/a
 
      06-04-2010
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
 
Reply With Quote
 
 
 
 
Jerry Hill
Guest
Posts: n/a
 
      06-04-2010
On Fri, Jun 4, 2010 at 1:31 PM, _wolf <(E-Mail Removed)> wrote:
> ¬* ¬*File "<whatever>", line 6
> ¬* ¬* ¬*#
> ¬* ¬* ¬*^
> ¬*SyntaxError: invalid syntax


I believe you're encountering this bug:
http://bugs.python.org/issue1184112

It's been fixed for 2.7 and 3.2. Until then, you'll need to work
around it. You can either append a newline to the end of any source
snippets that you are exec-ing, or if you're writing the code snippets
that are being exec-ed yourself, don't write them in such a way that
they trigger the bug.

--
Jerry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange syntax error Qu0ll C++ 1 04-26-2009 10:06 AM
Syntax error? What syntax error? Assignment fo default values? Mark Richards Perl Misc 3 11-18-2007 05:01 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
Question About Strange 'C' Code Syntax ( Well strange to me anyway ) Harvey Twyman C Programming 8 10-25-2003 05:54 AM
Strange IRB Syntax Error Robert Klemme Ruby 2 06-27-2003 03:08 PM



Advertisments