a proof-of-concept for embedding Python 3

2013.Apr.30

The following code is from a real proof-of-concept that I completed this evening.

The proof-of-concept is unfinished, but the following actually, really works.

I’ll blog about this more later this week and try to address some limitations.

The following was cut and paste from my interpreter.

This is not a good idea, but this is real code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ ls
lib  __python3__.c  __python3__.so
$ ls lib
libpython2.7.so  libpython2.7.so.1.0  libpython3.3dm.so  libpython3.3dm.so.1.0
$ LD_LIBRARY_PATH=lib python
Python 2.7.4 (default, Apr 27 2013, 14:06:30) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import __python3__
>>> __python3__.__file__
'__python3__.so'
>>> from sys import version_info; print (version_info)
sys.version_info(major=2, minor=7, micro=4, releaselevel='final', serial=0)
>>> __python3__.eval('from sys import version_info; print (version_info)')
sys.version_info(major=3, minor=3, micro=1, releaselevel='final', serial=0)
>>> from os import getpid; print (getpid())
19086
>>> __python3__.eval('from os import getpid; print (getpid())')
19086

No changes to Python 2.7 interpreter necessary.

Python 3.3 runs from within a C module (shared object) within the same process (no shelling out; same memory space.)

Next time: 1. why!? 2. the strategy behind this 3. object shims to interact with Python3 objects from Python2 4. garbage collector hooks to resolve Python3>-<Python2 circular dependencies 5. object filter for Python3 c-modules for use with the above