Skip to content
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

PoC: Remove global version variable; separate out version helpers #28

Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ def converter_init(self):
@staticmethod
@contextlib.contextmanager
def _clinic_version(new_version):
"""Helper for test_version_*() tests"""
_saved = clinic.version
clinic.version = new_version
"""Safely mutate the DSL VERSION (for version directive tests)."""
_saved = clinic.DSLParser.VERSION
clinic.DSLParser.VERSION = new_version
try:
yield
finally:
clinic.version = _saved
clinic.DSLParser.VERSION = _saved

def test_version_directive(self):
dataset = (
Expand Down
64 changes: 14 additions & 50 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
overload,
)


# Local includes.
import libclinic
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved


# TODO:
#
# soon:
Expand All @@ -61,8 +66,6 @@
# and keyword-only
#

version = '1'

NO_VARARG = "PY_SSIZE_T_MAX"
CLINIC_PREFIX = "__clinic_"
CLINIC_PREFIXED_ARGS = {
Expand Down Expand Up @@ -375,49 +378,6 @@ def pprint_words(items: list[str]) -> str:
return ", ".join(items[:-1]) + " and " + items[-1]


def version_splitter(s: str) -> tuple[int, ...]:
"""Splits a version string into a tuple of integers.

The following ASCII characters are allowed, and employ
the following conversions:
a -> -3
b -> -2
c -> -1
(This permits Python-style version strings such as "1.4b3".)
"""
version: list[int] = []
accumulator: list[str] = []
def flush() -> None:
if not accumulator:
fail(f'Unsupported version string: {s!r}')
version.append(int(''.join(accumulator)))
accumulator.clear()

for c in s:
if c.isdigit():
accumulator.append(c)
elif c == '.':
flush()
elif c in 'abc':
flush()
version.append('abc'.index(c) - 3)
else:
fail(f'Illegal character {c!r} in version string {s!r}')
flush()
return tuple(version)

def version_comparator(version1: str, version2: str) -> Literal[-1, 0, 1]:
iterator = itertools.zip_longest(
version_splitter(version1), version_splitter(version2), fillvalue=0
)
for a, b in iterator:
if a < b:
return -1
if a > b:
return 1
return 0


class CRenderData:
def __init__(self) -> None:

Expand Down Expand Up @@ -5206,6 +5166,8 @@ class DSLParser:
target_critical_section: list[str]
from_version_re = re.compile(r'([*/]) +\[from +(.+)\]')

VERSION: Final[str] = '1' # DSL version.

def __init__(self, clinic: Clinic) -> None:
self.clinic = clinic

Expand Down Expand Up @@ -5242,11 +5204,13 @@ def reset(self) -> None:
self.target_critical_section = []

def directive_version(self, required: str) -> None:
global version
if version_comparator(version, required) < 0:
fail("Insufficient Clinic version!\n"
f" Version: {version}\n"
f" Required: {required}")
try:
if libclinic.version_comparator(self.VERSION, required) < 0:
fail("Insufficient Clinic version!\n"
f" Version: {self.VERSION}\n"
f" Required: {required}")
except libclinic.ParseVersionError as err:
fail(str(err))
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

def directive_module(self, name: str) -> None:
fields = name.split('.')[:-1]
Expand Down
Loading