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..aa4e590d39a8a9 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,16 @@ 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: + fail(*args, + filename=self.filename, line_number=self.line_number) + def __iter__(self) -> BlockParser: return self @@ -1937,12 +1943,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 +1993,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 +2004,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 +2400,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 +2445,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 +2522,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=filename).find_start_re if not find_start_re.search(raw): return