Skip to content

Instantly share code, notes, and snippets.

@troeger
Last active February 24, 2016 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troeger/c42051893b806de2db41 to your computer and use it in GitHub Desktop.
Save troeger/c42051893b806de2db41 to your computer and use it in GitHub Desktop.
A minimal example for calling the PyPy interpreter from CPython.
#!/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