[pypy-commit] pypy cpyext-ext: (matti, arigo, ronan around)

arigo pypy.commits at gmail.com
Thu Feb 25 08:37:06 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: cpyext-ext
Changeset: r82503:1dfe3b071dc6
Date: 2016-02-25 14:35 +0100
http://bitbucket.org/pypy/pypy/changeset/1dfe3b071dc6/

Log:	(matti, arigo, ronan around)

	Test and fix: initialize ob_pypy_link correctly to 0

diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py
--- a/pypy/module/cpyext/object.py
+++ b/pypy/module/cpyext/object.py
@@ -16,8 +16,9 @@
 
 @cpython_api([Py_ssize_t], rffi.VOIDP)
 def PyObject_Malloc(space, size):
+    # returns non-zero-initialized memory, like CPython
     return lltype.malloc(rffi.VOIDP.TO, size,
-                         flavor='raw', zero=True)
+                         flavor='raw')
 
 @cpython_api([rffi.VOIDP], lltype.Void)
 def PyObject_Free(space, ptr):
@@ -189,6 +190,7 @@
     if not obj:
         PyErr_NoMemory(space)
     obj.c_ob_type = type
+    obj.c_ob_pypy_link = 0
     obj.c_ob_refcnt = 1
     return obj
 
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -53,6 +53,7 @@
                             flavor='raw', zero=True)
         pyobj = rffi.cast(PyObject, buf)
         pyobj.c_ob_refcnt = 1
+        #pyobj.c_ob_pypy_link should get assigned very quickly
         pyobj.c_ob_type = pytype
         return pyobj
 
@@ -325,6 +326,7 @@
 @cpython_api([PyObject], lltype.Void)
 def _Py_NewReference(space, obj):
     obj.c_ob_refcnt = 1
+    # XXX is it always useful to create the W_Root object here?
     w_type = from_ref(space, rffi.cast(PyObject, obj.c_ob_type))
     assert isinstance(w_type, W_TypeObject)
     get_typedescr(w_type.layout.typedef).realize(space, obj)
diff --git a/pypy/module/cpyext/test/test_object.py b/pypy/module/cpyext/test/test_object.py
--- a/pypy/module/cpyext/test/test_object.py
+++ b/pypy/module/cpyext/test/test_object.py
@@ -217,6 +217,20 @@
         AppTestCpythonExtensionBase.setup_class.im_func(cls)
         cls.w_tmpname = cls.space.wrap(str(py.test.ensuretemp("out", dir=0)))
 
+    def test_object_malloc(self):
+        module = self.import_extension('foo', [
+            ("malloctest", "METH_NOARGS",
+             """
+                 PyObject *obj = PyObject_MALLOC(sizeof(PyIntObject));
+                 obj = PyObject_Init(obj, &PyInt_Type);
+                 if (obj != NULL)
+                     ((PyIntObject *)obj)->ob_ival = -424344;
+                 return obj;
+             """)])
+        x = module.malloctest()
+        assert type(x) is int
+        assert x == -424344
+
     def test_TypeCheck(self):
         module = self.import_extension('foo', [
             ("typecheck", "METH_VARARGS",
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
@@ -506,6 +506,7 @@
                              flavor='raw', zero=True)
     pto = heaptype.c_ht_type
     pto.c_ob_refcnt = 1
+    pto.c_ob_pypy_link = 0
     pto.c_ob_type = metatype
     pto.c_tp_flags |= Py_TPFLAGS_HEAPTYPE
     pto.c_tp_as_number = heaptype.c_as_number


More information about the pypy-commit mailing list