Author: mattip <matti.pi...@gmail.com>
Branch: cpyext-ext
Changeset: r82616:d3f9504cf268
Date: 2016-02-29 16:57 -0500
http://bitbucket.org/pypy/pypy/changeset/d3f9504cf268/

Log:    fix test from 22fa8dfd0a2d by creating yet more alloc() functions

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -952,6 +952,8 @@
         cpyext_type_init = self.cpyext_type_init
         self.cpyext_type_init = None
         for pto, w_type in cpyext_type_init:
+            if space.is_w(w_type, space.w_str):
+                pto.c_tp_itemsize = 1
             finish_type_1(space, pto)
             finish_type_2(space, pto, w_type)
 
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
@@ -92,7 +92,7 @@
 
         if tp_alloc:
             def allocate(self, space, w_type, itemcount=0):
-                return tp_alloc(space, w_type)
+                return tp_alloc(space, w_type, itemcount)
 
         if tp_dealloc:
             def get_dealloc(self, space):
diff --git a/pypy/module/cpyext/stringobject.py 
b/pypy/module/cpyext/stringobject.py
--- a/pypy/module/cpyext/stringobject.py
+++ b/pypy/module/cpyext/stringobject.py
@@ -6,7 +6,7 @@
 from pypy.module.cpyext.pyerrors import PyErr_BadArgument
 from pypy.module.cpyext.pyobject import (
     PyObject, PyObjectP, Py_DecRef, make_ref, from_ref, track_reference,
-    make_typedescr, get_typedescr)
+    make_typedescr, get_typedescr, as_pyobj)
 
 ##
 ## Implementation of PyStringObject
@@ -62,12 +62,43 @@
     "Type description of PyStringObject"
     make_typedescr(space.w_str.layout.typedef,
                    basestruct=PyStringObject.TO,
+                   alloc = string_alloc,
                    attach=string_attach,
                    dealloc=string_dealloc,
                    realize=string_realize)
 
 PyString_Check, PyString_CheckExact = build_type_checkers("String", "w_str")
 
+def string_alloc(space, w_type, length):
+    '''
+    Yet another way to allocate a PyObject, this time a
+    PyStringObject. The first bit is copied from 
+    BaseCpyTypedescr.allocate, the bit after length>0
+    from string_attach. This is used as the tp_alloc function
+    for PyStringObject
+    '''
+    from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
+    pytype = as_pyobj(space, w_type)
+    pytype = rffi.cast(PyTypeObjectPtr, pytype)
+    assert pytype
+    size = pytype.c_tp_basicsize
+    buf = lltype.malloc(rffi.VOIDP.TO, size,
+                        flavor='raw', zero=True)
+    py_str = rffi.cast(PyStringObject, buf)
+    py_str.c_ob_refcnt = 1
+    py_str.c_ob_type = pytype
+    if length > 0:
+        py_str.c_buffer = lltype.malloc(rffi.CCHARP.TO, length+1,
+                                        flavor='raw', zero=True)
+        py_str.c_size = length
+        py_str.c_ob_sstate = rffi.cast(rffi.INT, 0) # SSTATE_NOT_INTERNED
+        s = rffi.charpsize2str(py_str.c_buffer, length+1)
+        w_obj = space.wrap(s)
+        py_str.c_ob_shash = space.hash_w(w_obj)
+        py_str.c_ob_sstate = rffi.cast(rffi.INT, 1) # SSTATE_INTERNED_MORTAL
+        track_reference(space, rffi.cast(PyObject, py_str), w_obj)
+    return rffi.cast(PyObject, py_str)
+
 def new_empty_str(space, length):
     """
     Allocate a PyStringObject and its buffer, but without a corresponding
diff --git a/pypy/module/cpyext/test/test_stringobject.py 
b/pypy/module/cpyext/test/test_stringobject.py
--- a/pypy/module/cpyext/test/test_stringobject.py
+++ b/pypy/module/cpyext/test/test_stringobject.py
@@ -99,6 +99,8 @@
                 char * p_str;
                 base = PyString_FromString("test");
                 type = base->ob_type;
+                if (type->tp_itemsize != 1)
+                    return PyLong_FromLong(type->tp_itemsize);
                 obj = (PyStringObject*)type->tp_alloc(type, 10);
                 if (PyString_GET_SIZE(obj) == 0)
                     return PyLong_FromLong(-1);
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
@@ -494,7 +494,7 @@
         PyObject_dealloc(space, obj)
 
 
-def type_alloc(space, w_metatype):
+def type_alloc(space, w_metatype, itemsize=0):
     metatype = rffi.cast(PyTypeObjectPtr, make_ref(space, w_metatype))
     # Don't increase refcount for non-heaptypes
     if metatype:
diff --git a/pypy/module/cpyext/unicodeobject.py 
b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -30,6 +30,7 @@
 def init_unicodeobject(space):
     make_typedescr(space.w_unicode.layout.typedef,
                    basestruct=PyUnicodeObject.TO,
+                   alloc = unicode_alloc,
                    attach=unicode_attach,
                    dealloc=unicode_dealloc,
                    realize=unicode_realize)
@@ -43,6 +44,30 @@
 
 Py_UNICODE = lltype.UniChar
 
+def unicode_alloc(space, w_type, length):
+    '''
+    see comments with string_alloc in stringobject.py
+    '''
+    from pypy.module.cpyext.typeobjectdefs import PyTypeObjectPtr
+    pytype = as_pyobj(space, w_type)
+    pytype = rffi.cast(PyTypeObjectPtr, pytype)
+    assert pytype
+    size = pytype.c_tp_basicsize
+    buf = lltype.malloc(rffi.VOIDP.TO, size,
+                        flavor='raw', zero=True)
+    py_uni = rffi.cast(PyUnicodeObject, buf)
+    py_uni.c_ob_refcnt = 1
+    py_uni.c_ob_type = pytype
+    if length > 0:
+        py_uni.c_str = lltype.malloc(rffi.CCHARP.TO, length+1,
+                                        flavor='raw', zero=True)
+        py_str.c_length = length
+        s = rffi.wcharpsize2unicode(py_uni.c_str, py_uni.c_length)
+        w_obj = space.wrap(s)
+        py_str.c_ob_shash = space.hash_w(w_obj)
+        track_reference(space, rffi.cast(PyObject, py_str), w_obj)
+    return rffi.cast(PyObject, py_str)
+
 def new_empty_unicode(space, length):
     """
     Allocate a PyUnicodeObject and its buffer, but without a corresponding
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to