Author: mattip <[email protected]>
Branch: cpyext-ext
Changeset: r83058:232d73c2ea06
Date: 2016-03-15 00:14 +0200
http://bitbucket.org/pypy/pypy/changeset/232d73c2ea06/

Log:    fix test since PyStringObject buffer is read only, fix segfault if
        buffer is NULL

diff --git a/pypy/module/cpyext/bytesobject.py 
b/pypy/module/cpyext/bytesobject.py
--- a/pypy/module/cpyext/bytesobject.py
+++ b/pypy/module/cpyext/bytesobject.py
@@ -134,6 +134,9 @@
     be modified after this call.
     """
     py_str = rffi.cast(PyStringObject, py_obj)
+    if not py_str.c_buffer:
+        py_str.c_buffer = lltype.malloc(rffi.CCHARP.TO, py_str.c_ob_size + 1,
+                                    flavor='raw', zero=True)
     s = rffi.charpsize2str(py_str.c_buffer, py_str.c_ob_size)
     w_obj = space.wrap(s)
     py_str.c_ob_shash = space.hash_w(w_obj)
diff --git a/pypy/module/cpyext/test/test_bytesobject.py 
b/pypy/module/cpyext/test/test_bytesobject.py
--- a/pypy/module/cpyext/test/test_bytesobject.py
+++ b/pypy/module/cpyext/test/test_bytesobject.py
@@ -98,21 +98,22 @@
                 PyStringObject *obj;
                 char * p_str;
                 base = PyString_FromString("test");
-                if (((PyStringObject*)base)->buffer == NULL)
-                    return PyLong_FromLong(-2);
+                if (PyString_GET_SIZE(base) != 4)
+                    return PyLong_FromLong(-PyString_GET_SIZE(base));
                 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);
-                memcpy(PyString_AS_STRING(obj), "works", 6);
+                if (PyString_GET_SIZE(obj) != 10)
+                    return PyLong_FromLong(PyString_GET_SIZE(obj));
+                /* cannot work, there is only RO access
+                memcpy(PyString_AS_STRING(obj), "works", 6); */
                 Py_INCREF(obj);
                 return (PyObject*)obj;
              """),
             ])
         s = module.tpalloc()
-        assert s == 'works\x00\x00\x00\x00\x00'
+        assert s == '\x00' * 10
 
     def test_AsString(self):
         module = self.import_extension('foo', [
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to