[Python-checkins] cpython (3.5): Issue #25137: Add a note to whatsnew/3.5.rst for nested functools.partial calls

berker.peksag python-checkins at python.org
Tue Sep 22 12:24:40 CEST 2015


https://hg.python.org/cpython/rev/fa766b6f12b5
changeset:   98171:fa766b6f12b5
branch:      3.5
parent:      98166:9a66286b970c
user:        Berker Peksag <berker.peksag at gmail.com>
date:        Tue Sep 22 13:08:16 2015 +0300
summary:
  Issue #25137: Add a note to whatsnew/3.5.rst for nested functools.partial calls

Also, properly skip the test_nested_optimization test for partial subclasses
and add a test for the suggested usage.

files:
  Doc/whatsnew/3.5.rst       |   6 ++++++
  Lib/test/test_functools.py |  18 +++++++++++++++---
  2 files changed, 21 insertions(+), 3 deletions(-)


diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -2441,6 +2441,12 @@
   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
 --------------------
 
diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py
--- a/Lib/test/test_functools.py
+++ b/Lib/test/test_functools.py
@@ -139,14 +139,23 @@
 
     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 @@
     if c_functools:
         partial = PartialSubclass
 
+    # partial subclasses are not optimized for nested calls
+    test_nested_optimization = None
+
 
 class TestPartialMethod(unittest.TestCase):
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list