Last active
February 24, 2016 21:36
-
-
Save troeger/c42051893b806de2db41 to your computer and use it in GitHub Desktop.
A minimal example for calling the PyPy interpreter from CPython.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
A minimal example for calling the PyPy interpreter from CPython. | |
Derived from pyinteractive.py. | |
This only intended to understand what is going on when. | |
PyPy triggers arbitary C compiler runs in between ... | |
''' | |
source="print 'Hello World'" | |
# The shorter way: | |
# | |
#from pypy.interpreter import main | |
#main.run_string(source) | |
print "### Importing standard object space ###" | |
from pypy.objspace.std import StdObjSpace | |
print "### Importing interpreter code ###" | |
from pypy.interpreter import eval, module | |
print "### Creating object space ###" | |
space = StdObjSpace() | |
w = space.wrap | |
print "### Starting byte code compilation ###" | |
w_code = space.builtin.call('compile', w(source), w('<string>'), w('exec'), w(0), w(0)) | |
pycode = space.interp_w(eval.Code, w_code) | |
print "### Configuring space ###" | |
mainmodule = module.Module(space, space.wrap('__main__')) | |
w_globals = mainmodule.w_dict | |
print "### Running it ###" | |
retval = pycode.exec_code(space, w_globals, w_globals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment