Author: Carl Friedrich Bolz <[email protected]>
Branch: cpyext-injection
Changeset: r87930:64b5d71654d3
Date: 2016-10-25 14:43 +0200
http://bitbucket.org/pypy/pypy/changeset/64b5d71654d3/

Log:    injected typedefs need a new mechanism for the mapping typedef ->
        w_type

        (next problem is ndarray.__new__ return the wrong type)

diff --git a/pypy/interpreter/typedef.py b/pypy/interpreter/typedef.py
--- a/pypy/interpreter/typedef.py
+++ b/pypy/interpreter/typedef.py
@@ -12,10 +12,14 @@
 
 
 class TypeDef(object):
+    _immutable_fields_ = ["w_type_injected?"]
+
     def __init__(self, __name, __base=None, __total_ordering__=None,
                  __buffer=None, **rawdict):
         "NOT_RPYTHON: initialization-time only"
         self.name = __name
+        self.injected_type = False
+        self.w_type_injected = None
         if __base is None:
             bases = []
         elif isinstance(__base, tuple):
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
@@ -3,7 +3,8 @@
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.typedef import unwrap_spec, TypeDef
 from pypy.interpreter.error import oefmt
-from pypy.module.cpyext.pyobject import as_pyobj, make_typedescr, 
track_reference
+from pypy.module.cpyext.pyobject import from_ref, make_typedescr,\
+     track_reference, PyObject
 from pypy.module.cpyext.api import PyObjectFields
 from pypy.module.cpyext.api import bootstrap_function
 from pypy.objspace.std.floatobject import W_FloatObject
@@ -30,15 +31,20 @@
 
 class W_ArrayObject(W_Root):
     def getclass(self, space):
-        return space.fromcache(Original).w_array_type
+        if type(self) is W_ArrayObject:
+            return space.fromcache(Original).w_array_type
+        return W_Root.getclass(self, space)
 W_ArrayObject.typedef = TypeDef("ndarray")
+W_ArrayObject.typedef.injected_type = True
+W_ArrayObject.typedef.acceptable_as_base_class = True
 
 class W_Float64Object(W_FloatObject):
     def getclass(self, space):
         return space.fromcache(Original).w_float64_type
 
 def array_realize(space, obj):
-    w_obj = W_ArrayObject()
+    w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
+    w_obj = space.allocate_instance(W_ArrayObject, w_type)
     w_obj.pyobj = rffi.cast(PyArrayObject, obj)
     track_reference(space, obj, w_obj)
     return w_obj
@@ -80,4 +86,4 @@
     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
+    org.w_array_type = w_array_type
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
@@ -465,6 +465,9 @@
 
         name = rffi.charp2str(pto.c_tp_name)
         newtypedef = inject_operators(space, name, dict_w, pto)
+        if newtypedef is not None:
+            assert newtypedef.injected_type
+            newtypedef.w_type_injected = self
         new_layout = (pto.c_tp_basicsize > rffi.sizeof(PyObject.TO) or
                       pto.c_tp_itemsize > 0 or newtypedef is not None)
 
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -126,6 +126,10 @@
         # typeobject.TypeCache maps a TypeDef instance to its
         # unique-for-this-space W_TypeObject instance
         assert typedef is not None
+        if typedef.injected_type:
+            assert typedef.w_type_injected is not None
+            assert typedef.w_type_injected.space is self
+            return typedef.w_type_injected
         return self.fromcache(TypeCache).getorbuild(typedef)
 
     @specialize.argtype(1)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to