[pypy-svn] r26712 - in pypy/dist/pypy: objspace/cpy rpython/rctypes/tool rpython/rctypes/tool/test translator/goal

arigo at codespeak.net arigo at codespeak.net
Wed May 3 10:55:55 CEST 2006


Author: arigo
Date: Wed May  3 10:55:53 2006
New Revision: 26712

Added:
   pypy/dist/pypy/rpython/rctypes/tool/test/test_compilemodule.py   (contents, props changed)
Modified:
   pypy/dist/pypy/objspace/cpy/capi.py
   pypy/dist/pypy/objspace/cpy/objspace.py
   pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py
   pypy/dist/pypy/translator/goal/targetdemomodule.py
Log:
(pedronis, arigo)  Finished tool/compilemodule.py.  Now we can say:

    python compilemodule.py _demo



Modified: pypy/dist/pypy/objspace/cpy/capi.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/capi.py	(original)
+++ pypy/dist/pypy/objspace/cpy/capi.py	Wed May  3 10:55:53 2006
@@ -94,6 +94,14 @@
 PyObject_Type.argtypes = [W_Object]
 PyObject_Type.restype = W_Object
 
+PyObject_Str = cpyapi.PyObject_Str
+PyObject_Str.argtyps = [W_Object]
+PyObject_Str.restype = W_Object
+
+PyObject_Repr = cpyapi.PyObject_Repr
+PyObject_Repr.argtyps = [W_Object]
+PyObject_Repr.restype = W_Object
+
 
 ###########################################################
 # ____________________ Number Protocol ____________________

Modified: pypy/dist/pypy/objspace/cpy/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/cpy/objspace.py	(original)
+++ pypy/dist/pypy/objspace/cpy/objspace.py	Wed May  3 10:55:53 2006
@@ -12,6 +12,7 @@
         self.options.geninterp = False
         self.w_int   = W_Object(int)
         self.w_tuple = W_Object(tuple)
+        self.w_str   = W_Object(str)
         self.w_None  = W_Object(None)
         self.w_False = W_Object(False)
         self.w_True  = W_Object(True)
@@ -69,6 +70,8 @@
     str_w   = staticmethod(PyString_AsString)
     iter    = staticmethod(PyObject_GetIter)
     type    = staticmethod(PyObject_Type)
+    str     = staticmethod(PyObject_Str)
+    repr    = staticmethod(PyObject_Repr)
 
     add     = staticmethod(PyNumber_Add)
     sub     = staticmethod(PyNumber_Subtract)

Modified: pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py
==============================================================================
--- pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py	(original)
+++ pypy/dist/pypy/rpython/rctypes/tool/compilemodule.py	Wed May  3 10:55:53 2006
@@ -7,14 +7,16 @@
 """
 
 import sys
-import pypy.rpython.rctypes.implementation
-from pypy.translator.goal.ann_override import PyPyAnnotatorPolicy
-from pypy.rpython.rctypes.tool.cpyobjspace import CPyObjSpace
-from pypy.translator.driver import TranslationDriver
 
 
 def compilemodule(modname):
     "Compile a PyPy module for CPython."
+    import pypy.rpython.rctypes.implementation
+    from pypy.objspace.cpy.objspace import CPyObjSpace
+    from pypy.objspace.cpy.wrappable import reraise
+    from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
+    from pypy.translator.driver import TranslationDriver
+    from pypy.interpreter.error import OperationError
 
     space = CPyObjSpace()
     ModuleClass = __import__('pypy.module.%s' % modname,
@@ -22,7 +24,42 @@
     module = ModuleClass(space, space.wrap(modname))
     w_moduledict = module.getdict()
 
-    XXX in-progress, for now see translator/goal/targetdemomodule.py
+    def __init__(mod):
+        w_mod = CPyObjSpace.W_Object(mod)
+        try:
+##          space.appexec([w_mod, w_moduledict],
+##            '''(mod, newdict):
+##                   old = mod.__dict__.copy()
+##                   for key in ['__name__', '__doc__', 'RPythonError']:
+##                       newdict[key] = old[key]
+##                   newdict['__rpython__'] = old
+##                   mod.__dict__.clear()
+##                   mod.__dict__.update(newdict)
+##            ''')
+            # the same at interp-level:
+            w_moddict = space.getattr(w_mod, space.wrap('__dict__'))
+            w_old = space.call_method(w_moddict, 'copy')
+            space.call_method(w_moddict, 'clear')
+            space.setitem(w_moddict, space.wrap('__rpython__'), w_old)
+            for key in ['__name__', '__doc__', 'RPythonError']:
+                w_key = space.wrap(key)
+                try:
+                    w1 = space.getitem(w_old, w_key)
+                except OperationError:
+                    pass
+                else:
+                    space.setitem(w_moddict, w_key, w1)
+            space.call_method(w_moddict, 'update', w_moduledict)
+
+        except OperationError, e:
+            reraise(e)
+
+    __init__.allow_someobjects = True
+
+    driver = TranslationDriver(extmod_name=modname)
+    driver.setup(__init__, [object], policy=CPyAnnotatorPolicy(space))
+    driver.proceed(['compile_c'])
+    return driver.cbuilder.c_ext_module
 
 
 if __name__ == '__main__':

Added: pypy/dist/pypy/rpython/rctypes/tool/test/test_compilemodule.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/rpython/rctypes/tool/test/test_compilemodule.py	Wed May  3 10:55:53 2006
@@ -0,0 +1,8 @@
+import py
+from pypy.rpython.rctypes.tool.compilemodule import compilemodule
+
+def test_demo():
+    mod = compilemodule('_demo')
+    res = mod.measuretime(1000, long)
+    assert type(res) is int
+    py.test.raises(mod.DemoError, mod.measuretime, -5, long)

Modified: pypy/dist/pypy/translator/goal/targetdemomodule.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetdemomodule.py	(original)
+++ pypy/dist/pypy/translator/goal/targetdemomodule.py	Wed May  3 10:55:53 2006
@@ -1,7 +1,9 @@
 from pypy.module._demo import Module, demo
 from pypy.objspace.cpy.ann_policy import CPyAnnotatorPolicy
 from pypy.objspace.cpy.objspace import CPyObjSpace
+from pypy.objspace.cpy.wrappable import reraise
 import pypy.rpython.rctypes.implementation
+from pypy.interpreter.error import OperationError
 
 
 space = CPyObjSpace()
@@ -10,8 +12,35 @@
 
 def __init__(mod):
     w_mod = CPyObjSpace.W_Object(mod)
-    w_dict = space.getattr(w_mod, space.wrap('__dict__'))
-    space.call_method(w_dict, 'update', w_moduledict)
+    try:
+##        space.appexec([w_mod, w_moduledict],
+##            '''(mod, newdict):
+##                   old = mod.__dict__.copy()
+##                   mod.__dict__.clear()
+##                   mod.__dict__['__rpython__'] = old
+##                   for key in ['__name__', '__doc__', 'RPythonError']:
+##                       if key in old:
+##                           mod.__dict__[key] = old[key]
+##                   mod.__dict__.update(newdict)
+##            ''')
+
+        # the same at interp-level:
+        w_moddict = space.getattr(w_mod, space.wrap('__dict__'))
+        w_old = space.call_method(w_moddict, 'copy')
+        space.call_method(w_moddict, 'clear')
+        space.setitem(w_moddict, space.wrap('__rpython__'), w_old)
+        for key in ['__name__', '__doc__', 'RPythonError']:
+            w_key = space.wrap(key)
+            try:
+                w1 = space.getitem(w_old, w_key)
+            except OperationError:
+                pass
+            else:
+                space.setitem(w_moddict, w_key, w1)
+        space.call_method(w_moddict, 'update', w_moduledict)
+
+    except OperationError, e:
+        reraise(e)
 __init__.allow_someobjects = True
 
 # _____ Define and setup target ___



More information about the Pypy-commit mailing list