8000 gh-64490: Fix bugs in argument clinic varargs processing by colorfulappl · Pull Request #32092 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-64490: Fix bugs in argument clinic varargs processing #32092

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

Merged
merged 28 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6793f38
Fix a bug which allows more than one varargs
colorfulappl Mar 24, 2022
6203962
Fix an array index out-of-bound bug
colorfulappl Mar 24, 2022
03be6f9
Fix a kwarg parse bug
colorfulappl Mar 24, 2022
cefe5e9
Merge branch 'main' into unpack_keywords_bugfix
colorfulappl Aug 11, 2022
45bde03
Regenerate clinic.test
colorfulappl Aug 11, 2022
bfc5241
Replace `pre_buffer` to a more descriptive name `nargs`
colorfulappl Aug 11, 2022
0b1523c
📜🤖 Added by blurb_it.
blurb-it[bot] Aug 11, 2022
411ad37
Replace inline literals with double backticks in news
colorfulappl Aug 11, 2022
c6a5171
Rename news
colorfulappl Aug 11, 2022
42f9273
Merge branch 'main' into unpack_keywords_bugfix
colorfulappl Aug 12, 2022
7fdd501
Simplify news
colorfulappl Aug 12, 2022
0aa4be4
Regenerate clinic.test
colorfulappl Aug 12, 2022
bbad2bb
Add test "allow no more than one vararg"
colorfulappl Aug 15, 2022
f7b6662
Merge branch 'main' into unpack_keywords_bugfix
colorfulappl Aug 15, 2022
4fb66ab
Regenerate clinic.test
colorfulappl Aug 15, 2022
8f11765
Modify news
colorfulappl Aug 15, 2022
de5b2a4
Update 2022-08-11-09-58-15.gh-issue-64490.PjwhM4.rst
erlend-aasland Aug 15, 2022
85f98ca
Sphinx syntax/format fix
10000 erlend-aasland Aug 15, 2022
58a3da5
Missing blank line
erlend-aasland Aug 15, 2022
0afce9d
Merge branch 'main' into unpack_keywords_bugfix
colorfulappl Nov 23, 2022
87314ff
Add Argument Clinic functional test cases
colorfulappl Nov 24, 2022
a48971d
Fix bug
colorfulappl Nov 24, 2022
e6a4e13
Do not generate unused `noptargs`
colorfulappl Nov 24, 2022
8592676
Update news
colorfulappl Nov 24, 2022
4c682bf
Update Lib/test/clinic.test
colorfulappl Nov 24, 2022
c3d6b73
Delete unnecessary function doc
colorfulappl Nov 24, 2022
8b26eb8
Merge branch 'main' into unpack_keywords_bugfix
colorfulappl Nov 24, 2022
3f88c54
Rerun make clinic
colorfulappl Nov 24, 2022
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
8 changes: 4 additions & 4 deletions Lib/test/clinic.test
Original file line number Diff line number Diff line change
Expand Up @@ -3856,7 +3856,7 @@ test_vararg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject
};
#undef KWTUPLE
PyObject *argsbuf[2];
Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_ssize_t noptargs = Py_MIN(nargs, 1) + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *a;
PyObject *__clinic_args = NULL;

Expand All @@ -3875,7 +3875,7 @@ exit:

static PyObject *
test_vararg_impl(PyObject *module, PyObject *a, PyObject *args)
/*[clinic end generated code: output=6661f3ca97d85e8c input=81d33815ad1bae6e]*/
/*[clinic end generated code: output=e98d5fe6742888b8 input=81d33815ad1bae6e]*/

/*[clinic input]
test_vararg_with_default
Expand Down Expand Up @@ -3929,7 +3929,7 @@ test_vararg_with_default(PyObject *module, PyObject *const *args, Py_ssize_t nar
};
#undef KWTUPLE
PyObject *argsbuf[3];
Py_ssize_t noptargs = 0 + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Py_ssize_t noptargs = Py_MIN(nargs, 1) + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
PyObject *a;
PyObject *__clinic_args = NULL;
int b = 0;
Expand Down Expand Up @@ -3958,7 +3958,7 @@ exit:
static PyObject *
test_vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args,
int b)
/*[clinic end generated code: output=5fe3cfccb1bef781 input=6e110b54acd9b22d]*/
/*[clinic end generated code: output=291e9a5a09831128 input=6e110b54acd9b22d]*/

/*[clinic input]
test_vararg_with_only_defaults
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_clinic.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@ def test_parameters_not_permitted_after_slash_for_now(self):
x: int
""")

def test_parameters_no_more_than_one_vararg(self):
s = self.parse_function_should_fail("""
module foo
foo.bar
*vararg1: object
*vararg2: object
""")
self.assertEqual(s, "Error on line 0:\nToo many var args\n")

def test_function_not_at_column_0(self):
function = self.parse_function("""
module foo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Argument Clinic varargs bugfixes

* Fix out-of-bounds error in :c:func:`!_PyArg_UnpackKeywordsWithVararg`.
* Fix incorrect check which allowed more than one varargs in clinic.py.
* Fix miscalculation of ``noptargs`` in generated code.

2 changes: 1 addition & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2586,7 +2586,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
current_arg = NULL;
}

buf[i + vararg + 1] = current_arg;
buf[i + 1] = current_arg;

if (current_arg) {
--nkwargs;
Expand Down
9 changes: 5 additions & 4 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ def output_templates(self, f):
vararg = NO_VARARG
pos_only = min_pos = max_pos = min_kw_only = pseudo_args = 0
for i, p in enumerate(parameters, 1):
if p.is_keyword_only() or vararg != NO_VARARG:
if p.is_keyword_only():
assert not p.is_positional_only()
if not p.is_optional():
min_kw_only = i - max_pos
Expand Down Expand Up @@ -1015,22 +1015,23 @@ def parser_body(prototype, *fields, declarations=''):
max_pos,
min_kw_only
)
nargs = "nargs"
else:
args_declaration = "_PyArg_UnpackKeywordsWithVararg", "%s, %s, %s, %s" % (
min_pos,
max_pos,
min_kw_only,
vararg
)
nargs = f"Py_MIN(nargs, {max_pos})" if max_pos else "0"
if not new_or_init:
flags = "METH_FASTCALL|METH_KEYWORDS"
parser_prototype = parser_prototype_fastcall_keywords
argname_fmt = 'args[%d]'
declarations = declare_parser(f)
declarations += "\nPyObject *argsbuf[%s];" % len(converters)
if has_optional_kw:
pre_buffer = "0" if vararg != NO_VARARG else "nargs"
declarations += "\nPy_ssize_t noptargs = %s + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - %d;" % (pre_buffer, min_pos + min_kw_only)
declarations += "\nPy_ssize_t noptargs = %s + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - %d;" % (nargs, min_pos + min_kw_only)
parser_code = [normalize_snippet("""
args = %s(args, nargs, NULL, kwnames, &_parser, %s, argsbuf);
if (!args) {{
Expand All @@ -1047,7 +1048,7 @@ def parser_body(prototype, *fields, declarations=''):
declarations += "\nPyObject * const *fastargs;"
declarations += "\nPy_ssize_t nargs = PyTuple_GET_SIZE(args);"
if has_optional_kw:
declarations += "\nPy_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (min_pos + min_kw_only)
declarations += "\nPy_ssize_t noptargs = %s + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - %d;" % (nargs, min_pos + min_kw_only)
parser_code = [normalize_snippet("""
fastargs = %s(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, %s, argsbuf);
if (!fastargs) {{
Expand Down
0