~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/rctypes/tool/compilemodule.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
"""
 
3
Usage:  compilemodule.py <module-name>
 
4
 
 
5
Compiles the PyPy extension module from  pypy/module/<module-name>/
 
6
into a regular CPython extension module.
 
7
"""
 
8
import autopath
 
9
import sys
 
10
 
 
11
from pypy.tool.error import debug
 
12
 
 
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
 
21
 
 
22
    space = CPyObjSpace()
 
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()
 
28
 
 
29
    def __init__(mod):
 
30
        w_mod = CPyObjSpace.W_Object(mod)
 
31
        try:
 
32
##          space.appexec([w_mod, w_moduledict],
 
33
##            '''(mod, newdict):
 
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)
 
40
##            ''')
 
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)
 
48
                try:
 
49
                    w1 = space.getitem(w_old, w_key)
 
50
                except OperationError:
 
51
                    pass
 
52
                else:
 
53
                    space.setitem(w_moddict, w_key, w1)
 
54
            space.call_method(w_moddict, 'update', w_moduledict)
 
55
 
 
56
        except OperationError, e:
 
57
            reraise(e)
 
58
 
 
59
    __init__.allow_someobjects = True
 
60
 
 
61
    driver = TranslationDriver(extmod_name=modname)
 
62
    driver.setup(__init__, [object], policy=CPyAnnotatorPolicy(space))
 
63
    try:
 
64
        driver.proceed(['compile_c'])
 
65
    except SystemExit:
 
66
        raise
 
67
    except:
 
68
        if not interactive:
 
69
            raise
 
70
        debug(driver)
 
71
        raise SystemExit(1)
 
72
    return driver.cbuilder.c_ext_module
 
73
 
 
74
def main(argv):
 
75
    if len(argv) != 2:
 
76
        print >> sys.stderr, __doc__
 
77
        sys.exit(2)
 
78
    c_ext_module = compilemodule(argv[1], interactive=True)
 
79
    print 'Created %r.' % (c_ext_module.__file__,)
 
80
 
 
81
 
 
82
if __name__ == '__main__':
 
83
    main(sys.argv)