Skip to content

Commit 7688964

Browse files
[3.11] gh-106368: Harden Argument Clinic parser tests (GH-106384) (#106388)
(cherry picked from commit 648688c) Co-authored-by: Erlend E. Aasland <[email protected]>
1 parent 3fdda97 commit 7688964

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

Lib/test/test_clinic.py

+68-42
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ def test_disallowed_grouping__two_top_groups_on_left(self):
642642
self.assertEqual(out, expected_msg)
643643

644644
def test_disallowed_grouping__two_top_groups_on_right(self):
645-
self.parse_function_should_fail("""
645+
out = self.parse_function_should_fail("""
646646
module foo
647647
foo.two_top_groups_on_right
648648
param: int
@@ -653,9 +653,14 @@ def test_disallowed_grouping__two_top_groups_on_right(self):
653653
group2 : int
654654
]
655655
""")
656+
msg = (
657+
"Function two_top_groups_on_right has an unsupported group "
658+
"configuration. (Unexpected state 6.b)"
659+
)
660+
self.assertIn(msg, out)
656661

657662
def test_disallowed_grouping__parameter_after_group_on_right(self):
658-
self.parse_function_should_fail("""
663+
out = self.parse_function_should_fail("""
659664
module foo
660665
foo.parameter_after_group_on_right
661666
param: int
@@ -666,9 +671,14 @@ def test_disallowed_grouping__parameter_after_group_on_right(self):
666671
group2 : int
667672
]
668673
""")
674+
msg = (
675+
"Function parameter_after_group_on_right has an unsupported group "
676+
"configuration. (Unexpected state 6.a)"
677+
)
678+
self.assertIn(msg, out)
669679

670680
def test_disallowed_grouping__group_after_parameter_on_left(self):
671-
self.parse_function_should_fail("""
681+
out = self.parse_function_should_fail("""
672682
module foo
673683
foo.group_after_parameter_on_left
674684
[
@@ -679,9 +689,14 @@ def test_disallowed_grouping__group_after_parameter_on_left(self):
679689
]
680690
param: int
681691
""")
692+
msg = (
693+
"Function group_after_parameter_on_left has an unsupported group "
694+
"configuration. (Unexpected state 2.b)"
695+
)
696+
self.assertIn(msg, out)
682697

683698
def test_disallowed_grouping__empty_group_on_left(self):
684-
self.parse_function_should_fail("""
699+
out = self.parse_function_should_fail("""
685700
module foo
686701
foo.empty_group
687702
[
@@ -691,9 +706,14 @@ def test_disallowed_grouping__empty_group_on_left(self):
691706
]
692707
param: int
693708
""")
709+
msg = (
710+
"Function empty_group has an empty group.\n"
711+
"All groups must contain at least one parameter."
712+
)
713+
self.assertIn(msg, out)
694714

695715
def test_disallowed_grouping__empty_group_on_right(self):
696-
self.parse_function_should_fail("""
716+
out = self.parse_function_should_fail("""
697717
module foo
698718
foo.empty_group
699719
param: int
@@ -703,6 +723,11 @@ def test_disallowed_grouping__empty_group_on_right(self):
703723
group2 : int
704724
]
705725
""")
726+
msg = (
727+
"Function empty_group has an empty group.\n"
728+
"All groups must contain at least one parameter."
729+
)
730+
self.assertIn(msg, out)
706731

707732
def test_no_parameters(self):
708733
function = self.parse_function("""
@@ -731,69 +756,60 @@ class foo.Bar "unused" "notneeded"
731756
self.assertEqual(1, len(function.parameters))
732757

733758
def test_illegal_module_line(self):
734-
self.parse_function_should_fail("""
759+
out = self.parse_function_should_fail("""
735760
module foo
736761
foo.bar => int
737762
/
738763
""")
764+
msg = "Illegal function name: foo.bar => int"
765+
self.assertIn(msg, out)
739766

740767
def test_illegal_c_basename(self):
741-
self.parse_function_should_fail("""
768+
out = self.parse_function_should_fail("""
742769
module foo
743770
foo.bar as 935
744771
/
745772
""")
773+
msg = "Illegal C basename: 935"
774+
self.assertIn(msg, out)
746775

747776
def test_single_star(self):
748-
self.parse_function_should_fail("""
749-
module foo
750-
foo.bar
751-
*
752-
*
753-
""")
754-
755-
def test_parameters_required_after_star_without_initial_parameters_or_docstring(self):
756-
self.parse_function_should_fail("""
757-
module foo
758-
foo.bar
759-
*
760-
""")
761-
762-
def test_parameters_required_after_star_without_initial_parameters_with_docstring(self):
763-
self.parse_function_should_fail("""
777+
out = self.parse_function_should_fail("""
764778
module foo
765779
foo.bar
766780
*
767-
Docstring here.
768-
""")
769-
770-
def test_parameters_required_after_star_with_initial_parameters_without_docstring(self):
771-
self.parse_function_should_fail("""
772-
module foo
773-
foo.bar
774-
this: int
775781
*
776782
""")
783+
self.assertIn("Function bar uses '*' more than once.", out)
777784

778-
def test_parameters_required_after_star_with_initial_parameters_and_docstring(self):
779-
self.parse_function_should_fail("""
780-
module foo
781-
foo.bar
782-
this: int
783-
*
784-
Docstring.
785-
""")
785+
def test_parameters_required_after_star(self):
786+
dataset = (
787+
"module foo\nfoo.bar\n *",
788+
"module foo\nfoo.bar\n *\nDocstring here.",
789+
"module foo\nfoo.bar\n this: int\n *",
790+
"module foo\nfoo.bar\n this: int\n *\nDocstring.",
791+
)
792+
msg = "Function bar specifies '*' without any parameters afterwards."
793+
for block in dataset:
794+
with self.subTest(block=block):
795+
out = self.parse_function_should_fail(block)
796+
self.assertIn(msg, out)
786797

787798
def test_single_slash(self):
788-
self.parse_function_should_fail("""
799+
out = self.parse_function_should_fail("""
789800
module foo
790801
foo.bar
791802
/
792803
/
793804
""")
805+
msg = (
806+
"Function bar has an unsupported group configuration. "
807+
"(Unexpected state 0.d)"
808+
)
809+
self.assertIn(msg, out)
794810

795811
def test_mix_star_and_slash(self):
796-
self.parse_function_should_fail("""
812+
out = self.parse_function_should_fail("""
797813
module foo
798814
foo.bar
799815
x: int
@@ -802,14 +818,24 @@ def test_mix_star_and_slash(self):
802818
z: int
803819
/
804820
""")
821+
msg = (
822+
"Function bar mixes keyword-only and positional-only parameters, "
823+
"which is unsupported."
824+
)
825+
self.assertIn(msg, out)
805826

806827
def test_parameters_not_permitted_after_slash_for_now(self):
807-
self.parse_function_should_fail("""
828+
out = self.parse_function_should_fail("""
808829
module foo
809830
foo.bar
810831
/
811832
x: int
812833
""")
834+
msg = (
835+
"Function bar has an unsupported group configuration. "
836+
"(Unexpected state 0.d)"
837+
)
838+
self.assertIn(msg, out)
813839

814840
def test_parameters_no_more_than_one_vararg(self):
815841
expected_msg = (

0 commit comments

Comments
 (0)