8000 gh-125028: Prohibit placeholders in partial keywords by dg-pb · Pull Request #126062 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-125028: Prohibit placeholders in partial keywords #126062

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

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
3 changes: 1 addition & 2 deletions Doc/library/functools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ The :mod:`functools` module defines the following functions:
>>> remove_first_dear(message)
'Hello, dear world!'

:data:`!Placeholder` has no special treatment when used in a keyword
argument to :func:`!partial`.
:data:`!Placeholder` cannot be passed to :func:`!partial` as a keyword argument.

.. versionchanged:: 3.14
Added support for :data:`Placeholder` in positional arguments.
Expand Down
2 changes: 2 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ def _partial_new(cls, func, /, *args, **keywords):
"or a descriptor")
if args and args[-1] is Placeholder:
raise TypeError("trailing Placeholders are not allowed")
if keywords and Placeholder in keywords.values():
raise TypeError("Placeholder cannot be passed as a keyword argument")
if isinstance(func, base_cls):
pto_phcount = func._phcount
tot_args = func.args
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ def test_placeholders_optimization(self):
self.assertEqual(p2.args, (PH, 0))
self.assertEqual(p2(1), ((1, 0), {}))

def test_placeholders_kw_restriction(self):
PH = self.module.Placeholder
with self.assertRaisesRegex(TypeError, "Placeholder"):
self.partial(capture, a=PH)

def test_construct_placeholder_singleton(self):
PH = self.module.Placeholder
tp = type(PH)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:data:`functools.Placeholder` cannot be passed to :func:`functools.partial` as a keyword argument.
32 changes: 17 additions & 15 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,29 +281,31 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
}

if (pto_kw == NULL || PyDict_GET_SIZE(pto_kw) == 0) {
if (kw == NULL) {
pto->kw = PyDict_New();
}
else if (Py_REFCNT(kw) == 1) {
pto->kw = Py_NewRef(kw);
}
else {
pto->kw = PyDict_Copy(kw);
}
pto->kw = PyDict_New();
}
else {
pto->kw = PyDict_Copy(pto_kw);
if (kw != NULL && pto->kw != NULL) {
if (PyDict_Merge(pto->kw, kw, 1) != 0) {
Py_DECREF(pto);
return NULL;
}
}
}
if (pto->kw == NULL) {
Py_DECREF(pto);
return NULL;
}
if (kw != NULL) {
PyObject *key, *val;
Py_ssize_t pos = 0;
while (PyDict_Next(kw, &pos, &key, &val)) {
if (val == phold) {
Py_DECREF(pto);
PyErr_SetString(PyExc_TypeError,
"Placeholder cannot be passed as a keyword argument");
return NULL;
}
if (PyDict_SetItem(pto->kw, key, val) < 0) {
Py_DECREF(pto);
return NULL;
}
}
}

partial_setvectorcall(pto);
return (PyObject *)pto;
Expand Down
Loading
0