mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +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 os.path
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
import types
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
test_tools.skip_if_missing('clinic')
|
test_tools.skip_if_missing('clinic')
|
||||||
|
|
@ -22,16 +21,9 @@ with test_tools.imports_under_tool('clinic'):
|
||||||
from clinic import DSLParser
|
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'):
|
def _make_clinic(*, filename='clinic_tests'):
|
||||||
clang = clinic.CLanguage(None)
|
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)
|
c.block_parser = clinic.BlockParser('', clang)
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
@ -60,11 +52,6 @@ def _expect_failure(tc, parser, code, errmsg, *, filename=None, lineno=None,
|
||||||
return cm.exception
|
return cm.exception
|
||||||
|
|
||||||
|
|
||||||
class MockClinic:
|
|
||||||
def __init__(self):
|
|
||||||
self.limited_capi = clinic.DEFAULT_LIMITED_CAPI
|
|
||||||
|
|
||||||
|
|
||||||
class ClinicWholeFileTest(TestCase):
|
class ClinicWholeFileTest(TestCase):
|
||||||
maxDiff = None
|
maxDiff = None
|
||||||
|
|
||||||
|
|
@ -138,7 +125,7 @@ class ClinicWholeFileTest(TestCase):
|
||||||
clang.body_prefix = "//"
|
clang.body_prefix = "//"
|
||||||
clang.start_line = "//[{dsl_name} start]"
|
clang.start_line = "//[{dsl_name} start]"
|
||||||
clang.stop_line = "//[{dsl_name} stop]"
|
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("""
|
raw = dedent("""
|
||||||
//[clinic start]
|
//[clinic start]
|
||||||
//module test
|
//module test
|
||||||
|
|
@ -704,9 +691,8 @@ class ParseFileUnitTest(TestCase):
|
||||||
self, *, filename, expected_error, verify=True, output=None
|
self, *, filename, expected_error, verify=True, output=None
|
||||||
):
|
):
|
||||||
errmsg = re.escape(dedent(expected_error).strip())
|
errmsg = re.escape(dedent(expected_error).strip())
|
||||||
ns = default_namespace()
|
|
||||||
with self.assertRaisesRegex(clinic.ClinicError, errmsg):
|
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:
|
def test_parse_file_no_extension(self) -> None:
|
||||||
self.expect_parsing_failure(
|
self.expect_parsing_failure(
|
||||||
|
|
@ -846,9 +832,9 @@ class ClinicBlockParserTest(TestCase):
|
||||||
|
|
||||||
blocks = list(clinic.BlockParser(input, language))
|
blocks = list(clinic.BlockParser(input, language))
|
||||||
writer = clinic.BlockPrinter(language)
|
writer = clinic.BlockPrinter(language)
|
||||||
mock_clinic = MockClinic()
|
c = _make_clinic()
|
||||||
for block in blocks:
|
for block in blocks:
|
||||||
writer.print_block(block, clinic=mock_clinic)
|
writer.print_block(block, clinic=c)
|
||||||
output = writer.f.getvalue()
|
output = writer.f.getvalue()
|
||||||
assert output == input, "output != input!\n\noutput " + repr(output) + "\n\n input " + repr(input)
|
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):
|
def _test_clinic(self, input, output):
|
||||||
language = clinic.CLanguage(None)
|
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['inert'] = InertParser(c)
|
||||||
c.parsers['copy'] = CopyParser(c)
|
c.parsers['copy'] = CopyParser(c)
|
||||||
computed = c.parse(input)
|
computed = c.parse(input)
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,6 @@ from typing import (
|
||||||
|
|
||||||
version = '1'
|
version = '1'
|
||||||
|
|
||||||
DEFAULT_LIMITED_CAPI = False
|
|
||||||
NO_VARARG = "PY_SSIZE_T_MAX"
|
NO_VARARG = "PY_SSIZE_T_MAX"
|
||||||
CLINIC_PREFIX = "__clinic_"
|
CLINIC_PREFIX = "__clinic_"
|
||||||
CLINIC_PREFIXED_ARGS = {
|
CLINIC_PREFIXED_ARGS = {
|
||||||
|
|
@ -2414,8 +2413,8 @@ impl_definition block
|
||||||
printer: BlockPrinter | None = None,
|
printer: BlockPrinter | None = None,
|
||||||
*,
|
*,
|
||||||
filename: str,
|
filename: str,
|
||||||
|
limited_capi: bool,
|
||||||
verify: bool = True,
|
verify: bool = True,
|
||||||
limited_capi: bool = False,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
# maps strings to Parser objects.
|
# maps strings to Parser objects.
|
||||||
# (instantiated from the "parsers" global.)
|
# (instantiated from the "parsers" global.)
|
||||||
|
|
@ -2612,11 +2611,10 @@ impl_definition block
|
||||||
def parse_file(
|
def parse_file(
|
||||||
filename: str,
|
filename: str,
|
||||||
*,
|
*,
|
||||||
ns: argparse.Namespace,
|
limited_capi: bool,
|
||||||
output: str | None = None,
|
output: str | None = None,
|
||||||
|
verify: bool = True,
|
||||||
) -> None:
|
) -> None:
|
||||||
verify = not ns.force
|
|
||||||
limited_capi = ns.limited_capi
|
|
||||||
if not output:
|
if not output:
|
||||||
output = filename
|
output = filename
|
||||||
|
|
||||||
|
|
@ -6190,7 +6188,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
|
||||||
continue
|
continue
|
||||||
if ns.verbose:
|
if ns.verbose:
|
||||||
print(path)
|
print(path)
|
||||||
parse_file(path, ns=ns)
|
parse_file(path,
|
||||||
|
verify=not ns.force, limited_capi=ns.limited_capi)
|
||||||
return
|
return
|
||||||
|
|
||||||
if not ns.filename:
|
if not ns.filename:
|
||||||
|
|
@ -6202,7 +6201,8 @@ def run_clinic(parser: argparse.ArgumentParser, ns: argparse.Namespace) -> None:
|
||||||
for filename in ns.filename:
|
for filename in ns.filename:
|
||||||
if ns.verbose:
|
if ns.verbose:
|
||||||
print(filename)
|
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:
|
def main(argv: list[str] | None = None) -> NoReturn:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue