]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113317, AC: Move warn() and fail() to libclinic.errors (#116770)
authorVictor Stinner <vstinner@python.org>
Thu, 14 Mar 2024 08:07:01 +0000 (09:07 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Mar 2024 08:07:01 +0000 (08:07 +0000)
Tools/clinic/clinic.py
Tools/clinic/libclinic/__init__.py
Tools/clinic/libclinic/errors.py

index 893f4cc12ed084edd4ddf6ee17a677c056ac0972..4c7c4dca37cd0ffd360b98c967c7dc04ee48a754 100755 (executable)
@@ -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:
 
index 738864a48c08d3f7e901739353073fba86e5bd9d..8efaad6539d7aee8e9ac84076f78d618af80b099 100644 (file)
@@ -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",
index afb21b02386fe7f15a0c671f2e89ca9ebc6c56ba..f06bdfbd864b2c0ab4bce14945f0228125b5afad 100644 (file)
@@ -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)