Skip to content

gh-108494: Argument clinic: Improve the parse_file() API #108575

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

Merged
merged 6 commits into from
Aug 28, 2023
Merged
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
26 changes: 6 additions & 20 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import os.path
import re
import sys
import types
import unittest

test_tools.skip_if_missing('clinic')
Expand All @@ -22,16 +21,9 @@
from clinic import DSLParser


def default_namespace():
ns = types.SimpleNamespace()
ns.force = False
ns.limited_capi = clinic.DEFAULT_LIMITED_CAPI
return ns


def _make_clinic(*, filename='clinic_tests'):
clang = clinic.CLanguage(None)
c = clinic.Clinic(clang, filename=filename)
c = clinic.Clinic(clang, filename=filename, limited_capi=False)
c.block_parser = clinic.BlockParser('', clang)
return c

Expand Down Expand Up @@ -60,11 +52,6 @@ def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None,
return cm.exception


class MockClinic:
def __init__(self):
self.limited_capi = clinic.DEFAULT_LIMITED_CAPI


class ClinicWholeFileTest(TestCase):
maxDiff = None

Expand Down Expand Up @@ -138,7 +125,7 @@ def test_parse_with_body_prefix(self):
clang.body_prefix = "//"
clang.start_line = "//[{dsl_name} start]"
clang.stop_line = "//[{dsl_name} stop]"
cl = clinic.Clinic(clang, filename="test.c")
cl = clinic.Clinic(clang, filename="test.c", limited_capi=False)
raw = dedent("""
//[clinic start]
//module test
Expand Down Expand Up @@ -704,9 +691,8 @@ def expect_parsing_failure(
self, *, filename, expected_error, verify=True, output=None
):
errmsg = re.escape(dedent(expected_error).strip())
ns = default_namespace()
with self.assertRaisesRegex(clinic.ClinicError, errmsg):
clinic.parse_file(filename, ns=ns)
clinic.parse_file(filename, limited_capi=False)

def test_parse_file_no_extension(self) -> None:
self.expect_parsing_failure(
Expand Down Expand Up @@ -846,9 +832,9 @@ def _test(self, input, output):

blocks = list(clinic.BlockParser(input, language))
writer = clinic.BlockPrinter(language)
mock_clinic = MockClinic()
c = _make_clinic()
for block in blocks:
writer.print_block(block, clinic=mock_clinic)
writer.print_block(block, clinic=c)
output = writer.f.getvalue()
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)

Expand All @@ -874,7 +860,7 @@ def test_round_trip_2(self):

def _test_clinic(self, input, output):
language = clinic.CLanguage(None)
c = clinic.Clinic(language, filename="file")
c = clinic.Clinic(language, filename="file", limited_capi=False)
c.parsers['inert'] = InertParser(c)
c.parsers['copy'] = CopyParser(c)
computed = c.parse(input)
Expand Down
14 changes: 7 additions & 7 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@

version = '1'

DEFAULT_LIMITED_CAPI = False
NO_VARARG = "PY_SSIZE_T_MAX"
CLINIC_PREFIX = "__clinic_"
CLINIC_PREFIXED_ARGS = {
Expand Down Expand Up @@ -2414,8 +2413,8 @@ def __init__(
printer: BlockPrinter | None = None,
*,
filename: str,
limited_capi: bool,
verify: bool = True,
limited_capi: bool = False,
) -> None:
# maps strings to Parser objects.
# (instantiated from the "parsers" global.)
Expand Down Expand Up @@ -2612,11 +2611,10 @@ def __repr__(self) -> str:
def parse_file(
filename: str,
*,
ns: argparse.Namespace,
limited_capi: bool,
output: str | None = None,
verify: bool = True,
) -> None:
verify = not ns.force
limited_capi = ns.limited_capi
if not output:
output = filename

Expand Down Expand Up @@ -6190,7 +6188,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
continue
if ns.verbose:
print(path)
parse_file(path, ns=ns)
parse_file(path,
verify=not ns.force, limited_capi=ns.limited_capi)
return

if not ns.filename:
Expand All @@ -6202,7 +6201,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
for filename in ns.filename:
if ns.verbose:
print(filename)
parse_file(filename, output=ns.output, ns=ns)
parse_file(filename, output=ns.output,
verify=not ns.force, limited_capi=ns.limited_capi)


def main(argv: list[str] | None = None) -> NoReturn:
Expand Down