FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-108494: Argument clinic: Improve the `parse_file()` API by AlexWaygood · Pull Request #108575 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 6 additions & 20 deletions Lib/test/test_clinic.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL