[pypy-commit] pypy cpyext-injection: Call the original version of the code from the injected version

arigo pypy.commits at gmail.com
Wed Oct 19 08:49:41 EDT 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: cpyext-injection
Changeset: r87873:c74dff9dcd31
Date: 2016-10-19 14:48 +0200
http://bitbucket.org/pypy/pypy/changeset/c74dff9dcd31/

Log:	Call the original version of the code from the injected version

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
@@ -10,12 +10,21 @@
     *(PyObjectFields + (("foo", rffi.INT),))))
 
 
+class Original:
+    def __init__(self, space):
+        pass
+
+
 @unwrap_spec(index=int)
 def injected_getitem(space, w_self, index):
-    py_obj = as_pyobj(space, w_self)
-    py_obj = rffi.cast(mytype_object, py_obj)
-    return space.wrap(index * rffi.getintfield(py_obj, "foo"))
-
+    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"))
+    else:
+        org = space.fromcache(Original)
+        return space.call_function(org.w_original_getitem, w_self,
+                                   space.wrap(index))
 
 injected_methods = {
     '__getitem__': interp2app(injected_getitem),
@@ -23,5 +32,7 @@
 
 def inject(space, name, dict_w, pto):
     assert name == 'test_module.test_mytype'
+    org = space.fromcache(Original)
+    org.w_original_getitem = dict_w['__getitem__']
     for key, value in injected_methods.items():
         dict_w[key] = space.wrap(value)
diff --git a/pypy/module/cpyext/test/test_injection.py b/pypy/module/cpyext/test/test_injection.py
--- a/pypy/module/cpyext/test/test_injection.py
+++ b/pypy/module/cpyext/test/test_injection.py
@@ -7,3 +7,4 @@
         module = self.import_module(name='injection')
         mything = module.test_mytype()
         assert mything[100] == 4200
+        assert mything[-100] == -100+42
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
@@ -456,7 +456,6 @@
     def __init__(self, space, pto):
         bases_w = space.fixedview(from_ref(space, pto.c_tp_bases))
         dict_w = {}
-        inject_operators(space, dict_w, pto)
 
         add_operators(space, dict_w, pto)
         convert_method_defs(space, dict_w, pto.c_tp_methods, self)
@@ -464,6 +463,7 @@
         convert_member_defs(space, dict_w, pto.c_tp_members, self)
 
         name = rffi.charp2str(pto.c_tp_name)
+        inject_operators(space, name, dict_w, pto)
         new_layout = (pto.c_tp_basicsize > rffi.sizeof(PyObject.TO) or
                       pto.c_tp_itemsize > 0)
 
@@ -965,8 +965,7 @@
         w_obj.mutated(None)
 
 
-def inject_operators(space, dict_w, pto):
-    name = rffi.charp2str(pto.c_tp_name)
+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)


More information about the pypy-commit mailing list