strange syntax error

Discussion in 'Python' started by _wolf, Jun 4, 2010.

  1. _wolf

    _wolf Guest

    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
    _wolf, Jun 4, 2010
    #1
    1. Advertising

  2. _wolf

    Jerry Hill Guest

    On Fri, Jun 4, 2010 at 1:31 PM, _wolf <> 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
    Jerry Hill, Jun 4, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Harvey Twyman
    Replies:
    8
    Views:
    562
    August Derleth
    Oct 25, 2003
  2. Qu0ll

    Strange syntax error

    Qu0ll, Apr 25, 2009, in forum: C++
    Replies:
    1
    Views:
    468
    James Kanze
    Apr 26, 2009
  3. Robert Klemme

    Strange IRB Syntax Error

    Robert Klemme, Jun 27, 2003, in forum: Ruby
    Replies:
    2
    Views:
    195
    Robert Klemme
    Jun 27, 2003
  4. Good Night Moon
    Replies:
    9
    Views:
    280
    Rick DeNatale
    Jul 25, 2007
  5. Mark Richards
    Replies:
    3
    Views:
    310
    Tad McClellan
    Nov 18, 2007
Loading...

Share This Page