mirror of
https://github.com/python/cpython.git
synced 2025-10-02 21:25:24 +00:00
gh-72249: Include the module name in the repr of partial object (GH-101910)
Co-authored-by: Anilyka Barry <vgr255@live.ca>
This commit is contained in:
parent
f082a05c67
commit
8f5be78bce
4 changed files with 27 additions and 18 deletions
|
@ -303,13 +303,13 @@ class partial:
|
||||||
|
|
||||||
@recursive_repr()
|
@recursive_repr()
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
qualname = type(self).__qualname__
|
cls = type(self)
|
||||||
|
qualname = cls.__qualname__
|
||||||
|
module = cls.__module__
|
||||||
args = [repr(self.func)]
|
args = [repr(self.func)]
|
||||||
args.extend(repr(x) for x in self.args)
|
args.extend(repr(x) for x in self.args)
|
||||||
args.extend(f"{k}={v!r}" for (k, v) in self.keywords.items())
|
args.extend(f"{k}={v!r}" for (k, v) in self.keywords.items())
|
||||||
if type(self).__module__ == "functools":
|
return f"{module}.{qualname}({', '.join(args)})"
|
||||||
return f"functools.{qualname}({', '.join(args)})"
|
|
||||||
return f"{qualname}({', '.join(args)})"
|
|
||||||
|
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
return type(self), (self.func,), (self.func, self.args,
|
return type(self), (self.func,), (self.func, self.args,
|
||||||
|
|
|
@ -31,10 +31,6 @@ c_functools = import_helper.import_fresh_module('functools',
|
||||||
|
|
||||||
decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])
|
decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])
|
||||||
|
|
||||||
_partial_types = [py_functools.partial]
|
|
||||||
if c_functools:
|
|
||||||
_partial_types.append(c_functools.partial)
|
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def replaced_module(name, replacement):
|
def replaced_module(name, replacement):
|
||||||
|
@ -207,10 +203,7 @@ class TestPartial:
|
||||||
kwargs = {'a': object(), 'b': object()}
|
kwargs = {'a': object(), 'b': object()}
|
||||||
kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
|
kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
|
||||||
'b={b!r}, a={a!r}'.format_map(kwargs)]
|
'b={b!r}, a={a!r}'.format_map(kwargs)]
|
||||||
if self.partial in _partial_types:
|
name = f"{self.partial.__module__}.{self.partial.__qualname__}"
|
||||||
name = 'functools.partial'
|
|
||||||
else:
|
|
||||||
name = self.partial.__name__
|
|
||||||
|
|
||||||
f = self.partial(capture)
|
f = self.partial(capture)
|
||||||
self.assertEqual(f'{name}({capture!r})', repr(f))
|
self.assertEqual(f'{name}({capture!r})', repr(f))
|
||||||
|
@ -229,10 +222,7 @@ class TestPartial:
|
||||||
for kwargs_repr in kwargs_reprs])
|
for kwargs_repr in kwargs_reprs])
|
||||||
|
|
||||||
def test_recursive_repr(self):
|
def test_recursive_repr(self):
|
||||||
if self.partial in _partial_types:
|
name = f"{self.partial.__module__}.{self.partial.__qualname__}"
|
||||||
name = 'functools.partial'
|
|
||||||
else:
|
|
||||||
name = self.partial.__name__
|
|
||||||
|
|
||||||
f = self.partial(capture)
|
f = self.partial(capture)
|
||||||
f.__setstate__((f, (), {}, {}))
|
f.__setstate__((f, (), {}, {}))
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
:func:`functools.partial`s of :func:`repr` has been improved to include the
|
||||||
|
:term:`module` name. Patched by Furkan Onder and Anilyka Barry.
|
|
@ -365,6 +365,8 @@ partial_repr(partialobject *pto)
|
||||||
{
|
{
|
||||||
PyObject *result = NULL;
|
PyObject *result = NULL;
|
||||||
PyObject *arglist;
|
PyObject *arglist;
|
||||||
|
PyObject *mod;
|
||||||
|
PyObject *name;
|
||||||
Py_ssize_t i, n;
|
Py_ssize_t i, n;
|
||||||
PyObject *key, *value;
|
PyObject *key, *value;
|
||||||
int status;
|
int status;
|
||||||
|
@ -399,13 +401,28 @@ partial_repr(partialobject *pto)
|
||||||
if (arglist == NULL)
|
if (arglist == NULL)
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
result = PyUnicode_FromFormat("%s(%R%U)", Py_TYPE(pto)->tp_name,
|
|
||||||
pto->fn, arglist);
|
mod = _PyType_GetModuleName(Py_TYPE(pto));
|
||||||
|
if (mod == NULL) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
name = PyType_GetQualName(Py_TYPE(pto));
|
||||||
|
if (name == NULL) {
|
||||||
|
Py_DECREF(mod);
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
result = PyUnicode_FromFormat("%S.%S(%R%U)", mod, name, pto->fn, arglist);
|
||||||
|
Py_DECREF(mod);
|
||||||
|
Py_DECREF(name);
|
||||||
Py_DECREF(arglist);
|
Py_DECREF(arglist);
|
||||||
|
|
||||||
done:
|
done:
|
||||||
Py_ReprLeave((PyObject *)pto);
|
Py_ReprLeave((PyObject *)pto);
|
||||||
return result;
|
return result;
|
||||||
|
error:
|
||||||
|
Py_DECREF(arglist);
|
||||||
|
Py_ReprLeave((PyObject *)pto);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Pickle strategy:
|
/* Pickle strategy:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue