3
Usage: compilemodule.py <module-name>
5
Compiles the PyPy extension module from pypy/module/<module-name>/
6
into a regular CPython extension module.
11
from pypy.tool.error import debug
13
def compilemodule(modname, interactive=False, basepath='pypy.module'):
14
"Compile a PyPy module for CPython."
15
import pypy.rpython.rctypes.implementation
16
from pypy.objspace.cpy.objspace import CPyObjSpace
17
from pypy.objspace.cpy.function import reraise
18
from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
19
from pypy.translator.driver import TranslationDriver
20
from pypy.interpreter.error import OperationError
23
space.config.translating = True
24
ModuleClass = __import__(basepath + '.%s' % modname,
25
None, None, ['Module']).Module
26
module = ModuleClass(space, space.wrap(modname))
27
w_moduledict = module.getdict()
30
w_mod = CPyObjSpace.W_Object(mod)
32
## space.appexec([w_mod, w_moduledict],
34
## old = mod.__dict__.copy()
35
## for key in ['__name__', '__doc__', 'RPythonError']:
36
## newdict[key] = old[key]
37
## newdict['__rpython__'] = old
38
## mod.__dict__.clear()
39
## mod.__dict__.update(newdict)
41
# the same at interp-level:
42
w_moddict = space.getattr(w_mod, space.wrap('__dict__'))
43
w_old = space.call_method(w_moddict, 'copy')
44
space.call_method(w_moddict, 'clear')
45
space.setitem(w_moddict, space.wrap('__rpython__'), w_old)
46
for key in ['__name__', '__doc__', 'RPythonError']:
47
w_key = space.wrap(key)
49
w1 = space.getitem(w_old, w_key)
50
except OperationError:
53
space.setitem(w_moddict, w_key, w1)
54
space.call_method(w_moddict, 'update', w_moduledict)
56
except OperationError, e:
59
__init__.allow_someobjects = True
61
driver = TranslationDriver(extmod_name=modname)
62
driver.setup(__init__, [object], policy=CPyAnnotatorPolicy(space))
64
driver.proceed(['compile_c'])
72
return driver.cbuilder.c_ext_module
76
print >> sys.stderr, __doc__
78
c_ext_module = compilemodule(argv[1], interactive=True)
79
print 'Created %r.' % (c_ext_module.__file__,)
82
if __name__ == '__main__':