| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -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"); | ||
| Expand Down Expand Up | @@ -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 thread
Comment on lines
-503
to
-504
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIs there a reason why we can remove this comment?
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityRestored, thanks for catching that!
Sorry, something went wrong.
All reactions
|
||
| /* 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); | ||
|
|
||
| Expand Down Expand Up | @@ -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) { | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityNot 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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityMakes sense. Should I remove the explicit check from Lib/functools.py and rely on the standard unpacking error?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.