Skip to content

Commit

Permalink
gh-110907: AC: Disallow using * with vararg (#110908)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Oct 16, 2023
1 parent 14d2d15 commit bad7a35
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,20 @@ def __init__(self):
"""
self.expect_failure(block, err, lineno=8)

def test_multiple_star_in_args(self):
err = "'my_test_func' uses '*' more than once."
block = """
/*[clinic input]
my_test_func
pos_arg: object
*args: object
*
kw_arg: object
[clinic start generated code]*/
"""
self.expect_failure(block, err, lineno=6)

def test_module_already_got_one(self):
err = "Already defined module 'm'!"
block = """
Expand Down
4 changes: 2 additions & 2 deletions Objects/clinic/typevarobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ typevar.__new__ as typevar_new
name: object(subclass_of="&PyUnicode_Type")
*constraints: object
*
bound: object = None
covariant: bool = False
contravariant: bool = False
Expand All @@ -340,7 +339,7 @@ static PyObject *
typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
PyObject *bound, int covariant, int contravariant,
int infer_variance)
/*[clinic end generated code: output=1d200450ee99226d input=2c07ab87c94f462b]*/
/*[clinic end generated code: output=1d200450ee99226d input=41ae33a916bfe76f]*/
{
if (covariant && contravariant) {
PyErr_SetString(PyExc_ValueError,
Expand Down
9 changes: 9 additions & 0 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5944,6 +5944,7 @@ def parse_star(self, function: Function, version: VersionTuple | None) -> None:
if version is None:
if self.keyword_only:
fail(f"Function {function.name!r} uses '*' more than once.")
self.check_previous_star()
self.check_remaining_star()
self.keyword_only = True
else:
Expand Down Expand Up @@ -6353,6 +6354,14 @@ def check_remaining_star(self, lineno: int | None = None) -> None:
fail(f"Function {self.function.name!r} specifies {symbol!r} "
f"without following parameters.", line_number=lineno)

def check_previous_star(self, lineno: int | None = None) -> None:
assert isinstance(self.function, Function)

for p in self.function.parameters.values():
if p.kind == inspect.Parameter.VAR_POSITIONAL:
fail(f"Function {self.function.name!r} uses '*' more than once.")


def do_post_block_processing_cleanup(self, lineno: int) -> None:
"""
Called when processing the block is done.
Expand Down

0 comments on commit bad7a35

Please sign in to comment.