]> granicus.if.org Git - python/commitdiff
Issue #25137: Add a note to whatsnew/3.5.rst for nested functools.partial calls
authorBerker Peksag <berker.peksag@gmail.com>
Tue, 22 Sep 2015 10:08:16 +0000 (13:08 +0300)
committerBerker Peksag <berker.peksag@gmail.com>
Tue, 22 Sep 2015 10:08:16 +0000 (13:08 +0300)
Also, properly skip the test_nested_optimization test for partial subclasses
and add a test for the suggested usage.

Doc/whatsnew/3.5.rst
Lib/test/test_functools.py

index 8c85b1498f4e63b22597ce260df290d53c8f470a..56cb00c293ccede7e3ddc6a7a65e28afc2f7c587 100644 (file)
@@ -2441,6 +2441,12 @@ Changes in the Python API
   module and the :func:`help` function.
   (Contributed by Serhiy Storchaka in :issue:`15582`.)
 
+* Nested :func:`functools.partial` calls are now flattened.  If you were
+  relying on the previous behavior, you can now either add an attribute to a
+  :func:`functools.partial` object or you can create a subclass of
+  :func:`functools.partial`.
+  (Contributed by Alexander Belopolsky in :issue:`7830`.)
+
 Changes in the C API
 --------------------
 
index ae929eca99f281b02b0a9e9514bd96bb613732e9..7ecf877b11de22b9acb3eef29f13961ce8d86ed3 100644 (file)
@@ -139,14 +139,23 @@ class TestPartial:
 
     def test_nested_optimization(self):
         partial = self.partial
-        # Only "true" partial is optimized
-        if partial.__name__ != 'partial':
-            return
         inner = partial(signature, 'asdf')
         nested = partial(inner, bar=True)
         flat = partial(signature, 'asdf', bar=True)
         self.assertEqual(signature(nested), signature(flat))
 
+    def test_nested_partial_with_attribute(self):
+        # see issue 25137
+        partial = self.partial
+
+        def foo(bar):
+            return bar
+
+        p = partial(foo, 'first')
+        p2 = partial(p, 'second')
+        p2.new_attr = 'spam'
+        self.assertEqual(p2.new_attr, 'spam')
+
 
 @unittest.skipUnless(c_functools, 'requires the C _functools module')
 class TestPartialC(TestPartial, unittest.TestCase):
@@ -238,6 +247,9 @@ class TestPartialCSubclass(TestPartialC):
     if c_functools:
         partial = PartialSubclass
 
+    # partial subclasses are not optimized for nested calls
+    test_nested_optimization = None
+
 
 class TestPartialMethod(unittest.TestCase):