Author: Armin Rigo <ar...@tunes.org>
Branch: py3.5
Changeset: r89969:451124f17d87
Date: 2017-02-06 10:13 +0100
http://bitbucket.org/pypy/pypy/changeset/451124f17d87/

Log:    hg merge default

diff --git a/pypy/doc/whatsnew-head.rst b/pypy/doc/whatsnew-head.rst
--- a/pypy/doc/whatsnew-head.rst
+++ b/pypy/doc/whatsnew-head.rst
@@ -143,3 +143,11 @@
 crypographically good way).  It is the default in PyPy3.  The default of
 PyPy2 remains unchanged: there are user programs out there that depend
 on constant hashes (or even sometimes on specific hash results).
+
+.. branch: dict-move-to-end
+
+Our dicts, which are always ordered, now have an extra "method" for
+Python 3.x which moves an item to first or last position.  In PyPy 3.5
+it is the standard ``OrderedDict.move_to_end()`` method, but the
+behavior is also available on Python 2.x or for the ``dict`` type by
+calling ``__pypy__.move_to_end(dict, key, last=True)``.
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
@@ -1,5 +1,5 @@
 from pypy.interpreter.baseobjspace import W_Root
-from pypy.interpreter.error import oefmt
+from pypy.interpreter.error import OperationError, oefmt
 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
@@ -75,6 +75,62 @@
             self.buffer.setslice(start, value.as_str())
 
 
+    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.buffer
+        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
@@ -104,6 +160,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),
     __doc__ = """ffi.buffer(cdata[, byte_size]):
 Return a read-write buffer object that references the raw C data
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/module/test_lib_pypy/cffi_tests/cffi0/test_cdata.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_cdata.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_cdata.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_cdata.py
@@ -27,6 +27,8 @@
     def _get_types(self):
         return "CData", "CType"
 
+    buffer = "buffer type"
+
 
 class FakeType(object):
     def __init__(self, cdecl):
diff --git a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py 
b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
--- a/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/cffi0/test_parsing.py
@@ -48,6 +48,8 @@
     def _get_types(self):
         return "CData", "CType"
 
+    buffer = "buffer type"
+
 class FakeType(object):
     def __init__(self, cdecl):
         self.cdecl = cdecl
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
@@ -1324,6 +1324,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
diff --git a/rpython/jit/metainterp/test/test_dict.py 
b/rpython/jit/metainterp/test/test_dict.py
--- a/rpython/jit/metainterp/test/test_dict.py
+++ b/rpython/jit/metainterp/test/test_dict.py
@@ -364,7 +364,7 @@
                     if n in mdict:
                         raise Exception
         self.meta_interp(f, [10])
-        self.check_simple_loop(call_may_force_i=0, call_i=3, call_n=1)
+        self.check_simple_loop(call_may_force_i=0, call_i=2, call_n=1)
 
     def test_dict_virtual(self):
         myjitdriver = JitDriver(greens = [], reds = 'auto')
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to