Author: Richard Plangger <[email protected]>
Branch: strbuf-as-buffer
Changeset: r89279:d044ec26c066
Date: 2016-12-30 17:14 +0100
http://bitbucket.org/pypy/pypy/changeset/d044ec26c066/
Log: catch case if from_buffer is str/unicode
diff --git a/lib_pypy/_ctypes/basics.py b/lib_pypy/_ctypes/basics.py
--- a/lib_pypy/_ctypes/basics.py
+++ b/lib_pypy/_ctypes/basics.py
@@ -84,18 +84,15 @@
return self.from_address(dll._handle.getaddressindll(name))
def from_buffer(self, obj, offset=0):
- from array import array
size = self._sizeofinstances()
- if isinstance(obj, array):
- # hack, buffer(array.array) will always return a readonly buffer.
+ if isinstance(obj, (str, unicode)):
+ # hack, buffer(str) will always return a readonly buffer.
# CPython calls PyObject_AsWriteBuffer(...) here!
- # array.array does not implement the buffer interface so we cannot
- # use memoryview here (neither on CPython)!
- buf = buffer(obj, offset, size)
- else:
- buf = memoryview(obj)[offset:]
- if buf.readonly:
- raise TypeError("Cannot use %s as modifiable buffer" %
str(type(obj)))
+ # str cannot be modified, thus rase a type error in this case
+ raise TypeError("Cannot use %s as modifiable buffer" %
str(type(obj)))
+
+ buf = buffer(obj, offset, size)
+
if len(buf) < size:
raise ValueError(
"Buffer size too small (%d instead of at least %d bytes)"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit