[pypy-commit] pypy cpyext-injection: Next level of injection

arigo pypy.commits at gmail.com
Wed Oct 19 08:40:11 EDT 2016


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

Log:	Next level of injection

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
@@ -1,10 +1,20 @@
+from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import unwrap_spec
+from pypy.module.cpyext.pyobject import as_pyobj
+from pypy.module.cpyext.api import PyObjectFields
+
+
+mytype_object = lltype.Ptr(lltype.Struct(
+    'mytype_object',
+    *(PyObjectFields + (("foo", rffi.INT),))))
 
 
 @unwrap_spec(index=int)
 def injected_getitem(space, w_self, index):
-    return space.wrap(index * 42)
+    py_obj = as_pyobj(space, w_self)
+    py_obj = rffi.cast(mytype_object, py_obj)
+    return space.wrap(index * rffi.getintfield(py_obj, "foo"))
 
 
 injected_methods = {
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
@@ -9,13 +9,15 @@
 static PyObject *
 mytype_item(mytype_object *o, Py_ssize_t i)
 {
-    return PyInt_FromLong(i + 42);
+    return PyInt_FromLong(i + o->foo);
 }
 
 static PyObject *
 mytype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    return type->tp_alloc(type, 0);
+    mytype_object *o = (mytype_object *)type->tp_alloc(type, 0);
+    o->foo = 42;
+    return (PyObject *)o;
 }
 
 static PySequenceMethods mytype_as_sequence = {


More information about the pypy-commit mailing list