[pypy-commit] pypy default: update to cffi/e46941e99bf4
arigo
pypy.commits at gmail.com
Mon Feb 6 03:57:46 EST 2017
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r89968:3e215d0078ab
Date: 2017-02-06 09:56 +0100
http://bitbucket.org/pypy/pypy/changeset/3e215d0078ab/
Log: update to cffi/e46941e99bf4
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
@@ -56,6 +56,62 @@
e.w_type = space.w_ValueError
raise
+ def _comparison_helper(self, space, w_other, mode):
+ if space.isinstance_w(w_other, space.w_unicode):
+ return space.w_NotImplemented
+ try:
+ other_buf = space.buffer_w(w_other, space.BUF_SIMPLE)
+ except OperationError as e:
+ if e.async(space):
+ raise
+ return space.w_NotImplemented
+ my_buf = self.buf
+ my_len = len(my_buf)
+ other_len = len(other_buf)
+ if other_len != my_len:
+ if mode == 'E':
+ return space.w_False
+ if mode == 'N':
+ return space.w_True
+ cmp = _memcmp(my_buf, other_buf, min(my_len, other_len))
+ if cmp == 0:
+ if my_len < other_len:
+ cmp = -1
+ elif my_len > other_len:
+ cmp = 1
+
+ if mode == 'L': res = cmp < 0
+ elif mode == 'l': res = cmp <= 0
+ elif mode == 'E': res = cmp == 0
+ elif mode == 'N': res = cmp != 0
+ elif mode == 'G': res = cmp > 0
+ elif mode == 'g': res = cmp >= 0
+ else: raise AssertionError
+
+ return space.newbool(res)
+
+ def descr_eq(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'E')
+ def descr_ne(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'N')
+ def descr_lt(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'L')
+ def descr_le(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'l')
+ def descr_gt(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'G')
+ def descr_ge(self, space, w_other):
+ return self._comparison_helper(space, w_other, 'g')
+
+def _memcmp(buf1, buf2, length):
+ # XXX very slow
+ for i in range(length):
+ if buf1[i] < buf2[i]:
+ return -1
+ if buf1[i] > buf2[i]:
+ return 1
+ return 0
+
@unwrap_spec(w_cdata=cdataobj.W_CData, size=int)
def MiniBuffer___new__(space, w_subtype, w_cdata, size=-1):
ctype = w_cdata.ctype
@@ -85,6 +141,12 @@
__len__ = interp2app(MiniBuffer.descr_len),
__getitem__ = interp2app(MiniBuffer.descr_getitem),
__setitem__ = interp2app(MiniBuffer.descr_setitem),
+ __eq__ = interp2app(MiniBuffer.descr_eq),
+ __ne__ = interp2app(MiniBuffer.descr_ne),
+ __lt__ = interp2app(MiniBuffer.descr_lt),
+ __le__ = interp2app(MiniBuffer.descr_le),
+ __gt__ = interp2app(MiniBuffer.descr_gt),
+ __ge__ = interp2app(MiniBuffer.descr_ge),
__weakref__ = make_weakref_descr(MiniBuffer),
__str__ = interp2app(MiniBuffer.descr_str),
__doc__ = """ffi.buffer(cdata[, byte_size]):
diff --git a/pypy/module/_cffi_backend/test/test_ffi_obj.py b/pypy/module/_cffi_backend/test/test_ffi_obj.py
--- a/pypy/module/_cffi_backend/test/test_ffi_obj.py
+++ b/pypy/module/_cffi_backend/test/test_ffi_obj.py
@@ -259,6 +259,25 @@
assert ffi.buffer(cdata=a, size=2)[:] == b'\x05\x06'
assert type(ffi.buffer(a)) is ffi.buffer
+ def test_ffi_buffer_comparisons(self):
+ import _cffi_backend as _cffi1_backend
+ ffi = _cffi1_backend.FFI()
+ ba = bytearray(range(100, 110))
+ assert ba == memoryview(ba) # justification for the following
+ a = ffi.new("uint8_t[]", list(ba))
+ c = ffi.new("uint8_t[]", [99] + list(ba))
+ b_full = ffi.buffer(a)
+ b_short = ffi.buffer(a, 3)
+ b_mid = ffi.buffer(a, 6)
+ b_other = ffi.buffer(c, 6)
+ content = b_full[:]
+ assert content == b_full == ba
+ assert b_short < b_mid < b_full
+ assert b_other < b_short < b_mid < b_full
+ assert ba > b_mid > ba[0:2]
+ assert b_short != ba[1:4]
+ assert b_short != 42
+
def test_ffi_from_buffer(self):
import _cffi_backend as _cffi1_backend
import array
diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -1254,6 +1254,7 @@
@specialize.argtype(1)
def _memcmp(selfvalue, buffer, length):
+ # XXX that's very slow if selfvalue or buffer are Buffer objects
for i in range(length):
if selfvalue[i] < buffer[i]:
return -1
More information about the pypy-commit
mailing list