[pypy-commit] pypy refactor-buffer-api: unicodeobject has old buffer interface
bdkearns
noreply at buildbot.pypy.org
Thu Apr 24 09:40:36 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch: refactor-buffer-api
Changeset: r70923:bd46658a05eb
Date: 2014-04-24 03:38 -0400
http://bitbucket.org/pypy/pypy/changeset/bd46658a05eb/
Log: unicodeobject has old buffer interface
diff --git a/pypy/module/__pypy__/test/test_bytebuffer.py b/pypy/module/__pypy__/test/test_bytebuffer.py
--- a/pypy/module/__pypy__/test/test_bytebuffer.py
+++ b/pypy/module/__pypy__/test/test_bytebuffer.py
@@ -25,3 +25,5 @@
assert str(b) == "\x00xy" + "\x00" * 7
b[4:8:2] = 'zw'
assert str(b) == "\x00xy\x00z\x00w" + "\x00" * 3
+ b[6:10] = u'#'
+ assert str(b) == "\x00xy\x00z\x00#" + "\x00" * 3
diff --git a/pypy/module/_io/test/test_bufferedio.py b/pypy/module/_io/test/test_bufferedio.py
--- a/pypy/module/_io/test/test_bufferedio.py
+++ b/pypy/module/_io/test/test_bufferedio.py
@@ -139,6 +139,8 @@
raw = _io.FileIO(self.tmpfile)
f = _io.BufferedReader(raw)
assert f.readinto(a) == 5
+ exc = raises(TypeError, f.readinto, u"hello")
+ assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, f.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, f.readinto, memoryview(b"hello"))
@@ -239,7 +241,8 @@
import _io
raw = _io.FileIO(self.tmpfile, 'w')
f = _io.BufferedWriter(raw)
- f.write("abcd")
+ f.write("ab")
+ f.write(u"cd")
f.close()
assert self.readfile() == "abcd"
diff --git a/pypy/module/_io/test/test_bytesio.py b/pypy/module/_io/test/test_bytesio.py
--- a/pypy/module/_io/test/test_bytesio.py
+++ b/pypy/module/_io/test/test_bytesio.py
@@ -38,6 +38,8 @@
f = _io.BytesIO()
assert f.write("") == 0
assert f.write("hello") == 5
+ exc = raises(TypeError, f.write, u"lo")
+ assert str(exc.value) == "'unicode' does not have the buffer interface"
import gc; gc.collect()
assert f.getvalue() == "hello"
f.close()
@@ -97,6 +99,8 @@
a2 = bytearray('testing')
assert b.readinto(a1) == 1
assert b.readinto(a2) == 4
+ exc = raises(TypeError, b.readinto, u"hello")
+ assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, b.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, b.readinto, memoryview(b"hello"))
diff --git a/pypy/module/_io/test/test_fileio.py b/pypy/module/_io/test/test_fileio.py
--- a/pypy/module/_io/test/test_fileio.py
+++ b/pypy/module/_io/test/test_fileio.py
@@ -82,7 +82,8 @@
import _io
filename = self.tmpfile + '_w'
f = _io.FileIO(filename, 'wb')
- f.write("test")
+ f.write("te")
+ f.write(u"st")
# try without flushing
f2 = _io.FileIO(filename, 'rb')
assert f2.read() == "test"
@@ -135,6 +136,8 @@
a = bytearray('x' * 10)
f = _io.FileIO(self.tmpfile, 'r+')
assert f.readinto(a) == 10
+ exc = raises(TypeError, f.readinto, u"hello")
+ assert str(exc.value) == "cannot use unicode as modifiable buffer"
exc = raises(TypeError, f.readinto, buffer(b"hello"))
assert str(exc.value) == "must be read-write buffer, not buffer"
exc = raises(TypeError, f.readinto, memoryview(b"hello"))
diff --git a/pypy/objspace/std/bufferobject.py b/pypy/objspace/std/bufferobject.py
--- a/pypy/objspace/std/bufferobject.py
+++ b/pypy/objspace/std/bufferobject.py
@@ -37,18 +37,7 @@
@staticmethod
@unwrap_spec(offset=int, size=int)
def descr_new_buffer(space, w_subtype, w_object, offset=0, size=-1):
- if space.isinstance_w(w_object, space.w_unicode):
- # unicode objects support the old buffer interface
- # but not the new buffer interface (change in python 2.7)
- from rpython.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
- unistr = space.unicode_w(w_object)
- builder = StringBuilder(len(unistr) * UNICODE_SIZE)
- for unich in unistr:
- pack_unichar(unich, builder)
- buf = StringBuffer(builder.build())
- else:
- buf = space.readbuf_w(w_object)
-
+ buf = space.readbuf_w(w_object)
if offset == 0 and size == -1:
return W_Buffer(buf)
# handle buffer slices
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -2,7 +2,8 @@
from rpython.rlib.objectmodel import (
compute_hash, compute_unique_id, import_from_mixin)
-from rpython.rlib.rstring import UnicodeBuilder
+from rpython.rlib.buffer import StringBuffer
+from rpython.rlib.rstring import StringBuilder, UnicodeBuilder
from rpython.rlib.runicode import (
make_unicode_escape_function, str_decode_ascii, str_decode_utf_8,
unicode_encode_ascii, unicode_encode_utf_8)
@@ -64,6 +65,17 @@
def unicode_w(self, space):
return self._value
+ def readbuf_w(self, space):
+ from rpython.rlib.rstruct.unichar import pack_unichar, UNICODE_SIZE
+ builder = StringBuilder(len(self._value) * UNICODE_SIZE)
+ for unich in self._value:
+ pack_unichar(unich, builder)
+ return StringBuffer(builder.build())
+
+ def writebuf_w(self, space):
+ raise OperationError(space.w_TypeError, space.wrap(
+ "cannot use unicode as modifiable buffer"))
+
def listview_unicode(w_self):
return _create_list_from_unicode(w_self._value)
More information about the pypy-commit
mailing list