Author: Amaury Forgeot d'Arc <[email protected]>
Branch: py3.5
Changeset: r88364:b6dc4513e9e5
Date: 2016-11-13 17:24 +0100
http://bitbucket.org/pypy/pypy/changeset/b6dc4513e9e5/

Log:    Some fixes in functools

diff --git a/lib-python/3/test/test_functools.py 
b/lib-python/3/test/test_functools.py
--- a/lib-python/3/test/test_functools.py
+++ b/lib-python/3/test/test_functools.py
@@ -129,6 +129,7 @@
         p = proxy(f)
         self.assertEqual(f.func, p.func)
         f = None
+        support.gc_collect()
         self.assertRaises(ReferenceError, getattr, p, 'func')
 
     def test_with_bound_and_unbound_methods(self):
@@ -229,13 +230,7 @@
                 raise IndexError
 
         f = self.partial(object)
-        if support.check_impl_detail(pypy=True):
-            # CPython fails, pypy does not :-)
-            f.__setstate__(BadSequence())
-        else:
-            self.assertRaisesRegex(SystemError,
-                    "new style getargs format but argument is not a tuple",
-                    f.__setstate__, BadSequence())
+        self.assertRaises(TypeError, f.__setstate__, BadSequence())
 
 
 class TestPartialPy(TestPartial, unittest.TestCase):
diff --git a/lib_pypy/_functools.py b/lib_pypy/_functools.py
--- a/lib_pypy/_functools.py
+++ b/lib_pypy/_functools.py
@@ -42,6 +42,13 @@
         self, func, args = args[0], args[1], args[2:]
         if not callable(func):
             raise TypeError("the first argument must be callable")
+        if isinstance(func, partial):
+            args = func._args + args
+            tmpkw = func._keywords.copy()
+            tmpkw.update(keywords)
+            keywords = tmpkw
+            del tmpkw
+            func = func._func
         self._func = func
         self._args = args
         self._keywords = keywords
@@ -113,3 +120,24 @@
             self.__dict__.clear()
         else:
             self.__dict__.update(d)
+
+
+@builtinify
+def cmp_to_key(mycmp):
+    """Convert a cmp= function into a key= function"""
+    class K(object):
+        __slots__ = ['obj']
+        def __init__(self, obj):
+            self.obj = obj
+        def __lt__(self, other):
+            return mycmp(self.obj, other.obj) < 0
+        def __gt__(self, other):
+            return mycmp(self.obj, other.obj) > 0
+        def __eq__(self, other):
+            return mycmp(self.obj, other.obj) == 0
+        def __le__(self, other):
+            return mycmp(self.obj, other.obj) <= 0
+        def __ge__(self, other):
+            return mycmp(self.obj, other.obj) >= 0
+        __hash__ = None
+    return K
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to