Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bring functools.py partial implementation more in line with C code
in partial.__new__, before checking for the existence of the attribute
'func', first check whether the argument is an instance of partial.
  • Loading branch information
cfbolz committed Dec 14, 2022
commit c14955c1ee5abf94db58f1b26728f040d5302c46
2 changes: 1 addition & 1 deletion Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def __new__(cls, func, /, *args, **keywords):
if not callable(func):
raise TypeError("the first argument must be callable")

if hasattr(func, "func"):
if isinstance(func, partial) and hasattr(func, "func"):
args = func.args + args
keywords = {**func.keywords, **keywords}
func = func.func
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ def test_nested_optimization(self):
flat = partial(signature, 'asdf', bar=True)
self.assertEqual(signature(nested), signature(flat))

def test_nested_optimization_bug(self):
partial = self.partial
class Builder:
def __call__(self, tag, *children, **attrib):
return (tag, children, attrib)

def __getattr__(self, tag):
return partial(self, tag)

B = Builder()
m = B.m
assert m(1, 2, a=2) == ('m', (1, 2), dict(a=2))

def test_nested_partial_with_attribute(self):
# see issue 25137
partial = self.partial
Expand Down