diff options
author | Victor Stinner <vstinner@python.org> | 2024-03-14 08:07:01 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-14 08:07:01 (GMT) |
commit | a18c9854e8c255981f07c0a1c1503253f85b7540 (patch) | |
tree | c487ee06c29dea426164b0a92c4ffb21bb9ed70e /Tools | |
parent | 3a25d9c5a95d4e57513ea7edd9e184f4609ebe20 (diff) | |
download | cpython-a18c9854e8c255981f07c0a1c1503253f85b7540.zip cpython-a18c9854e8c255981f07c0a1c1503253f85b7540.tar.gz cpython-a18c9854e8c255981f07c0a1c1503253f85b7540.tar.bz2 |
gh-113317, AC: Move warn() and fail() to libclinic.errors (#116770)
Diffstat (limited to 'Tools')
-rwxr-xr-x | Tools/clinic/clinic.py | 48 | ||||
-rw-r--r-- | Tools/clinic/libclinic/__init__.py | 4 | ||||
-rw-r--r-- | Tools/clinic/libclinic/errors.py | 46 |
3 files changed, 51 insertions, 47 deletions
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py index 893f4cc..4c7c4dc 100755 --- a/Tools/clinic/clinic.py +++ b/Tools/clinic/clinic.py @@ -44,14 +44,13 @@ from typing import ( Protocol, TypeVar, cast, - overload, ) # Local imports. import libclinic import libclinic.cpp -from libclinic import ClinicError +from libclinic import ClinicError, fail, warn # TODO: @@ -94,51 +93,6 @@ NULL = Null() TemplateDict = dict[str, str] -@overload -def warn_or_fail( - *args: object, - fail: Literal[True], - filename: str | None = None, - line_number: int | None = None, -) -> NoReturn: ... - -@overload -def warn_or_fail( - *args: object, - fail: Literal[False] = False, - filename: str | None = None, - line_number: int | None = None, -) -> None: ... - -def warn_or_fail( - *args: object, - fail: bool = False, - filename: str | None = None, - line_number: int | None = None, -) -> None: - joined = " ".join([str(a) for a in args]) - error = ClinicError(joined, filename=filename, lineno=line_number) - if fail: - raise error - else: - print(error.report(warn_only=True)) - - -def warn( - *args: object, - filename: str | None = None, - line_number: int | None = None, -) -> None: - return warn_or_fail(*args, filename=filename, line_number=line_number, fail=False) - -def fail( - *args: object, - filename: str | None = None, - line_number: int | None = None, -) -> NoReturn: - warn_or_fail(*args, filename=filename, line_number=line_number, fail=True) - - class CRenderData: def __init__(self) -> None: diff --git a/Tools/clinic/libclinic/__init__.py b/Tools/clinic/libclinic/__init__.py index 738864a..8efaad6 100644 --- a/Tools/clinic/libclinic/__init__.py +++ b/Tools/clinic/libclinic/__init__.py @@ -2,6 +2,8 @@ from typing import Final from .errors import ( ClinicError, + warn, + fail, ) from .formatting import ( SIG_END_MARKER, @@ -32,6 +34,8 @@ from .utils import ( __all__ = [ # Error handling "ClinicError", + "warn", + "fail", # Formatting helpers "SIG_END_MARKER", diff --git a/Tools/clinic/libclinic/errors.py b/Tools/clinic/libclinic/errors.py index afb21b0..f06bdfb 100644 --- a/Tools/clinic/libclinic/errors.py +++ b/Tools/clinic/libclinic/errors.py @@ -1,4 +1,5 @@ import dataclasses as dc +from typing import Literal, NoReturn, overload @dc.dataclass @@ -24,3 +25,48 @@ class ClinicError(Exception): class ParseError(ClinicError): pass + + +@overload +def warn_or_fail( + *args: object, + fail: Literal[True], + filename: str | None = None, + line_number: int | None = None, +) -> NoReturn: ... + +@overload +def warn_or_fail( + *args: object, + fail: Literal[False] = False, + filename: str | None = None, + line_number: int | None = None, +) -> None: ... + +def warn_or_fail( + *args: object, + fail: bool = False, + filename: str | None = None, + line_number: int | None = None, +) -> None: + joined = " ".join([str(a) for a in args]) + error = ClinicError(joined, filename=filename, lineno=line_number) + if fail: + raise error + else: + print(error.report(warn_only=True)) + + +def warn( + *args: object, + filename: str | None = None, + line_number: int | None = None, +) -> None: + return warn_or_fail(*args, filename=filename, line_number=line_number, fail=False) + +def fail( + *args: object, + filename: str | None = None, + line_number: int | None = None, +) -> NoReturn: + warn_or_fail(*args, filename=filename, line_number=line_number, fail=True) |