Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-113317, Argument Clinic: Add BlockParser.fail() method #115369

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
33 changes: 22 additions & 11 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 "
Expand Down Expand Up @@ -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

Expand Down
Loading