| 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 @@ | ||
| Improve error message for function calls with bad keyword arguments via getargs |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -8,6 +8,7 @@ | |
| #include "pycore_modsupport.h" // export _PyArg_NoKeywords() | ||
| #include "pycore_pylifecycle.h" // _PyArg_Fini | ||
| #include "pycore_tuple.h" // _PyTuple_ITEMS() | ||
| #include "pycore_pyerrors.h" // _Py_CalculateSuggestions() | ||
|
|
||
| /* Export Stable ABIs (abi only) */ | ||
| PyAPI_FUNC(int) _PyArg_Parse_SizeT(PyObject *, const char *, ...); | ||
| Expand Down Expand Up | @@ -1424,12 +1425,31 @@ error_unexpected_keyword_arg(PyObject *kwargs, PyObject *kwnames, PyObject *kwtu | |
| int match = PySequence_Contains(kwtuple, keyword); | ||
| if (match <= 0) { | ||
| if (!match) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "'%S' is an invalid keyword " | ||
| "argument for %.200s%s", | ||
| keyword, | ||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()"); | ||
| PyObject *kwlist = PySequence_List(kwtuple); | ||
| if (!kwlist) { | ||
| return; | ||
| } | ||
| PyObject *suggestion_keyword = _Py_CalculateSuggestions(kwlist, keyword); | ||
| Py_DECREF(kwlist); | ||
|
|
||
| if (suggestion_keyword) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%.200s%s got an unexpected keyword argument '%S'." | ||
| " Did you mean '%S'?", | ||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()", | ||
| keyword, | ||
| suggestion_keyword); | ||
| Py_DECREF(suggestion_keyword); | ||
| } | ||
| else { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%.200s%s got an unexpected keyword argument '%S'", | ||
|
Comment thread
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 QualityI made this error message match the one in ceval.c
Sorry, something went wrong.
All reactions
|
||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()", | ||
| keyword); | ||
| } | ||
|
|
||
| } | ||
| return; | ||
| } | ||
| Expand Down Expand Up | @@ -1457,6 +1477,9 @@ PyArg_ValidateKeywordArguments(PyObject *kwargs) | |
| return 1; | ||
| } | ||
|
|
||
| static PyObject * | ||
| new_kwtuple(const char * const *keywords, int total, int pos); | ||
|
|
||
| #define IS_END_OF_FORMAT(c) (c == '\0' || c == ';' || c == ':') | ||
|
|
||
| static int | ||
| Expand Down Expand Up | @@ -1722,12 +1745,35 @@ vgetargskeywords(PyObject *args, PyObject *kwargs, const char *format, | |
| } | ||
| } | ||
| if (!match) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "'%U' is an invalid keyword " | ||
| "argument for %.200s%s", | ||
| key, | ||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()"); | ||
| PyObject *_pykwtuple = new_kwtuple(kwlist, len, pos); | ||
| if (!_pykwtuple) { | ||
| return cleanreturn(0, &freelist); | ||
| } | ||
| PyObject *pykwlist = PySequence_List(_pykwtuple); | ||
| Py_DECREF(_pykwtuple); | ||
| if (!pykwlist) { | ||
| return cleanreturn(0, &freelist); | ||
| } | ||
| PyObject *suggestion_keyword = _Py_CalculateSuggestions(pykwlist, key); | ||
| Py_DECREF(pykwlist); | ||
|
|
||
| if (suggestion_keyword) { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%.200s%s got an unexpected keyword argument '%S'." | ||
| " Did you mean '%S'?", | ||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()", | ||
| key, | ||
| suggestion_keyword); | ||
| Py_DECREF(suggestion_keyword); | ||
| } | ||
| else { | ||
| PyErr_Format(PyExc_TypeError, | ||
| "%.200s%s got an unexpected keyword argument '%S'", | ||
| (fname == NULL) ? "this function" : fname, | ||
| (fname == NULL) ? "" : "()", | ||
| key); | ||
| } | ||
| return cleanreturn(0, &freelist); | ||
| } | ||
| } | ||
| 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 QualityDo you mind adding a explicit test for when the suggestion fails (the 'else' case in the conditional here
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 QualityThanks for the review! I added this test.
I also made a similar edit in the vgetargskeywords code path and added tests.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.