[pypy-commit] pypy cpyext-injection: Works.

arigo pypy.commits at gmail.com
Wed Oct 19 08:15:37 EDT 2016


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

Log:	Works.

diff --git a/pypy/module/cpyext/injection/__init__.py b/pypy/module/cpyext/injection/__init__.py
new file mode 100644
diff --git a/pypy/module/cpyext/injection/_test_module.py b/pypy/module/cpyext/injection/_test_module.py
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/injection/_test_module.py
@@ -0,0 +1,17 @@
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.typedef import unwrap_spec
+
+
+ at unwrap_spec(index=int)
+def injected_getitem(space, w_self, index):
+    return space.wrap(index * 42)
+
+
+injected_methods = {
+    '__getitem__': interp2app(injected_getitem),
+}
+
+def inject(space, name, dict_w, pto):
+    assert name == 'test_module.test_mytype'
+    for key, value in injected_methods.items():
+        dict_w[key] = space.wrap(value)
diff --git a/pypy/module/cpyext/test/injection.c b/pypy/module/cpyext/test/injection.c
--- a/pypy/module/cpyext/test/injection.c
+++ b/pypy/module/cpyext/test/injection.c
@@ -124,6 +124,7 @@
 
     if (PyType_Ready(&mytype_type) < 0)
         INITERROR;
+    PyModule_AddObject(module, "test_mytype", (PyObject *)&mytype_type);
 #if PY_MAJOR_VERSION >=3
     return module;
 #endif
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
@@ -2,5 +2,8 @@
 
 
 class AppTestTypeObject(AppTestCpythonExtensionBase):
-    def test_module_exists(self):
+
+    def test_getitem_basic(self):
         module = self.import_module(name='injection')
+        mything = module.test_mytype()
+        assert mything[100] == 4200
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
@@ -1,7 +1,7 @@
 import os
 
 from rpython.rlib import jit
-from rpython.rlib.objectmodel import specialize
+from rpython.rlib.objectmodel import specialize, we_are_translated
 from rpython.rlib.rstring import rsplit
 from rpython.rtyper.annlowlevel import llhelper
 from rpython.rtyper.lltypesystem import rffi, lltype
@@ -967,5 +967,6 @@
 
 def inject_operators(space, dict_w, pto):
     name = rffi.charp2str(pto.c_tp_name)
-    if name == 'test_module.test_mytype':
-        pass #xxx
+    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