Author: Matti Picus <[email protected]>
Branch: cpyext-ext
Changeset: r84889:078b429edc10
Date: 2016-06-03 09:35 +0300
http://bitbucket.org/pypy/pypy/changeset/078b429edc10/
Log: use resizable_list_supporting_raw_ptr in std/bytearrayobject, now
passes some cpyext tests
diff --git a/pypy/module/cpyext/bytearrayobject.py
b/pypy/module/cpyext/bytearrayobject.py
--- a/pypy/module/cpyext/bytearrayobject.py
+++ b/pypy/module/cpyext/bytearrayobject.py
@@ -17,12 +17,10 @@
# For the convenience of C programmers, the bytes type is considered
# to contain a char pointer, not an unsigned char pointer.
-# XXX The underlying data array is mutable, cpython gives direct access
-# to ob_bytes as a RW pointer to bytes. How can we do this?
-# One proposal is to make W_Bytearray.data into a nonmovable gc list
-# as part of as_pyobj(), and expose data only through PyByteArray_AS_STRING
-# Under this strategy ob_bytes could possibly not reflect the current state
-# of the object
+# Expose data as a rw cchar* only through PyByteArray_AsString
+# Under this strategy the pointer could loose its synchronization with
+# the underlying space.w_bytearray if PyByteArray_Resize is called, so
+# hopefully the use of the pointer is short-lived
PyByteArrayObjectStruct = lltype.ForwardReference()
PyByteArrayObject = lltype.Ptr(PyByteArrayObjectStruct)
@@ -122,14 +120,3 @@
"""Resize the internal buffer of bytearray to len."""
raise NotImplementedError
-@cpython_api([PyObject], rffi.CCHARP)
-def PyByteArray_AS_STRING(space, bytearray):
- """Macro version of PyByteArray_AsString()."""
- raise NotImplementedError
-
-@cpython_api([PyObject], Py_ssize_t, error=-1)
-def PyByteArray_GET_SIZE(space, bytearray):
- """Macro version of PyByteArray_Size()."""
- raise NotImplementedError
-
-
diff --git a/pypy/module/cpyext/include/bytearrayobject.h
b/pypy/module/cpyext/include/bytearrayobject.h
--- a/pypy/module/cpyext/include/bytearrayobject.h
+++ b/pypy/module/cpyext/include/bytearrayobject.h
@@ -17,6 +17,9 @@
* While CPython exposes interfaces to this object, pypy does not
*/
+#define PyByteArray_GET_SIZE(op) PyByteArray_Size((PyObject*)(op))
+#define PyByteArray_AS_STRING(op) PyByteArray_AsString((PyObject*)(op))
+
/* Object layout */
typedef struct {
PyObject_VAR_HEAD
diff --git a/pypy/objspace/std/bytearrayobject.py
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -6,6 +6,8 @@
from rpython.rlib.rstring import StringBuilder, ByteListBuilder
from rpython.rlib.debug import check_list_of_chars
from rpython.rtyper.lltypesystem import rffi
+from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr,
+ nonmoving_raw_ptr_for_resizable_list)
from pypy.interpreter.baseobjspace import W_Root
from pypy.interpreter.error import OperationError, oefmt
@@ -25,7 +27,7 @@
def __init__(self, data):
check_list_of_chars(data)
- self.data = data
+ self.data = resizable_list_supporting_raw_ptr(data)
def __repr__(self):
"""representation for debugging purposes"""
@@ -227,11 +229,11 @@
else:
if count < 0:
raise oefmt(space.w_ValueError, "bytearray negative count")
- self.data = ['\0'] * count
+ self.data = resizable_list_supporting_raw_ptr(['\0'] * count)
return
data = makebytearraydata_w(space, w_source)
- self.data = data
+ self.data = resizable_list_supporting_raw_ptr(data)
def descr_repr(self, space):
s = self.data
@@ -1251,7 +1253,7 @@
self.data[start + i] = string[i]
def get_raw_address(self):
- return rffi.cast(rffi.CCHARP, 0)
+ return nonmoving_raw_ptr_for_resizable_list(self.data)
@specialize.argtype(1)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit