Author: Richard Plangger <planri...@gmail.com> Branch: cpyext-injection Changeset: r87881:fbb30091e897 Date: 2016-10-19 18:40 +0200 http://bitbucket.org/pypy/pypy/changeset/fbb30091e897/
Log: (arigato, fijal, plan_rich) create a new type def. myitem, need to redirect properly diff --git a/pypy/module/cpyext/__init__.py b/pypy/module/cpyext/__init__.py --- a/pypy/module/cpyext/__init__.py +++ b/pypy/module/cpyext/__init__.py @@ -74,6 +74,7 @@ import pypy.module.cpyext.pystrtod import pypy.module.cpyext.pytraceback import pypy.module.cpyext.methodobject +import pypy.module.cpyext.injection._test_module # now that all rffi_platform.Struct types are registered, configure them api.configure_types() 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,8 +1,11 @@ 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.interpreter.typedef import unwrap_spec, TypeDef +from pypy.module.cpyext.pyobject import as_pyobj, make_typedescr, track_reference from pypy.module.cpyext.api import PyObjectFields +from pypy.interpreter import typedef +from pypy.objspace.std.intobject import W_IntObject +from pypy.module.cpyext.api import bootstrap_function mytype_object = lltype.Ptr(lltype.Struct( @@ -26,11 +29,36 @@ return space.call_function(org.w_original_getitem, w_self, space.wrap(index)) +class W_MyItem(W_IntObject): + pass + +W_MyItem.typedef = TypeDef("mything", __doc__ = "foo") + +def mything_attach(space, py_obj, w_obj): + py_mything = rffi.cast(mytype_object, py_obj) + rffi.setintfield(py_mything, 'foo', space.int_w(w_obj)) + +def mything_realize(space, obj): + intval = rffi.cast(lltype.Signed, rffi.cast(mytype_object, obj).foo) + w_obj = W_MyItem(intval) + track_reference(space, obj, w_obj) + return w_obj + +@bootstrap_function +def init_mything(space): + make_typedescr(W_MyItem.typedef, + basestruct=mytype_object.TO, + attach=mything_attach, + realize=mything_realize) + @unwrap_spec(arg=int) def injected_make(space, arg): if arg == 15: org = space.fromcache(Original) 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.w_Ellipsis 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 @@ -129,7 +129,6 @@ initinjection(void) #endif { - PyObject *d; #if PY_MAJOR_VERSION >= 3 PyObject *module = PyModule_Create(&moduledef); #else 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 @@ -11,6 +11,7 @@ return space.w_False cls.w_is_there_an_pyobj_version = cls.space.wrap( interp2app(is_there_an_pyobj_version)) + AppTestCpythonExtensionBase.setup_class.im_func(cls) def test_getitem_basic(self): module = self.import_module(name='injection') _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit