Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy 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 from13 commits
Commits
Show all changes
16 commits
Select commitHold shift + click to select a range
8b08ed5
impl
dg-pbOct 9, 2024
bea722a
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 9, 2024
f346390
Merge remote-tracking branch 'upstream/main' into gh-125028-prohibit-…
dg-pbOct 17, 2024
c7ee84b
Merge remote-tracking branch 'upstream/main' into gh-125028-prohibit-…
dg-pbOct 28, 2024
e741a5c
remove news
dg-pbOct 28, 2024
71e65c3
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 28, 2024
294f189
no loop for check
dg-pbOct 28, 2024
5abc61e
Idiomatic C
dg-pbJan 5, 2025
656361b
remove unrelated changes
dg-pbJan 5, 2025
da4214a
loosen the regex in test
dg-pbJan 5, 2025
26dded8
improved error message
dg-pbJan 5, 2025
e8d7521
doc updates
dg-pbJan 5, 2025
106e6bc
decouple error checking and kw merging + tests
dg-pbJan 6, 2025
5e151aa
make test meaningful for ANY check
dg-pbJan 6, 2025
38ff471
Use ALWAYS_EQ instead of ANY
dg-pbJan 7, 2025
85955d6
comment update
dg-pbJan 7, 2025
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 deletionsDoc/library/functools.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsLib/functools.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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
16 changes: 16 additions & 0 deletionsLib/test/test_functools.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -233,6 +233,12 @@ 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, unittest.mock.ANY)
actual_args, actual_kwds = p()
self.assertEqual(actual_args, (unittest.mock.ANY,))
self.assertEqual(actual_kwds, {})

def test_placeholders_optimization(self):
PH = self.module.Placeholder
Expand All@@ -249,6 +255,16 @@ 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=unittest.mock.ANY)
actual_args, actual_kwds = p()
self.assertEqual(actual_args, ())
self.assertEqual(actual_kwds, {'a': unittest.mock.ANY})

def test_construct_placeholder_singleton(self):
PH = self.module.Placeholder
tp = type(PH)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
:data:`functools.Placeholder` cannot be passed to :func:`functools.partial` as a keyword argument.
13 changes: 13 additions & 0 deletionsModules/_functoolsmodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp