Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
3 changes: 3 additions & 0 deletions Lib/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ def _partial_new(cls, func, /, *args, **keywords):
"or a descriptor")
if args and args[-1] is Placeholder:
raise TypeError("trailing Placeholders are not allowed")
for value in keywords.values():
if value is Placeholder:
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
19 changes: 19 additions & 0 deletions Lib/test/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import contextlib
from inspect import Signature

from test.support import ALWAYS_EQ
from test.support import import_helper
from test.support import threading_helper

Expand Down Expand Up @@ -233,6 +234,13 @@ def test_placeholders(self):
actual_args, actual_kwds = p('x', 'y')
self.assertEqual(actual_args, ('x', 0, 'y', 1))
self.assertEqual(actual_kwds, {})
# Checks via `is` and not `eq`
# thus unittest.mock.ANY isn't treated as Placeholder
p = self.partial(capture, ALWAYS_EQ)
actual_args, actual_kwds = p()
self.assertEqual(len(actual_args), 1)
self.assertIs(actual_args[0], ALWAYS_EQ)
self.assertEqual(actual_kwds, {})

def test_placeholders_optimization(self):
PH = self.module.Placeholder
Expand All @@ -249,6 +257,17 @@ 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)
# Passes, as checks via `is` and not `eq`
p = self.partial(capture, a=ALWAYS_EQ)
actual_args, actual_kwds = p()
self.assertEqual(actual_args, ())
self.assertEqual(len(actual_kwds), 1)
self.assertIs(actual_kwds['a'], ALWAYS_EQ)

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.
13 changes: 13 additions & 0 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
return NULL;
}

/* keyword Placeholder prohibition */
if (kw != NULL) {
PyObject *key, *val;
Py_ssize_t pos = 0;
while (PyDict_Next(kw, &pos, &key, &val)) {
if (val == phold) {
PyErr_SetString(PyExc_TypeError,
"Placeholder cannot be passed as a keyword argument");
return NULL;
}
}
}

/* check wrapped function / object */
pto_args = pto_kw = NULL;
int res = PyObject_TypeCheck(func, state->partial_type);
Expand Down
Loading