Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-72249: Include the module name in the repr of partial object #101910

Merged
merged 18 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ def __call__(self, /, *args, **keywords):
@recursive_repr()
def __repr__(self):
qualname = type(self).__qualname__
module = type(self).__module__
furkanonder marked this conversation as resolved.
Show resolved Hide resolved
args = [repr(self.func)]
args.extend(repr(x) for x in self.args)
args.extend(f"{k}={v!r}" for (k, v) in self.keywords.items())
if type(self).__module__ == "functools":
return f"functools.{qualname}({', '.join(args)})"
return f"{qualname}({', '.join(args)})"
return f"{module}.{qualname}({', '.join(args)})"

def __reduce__(self):
return type(self), (self.func,), (self.func, self.args,
Expand Down
10 changes: 2 additions & 8 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ def test_repr(self):
kwargs = {'a': object(), 'b': object()}
kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
'b={b!r}, a={a!r}'.format_map(kwargs)]
if self.partial in (c_functools.partial, py_functools.partial):
name = 'functools.partial'
else:
name = self.partial.__name__
name = f"{self.partial.__module__}.{self.partial.__qualname__}"

f = self.partial(capture)
self.assertEqual(f'{name}({capture!r})', repr(f))
Expand All @@ -223,10 +220,7 @@ def test_repr(self):
for kwargs_repr in kwargs_reprs])

def test_recursive_repr(self):
if self.partial in (c_functools.partial, py_functools.partial):
name = 'functools.partial'
else:
name = self.partial.__name__
name = f"{self.partial.__module__}.{self.partial.__qualname__}"

f = self.partial(capture)
f.__setstate__((f, (), {}, {}))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Include the module name in the repr of partial object.
Change by Furkan Onder and Anilyka Barry.
17 changes: 15 additions & 2 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ partial_repr(partialobject *pto)
{
PyObject *result = NULL;
PyObject *arglist;
PyObject *mod;
Py_ssize_t i, n;
PyObject *key, *value;
int status;
Expand Down Expand Up @@ -399,8 +400,20 @@ partial_repr(partialobject *pto)
if (arglist == NULL)
goto done;
}
result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name,
pto->fn, arglist);

mod = PyObject_GetAttrString((PyObject *)pto, "__module__");
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
if (mod == NULL) {
result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, pto->fn, arglist);
}
else {
if (strcmp(PyUnicode_AsUTF8(mod), "functools") == 0) {
result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name, pto->fn, arglist);
}
else {
result = PyUnicode_FromFormat("%U.%s(%R%U)", mod, Py_TYPE(pto)->tp_name, pto->fn, arglist);
serhiy-storchaka marked this conversation as resolved.
Show resolved Hide resolved
}
}

Py_DECREF(arglist);

done:
Expand Down