Bug when using with_statement with exec

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matimus

    Bug when using with_statement with exec

    I think I'm going to create a new issue in Pythons issue database, but
    I wanted to run it by the news group first. See if I can get any
    useful feed back.

    The following session demonstrates the issue:

    Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
    (Intel)] on
    win32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>exec "def foo():\n return 0" # no ending newline works fine
    >>foo()
    0
    >>exec "def foo():\n return 1\n" # with an ending newline works fine
    >>foo()
    1
    >>from __future__ import with_statement
    >>exec "def foo():\n return 2\n" # with an ending newline works fine
    >>foo()
    2
    >>exec "def foo():\n return 3" # without an ending new line... breaks
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 2
    return 3
    ^
    SyntaxError: invalid syntax



    If I create a function by using exec on a string and _don't_ end the
    string with a new-line it will work just fine unless I "from
    __future__ import with_statement" . I can imagine that it is probably a
    good practice to always end function body's with a new-line, but the
    requirement should be consistent and the syntax error text could be a
    _little_ more descriptive (and flag something other than the 2nd to
    last character). Note that you don't need to be in the interpreter to
    reproduce this issue.

    I searched python's issue database and didn't see anything similar to
    this. If there is already an issue related to this, please point me to
    it... or better yet, let me know how you found it.

    Matt
  • Jerry Hill

    #2
    Re: Bug when using with_statement with exec

    On Mon, Jul 14, 2008 at 6:00 PM, Matimus <mccredie@gmail .comwrote:
    If I create a function by using exec on a string and _don't_ end the
    string with a new-line it will work just fine unless I "from
    __future__ import with_statement" .
    [snip]
    I searched python's issue database and didn't see anything similar to
    this. If there is already an issue related to this, please point me to
    it... or better yet, let me know how you found it.
    Possibly related to http://bugs.python.org/issue1184112, and/or


    Since you asked, I found it by googling the words "python exec with
    newline". The second item was an email on Python-Dev with a similar
    bug and referencing an issue id. The comments on that issue led to
    the other one.

    --
    Jerry

    Comment

    Working...