mirror of
https://github.com/python/cpython.git
synced 2025-10-02 13:22:19 +00:00
[3.11] gh-106368: Harden Argument Clinic parser tests (GH-106384) (#106388)
(cherry picked from commit 648688c137
)
Co-authored-by: Erlend E. Aasland <erlend@python.org>
This commit is contained in:
parent
3fdda976db
commit
76889641e6
1 changed files with 68 additions and 42 deletions
|
@ -642,7 +642,7 @@ class ClinicParserTest(TestCase):
|
||||||
self.assertEqual(out, expected_msg)
|
self.assertEqual(out, expected_msg)
|
||||||
|
|
||||||
def test_disallowed_grouping__two_top_groups_on_right(self):
|
def test_disallowed_grouping__two_top_groups_on_right(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.two_top_groups_on_right
|
foo.two_top_groups_on_right
|
||||||
param: int
|
param: int
|
||||||
|
@ -653,9 +653,14 @@ class ClinicParserTest(TestCase):
|
||||||
group2 : int
|
group2 : int
|
||||||
]
|
]
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function two_top_groups_on_right has an unsupported group "
|
||||||
|
"configuration. (Unexpected state 6.b)"
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_disallowed_grouping__parameter_after_group_on_right(self):
|
def test_disallowed_grouping__parameter_after_group_on_right(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.parameter_after_group_on_right
|
foo.parameter_after_group_on_right
|
||||||
param: int
|
param: int
|
||||||
|
@ -666,9 +671,14 @@ class ClinicParserTest(TestCase):
|
||||||
group2 : int
|
group2 : int
|
||||||
]
|
]
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function parameter_after_group_on_right has an unsupported group "
|
||||||
|
"configuration. (Unexpected state 6.a)"
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_disallowed_grouping__group_after_parameter_on_left(self):
|
def test_disallowed_grouping__group_after_parameter_on_left(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.group_after_parameter_on_left
|
foo.group_after_parameter_on_left
|
||||||
[
|
[
|
||||||
|
@ -679,9 +689,14 @@ class ClinicParserTest(TestCase):
|
||||||
]
|
]
|
||||||
param: int
|
param: int
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function group_after_parameter_on_left has an unsupported group "
|
||||||
|
"configuration. (Unexpected state 2.b)"
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_disallowed_grouping__empty_group_on_left(self):
|
def test_disallowed_grouping__empty_group_on_left(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.empty_group
|
foo.empty_group
|
||||||
[
|
[
|
||||||
|
@ -691,9 +706,14 @@ class ClinicParserTest(TestCase):
|
||||||
]
|
]
|
||||||
param: int
|
param: int
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function empty_group has an empty group.\n"
|
||||||
|
"All groups must contain at least one parameter."
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_disallowed_grouping__empty_group_on_right(self):
|
def test_disallowed_grouping__empty_group_on_right(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.empty_group
|
foo.empty_group
|
||||||
param: int
|
param: int
|
||||||
|
@ -703,6 +723,11 @@ class ClinicParserTest(TestCase):
|
||||||
group2 : int
|
group2 : int
|
||||||
]
|
]
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function empty_group has an empty group.\n"
|
||||||
|
"All groups must contain at least one parameter."
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_no_parameters(self):
|
def test_no_parameters(self):
|
||||||
function = self.parse_function("""
|
function = self.parse_function("""
|
||||||
|
@ -731,69 +756,60 @@ class ClinicParserTest(TestCase):
|
||||||
self.assertEqual(1, len(function.parameters))
|
self.assertEqual(1, len(function.parameters))
|
||||||
|
|
||||||
def test_illegal_module_line(self):
|
def test_illegal_module_line(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar => int
|
foo.bar => int
|
||||||
/
|
/
|
||||||
""")
|
""")
|
||||||
|
msg = "Illegal function name: foo.bar => int"
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_illegal_c_basename(self):
|
def test_illegal_c_basename(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar as 935
|
foo.bar as 935
|
||||||
/
|
/
|
||||||
""")
|
""")
|
||||||
|
msg = "Illegal C basename: 935"
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_single_star(self):
|
def test_single_star(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar
|
foo.bar
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
""")
|
""")
|
||||||
|
self.assertIn("Function bar uses '*' more than once.", out)
|
||||||
|
|
||||||
def test_parameters_required_after_star_without_initial_parameters_or_docstring(self):
|
def test_parameters_required_after_star(self):
|
||||||
self.parse_function_should_fail("""
|
dataset = (
|
||||||
module foo
|
"module foo\nfoo.bar\n *",
|
||||||
foo.bar
|
"module foo\nfoo.bar\n *\nDocstring here.",
|
||||||
*
|
"module foo\nfoo.bar\n this: int\n *",
|
||||||
""")
|
"module foo\nfoo.bar\n this: int\n *\nDocstring.",
|
||||||
|
)
|
||||||
def test_parameters_required_after_star_without_initial_parameters_with_docstring(self):
|
msg = "Function bar specifies '*' without any parameters afterwards."
|
||||||
self.parse_function_should_fail("""
|
for block in dataset:
|
||||||
module foo
|
with self.subTest(block=block):
|
||||||
foo.bar
|
out = self.parse_function_should_fail(block)
|
||||||
*
|
self.assertIn(msg, out)
|
||||||
Docstring here.
|
|
||||||
""")
|
|
||||||
|
|
||||||
def test_parameters_required_after_star_with_initial_parameters_without_docstring(self):
|
|
||||||
self.parse_function_should_fail("""
|
|
||||||
module foo
|
|
||||||
foo.bar
|
|
||||||
this: int
|
|
||||||
*
|
|
||||||
""")
|
|
||||||
|
|
||||||
def test_parameters_required_after_star_with_initial_parameters_and_docstring(self):
|
|
||||||
self.parse_function_should_fail("""
|
|
||||||
module foo
|
|
||||||
foo.bar
|
|
||||||
this: int
|
|
||||||
*
|
|
||||||
Docstring.
|
|
||||||
""")
|
|
||||||
|
|
||||||
def test_single_slash(self):
|
def test_single_slash(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar
|
foo.bar
|
||||||
/
|
/
|
||||||
/
|
/
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function bar has an unsupported group configuration. "
|
||||||
|
"(Unexpected state 0.d)"
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_mix_star_and_slash(self):
|
def test_mix_star_and_slash(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar
|
foo.bar
|
||||||
x: int
|
x: int
|
||||||
|
@ -802,14 +818,24 @@ class ClinicParserTest(TestCase):
|
||||||
z: int
|
z: int
|
||||||
/
|
/
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function bar mixes keyword-only and positional-only parameters, "
|
||||||
|
"which is unsupported."
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_parameters_not_permitted_after_slash_for_now(self):
|
def test_parameters_not_permitted_after_slash_for_now(self):
|
||||||
self.parse_function_should_fail("""
|
out = self.parse_function_should_fail("""
|
||||||
module foo
|
module foo
|
||||||
foo.bar
|
foo.bar
|
||||||
/
|
/
|
||||||
x: int
|
x: int
|
||||||
""")
|
""")
|
||||||
|
msg = (
|
||||||
|
"Function bar has an unsupported group configuration. "
|
||||||
|
"(Unexpected state 0.d)"
|
||||||
|
)
|
||||||
|
self.assertIn(msg, out)
|
||||||
|
|
||||||
def test_parameters_no_more_than_one_vararg(self):
|
def test_parameters_no_more_than_one_vararg(self):
|
||||||
expected_msg = (
|
expected_msg = (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue