diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/functools.py | 8 | ||||
-rw-r--r-- | Lib/test/test_functools.py | 10 |
2 files changed, 18 insertions, 0 deletions
diff --git a/Lib/functools.py b/Lib/functools.py index 20a26f9..91e9685 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -241,6 +241,14 @@ def partial(func, *args, **keywords): """New function with partial application of the given arguments and keywords. """ + if hasattr(func, 'func'): + args = func.args + args + tmpkw = func.keywords.copy() + tmpkw.update(keywords) + keywords = tmpkw + del tmpkw + func = func.func + def newfunc(*fargs, **fkeywords): newkeywords = keywords.copy() newkeywords.update(fkeywords) diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 55b96b4..c549ac4 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -131,6 +131,16 @@ class TestPartial: join = self.partial(''.join) self.assertEqual(join(data), '0123456789') + 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)) + @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestPartialC(TestPartial, unittest.TestCase): |