469,945 Members | 2,262 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

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
Jul 14 '08 #1
1 1218
On Mon, Jul 14, 2008 at 6:00 PM, Matimus <mc******@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
http://bugs.python.org/issue501622?

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
Jul 14 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andrew Crowe | last post: by
1 post views Thread by Rasjid Wilcox | last post: by
4 posts views Thread by comp.lang.php | last post: by
10 posts views Thread by David C. Ullrich | last post: by
reply views Thread by Chris Rebert | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.