From acfd270163fba4cdc705304136c08c9c4a7de804 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 28 Jul 2023 12:28:40 +0100 Subject: [PATCH 1/2] Argument clinic: Fix minor bug in `state_modulename_name` --- Lib/test/test_clinic.py | 17 +++++++++++++++++ Tools/clinic/clinic.py | 17 ++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index d21c7d84c88d2d..fa44cd0076856b 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -84,6 +84,7 @@ def __init__(self): ('parser_definition', d('block')), ('impl_definition', d('block')), )) + self.functions = [] def get_destination(self, name): d = self.destinations.get(name) @@ -104,6 +105,9 @@ def directive(self, name, args): _module_and_class = clinic.Clinic._module_and_class + def __repr__(self): + return "" + class ClinicWholeFileTest(_ParserBase): def setUp(self): @@ -672,6 +676,19 @@ def test_c_name(self): """) self.assertEqual("os_stat_fn", function.c_basename) + def test_cloning_nonexistent_function_correctly_fails(self): + stdout = self.parse_function_should_fail(""" + cloned = fooooooooooooooooooooooo + This is trying to clone a nonexistent function!! + """) + expected_error = """\ +cls=None, module=, existing='fooooooooooooooooooooooo' +(cls or module).functions=[] +Error on line 0: +Couldn't find existing function 'fooooooooooooooooooooooo'! +""" + self.assertEqual(expected_error, stdout) + def test_return_converter(self): function = self.parse_function(""" module os diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index a343dc5c7fc080..556dfd60611972 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -4703,15 +4703,14 @@ def state_modulename_name(self, line: str | None) -> None: function_name = fields.pop() module, cls = self.clinic._module_and_class(fields) - for existing_function in (cls or module).functions: - if existing_function.name == function_name: - break - else: - existing_function = None - if not existing_function: - print("class", cls, "module", module, "existing", existing) - print("cls. functions", cls.functions) - fail("Couldn't find existing function " + repr(existing) + "!") + try: + existing_function = next( + f for f in (cls or module).functions if f.name == function_name + ) + except StopIteration: + print(f"{cls=}, {module=}, {existing=}") + print(f"{(cls or module).functions=}") + fail(f"Couldn't find existing function {existing!r}!") fields = [x.strip() for x in full_name.split('.')] function_name = fields.pop() From d4b31b4b401e1f7a6f8687f7e1d94e21f03d3cc9 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Fri, 28 Jul 2023 17:31:32 +0100 Subject: [PATCH 2/2] Address Serhiy's review --- Tools/clinic/clinic.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 556dfd60611972..39c6de35273019 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -4703,11 +4703,10 @@ def state_modulename_name(self, line: str | None) -> None: function_name = fields.pop() module, cls = self.clinic._module_and_class(fields) - try: - existing_function = next( - f for f in (cls or module).functions if f.name == function_name - ) - except StopIteration: + for existing_function in (cls or module).functions: + if existing_function.name == function_name: + break + else: print(f"{cls=}, {module=}, {existing=}") print(f"{(cls or module).functions=}") fail(f"Couldn't find existing function {existing!r}!")