Author: fijal
Branch: cpyext-injection
Changeset: r87918:83c4556ba270
Date: 2016-10-24 18:42 +0200
http://bitbucket.org/pypy/pypy/changeset/83c4556ba270/
Log: improve attachment of array and numpy array type
diff --git a/pypy/module/cpyext/injection/injection.py
b/pypy/module/cpyext/injection/injection.py
--- a/pypy/module/cpyext/injection/injection.py
+++ b/pypy/module/cpyext/injection/injection.py
@@ -3,10 +3,10 @@
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)
+ return inject(space, name, dict_w, pto)
if name == 'numpy.ndarray':
from pypy.module.cpyext.injection.numpy import inject_operator
- inject_operator(space, name, dict_w, pto)
+ return inject_operator(space, name, dict_w, pto)
def inject_global(space, w_func, modulename, funcname):
if (not we_are_translated() and modulename == 'injection'
diff --git a/pypy/module/cpyext/injection/numpy.py
b/pypy/module/cpyext/injection/numpy.py
--- a/pypy/module/cpyext/injection/numpy.py
+++ b/pypy/module/cpyext/injection/numpy.py
@@ -29,18 +29,17 @@
self.injected_methods_w.append((key, space.wrap(value)))
class W_ArrayObject(W_Root):
- pass
+ def getclass(self, space):
+ return space.fromcache(Original).w_array_type
W_ArrayObject.typedef = TypeDef("ndarray")
class W_Float64Object(W_FloatObject):
def getclass(self, space):
- org = space.fromcache(Original)
- w_type = org.w_float64_type
- return w_type
+ return space.fromcache(Original).w_float64_type
def array_realize(space, obj):
- intval = rffi.cast(lltype.Signed, rffi.cast(PyArrayObject, obj).foo)
- w_obj = W_ArrayObject(intval)
+ w_obj = W_ArrayObject()
+ w_obj.pyobj = rffi.cast(PyArrayObject, obj)
track_reference(space, obj, w_obj)
return w_obj
@@ -52,7 +51,8 @@
@unwrap_spec(index=int)
def injected_getitem(space, w_self, index):
- py_obj = rffi.cast(PyArrayObject, as_pyobj(space, w_self))
+ assert isinstance(w_self, W_ArrayObject)
+ py_obj = rffi.cast(PyArrayObject, w_self.pyobj)
if index < 0 or index >= py_obj.dimensions[0]:
raise oefmt(space.w_IndexError, "index out of bounds")
data = rffi.cast(rffi.DOUBLEP, py_obj.data)
@@ -68,6 +68,7 @@
org.w_original_getitem = dict_w['__getitem__']
for key, w_value in org.injected_methods_w:
dict_w[key] = w_value
+ return W_ArrayObject.typedef
def inject_module(space, w_mod, name):
assert name == 'numpy.core.multiarray'
@@ -75,5 +76,8 @@
w_type = space.appexec([w_mod], """(mod):
return mod.typeinfo['DOUBLE'][-1]
""")
+ w_array_type = space.getattr(w_mod, space.wrap('ndarray'))
+ assert isinstance(w_array_type, W_TypeObject)
assert isinstance(w_type, W_TypeObject)
org.w_float64_type = w_type
+ org.w_array_type = w_array_type
\ No newline at end of file
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
@@ -464,12 +464,14 @@
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)
+ newtypedef = inject_operators(space, name, dict_w, pto)
new_layout = (pto.c_tp_basicsize > rffi.sizeof(PyObject.TO) or
- pto.c_tp_itemsize > 0)
+ pto.c_tp_itemsize > 0 or newtypedef is not None)
W_TypeObject.__init__(self, space, name,
bases_w or [space.w_object], dict_w, force_new_layout=new_layout)
+ if newtypedef is not None:
+ self.layout.typedef = newtypedef
self.flag_cpytype = True
self.flag_heaptype = False
# if a sequence or a mapping, then set the flag to force it
@@ -774,7 +776,6 @@
try:
w_obj = _type_realize(space, py_obj)
finally:
- name = rffi.charp2str(pto.c_tp_name)
pto.c_tp_flags &= ~Py_TPFLAGS_READYING
pto.c_tp_flags |= Py_TPFLAGS_READY
return w_obj
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit