[pypy-commit] pypy py3k: rekill buffer and some py2 only tests
pjenvey
noreply at buildbot.pypy.org
Wed Apr 30 01:11:58 CEST 2014
Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r71071:38c291084e78
Date: 2014-04-29 16:07 -0700
http://bitbucket.org/pypy/pypy/changeset/38c291084e78/
Log: rekill buffer and some py2 only tests
diff --git a/pypy/objspace/std/bufferobject.py b/pypy/objspace/std/bufferobject.py
deleted file mode 100644
--- a/pypy/objspace/std/bufferobject.py
+++ /dev/null
@@ -1,159 +0,0 @@
-"""
-Implementation of the 'buffer' and 'memoryview' types.
-"""
-import operator
-
-from rpython.rlib.buffer import Buffer, StringBuffer, SubBuffer
-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
-
-
-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)
- self.buf = buf
-
- def buffer_w(self, space, flags):
- space.check_buf_flags(flags, self.buf.readonly)
- return self.buf
-
- def readbuf_w(self, space):
- return self.buf
-
- def writebuf_w(self, space):
- if self.buf.readonly:
- raise OperationError(space.w_TypeError, space.wrap(
- "buffer is read-only"))
- 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):
- 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"))
- buf = 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)
-
- def descr_setitem(self, space, w_index, w_obj):
- if self.buf.readonly:
- raise OperationError(space.w_TypeError,
- space.wrap("buffer is read-only"))
- start, stop, step, size = space.decode_index4(w_index, self.buf.getlength())
- value = space.readbuf_w(w_obj)
- if step == 0: # index only
- if value.getlength() != 1:
- msg = "right operand must be a single byte"
- raise OperationError(space.w_TypeError, space.wrap(msg))
- self.buf.setitem(start, value.getitem(0))
- else:
- if value.getlength() != size:
- msg = "right operand length must match slice length"
- raise OperationError(space.w_TypeError, space.wrap(msg))
- if step == 1:
- self.buf.setslice(start, value.as_str())
- else:
- for i in range(size):
- self.buf.setitem(start + i * step, value.getitem(i))
-
- 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 self.buf.readonly:
- info = 'read-only buffer'
- else:
- info = 'read-write 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/test/test_bufferobject.py b/pypy/objspace/std/test/test_bufferobject.py
deleted file mode 100644
--- a/pypy/objspace/std/test/test_bufferobject.py
+++ /dev/null
@@ -1,199 +0,0 @@
-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]')
- exc = raises(TypeError, "b[0] = 'W'")
- assert str(exc.value) == "buffer is read-only"
- exc = raises(TypeError, "b[:] = '12345'")
- assert str(exc.value) == "buffer is read-only"
- exc = raises(TypeError, 'b[5] = "."')
- assert str(exc.value) == "buffer is read-only"
- exc = raises(TypeError, "b[4:2] = ''")
- assert str(exc.value) == "buffer is read-only"
- assert str(b) == 'world'
- assert a.tostring() == 'hello world'
-
- b = buffer(b, 2)
- assert len(b) == 3
- assert b[0] == 'r'
- assert b[:] == 'rld'
- raises(IndexError, 'b[3]')
- exc = raises(TypeError, "b[1] = 'X'")
- assert str(exc.value) == "buffer is read-only"
- exc = raises(TypeError, 'b[3] = "."')
- assert str(exc.value) == "buffer is read-only"
- assert a.tostring() == 'hello world'
-
- 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]')
- exc = raises(TypeError, "b[0] = 'E'")
- assert str(exc.value) == "buffer is read-only"
- assert str(b) == 'ello wor'
- assert a.tostring() == 'hello world'
- exc = raises(TypeError, "b[:] = '12345678'")
- assert str(exc.value) == "buffer is read-only"
- assert a.tostring() == 'hello world'
- exc = raises(TypeError, 'b[8] = "."')
- assert str(exc.value) == "buffer is read-only"
-
- b = buffer(b, 2, 3)
- assert len(b) == 3
- assert b[2] == ' '
- assert b[:] == 'lo '
- raises(IndexError, 'b[3]')
- exc = raises(TypeError, "b[1] = 'X'")
- assert str(exc.value) == "buffer is read-only"
- assert a.tostring() == 'hello world'
- exc = raises(TypeError, 'b[3] = "."')
- assert str(exc.value) == "buffer is read-only"
-
- b = buffer(a, 55)
- assert len(b) == 0
- assert b[:] == ''
- b = buffer(a, 6, 999)
- assert len(b) == 5
- assert b[:] == 'world'
-
- 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_floatobject.py b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -145,10 +145,8 @@
assert repr(float("+nan")) == "nan"
assert repr(float("-nAn")) == "nan"
- assert float(buffer("inf")) == inf
- assert float(bytearray("inf")) == inf
- exc = raises(TypeError, float, memoryview("inf"))
- assert str(exc.value) == "float() argument must be a string or a number"
+ assert float(memoryview(b"inf")) == inf
+ assert float(bytearray(b"inf")) == inf
def test_float_unicode(self):
# u00A0 and u2000 are some kind of spaces
diff --git a/pypy/objspace/std/test/test_strbufobject.py b/pypy/objspace/std/test/test_strbufobject.py
--- a/pypy/objspace/std/test/test_strbufobject.py
+++ b/pypy/objspace/std/test/test_strbufobject.py
@@ -46,7 +46,6 @@
def test_buffer(self):
s = 'a'.__add__('b')
- assert buffer(s) == buffer('ab')
assert memoryview(s) == 'ab'
def test_add_strbuf(self):
More information about the pypy-commit
mailing list