Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3k
Changeset: r59041:7960edd9dd95
Date: 2012-11-20 23:53 +0100
http://bitbucket.org/pypy/pypy/changeset/7960edd9dd95/
Log: cpyext: fix PyObject_AsCharBuffer, and remove
PyString_AsEncodedObject and PyString_AsDecodedObject which are "not
available in 3.x and do not have a PyBytes alias"
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
@@ -14,6 +14,10 @@
import pypy.module.__builtin__.operation as operation
+# from include/object.h
+PyBUF_SIMPLE = 0x0000
+PyBUF_WRITABLE = 0x0001
+
@cpython_api([Py_ssize_t], rffi.VOIDP)
def PyObject_MALLOC(space, size):
return lltype.malloc(rffi.VOIDP.TO, size,
@@ -403,18 +407,21 @@
pto = obj.c_ob_type
pb = pto.c_tp_as_buffer
- if not (pb and pb.c_bf_getreadbuffer and pb.c_bf_getsegcount):
+ if not (pb and pb.c_bf_getbuffer):
raise OperationError(space.w_TypeError, space.wrap(
- "expected a character buffer object"))
- if generic_cpy_call(space, pb.c_bf_getsegcount,
- obj, lltype.nullptr(Py_ssize_tP.TO)) != 1:
- raise OperationError(space.w_TypeError, space.wrap(
- "expected a single-segment buffer object"))
- size = generic_cpy_call(space, pb.c_bf_getcharbuffer,
- obj, 0, bufferp)
- if size < 0:
- return -1
- sizep[0] = size
+ "expected an object with the buffer interface"))
+ with lltype.scoped_alloc(Py_buffer) as view:
+ if generic_cpy_call(space, pb.c_bf_getbuffer,
+ obj, view, rffi.cast(rffi.INT_real, PyBUF_SIMPLE)):
+ return -1
+
+ bufferp[0] = rffi.cast(rffi.CCHARP, view.c_buf)
+ sizep[0] = view.c_len
+
+ if pb.c_bf_releasebuffer:
+ generic_cpy_call(space, pb.c_bf_releasebuffer,
+ obj, view)
+ Py_DecRef(space, view.c_obj)
return 0
# Also in include/object.h
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
@@ -269,45 +269,6 @@
Py_DecRef(space, string[0])
string[0] = make_ref(space, w_str)
-@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
-def PyString_AsEncodedObject(space, w_str, encoding, errors):
- """Encode a string object using the codec registered for encoding and
return
- the result as Python object. encoding and errors have the same meaning as
- the parameters of the same name in the string encode() method. The codec to
- be used is looked up using the Python codec registry. Return NULL if an
- exception was raised by the codec.
-
- This function is not available in 3.x and does not have a PyBytes alias."""
- if not PyString_Check(space, w_str):
- PyErr_BadArgument(space)
-
- w_encoding = w_errors = space.w_None
- if encoding:
- w_encoding = space.wrap(rffi.charp2str(encoding))
- if errors:
- w_errors = space.wrap(rffi.charp2str(errors))
- return space.call_method(w_str, 'encode', w_encoding, w_errors)
-
-@cpython_api([PyObject, rffi.CCHARP, rffi.CCHARP], PyObject)
-def PyString_AsDecodedObject(space, w_str, encoding, errors):
- """Decode a string object by passing it to the codec registered
- for encoding and return the result as Python object. encoding and
- errors have the same meaning as the parameters of the same name in
- the string encode() method. The codec to be used is looked up
- using the Python codec registry. Return NULL if an exception was
- raised by the codec.
-
- This function is not available in 3.x and does not have a PyBytes alias."""
- if not PyString_Check(space, w_str):
- PyErr_BadArgument(space)
-
- w_encoding = w_errors = space.w_None
- if encoding:
- w_encoding = space.wrap(rffi.charp2str(encoding))
- if errors:
- w_errors = space.wrap(rffi.charp2str(errors))
- return space.call_method(w_str, "decode", w_encoding, w_errors)
-
@cpython_api([PyObject, PyObject], PyObject)
def _PyString_Join(space, w_sep, w_seq):
return space.call_method(w_sep, 'join', w_seq)
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
@@ -176,21 +176,6 @@
Py_DecRef(space, ar[0])
lltype.free(ar, flavor='raw')
- def test_string_buffer(self, space, api):
- py_str = new_empty_str(space, 10)
- c_buf = py_str.c_ob_type.c_tp_as_buffer
- assert c_buf
- py_obj = rffi.cast(PyObject, py_str)
- assert c_buf.c_bf_getsegcount(py_obj, lltype.nullptr(Py_ssize_tP.TO))
== 1
- ref = lltype.malloc(Py_ssize_tP.TO, 1, flavor='raw')
- assert c_buf.c_bf_getsegcount(py_obj, ref) == 1
- assert ref[0] == 10
- lltype.free(ref, flavor='raw')
- ref = lltype.malloc(rffi.VOIDPP.TO, 1, flavor='raw')
- assert c_buf.c_bf_getreadbuffer(py_obj, 0, ref) == 10
- lltype.free(ref, flavor='raw')
- Py_DecRef(space, py_obj)
-
def test_Concat(self, space, api):
ref = make_ref(space, space.wrapbytes('abc'))
ptr = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
@@ -237,43 +222,6 @@
rffi.free_charp(buf)
assert w_s1 is w_s2
- def test_AsEncodedObject(self, space, api):
- ptr = space.wrap('abc')
-
- errors = rffi.str2charp("strict")
-
- encoding = rffi.str2charp("hex")
- res = api.PyString_AsEncodedObject(
- ptr, encoding, errors)
- assert space.unwrap(res) == "616263"
-
- res = api.PyString_AsEncodedObject(
- ptr, encoding, lltype.nullptr(rffi.CCHARP.TO))
- assert space.unwrap(res) == "616263"
- rffi.free_charp(encoding)
-
- encoding = rffi.str2charp("unknown_encoding")
- self.raises(space, api, LookupError, api.PyString_AsEncodedObject,
- ptr, encoding, errors)
- rffi.free_charp(encoding)
-
- rffi.free_charp(errors)
-
- res = api.PyString_AsEncodedObject(
- ptr, lltype.nullptr(rffi.CCHARP.TO),
lltype.nullptr(rffi.CCHARP.TO))
- assert space.unwrap(res) == "abc"
-
- self.raises(space, api, TypeError, api.PyString_AsEncodedObject,
- space.wrap(2), lltype.nullptr(rffi.CCHARP.TO),
lltype.nullptr(rffi.CCHARP.TO)
- )
-
- def test_AsDecodedObject(self, space, api):
- w_str = space.wrap('caf\xe9')
- encoding = rffi.str2charp("latin-1")
- w_res = api.PyString_AsDecodedObject(w_str, encoding, None)
- rffi.free_charp(encoding)
- assert space.unwrap(w_res) == u"caf\xe9"
-
def test_eq(self, space, api):
assert 1 == api._PyString_Eq(space.wrapbytes("hello"),
space.wrapbytes("hello"))
assert 0 == api._PyString_Eq(space.wrapbytes("hello"),
space.wrapbytes("world"))
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit