From 98731082f9501489326b51d67226d8d751ddc101 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Feb 2024 22:51:25 +0100 Subject: [PATCH 1/3] gh-113317, Argument Clinic: Add BlockParser.fail() method Add filename parameter to BlockParser constructor. --- Lib/test/test_clinic.py | 4 ++-- Tools/clinic/clinic.py | 34 +++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_clinic.py b/Lib/test/test_clinic.py index e987ce54605497..47c068bbaa60d6 100644 --- a/Lib/test/test_clinic.py +++ b/Lib/test/test_clinic.py @@ -24,7 +24,7 @@ def _make_clinic(*, filename='clinic_tests'): clang = clinic.CLanguage(filename) c = clinic.Clinic(clang, filename=filename, limited_capi=False) - c.block_parser = clinic.BlockParser('', clang) + c.block_parser = clinic.BlockParser('', clang, filename=filename) return c @@ -780,7 +780,7 @@ class ClinicBlockParserTest(TestCase): def _test(self, input, output): language = clinic.CLanguage(None) - blocks = list(clinic.BlockParser(input, language)) + blocks = list(clinic.BlockParser(input, language, filename=None)) writer = clinic.BlockPrinter(language) c = _make_clinic() for block in blocks: diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index db57d17899af93..6316163dbcfbff 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1855,7 +1855,8 @@ def __init__( input: str, language: Language, *, - verify: bool = True + filename: str | None, + verify: bool = True, ) -> None: """ "input" should be a str object @@ -1875,11 +1876,17 @@ def __init__( whole_line=False) self.start_re = libclinic.create_regex(before, after) self.verify = verify + self.filename = filename self.last_checksum_re: re.Pattern[str] | None = None self.last_dsl_name: str | None = None self.dsl_name: str | None = None self.first_block = True + def fail(self, *args: object) -> NoReturn: + warn_or_fail(*args, + filename=self.filename, line_number=self.line_number, + fail=True) + def __iter__(self) -> BlockParser: return self @@ -1937,12 +1944,12 @@ def is_stop_line(line: str) -> bool: if line.startswith(stop_line): remainder = line.removeprefix(stop_line) if remainder and not remainder.isspace(): - fail(f"Garbage after stop line: {remainder!r}") + self.fail(f"Garbage after stop line: {remainder!r}") return True else: # gh-92256: don't allow incorrectly formatted stop lines if line.lstrip().startswith(stop_line): - fail(f"Whitespace is not allowed before the stop line: {line!r}") + self.fail(f"Whitespace is not allowed before the stop line: {line!r}") return False # consume body of program @@ -1987,7 +1994,7 @@ def is_stop_line(line: str) -> bool: for field in shlex.split(arguments): name, equals, value = field.partition('=') if not equals: - fail(f"Mangled Argument Clinic marker line: {line!r}") + self.fail(f"Mangled Argument Clinic marker line: {line!r}") d[name.strip()] = value.strip() if self.verify: @@ -1998,10 +2005,10 @@ def is_stop_line(line: str) -> bool: computed = libclinic.compute_checksum(output, len(checksum)) if checksum != computed: - fail("Checksum mismatch! " - f"Expected {checksum!r}, computed {computed!r}. " - "Suggested fix: remove all generated code including " - "the end marker, or use the '-f' option.") + self.fail("Checksum mismatch! " + f"Expected {checksum!r}, computed {computed!r}. " + "Suggested fix: remove all generated code including " + "the end marker, or use the '-f' option.") else: # put back output output_lines = output.splitlines(keepends=True) @@ -2394,7 +2401,9 @@ def get_destination_buffer( def parse(self, input: str) -> str: printer = self.printer - self.block_parser = BlockParser(input, self.language, verify=self.verify) + self.block_parser = BlockParser(input, self.language, + verify=self.verify, + filename=self.filename) for block in self.block_parser: dsl_name = block.dsl_name if dsl_name: @@ -2437,7 +2446,10 @@ def parse(self, input: str) -> str: f"can't make directory {dirname!r}!") if self.verify: with open(destination.filename) as f: - parser_2 = BlockParser(f.read(), language=self.language) + content = f.read() + parser_2 = BlockParser(content, + language=self.language, + filename=destination.filename) blocks = list(parser_2) if (len(blocks) != 1) or (blocks[0].input != 'preserve\n'): fail(f"Modified destination file " @@ -2511,7 +2523,7 @@ def parse_file( raw = f.read() # exit quickly if there are no clinic markers in the file - find_start_re = BlockParser("", language).find_start_re + find_start_re = BlockParser("", language, filename=None).find_start_re if not find_start_re.search(raw): return From 67dab577abfbe53cdc490c40b0aeb48867a3281e Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 12 Feb 2024 23:15:44 +0100 Subject: [PATCH 2/3] parse_file(): pass filename --- Tools/clinic/clinic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 6316163dbcfbff..fa27e6c79b0f90 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -2523,7 +2523,7 @@ def parse_file( raw = f.read() # exit quickly if there are no clinic markers in the file - find_start_re = BlockParser("", language, filename=None).find_start_re + find_start_re = BlockParser("", language, filename=filename).find_start_re if not find_start_re.search(raw): return From 44007859344990772f206a3547e2a6c40836d272 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 14 Feb 2024 22:59:00 +0100 Subject: [PATCH 3/3] Update Tools/clinic/clinic.py Co-authored-by: Erlend E. Aasland --- Tools/clinic/clinic.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index fa27e6c79b0f90..aa4e590d39a8a9 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -1883,9 +1883,8 @@ def __init__( self.first_block = True def fail(self, *args: object) -> NoReturn: - warn_or_fail(*args, - filename=self.filename, line_number=self.line_number, - fail=True) + fail(*args, + filename=self.filename, line_number=self.line_number) def __iter__(self) -> BlockParser: return self