473,386 Members | 1,706 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,386 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 1824

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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.