Author: Richard Plangger <[email protected]>
Branch: cpyext-injection
Changeset: r87888:2952096be266
Date: 2016-10-20 11:22 +0200
http://bitbucket.org/pypy/pypy/changeset/2952096be266/

Log:    (arigato, plan_rich) injecting a type of a pypy object to delegate
        to a cpython type

diff --git a/pypy/module/cpyext/injection/_test_module.py 
b/pypy/module/cpyext/injection/_test_module.py
--- a/pypy/module/cpyext/injection/_test_module.py
+++ b/pypy/module/cpyext/injection/_test_module.py
@@ -21,18 +21,20 @@
 @unwrap_spec(index=int)
 def injected_getitem(space, w_self, index):
     if index > 0:
-        py_obj = as_pyobj(space, w_self)
-        py_obj = rffi.cast(mytype_object, py_obj)
-        return space.wrap(index * rffi.getintfield(py_obj, "foo"))
+        intval = space.int_w(w_self)
+        return space.wrap(index * intval)
     else:
         org = space.fromcache(Original)
         return space.call_function(org.w_original_getitem, w_self,
                                    space.wrap(index))
 
-class W_MyItem(W_IntObject):
-    pass
+class W_MyThing(W_IntObject):
+    def getclass(self, space):
+        org = space.fromcache(Original)
+        w_type = org.w_original_type
+        return w_type
 
-W_MyItem.typedef = TypeDef("mything", __doc__ = "foo")
+W_MyThing.typedef = TypeDef("mything", __doc__ = "foo")
 
 def mything_attach(space, py_obj, w_obj):
     py_mything = rffi.cast(mytype_object, py_obj)
@@ -40,13 +42,13 @@
 
 def mything_realize(space, obj):
     intval = rffi.cast(lltype.Signed, rffi.cast(mytype_object, obj).foo)
-    w_obj = W_MyItem(intval)
+    w_obj = W_MyThing(intval)
     track_reference(space, obj, w_obj)
     return w_obj
 
 @bootstrap_function
 def init_mything(space):
-    make_typedescr(W_MyItem.typedef,
+    make_typedescr(W_MyThing.typedef,
                    basestruct=mytype_object.TO,
                    attach=mything_attach,
                    realize=mything_realize)
@@ -58,7 +60,7 @@
         return space.call_function(org.w_original_make, space.wrap(arg))
     if arg == 25:
         org = space.fromcache(Original)
-        return space.wrap(W_MyItem(2))
+        return space.wrap(W_MyThing(arg))
     return space.w_Ellipsis
 
 
@@ -79,3 +81,9 @@
     org = space.fromcache(Original)
     org.w_original_make = w_func
     return space.wrap(interp_injected_make)
+
+def inject_module(space, w_mod, name):
+    assert name == 'injection'
+    org = space.fromcache(Original)
+    w_type = space.getattr(w_mod, space.wrap('test_mytype'))
+    org.w_original_type = w_type
diff --git a/pypy/module/cpyext/modsupport.py b/pypy/module/cpyext/modsupport.py
--- a/pypy/module/cpyext/modsupport.py
+++ b/pypy/module/cpyext/modsupport.py
@@ -9,6 +9,7 @@
     PyMethodDef, PyDescr_NewClassMethod, PyStaticMethod_New)
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
 from pypy.module.cpyext.state import State
+from pypy.module.cpyext.injection.injection import inject_global
 from pypy.interpreter.error import oefmt
 
 #@cpython_api([rffi.CCHARP], PyObject)
@@ -134,9 +135,3 @@
     raise NotImplementedError
 
 
-def inject_global(space, w_func, modulename, funcname):
-    if (not we_are_translated() and modulename == 'injection'
-          and funcname == 'make'):
-        from pypy.module.cpyext.injection._test_module import inject_global
-        w_func = inject_global(space, w_func, funcname)
-    return w_func
diff --git a/pypy/module/cpyext/state.py b/pypy/module/cpyext/state.py
--- a/pypy/module/cpyext/state.py
+++ b/pypy/module/cpyext/state.py
@@ -2,6 +2,7 @@
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.executioncontext import AsyncAction
+from pypy.module.cpyext.injection.injection import inject_module
 from rpython.rtyper.lltypesystem import lltype
 from rpython.rtyper.annlowlevel import llhelper
 from rpython.rlib.rdynload import DLLHANDLE
@@ -141,6 +142,8 @@
         w_dict = w_mod.getdict(space)
         w_copy = space.call_method(w_dict, 'copy')
         self.extensions[path] = w_copy
+        #
+        inject_module(space, w_mod, name)
 
 
 class PyObjDeallocAction(AsyncAction):
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -5,6 +5,7 @@
 from rpython.rlib.rstring import rsplit
 from rpython.rtyper.annlowlevel import llhelper
 from rpython.rtyper.lltypesystem import rffi, lltype
+from pypy.module.cpyext.injection.injection import inject_operators
 
 from pypy.interpreter.baseobjspace import W_Root, DescrMismatch
 from pypy.interpreter.error import oefmt
@@ -965,7 +966,3 @@
         w_obj.mutated(None)
 
 
-def inject_operators(space, name, dict_w, pto):
-    if not we_are_translated() and name == 'test_module.test_mytype':
-        from pypy.module.cpyext.injection._test_module import inject
-        inject(space, name, dict_w, pto)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to