[pypy-commit] pypy refactor-buffer-api: split W_Buffer/W_MemoryView implementation files
bdkearns
noreply at buildbot.pypy.org
Tue Apr 22 20:34:06 CEST 2014
Author: Brian Kearns <bdkearns at gmail.com>
Branch: refactor-buffer-api
Changeset: r70863:0410fe168a2e
Date: 2014-03-21 14:31 -0400
http://bitbucket.org/pypy/pypy/changeset/0410fe168a2e/
Log: split W_Buffer/W_MemoryView implementation files
diff --git a/pypy/module/_cffi_backend/cbuffer.py b/pypy/module/_cffi_backend/cbuffer.py
--- a/pypy/module/_cffi_backend/cbuffer.py
+++ b/pypy/module/_cffi_backend/cbuffer.py
@@ -3,7 +3,7 @@
from pypy.interpreter.gateway import unwrap_spec, interp2app
from pypy.interpreter.typedef import TypeDef, make_weakref_descr
from pypy.module._cffi_backend import cdataobj, ctypeptr, ctypearray
-from pypy.objspace.std.memoryview import W_Buffer
+from pypy.objspace.std.bufferobject import W_Buffer
from rpython.rtyper.annlowlevel import llstr
from rpython.rtyper.lltypesystem import rffi
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -22,7 +22,6 @@
from pypy.interpreter.nestedscope import Cell
from pypy.interpreter.module import Module
from pypy.interpreter.function import StaticMethod
-from pypy.objspace.std.memoryview import W_MemoryView
from pypy.objspace.std.sliceobject import W_SliceObject
from pypy.module.__builtin__.descriptor import W_Property
from pypy.module.__builtin__.interp_classobj import W_ClassObject
@@ -474,7 +473,7 @@
"PyLong_Type": "space.w_long",
"PyComplex_Type": "space.w_complex",
"PyByteArray_Type": "space.w_bytearray",
- "PyMemoryView_Type": "space.gettypeobject(W_MemoryView.typedef)",
+ "PyMemoryView_Type": "space.w_memoryview",
"PyArray_Type": "space.gettypeobject(W_NDimArray.typedef)",
"PyBaseObject_Type": "space.w_object",
'PyNone_Type': 'space.type(space.w_None)',
diff --git a/pypy/module/cpyext/bufferobject.py b/pypy/module/cpyext/bufferobject.py
--- a/pypy/module/cpyext/bufferobject.py
+++ b/pypy/module/cpyext/bufferobject.py
@@ -6,7 +6,7 @@
PyObjectFields, PyObject)
from pypy.module.cpyext.pyobject import make_typedescr, Py_DecRef, make_ref
from pypy.module.array.interp_array import ArrayBuffer
-from pypy.objspace.std.memoryview import W_Buffer
+from pypy.objspace.std.bufferobject import W_Buffer
PyBufferObjectStruct = lltype.ForwardReference()
diff --git a/pypy/objspace/std/bufferobject.py b/pypy/objspace/std/bufferobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/objspace/std/bufferobject.py
@@ -0,0 +1,157 @@
+"""
+Implementation of the 'buffer' and 'memoryview' types.
+"""
+import operator
+
+from pypy.interpreter import buffer
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.error import OperationError
+from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.typedef import TypeDef
+from rpython.rlib.objectmodel import compute_hash
+from rpython.rlib.rstring import StringBuilder
+from pypy.objspace.std.memoryobject import _buffer_setitem
+
+
+class W_Buffer(W_Root):
+ """Implement the built-in 'buffer' type as a wrapper around
+ an interp-level buffer.
+ """
+
+ def __init__(self, buf):
+ assert isinstance(buf, buffer.Buffer)
+ self.buf = buf
+
+ def buffer_w(self, space, flags):
+ return self.buf
+
+ def readbuf_w(self, space):
+ return self.buf
+
+ def writebuf_w(self, space):
+ return self.buf
+
+ def charbuf_w(self, space):
+ return self.buf.as_str()
+
+ @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)
+ from pypy.interpreter.buffer import StringBuffer
+ buf = StringBuffer(builder.build())
+ else:
+ buf = space.readbuf_w(w_object)
+
+ if offset == 0 and size == -1:
+ return W_Buffer(buf)
+ # handle buffer slices
+ if offset < 0:
+ raise OperationError(space.w_ValueError,
+ space.wrap("offset must be zero or positive"))
+ if size < -1:
+ raise OperationError(space.w_ValueError,
+ space.wrap("size must be zero or positive"))
+ if isinstance(buf, buffer.RWBuffer):
+ buf = buffer.RWSubBuffer(buf, offset, size)
+ else:
+ buf = buffer.SubBuffer(buf, offset, size)
+ return W_Buffer(buf)
+
+ def descr_len(self, space):
+ return space.wrap(self.buf.getlength())
+
+ def descr_getitem(self, space, w_index):
+ start, stop, step, size = space.decode_index4(w_index, self.buf.getlength())
+ if step == 0: # index only
+ return space.wrap(self.buf.getitem(start))
+ res = self.buf.getslice(start, stop, step, size)
+ return space.wrap(res)
+
+ @unwrap_spec(newstring='bufferstr')
+ def descr_setitem(self, space, w_index, newstring):
+ if not isinstance(self.buf, buffer.RWBuffer):
+ raise OperationError(space.w_TypeError,
+ space.wrap("buffer is read-only"))
+ _buffer_setitem(space, self.buf, w_index, newstring)
+
+ def descr_str(self, space):
+ return space.wrap(self.buf.as_str())
+
+ @unwrap_spec(other='bufferstr')
+ def descr_add(self, space, other):
+ return space.wrap(self.buf.as_str() + other)
+
+ def _make_descr__cmp(name):
+ def descr__cmp(self, space, w_other):
+ if not isinstance(w_other, W_Buffer):
+ return space.w_NotImplemented
+ # xxx not the most efficient implementation
+ str1 = self.buf.as_str()
+ str2 = w_other.buf.as_str()
+ return space.wrap(getattr(operator, name)(str1, str2))
+ descr__cmp.func_name = name
+ return descr__cmp
+
+ descr_eq = _make_descr__cmp('eq')
+ descr_ne = _make_descr__cmp('ne')
+ descr_lt = _make_descr__cmp('lt')
+ descr_le = _make_descr__cmp('le')
+ descr_gt = _make_descr__cmp('gt')
+ descr_ge = _make_descr__cmp('ge')
+
+ def descr_hash(self, space):
+ return space.wrap(compute_hash(self.buf.as_str()))
+
+ def descr_mul(self, space, w_times):
+ # xxx not the most efficient implementation
+ w_string = space.wrap(self.buf.as_str())
+ # use the __mul__ method instead of space.mul() so that we
+ # return NotImplemented instead of raising a TypeError
+ return space.call_method(w_string, '__mul__', w_times)
+
+ def descr_repr(self, space):
+ if isinstance(self.buf, buffer.RWBuffer):
+ info = 'read-write buffer'
+ else:
+ info = 'read-only buffer'
+ addrstring = self.getaddrstring(space)
+
+ return space.wrap("<%s for 0x%s, size %d>" %
+ (info, addrstring, self.buf.getlength()))
+
+W_Buffer.typedef = TypeDef(
+ "buffer",
+ __doc__ = """\
+buffer(object [, offset[, size]])
+
+Create a new buffer object which references the given object.
+The buffer will reference a slice of the target object from the
+start of the object (or at the specified offset). The slice will
+extend to the end of the target object (or with the specified size).
+""",
+ __new__ = interp2app(W_Buffer.descr_new_buffer),
+ __len__ = interp2app(W_Buffer.descr_len),
+ __getitem__ = interp2app(W_Buffer.descr_getitem),
+ __setitem__ = interp2app(W_Buffer.descr_setitem),
+ __str__ = interp2app(W_Buffer.descr_str),
+ __add__ = interp2app(W_Buffer.descr_add),
+ __eq__ = interp2app(W_Buffer.descr_eq),
+ __ne__ = interp2app(W_Buffer.descr_ne),
+ __lt__ = interp2app(W_Buffer.descr_lt),
+ __le__ = interp2app(W_Buffer.descr_le),
+ __gt__ = interp2app(W_Buffer.descr_gt),
+ __ge__ = interp2app(W_Buffer.descr_ge),
+ __hash__ = interp2app(W_Buffer.descr_hash),
+ __mul__ = interp2app(W_Buffer.descr_mul),
+ __rmul__ = interp2app(W_Buffer.descr_mul),
+ __repr__ = interp2app(W_Buffer.descr_repr),
+)
+W_Buffer.typedef.acceptable_as_base_class = False
diff --git a/pypy/objspace/std/memoryview.py b/pypy/objspace/std/memoryobject.py
rename from pypy/objspace/std/memoryview.py
rename to pypy/objspace/std/memoryobject.py
--- a/pypy/objspace/std/memoryview.py
+++ b/pypy/objspace/std/memoryobject.py
@@ -8,8 +8,6 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import interp2app, unwrap_spec
from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from rpython.rlib.objectmodel import compute_hash
-from rpython.rlib.rstring import StringBuilder
def _buffer_setitem(space, buf, w_index, newstring):
@@ -31,150 +29,6 @@
" slicing with a step"))
-class W_Buffer(W_Root):
- """Implement the built-in 'buffer' type as a wrapper around
- an interp-level buffer.
- """
-
- def __init__(self, buf):
- assert isinstance(buf, buffer.Buffer)
- self.buf = buf
-
- def buffer_w(self, space, flags):
- return self.buf
-
- def readbuf_w(self, space):
- return self.buf
-
- def writebuf_w(self, space):
- return self.buf
-
- def charbuf_w(self, space):
- return self.buf.as_str()
-
- @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)
- from pypy.interpreter.buffer import StringBuffer
- buf = StringBuffer(builder.build())
- else:
- buf = space.readbuf_w(w_object)
-
- if offset == 0 and size == -1:
- return W_Buffer(buf)
- # handle buffer slices
- if offset < 0:
- raise OperationError(space.w_ValueError,
- space.wrap("offset must be zero or positive"))
- if size < -1:
- raise OperationError(space.w_ValueError,
- space.wrap("size must be zero or positive"))
- if isinstance(buf, buffer.RWBuffer):
- buf = buffer.RWSubBuffer(buf, offset, size)
- else:
- buf = buffer.SubBuffer(buf, offset, size)
- return W_Buffer(buf)
-
- def descr_len(self, space):
- return space.wrap(self.buf.getlength())
-
- def descr_getitem(self, space, w_index):
- start, stop, step, size = space.decode_index4(w_index, self.buf.getlength())
- if step == 0: # index only
- return space.wrap(self.buf.getitem(start))
- res = self.buf.getslice(start, stop, step, size)
- return space.wrap(res)
-
- @unwrap_spec(newstring='bufferstr')
- def descr_setitem(self, space, w_index, newstring):
- if not isinstance(self.buf, buffer.RWBuffer):
- raise OperationError(space.w_TypeError,
- space.wrap("buffer is read-only"))
- _buffer_setitem(space, self.buf, w_index, newstring)
-
- def descr_str(self, space):
- return space.wrap(self.buf.as_str())
-
- @unwrap_spec(other='bufferstr')
- def descr_add(self, space, other):
- return space.wrap(self.buf.as_str() + other)
-
- def _make_descr__cmp(name):
- def descr__cmp(self, space, w_other):
- if not isinstance(w_other, W_Buffer):
- return space.w_NotImplemented
- # xxx not the most efficient implementation
- str1 = self.buf.as_str()
- str2 = w_other.buf.as_str()
- return space.wrap(getattr(operator, name)(str1, str2))
- descr__cmp.func_name = name
- return descr__cmp
-
- descr_eq = _make_descr__cmp('eq')
- descr_ne = _make_descr__cmp('ne')
- descr_lt = _make_descr__cmp('lt')
- descr_le = _make_descr__cmp('le')
- descr_gt = _make_descr__cmp('gt')
- descr_ge = _make_descr__cmp('ge')
-
- def descr_hash(self, space):
- return space.wrap(compute_hash(self.buf.as_str()))
-
- def descr_mul(self, space, w_times):
- # xxx not the most efficient implementation
- w_string = space.wrap(self.buf.as_str())
- # use the __mul__ method instead of space.mul() so that we
- # return NotImplemented instead of raising a TypeError
- return space.call_method(w_string, '__mul__', w_times)
-
- def descr_repr(self, space):
- if isinstance(self.buf, buffer.RWBuffer):
- info = 'read-write buffer'
- else:
- info = 'read-only buffer'
- addrstring = self.getaddrstring(space)
-
- return space.wrap("<%s for 0x%s, size %d>" %
- (info, addrstring, self.buf.getlength()))
-
-W_Buffer.typedef = TypeDef(
- "buffer",
- __doc__ = """\
-buffer(object [, offset[, size]])
-
-Create a new buffer object which references the given object.
-The buffer will reference a slice of the target object from the
-start of the object (or at the specified offset). The slice will
-extend to the end of the target object (or with the specified size).
-""",
- __new__ = interp2app(W_Buffer.descr_new_buffer),
- __len__ = interp2app(W_Buffer.descr_len),
- __getitem__ = interp2app(W_Buffer.descr_getitem),
- __setitem__ = interp2app(W_Buffer.descr_setitem),
- __str__ = interp2app(W_Buffer.descr_str),
- __add__ = interp2app(W_Buffer.descr_add),
- __eq__ = interp2app(W_Buffer.descr_eq),
- __ne__ = interp2app(W_Buffer.descr_ne),
- __lt__ = interp2app(W_Buffer.descr_lt),
- __le__ = interp2app(W_Buffer.descr_le),
- __gt__ = interp2app(W_Buffer.descr_gt),
- __ge__ = interp2app(W_Buffer.descr_ge),
- __hash__ = interp2app(W_Buffer.descr_hash),
- __mul__ = interp2app(W_Buffer.descr_mul),
- __rmul__ = interp2app(W_Buffer.descr_mul),
- __repr__ = interp2app(W_Buffer.descr_repr),
-)
-W_Buffer.typedef.acceptable_as_base_class = False
-
-
class W_MemoryView(W_Root):
"""Implement the built-in 'memoryview' type as a wrapper around
an interp-level buffer.
diff --git a/pypy/objspace/std/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -63,7 +63,8 @@
from pypy.objspace.std import unicodeobject
from pypy.objspace.std import dictproxyobject
from pypy.objspace.std import proxyobject
- from pypy.objspace.std.memoryview import W_Buffer, W_MemoryView
+ from pypy.objspace.std import bufferobject
+ from pypy.objspace.std import memoryobject
import pypy.objspace.std.default # register a few catch-all multimethods
import pypy.objspace.std.marshal_impl # install marshal multimethods
@@ -83,8 +84,8 @@
self.pythontypes.append(intobject.W_IntObject.typedef)
self.pythontypes.append(boolobject.W_BoolObject.typedef)
self.pythontypes.append(longobject.W_LongObject.typedef)
- self.pythontypes.append(W_Buffer.typedef)
- self.pythontypes.append(W_MemoryView.typedef)
+ self.pythontypes.append(bufferobject.W_Buffer.typedef)
+ self.pythontypes.append(memoryobject.W_MemoryView.typedef)
# the set of implementation types
self.typeorder = {
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -24,7 +24,7 @@
from pypy.objspace.std.iterobject import W_AbstractSeqIterObject
from pypy.objspace.std.listobject import W_ListObject
from pypy.objspace.std.longobject import W_LongObject, newlong
-from pypy.objspace.std.memoryview import W_Buffer
+from pypy.objspace.std.bufferobject import W_Buffer
from pypy.objspace.std.noneobject import W_NoneObject
from pypy.objspace.std.objectobject import W_ObjectObject
from pypy.objspace.std.iterobject import W_SeqIterObject
diff --git a/pypy/objspace/std/test/test_bufferobject.py b/pypy/objspace/std/test/test_bufferobject.py
new file mode 100644
--- /dev/null
+++ b/pypy/objspace/std/test/test_bufferobject.py
@@ -0,0 +1,190 @@
+class AppTestBuffer:
+ spaceconfig = dict(usemodules=['array'])
+
+ def test_init(self):
+ import sys
+ class A(object):
+ def __buffer__(self):
+ return buffer('123')
+ if '__pypy__' not in sys.builtin_module_names:
+ raises(TypeError, buffer, A())
+ else:
+ assert buffer(A()) == buffer('123')
+
+ def test_unicode_buffer(self):
+ import sys
+ b = buffer(u"ab")
+ if sys.maxunicode == 65535: # UCS2 build
+ assert len(b) == 4
+ if sys.byteorder == "big":
+ assert b[0:4] == "\x00a\x00b"
+ else:
+ assert b[0:4] == "a\x00b\x00"
+ else: # UCS4 build
+ assert len(b) == 8
+ if sys.byteorder == "big":
+ assert b[0:8] == "\x00\x00\x00a\x00\x00\x00b"
+ else:
+ assert b[0:8] == "a\x00\x00\x00b\x00\x00\x00"
+
+ def test_array_buffer(self):
+ import array
+ b = buffer(array.array("B", [1, 2, 3]))
+ assert len(b) == 3
+ assert b[0:3] == "\x01\x02\x03"
+
+ def test_nonzero(self):
+ assert buffer('\x00')
+ assert not buffer('')
+ import array
+ assert buffer(array.array("B", [0]))
+ assert not buffer(array.array("B", []))
+
+ def test_str(self):
+ assert str(buffer('hello')) == 'hello'
+
+ def test_repr(self):
+ # from 2.5.2 lib tests
+ assert repr(buffer('hello')).startswith('<read-only buffer for 0x')
+
+ def test_add(self):
+ assert buffer('abc') + 'def' == 'abcdef'
+ import array
+ assert buffer('abc') + array.array('c', 'def') == 'abcdef'
+
+ def test_cmp(self):
+ assert buffer('ab') != 'ab'
+ assert not ('ab' == buffer('ab'))
+ assert buffer('ab') == buffer('ab')
+ assert not (buffer('ab') != buffer('ab'))
+ assert not (buffer('ab') < buffer('ab'))
+ assert buffer('ab') <= buffer('ab')
+ assert not (buffer('ab') > buffer('ab'))
+ assert buffer('ab') >= buffer('ab')
+ assert buffer('ab') != buffer('abc')
+ assert buffer('ab') < buffer('abc')
+ assert buffer('ab') <= buffer('ab')
+ assert buffer('ab') > buffer('aa')
+ assert buffer('ab') >= buffer('ab')
+
+ def test_hash(self):
+ assert hash(buffer('hello')) == hash('hello')
+
+ def test_mul(self):
+ assert buffer('ab') * 5 == 'ababababab'
+ assert buffer('ab') * (-2) == ''
+ assert 5 * buffer('ab') == 'ababababab'
+ assert (-2) * buffer('ab') == ''
+
+ def test_offset_size(self):
+ b = buffer('hello world', 6)
+ assert len(b) == 5
+ assert b[0] == 'w'
+ assert b[:] == 'world'
+ raises(IndexError, 'b[5]')
+ b = buffer(b, 2)
+ assert len(b) == 3
+ assert b[0] == 'r'
+ assert b[:] == 'rld'
+ raises(IndexError, 'b[3]')
+ b = buffer('hello world', 1, 8)
+ assert len(b) == 8
+ assert b[0] == 'e'
+ assert b[:] == 'ello wor'
+ raises(IndexError, 'b[8]')
+ b = buffer(b, 2, 3)
+ assert len(b) == 3
+ assert b[2] == ' '
+ assert b[:] == 'lo '
+ raises(IndexError, 'b[3]')
+ b = buffer('hello world', 55)
+ assert len(b) == 0
+ assert b[:] == ''
+ b = buffer('hello world', 6, 999)
+ assert len(b) == 5
+ assert b[:] == 'world'
+
+ raises(ValueError, buffer, "abc", -1)
+ raises(ValueError, buffer, "abc", 0, -2)
+
+ def test_rw_offset_size(self):
+ import array
+
+ a = array.array("c", 'hello world')
+ b = buffer(a, 6)
+ assert len(b) == 5
+ assert b[0] == 'w'
+ assert b[:] == 'world'
+ raises(IndexError, 'b[5]')
+ b[0] = 'W'
+ assert str(b) == 'World'
+ assert a.tostring() == 'hello World'
+ b[:] = '12345'
+ assert a.tostring() == 'hello 12345'
+ raises(IndexError, 'b[5] = "."')
+ b[4:2] = ''
+ assert a.tostring() == 'hello 12345'
+
+ b = buffer(b, 2)
+ assert len(b) == 3
+ assert b[0] == '3'
+ assert b[:] == '345'
+ raises(IndexError, 'b[3]')
+ b[1] = 'X'
+ assert a.tostring() == 'hello 123X5'
+ raises(IndexError, 'b[3] = "."')
+
+ a = array.array("c", 'hello world')
+ b = buffer(a, 1, 8)
+ assert len(b) == 8
+ assert b[0] == 'e'
+ assert b[:] == 'ello wor'
+ raises(IndexError, 'b[8]')
+ b[0] = 'E'
+ assert str(b) == 'Ello wor'
+ assert a.tostring() == 'hEllo world'
+ b[:] = '12345678'
+ assert a.tostring() == 'h12345678ld'
+ raises(IndexError, 'b[8] = "."')
+
+ b = buffer(b, 2, 3)
+ assert len(b) == 3
+ assert b[2] == '5'
+ assert b[:] == '345'
+ raises(IndexError, 'b[3]')
+ b[1] = 'X'
+ assert a.tostring() == 'h123X5678ld'
+ raises(IndexError, 'b[3] = "."')
+
+ b = buffer(a, 55)
+ assert len(b) == 0
+ assert b[:] == ''
+ b = buffer(a, 6, 999)
+ assert len(b) == 5
+ assert b[:] == '678ld'
+
+ raises(ValueError, buffer, a, -1)
+ raises(ValueError, buffer, a, 0, -2)
+
+ def test_slice(self):
+ # Test extended slicing by comparing with list slicing.
+ s = "".join(chr(c) for c in list(range(255, -1, -1)))
+ b = buffer(s)
+ indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
+ for start in indices:
+ for stop in indices:
+ # Skip step 0 (invalid)
+ for step in indices[1:]:
+ assert b[start:stop:step] == s[start:stop:step]
+
+ def test_getitem_only_ints(self):
+ class MyInt(object):
+ def __init__(self, x):
+ self.x = x
+
+ def __int__(self):
+ return self.x
+
+ buf = buffer('hello world')
+ raises(TypeError, "buf[MyInt(0)]")
+ raises(TypeError, "buf[MyInt(0):MyInt(5)]")
diff --git a/pypy/objspace/std/test/test_memoryview.py b/pypy/objspace/std/test/test_memoryobject.py
rename from pypy/objspace/std/test/test_memoryview.py
rename to pypy/objspace/std/test/test_memoryobject.py
--- a/pypy/objspace/std/test/test_memoryview.py
+++ b/pypy/objspace/std/test/test_memoryobject.py
@@ -1,195 +1,3 @@
-class AppTestBuffer:
- spaceconfig = dict(usemodules=['array'])
-
- def test_init(self):
- import sys
- class A(object):
- def __buffer__(self):
- return buffer('123')
- if '__pypy__' not in sys.builtin_module_names:
- raises(TypeError, buffer, A())
- else:
- assert buffer(A()) == buffer('123')
-
- def test_unicode_buffer(self):
- import sys
- b = buffer(u"ab")
- if sys.maxunicode == 65535: # UCS2 build
- assert len(b) == 4
- if sys.byteorder == "big":
- assert b[0:4] == "\x00a\x00b"
- else:
- assert b[0:4] == "a\x00b\x00"
- else: # UCS4 build
- assert len(b) == 8
- if sys.byteorder == "big":
- assert b[0:8] == "\x00\x00\x00a\x00\x00\x00b"
- else:
- assert b[0:8] == "a\x00\x00\x00b\x00\x00\x00"
-
- def test_array_buffer(self):
- import array
- b = buffer(array.array("B", [1, 2, 3]))
- assert len(b) == 3
- assert b[0:3] == "\x01\x02\x03"
-
- def test_nonzero(self):
- assert buffer('\x00')
- assert not buffer('')
- import array
- assert buffer(array.array("B", [0]))
- assert not buffer(array.array("B", []))
-
- def test_str(self):
- assert str(buffer('hello')) == 'hello'
-
- def test_repr(self):
- # from 2.5.2 lib tests
- assert repr(buffer('hello')).startswith('<read-only buffer for 0x')
-
- def test_add(self):
- assert buffer('abc') + 'def' == 'abcdef'
- import array
- assert buffer('abc') + array.array('c', 'def') == 'abcdef'
-
- def test_cmp(self):
- assert buffer('ab') != 'ab'
- assert not ('ab' == buffer('ab'))
- assert buffer('ab') == buffer('ab')
- assert not (buffer('ab') != buffer('ab'))
- assert not (buffer('ab') < buffer('ab'))
- assert buffer('ab') <= buffer('ab')
- assert not (buffer('ab') > buffer('ab'))
- assert buffer('ab') >= buffer('ab')
- assert buffer('ab') != buffer('abc')
- assert buffer('ab') < buffer('abc')
- assert buffer('ab') <= buffer('ab')
- assert buffer('ab') > buffer('aa')
- assert buffer('ab') >= buffer('ab')
-
- def test_hash(self):
- assert hash(buffer('hello')) == hash('hello')
-
- def test_mul(self):
- assert buffer('ab') * 5 == 'ababababab'
- assert buffer('ab') * (-2) == ''
- assert 5 * buffer('ab') == 'ababababab'
- assert (-2) * buffer('ab') == ''
-
- def test_offset_size(self):
- b = buffer('hello world', 6)
- assert len(b) == 5
- assert b[0] == 'w'
- assert b[:] == 'world'
- raises(IndexError, 'b[5]')
- b = buffer(b, 2)
- assert len(b) == 3
- assert b[0] == 'r'
- assert b[:] == 'rld'
- raises(IndexError, 'b[3]')
- b = buffer('hello world', 1, 8)
- assert len(b) == 8
- assert b[0] == 'e'
- assert b[:] == 'ello wor'
- raises(IndexError, 'b[8]')
- b = buffer(b, 2, 3)
- assert len(b) == 3
- assert b[2] == ' '
- assert b[:] == 'lo '
- raises(IndexError, 'b[3]')
- b = buffer('hello world', 55)
- assert len(b) == 0
- assert b[:] == ''
- b = buffer('hello world', 6, 999)
- assert len(b) == 5
- assert b[:] == 'world'
-
- raises(ValueError, buffer, "abc", -1)
- raises(ValueError, buffer, "abc", 0, -2)
-
- def test_rw_offset_size(self):
- import array
-
- a = array.array("c", 'hello world')
- b = buffer(a, 6)
- assert len(b) == 5
- assert b[0] == 'w'
- assert b[:] == 'world'
- raises(IndexError, 'b[5]')
- b[0] = 'W'
- assert str(b) == 'World'
- assert a.tostring() == 'hello World'
- b[:] = '12345'
- assert a.tostring() == 'hello 12345'
- raises(IndexError, 'b[5] = "."')
- b[4:2] = ''
- assert a.tostring() == 'hello 12345'
-
- b = buffer(b, 2)
- assert len(b) == 3
- assert b[0] == '3'
- assert b[:] == '345'
- raises(IndexError, 'b[3]')
- b[1] = 'X'
- assert a.tostring() == 'hello 123X5'
- raises(IndexError, 'b[3] = "."')
-
- a = array.array("c", 'hello world')
- b = buffer(a, 1, 8)
- assert len(b) == 8
- assert b[0] == 'e'
- assert b[:] == 'ello wor'
- raises(IndexError, 'b[8]')
- b[0] = 'E'
- assert str(b) == 'Ello wor'
- assert a.tostring() == 'hEllo world'
- b[:] = '12345678'
- assert a.tostring() == 'h12345678ld'
- raises(IndexError, 'b[8] = "."')
-
- b = buffer(b, 2, 3)
- assert len(b) == 3
- assert b[2] == '5'
- assert b[:] == '345'
- raises(IndexError, 'b[3]')
- b[1] = 'X'
- assert a.tostring() == 'h123X5678ld'
- raises(IndexError, 'b[3] = "."')
-
- b = buffer(a, 55)
- assert len(b) == 0
- assert b[:] == ''
- b = buffer(a, 6, 999)
- assert len(b) == 5
- assert b[:] == '678ld'
-
- raises(ValueError, buffer, a, -1)
- raises(ValueError, buffer, a, 0, -2)
-
- def test_slice(self):
- # Test extended slicing by comparing with list slicing.
- s = "".join(chr(c) for c in list(range(255, -1, -1)))
- b = buffer(s)
- indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
- for start in indices:
- for stop in indices:
- # Skip step 0 (invalid)
- for step in indices[1:]:
- assert b[start:stop:step] == s[start:stop:step]
-
- def test_getitem_only_ints(self):
- class MyInt(object):
- def __init__(self, x):
- self.x = x
-
- def __int__(self):
- return self.x
-
- buf = buffer('hello world')
- raises(TypeError, "buf[MyInt(0)]")
- raises(TypeError, "buf[MyInt(0):MyInt(5)]")
-
-
class AppTestMemoryView:
def test_basic(self):
v = memoryview("abc")
More information about the pypy-commit
mailing list