compile function and future statements
fuzzyman at gmail.com
Mon Mar 20 13:29:42 CET 2006
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.
More information about the Python-list