-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
gh-155733: Validate keyword argument keys in operator.methodcaller and functools.partial #155779
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
Open
pranavchoudhary-tech
wants to merge
7
commits into
python:main
Choose a base branch
from
pranavchoudhary-tech:fix-nonstring-kwargs-155733
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3959ee4
gh-155733: validate keyword keys in operator.methodcaller and functoo…
pranavchoudhary-tech ac51603
Merge remote-tracking branch 'upstream/main' into fix-nonstring-kwarg…
pranavchoudhary-tech c127844
gh-155733: Add news entry for non-string keyword argument validation
pranavchoudhary-tech c3a2dfa
Merge remote-tracking branch 'upstream/main' into fix-nonstring-kwarg…
pranavchoudhary-tech 5003a53
gh-155733: Validate string keys in partial.__setstate__
pranavchoudhary-tech e1c77fe
Merge remote-tracking branch 'upstream/main' into fix-nonstring-kwarg…
pranavchoudhary-tech 2202b91
style: update NEWS formatting, restore comment, and rename valid_keys…
pranavchoudhary-tech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/C_API/2026-08-14-10-54-00.gh-issue-155733.b2A34f.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Validate keyword arguments to :func:`operator.methodcaller` and :func:`functools.partial` to raise a :exc:`TypeError` instead of crashing with non-string keys. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,11 +190,16 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |
| return NULL; | ||
| } | ||
|
|
||
| /* keyword Placeholder prohibition */ | ||
| /* keyword Placeholder prohibition and key type validation */ | ||
| if (kw != NULL) { | ||
| PyObject *key, *val; | ||
| Py_ssize_t pos = 0; | ||
| while (PyDict_Next(kw, &pos, &key, &val)) { | ||
| if (!PyUnicode_Check(key)) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "keywords must be strings"); | ||
| return NULL; | ||
| } | ||
| if (val == phold) { | ||
| PyErr_SetString(PyExc_TypeError, | ||
| "Placeholder cannot be passed as a keyword argument"); | ||
|
|
@@ -500,18 +505,29 @@ partial_vectorcall(PyObject *self, PyObject *const *args, | |
| PyTuple_SET_ITEM(tot_kwnames, pto_nkwds + i, key); | ||
| } | ||
|
|
||
| /* Copy pto_keywords with overlapping call keywords merged | ||
| * Note, tail is already coppied. */ | ||
|
Comment on lines
-503
to
-504
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason why we can remove this comment?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restored, thanks for catching that! |
||
| /* Copy pto_keywords with overlapping call keywords merged. | ||
| * Note, tail is already copied. */ | ||
| Py_ssize_t pos = 0, i = 0; | ||
| PyObject *keyword_dict = n_merges ? pto_kw_merged : partial_keywords; | ||
| int valid_kwargs = 1; | ||
| Py_BEGIN_CRITICAL_SECTION(keyword_dict); | ||
| while (PyDict_Next(keyword_dict, &pos, &key, &val)) { | ||
| if (!PyUnicode_Check(key)) { | ||
| valid_kwargs = 0; | ||
| break; | ||
| } | ||
| assert(i < pto_nkwds); | ||
| PyTuple_SET_ITEM(tot_kwnames, i, Py_NewRef(key)); | ||
| stack[tot_nargs + i] = val; | ||
| i++; | ||
| } | ||
| Py_END_CRITICAL_SECTION(); | ||
| if (!valid_kwargs) { | ||
| PyErr_SetString(PyExc_TypeError, "keywords must be strings"); | ||
| Py_XDECREF(pto_kw_merged); | ||
| Py_DECREF(tot_kwnames); | ||
| goto clean_stack; | ||
| } | ||
| assert(i == pto_nkwds); | ||
| Py_XDECREF(pto_kw_merged); | ||
|
|
||
|
|
@@ -816,6 +832,16 @@ partial_setstate(PyObject *self, PyObject *state) | |
| PyErr_SetString(PyExc_TypeError, "invalid partial state"); | ||
| return NULL; | ||
| } | ||
| if (kw != Py_None) { | ||
| Py_ssize_t pos = 0; | ||
| PyObject *key, *val; | ||
| while (PyDict_Next(kw, &pos, &key, &val)) { | ||
| if (!PyUnicode_Check(key)) { | ||
| PyErr_SetString(PyExc_TypeError, "keywords must be strings"); | ||
| return NULL; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Py_ssize_t nargs = PyTuple_GET_SIZE(fnargs); | ||
| if (nargs && PyTuple_GET_ITEM(fnargs, nargs - 1) == pto->placeholder) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you really need this check. You'll get the error when calling the function anyway. For instance, we don't check that Placeholder isn't passed as a keyword value either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Should I remove the explicit check from Lib/functools.py and rely on the standard unpacking error?