From: Benjamin Peterson on
Iuri <iurisilvio <at> gmail.com> writes:
> Any ideas about what is happening?

Until Python 2.7/3.2, compile() does't like sources which don't end in a newline.



From: Steven D'Aprano on
On Wed, 19 May 2010 22:31:31 +0000, Benjamin Peterson wrote:

> Iuri <iurisilvio <at> gmail.com> writes:
>> Any ideas about what is happening?
>
> Until Python 2.7/3.2, compile() does't like sources which don't end in a
> newline.


Are you sure about that?


>>> x = compile("print __import__('sys').version, 'see ma, no newline'",
"", "single")
>>> exec x
2.5 (r25:51908, Nov 6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] see ma, no newline


The original post isn't showing up for me, so I don't know the full
context, but it seems to me that compile can deal with the lack of
newline.


>>> x = compile("""import sys
.... print sys.version
.... print 'see ma, still no final newline!'""", "", "exec")
>>> exec x
2.5 (r25:51908, Nov 6 2007, 16:54:01)
[GCC 4.1.2 20070925 (Red Hat 4.1.2-27)]
see ma, still no final newline!




--
Steven
From: Jerry Hill on
On Wed, May 19, 2010 at 8:52 PM, Steven D'Aprano
<steve-REMOVE-THIS(a)cybersource.com.au> wrote:
> On Wed, 19 May 2010 22:31:31 +0000, Benjamin Peterson wrote:
>> Iuri <iurisilvio <at> gmail.com> writes:
>>> Any ideas about what is happening?
>>
>> Until Python 2.7/3.2, compile() does't like sources which don't end in a
>> newline.

It appears to be this bug: http://bugs.python.org/issue1184112 which
is still open. Maybe a fix got checked in without the bug being
closed? I don't have 2.7 or 3.2 here to test with.

> The original post isn't showing up for me, so I don't know the full
> context, but it seems to me that compile can deal with the lack of
> newline.

The original example was:
compile("for i in [1,2,3]:\n pass\n#end", "test_file.py", "exec")

Bug 1184112 is only triggered with an indented block followed by a
comment with no newline following the comment.

--
Jerry
 | 
Pages: 1
Prev: struct
Next: Python Script for Website Mirroring