Skip to content

Commit

Permalink
Quick-and-dirty: separate out version helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
erlend-aasland committed Dec 20, 2023
1 parent 24277a7 commit 420b8e5
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 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


# TODO:
#
# soon:
Expand Down Expand Up @@ -373,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 @@ -5242,10 +5204,13 @@ def reset(self) -> None:
self.target_critical_section = []

def directive_version(self, required: str) -> None:
if version_comparator(self.VERSION, required) < 0:
fail("Insufficient Clinic version!\n"
f" Version: {self.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))

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

0 comments on commit 420b8e5

Please sign in to comment.