Skip to main content

test / develop / integrate python 3.0 code natively under robust, software-rich python 2.5 environment

Project description

################################################################################
REVISION DATE: 20080630

AUTHOR: kai zhu - kaizhu@hpc.usc.edu

HOMEPAGE: http://www-rcf.usc.edu/~kaizhu/work/py3to2

NOTE:

this is a working proof-of-concept intended for developers and hackers.
i have built and run it successfully under redhat and winxp-cygwin.

ABSTRACT:

py3to2 is a minimal patch/module that backports python 3k opcode instructions
to python 2.x. this allows python 2.x to run python 3k code natively @ the
bytecode-level.

the motivation is that 3k is buggy & lacks many useful 2.x modules. the lack
of 3rd-party modules in particular (eg scipy, PIL, ...) is a hindrance to 3k
development/testing. py3to2 addresses these issues by allowing 3k code to b
developed / integrated / run natively under the robust, software-rich, 2.x
environment.


MECHANISM:

1. py3to2 starts up a 3k server w/ pipe io
2. u input 3k code as a string into py3to2 which pipes it to the 3k server
3. the 3k server bytecompiles the string into a 3k codeobject
4. the 3k codeobject is serialized & piped back to py3to2
5. py3to2 unserializes the return pipe to a 2.x++ codeobject
6. the 2.x++ codeobject is exec/eval'd normally w/ the addition of the
backported 3k opcodes.

in theory, this mechanism should transparently implement any bytecode-level 3k
feature. performance and robustness should b minimally impacted, since the
compiled code is directly run as native 2.x++ bytecode.

3K FEATURES VERIFIED TO WORK:

pep3104 Access to Names in Outer Scopes
pep3105 Make print a function
pep3129 Class Decorators
pep3132 Extended Iterable Unpacking

3K FEATURES WHICH WILL NOT WORK:

* pep 3102 - keyword-only arguments
this 3k feature requires altering 2.x opcodes & c-structs in a non-backward
compatible way (there is a pure python backport however, thru pre-parsing &
dynamic programming, that i hope to make publicly available).

* 3k builtins/extension modules (bytes, bytearray, ctypes, ...)
...obviously anything that can't b implemented using opcodes alone (namely
c-structs). the bright side is most of these c-extensions can b easily emulated
to ur heart's content in pure python (this patch takes care of the hard stuff
like faithfully implementing 3k language syntax & sugar ;)

REQUIREMENTS:

* python2.5.2 or python2.6b1 src (to b patched)
* python3.x executable (to act as a 3k server)

py3to2 files: (download from http://www-rcf.usc.edu/~kaizhu/work/py3to2)
* ceval.c (patched w/ 3k opcodes)
* bltinmodue.c (patched w/ __build_class__ function)
* py3to2.py & _py3to2.py

BUILD/INSTALLATION:

* replace python2.x/Python/ceval.c, build & install
(eg './configure --prefix=/installdir; make clean; make; make install')

* copy py3to2.py & _py3to2.py to /installdir/.../site-packages/

* copy/link python3.x executable to python3k (eg 'ln -s python3.x python3k') &
make sure its in ur $PATH var

USAGE:
1. import py3to2
2. this will give u a codeobject compiler: 'py3to2.compile_py3k'
(similar to __builtin__.compile - type help(compile) for help info)

example demonstrating pep3132 extended unpacking syntax: a,b,*c = 1,2,3,4

Python 2.5.2 (r252:60911, Jun 27 2008, 21:19:51)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> import py3to2
py3to2 server restarting with io: (4, 5)
py3to2 server: Python 3.0b1 (r30b1:64395, Jun 24 2008, 21:53:55)
py3to2 server: [GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
py3to2 server: Type "help", "copyright", "credits" or "license" for more
information.
py3to2 server: <module 'py3to2' from 'py3to2.py'>

>>> source = "a,b,*c = 1,2,3,4"
>>> codeobject = py3to2.compile_py3k(source,"","exec")
1 0 LOAD_CONST 5 ((1, 2, 3, 4))
3 UNPACK_EX 2
6 STORE_NAME 0 (a)
9 STORE_NAME 1 (b)
12 STORE_NAME 2 (c)
15 LOAD_CONST 4 (None)
18 RETURN_VALUE

>>> exec(codeobject)
>>> print a, b, c
1, 2, [3, 4]
>>>



CHANGELOG:

20080630
added __build_class__ function to bltmodule.c. tested class decorators to b
working.

Project details


Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page