Skip to content

Commit 242bef4

Browse files
gh-108494: Argument clinic: Improve the parse_file() API (#108575)
Co-authored-by: Victor Stinner <[email protected]>
1 parent 47d7eba commit 242bef4

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

Lib/test/test_clinic.py

+6-20
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import os.path
1414
import re
1515
import sys
16-
import types
1716
import unittest
1817

1918
test_tools.skip_if_missing('clinic')
@@ -22,16 +21,9 @@
2221
from clinic import DSLParser
2322

2423

25-
def default_namespace():
26-
ns = types.SimpleNamespace()
27-
ns.force = False
28-
ns.limited_capi = clinic.DEFAULT_LIMITED_CAPI
29-
return ns
30-
31-
3224
def _make_clinic(*, filename='clinic_tests'):
3325
clang = clinic.CLanguage(None)
34-
c = clinic.Clinic(clang, filename=filename)
26+
c = clinic.Clinic(clang, filename=filename, limited_capi=False)
3527
c.block_parser = clinic.BlockParser('', clang)
3628
return c
3729

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

6254

63-
class MockClinic:
64-
def __init__(self):
65-
self.limited_capi = clinic.DEFAULT_LIMITED_CAPI
66-
67-
6855
class ClinicWholeFileTest(TestCase):
6956
maxDiff = None
7057

@@ -138,7 +125,7 @@ def test_parse_with_body_prefix(self):
138125
clang.body_prefix = "//"
139126
clang.start_line = "//[{dsl_name} start]"
140127
clang.stop_line = "//[{dsl_name} stop]"
141-
cl = clinic.Clinic(clang, filename="test.c")
128+
cl = clinic.Clinic(clang, filename="test.c", limited_capi=False)
142129
raw = dedent("""
143130
//[clinic start]
144131
//module test
@@ -704,9 +691,8 @@ def expect_parsing_failure(
704691
self, *, filename, expected_error, verify=True, output=None
705692
):
706693
errmsg = re.escape(dedent(expected_error).strip())
707-
ns = default_namespace()
708694
with self.assertRaisesRegex(clinic.ClinicError, errmsg):
709-
clinic.parse_file(filename, ns=ns)
695+
clinic.parse_file(filename, limited_capi=False)
710696

711697
def test_parse_file_no_extension(self) -> None:
712698
self.expect_parsing_failure(
@@ -846,9 +832,9 @@ def _test(self, input, output):
846832

847833
blocks = list(clinic.BlockParser(input, language))
848834
writer = clinic.BlockPrinter(language)
849-
mock_clinic = MockClinic()
835+
c = _make_clinic()
850836
for block in blocks:
851-
writer.print_block(block, clinic=mock_clinic)
837+
writer.print_block(block, clinic=c)
852838
output = writer.f.getvalue()
853839
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)
854840

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

875861
def _test_clinic(self, input, output):
876862
language = clinic.CLanguage(None)
877-
c = clinic.Clinic(language, filename="file")
863+
c = clinic.Clinic(language, filename="file", limited_capi=False)
878864
c.parsers['inert'] = InertParser(c)
879865
c.parsers['copy'] = CopyParser(c)
880866
computed = c.parse(input)

Tools/clinic/clinic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363

6464
version = '1'
6565

66-
DEFAULT_LIMITED_CAPI = False
6766
NO_VARARG = "PY_SSIZE_T_MAX"
6867
CLINIC_PREFIX = "__clinic_"
6968
CLINIC_PREFIXED_ARGS = {
@@ -2414,8 +2413,8 @@ def __init__(
24142413
printer: BlockPrinter | None = None,
24152414
*,
24162415
filename: str,
2416+
limited_capi: bool,
24172417
verify: bool = True,
2418-
limited_capi: bool = False,
24192418
) -> None:
24202419
# maps strings to Parser objects.
24212420
# (instantiated from the "parsers" global.)
@@ -2612,11 +2611,10 @@ def __repr__(self) -> str:
26122611
def parse_file(
26132612
filename: str,
26142613
*,
2615-
ns: argparse.Namespace,
2614+
limited_capi: bool,
26162615
output: str | None = None,
2616+
verify: bool = True,
26172617
) -> None:
2618-
verify = not ns.force
2619-
limited_capi = ns.limited_capi
26202618
if not output:
26212619
output = filename
26222620

@@ -6190,7 +6188,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
61906188
continue
61916189
if ns.verbose:
61926190
print(path)
6193-
parse_file(path, ns=ns)
6191+
parse_file(path,
6192+
verify=not ns.force, limited_capi=ns.limited_capi)
61946193
return
61956194

61966195
if not ns.filename:
@@ -6202,7 +6201,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
62026201
for filename in ns.filename:
62036202
if ns.verbose:
62046203
print(filename)
6205-
parse_file(filename, output=ns.output, ns=ns)
6204+
parse_file(filename, output=ns.output,
6205+
verify=not ns.force, limited_capi=ns.limited_capi)
62066206

62076207

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

0 commit comments

Comments
 (0)