mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
gh-108494: Argument clinic: Improve the parse_file() API (#108575)
Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
parent
47d7eba889
commit
242bef459b
2 changed files with 13 additions and 27 deletions
|
|
@ -13,7 +13,6 @@ import inspect
|
|||
import os.path
|
||||
import re
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
|
||||
test_tools.skip_if_missing('clinic')
|
||||
|
|
@ -22,16 +21,9 @@ with test_tools.imports_under_tool('clinic'):
|
|||
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
|
||||
|
||||
|
|
@ -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
|
||||
|
||||
|
|
@ -138,7 +125,7 @@ class ClinicWholeFileTest(TestCase):
|
|||
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
|
||||
|
|
@ -704,9 +691,8 @@ class ParseFileUnitTest(TestCase):
|
|||
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(
|
||||
|
|
@ -846,9 +832,9 @@ class ClinicBlockParserTest(TestCase):
|
|||
|
||||
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)
|
||||
|
||||
|
|
@ -874,7 +860,7 @@ xyz
|
|||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -63,7 +63,6 @@ from typing import (
|
|||
|
||||
version = '1'
|
||||
|
||||
DEFAULT_LIMITED_CAPI = False
|
||||
NO_VARARG = "PY_SSIZE_T_MAX"
|
||||
CLINIC_PREFIX = "__clinic_"
|
||||
CLINIC_PREFIXED_ARGS = {
|
||||
|
|
@ -2414,8 +2413,8 @@ impl_definition block
|
|||
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.)
|
||||
|
|
@ -2612,11 +2611,10 @@ impl_definition block
|
|||
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
|
||||
|
||||
|
|
@ -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:
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue