473,910 Members | 7,379 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compile() and comments

I've noticed an odd behavior with compile() and code that does not
contain a trailing newline: if the last line is a comment inside of
any block, a syntax error is thrown, but if the last line is a non-
comment Python statement, there is no error. Here's an example (using
2.5.1 on OS X)
>>txt = """
.... def foo():
.... print 'bar' """
>>compcode = compile(t.strip (), "", "exec")
compcode
<code object <moduleat 0x79608, file "", line 2>
>>txt += " # Comment on last line"
compcode = compile(txt.str ip(), "", "exec")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 4
# Comment on last line
^
SyntaxError: invalid syntax
>>compcode = compile(txt.str ip() + "\n", "", "exec")
compcode
<code object <moduleat 0x79a40, file "", line 2>

Obviously the easy workaround is to add a newline and all is well, so
this isn't a show-stopper, but is this a bug?

-- Ed Leafe

Oct 13 '08 #1
6 1966
Hello Ed,

It is certainly an odd restriction, but the docs for compile [1] do
explicitly state that the input must be newline terminated.

When compiling multi-line statements, two caveats apply: line
endings must be represented by a single newline character ('\n'), and
the input must be terminated by at least one newline character.

I always throw an extra newline onto any input to compile. I think the
behaviour maybe for detecting incomplete statements when you pass in
the obscure 'don't imply dedent' flag for interactive interpreter
loops.

[1] http://www.python.org/doc/2.5.2/lib/built-in-funcs.html

Michael Foord

On Oct 13, 1:06 pm, Ed Leafe <e...@leafe.com wrote:
I've noticed an odd behavior with compile() and code that does not
contain a trailing newline: if the last line is a comment inside of
any block, a syntax error is thrown, but if the last line is a non-
comment Python statement, there is no error. Here's an example (using
2.5.1 on OS X)
>>txt = """
... def foo():
... print 'bar' """
>>compcode = compile(t.strip (), "", "exec")
>>compcode
<code object <moduleat 0x79608, file "", line 2>
>>txt += " # Comment on last line"
>>compcode = compile(txt.str ip(), "", "exec")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 4
# Comment on last line
^
SyntaxError: invalid syntax
>>compcode = compile(txt.str ip() + "\n", "", "exec")
>>compcode
<code object <moduleat 0x79a40, file "", line 2>

Obviously the easy workaround is to add a newline and all is well, so
this isn't a show-stopper, but is this a bug?

-- Ed Leafe

--
http://www.ironpythoninaction.com/
Oct 13 '08 #2
On Oct 13, 2008, at 8:35 AM, Fuzzyman wrote:
It is certainly an odd restriction, but the docs for compile [1] do
explicitly state that the input must be newline terminated.

Understood; what I found odd was that if the last non-newline-
terminated statement was *not* a comment, no error was thrown.
-- Ed Leafe

Oct 13 '08 #3
Ed Leafe wrote:
On Oct 13, 2008, at 8:35 AM, Fuzzyman wrote:
>It is certainly an odd restriction, but the docs for compile [1] do
explicitly state that the input must be newline terminated.


Understood; what I found odd was that if the last
non-newline-terminated statement was *not* a comment, no error was thrown.
It kind of lulls one to sleep, then WHAM.

As near as I can tell, 2.5.2 and 3.0c1 require the comment to be on a
separate line to raise an exception.
>>print (compile("def f():\n pass #haha",'','exec '))
<code object <moduleat 00AADAD0, file "", line 1>
>>print (compile("def f():\n pass\n#haha",'' ,'exec')) # or
print (compile("def f():\n pass\n #haha",'','exec '))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 3
#haha

http://bugs.python.org/issue1479099
'compile' built-in function failures when missing EOL

Ideally, doc should match behavior. Consistent rejection would be
better. Other implementations might do this.

I also included this issue in doc issue
As near as I can tell, for 2.5.2 and 3.0c1, the limitation on compile
only applies when the last line only contains a comment.
>>print (compile("def f():\n pass #haha",'','exec '))
<code object <moduleat 00AADAD0, file "", line 1>
>>print (compile("def f():\n pass\n#haha",'' ,'exec')) # or
print (compile("def f():\n pass\n #haha",'','exec '))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "", line 3
#haha

I would prefer more consistent behavior. I have opened a separate doc
issue that includes the documentation of this issue.
http://bugs.python.org/issue4118

Terry Jan Reedy

Oct 13 '08 #4
On Oct 13, 2008, at 7:20 PM, Terry Reedy wrote:
I would prefer more consistent behavior. I have opened a separate
doc issue that includes the documentation of this issue.
http://bugs.python.org/issue4118
Again, it was not a show-stopper by any means; more of a curiosity.
Thanks for verifying the inconsistency.

-- Ed Leafe

Oct 14 '08 #5
Ed Leafe wrote:
On Oct 13, 2008, at 8:35 AM, Fuzzyman wrote:
>It is certainly an odd restriction, but the docs for compile [1] do
explicitly state that the input must be newline terminated.


Understood; what I found odd was that if the last
non-newline-terminated statement was *not* a comment, no error was thrown.
What if the last line is indented?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 14 '08 #6
Steve Holden wrote:
Ed Leafe wrote:
>On Oct 13, 2008, at 8:35 AM, Fuzzyman wrote:
>>It is certainly an odd restriction, but the docs for compile [1] do
explicitly state that the input must be newline terminated.

Understood; what I found odd was that if the last
non-newline-terminated statement was *not* a comment, no error was thrown.
What if the last line is indented?
Unterminated last-line comments raise an exception indented or not. I
did not test 'regular' non-indented lines.

Oct 14 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
466
by: Vince | last post by:
I'm getting this message when I try and run a .net web app in the development system: CS0016: Could not write to output file 'c#:\windows nt\Microsoft..net framework\v1.1.4322\Temporary ASP.NET Files\ssrpttest\44d149b5\fdee4a07\c22wlbzl.dll' -- 'Access is denied. ' I just upgraded to visual studio .net 2003 and now none of my applications wil work!
65
4110
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the following error ______________________________ Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland d:\temp\complex\temp.cpp: Error E2333 d:\temp\complex\temp.cpp 73: Class member 'Complex::conjugate(Complex)' declared outside its class Error...
3
1488
by: Ruben Campos | last post by:
Greetings. Some time ago, I had writing a CVector <T, N> class, which implements an algebraic vector of arbitrary both dimension and scalar type. First, I defined the interface for the generic algebraic vector class. The problem I encountered there was that algebraic vectors of some concrete dimensions were susceptible to include some extra methods not included in the most generic case. For example, 3-dimensional vectors include the...
4
2064
by: Jari Aalto | last post by:
Please suggest comments how can I make this script to work from bash. Also how can I skip better the argument from command line without hte extra variable i? #!/bin/bash function compile () { python -c ' import os, sys, py_compile;
52
6849
by: entropy123 | last post by:
Hey all, I'm working with some legacy C code and I would like to compile it as a CPP file. I get the following error message: driver.cpp:87: cannot convert `void *' to `GenericStruct *' in assignment
0
1867
by: Fuzzyman | last post by:
Hello all, The following is a copy of a blog entry. It's asking a question about future statements and the built in compile function. I'd appreciate any pointers or comments about possible approaches. `Movable Python <http://www.voidspace.org.uk/python/movpy/>`_ supports running both Python scripts and ``.pyc`` bytecode files. It does this by compiling scripts to bytecode, or extracting the code object from bytecode files, and then...
26
2900
by: Martin Jørgensen | last post by:
Hi, I don't understand these errors I get: g++ Persort.cpp Persort.cpp: In function 'int main()': Persort.cpp:43: error: name lookup of 'j' changed for new ISO 'for' scoping Persort.cpp:37: error: using obsolete binding at 'j'
10
2027
by: Martin Jørgensen | last post by:
Hi, I got this piece of code, but I won't compile: #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// struct link //one element of list { int data; //data item
12
2774
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
0
9879
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11349
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9727
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7250
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4337
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3360
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.