473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

compile function and future statements

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 calling ``exec``.

When you call `compile
<http://docs.python.org/dev/lib/built-in-funcs.html>`_ you can pass in
an optional ``flags`` argument which tell Python which future
statements to compile the code with. I currently don't do this, which
means that ``from __future__ import ...`` statements are ignored.

For Python 2.4.2 the most significant of these is `division
<http://www.python.org/doc/peps/pep-0238/>`_. Code which expects
integer division to yield floats is just plain broken if this future
statement is ignored.

I'd like to fix this in the next release of **Movable Python**.

First of all, I *assume* that the code objects contained in bytecode
files are already compiled with the relevant future statements. This
would mean that no further action is necessary. Can anyone confirm if
this is correct ? [#]_

For Python scripts there are a couple of possible approaches.

The simplest approach would be to use a simple regular expression to
find statements that look like
``from __future__ ...``. Unfortunately this could score false positives
for comments and docstrings. [#]_

Another alternative would be to parse the code into an abstract syntax
tree using the `compiler module
<http://docs.python.org/lib/module-compiler.html>`_. I would then have
to recognise the import statements in the nodes. I can see that there
is an `Import node
<http://docs.python.org/lib/module-compiler.ast.html>`_ does anyone
have any hints for me with this approach ? I would prefer a solution
that works across Python versions 2.2 to 2.5 and beyond.

Ominously, the ``compile.compile`` function has unsupported options for
passing in ``flags``, so it looks like it doesn't handle it
automatically from the parse tree.

... [#] It will actually be easy enough to test.
... [#] Future statements shuld only occur at the toplevel of code, and
in fact ought to occur before any other statements (although that was
broken in Python 2.4), so it ought to be possible to minimize false
positives a bit.

Mar 20 '06 #1
0 1827

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

Similar topics

4
by: Narendra C. Tulpule | last post by:
Hi, is there any way to allow recusrion in compile()? Something like: src_code = 'def fact(n):\n\tif n <= 1:\n\t\treturn 1\n\telse:' + \ '\n\t\treturn n * fact(n-1)\n\nprint fact(12)\n' cobj =...
8
by: janeaustine50 | last post by:
Python's InteractiveInterpreter uses the built-in compile function. According to the ref. manual, it doesn't seem to concern about the encoding of the source string. When I hand in an unicode...
9
by: exits funnel | last post by:
Hello, I was playing around a accidently realized that the following compiles: //BEGIN CODE class foo { public: void push(int* iptr) { } int* peek( )const { }
10
by: Jean-David Beyer | last post by:
I have some programs running on Red Hat Linux 7.3 working with IBM DB2 V6.1 (with all the FixPacks) on my old machine. I have just installed IBM DB2 V8.1 on this (new) machine running Red Hat...
4
by: Lodewijk Smit | last post by:
Hi, In the C standard of 1999 additional mathematical functions are added. For example, beside the traditional double sin(double x); there are also the functions: float sinf(float x);...
64
by: Morgan Cheng | last post by:
Hi All, I was taught that argument valuse is not supposed to be changed in function body. Say, below code is not good. void foo1(int x) { x ++; printf("x+1 = %d\n", x); } It should be...
0
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...
12
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() { // ... }
7
by: jmartmem | last post by:
Greetings, I have an ASP page with a form (form1) that contains a JavaScript validation function that is activated onSubmit. The function contains a series of IF statements to alert the user to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.