Skip to content

Commit

Permalink
gh-113317, AC: Move warn() and fail() to libclinic.errors (#116770)
Browse files Browse the repository at this point in the history
  • Loading branch information
vstinner committed Mar 14, 2024
1 parent 3a25d9c commit a18c985
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 47 deletions.
48 changes: 1 addition & 47 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@
Protocol,
TypeVar,
cast,
overload,
)


# Local imports.
import libclinic
import libclinic.cpp
from libclinic import ClinicError
from libclinic import ClinicError, fail, warn


# TODO:
Expand Down Expand Up @@ -94,51 +93,6 @@ def __repr__(self) -> str:
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:

Expand Down
4 changes: 4 additions & 0 deletions Tools/clinic/libclinic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .errors import (
ClinicError,
warn,
fail,
)
from .formatting import (
SIG_END_MARKER,
Expand Down Expand Up @@ -32,6 +34,8 @@
__all__ = [
# Error handling
"ClinicError",
"warn",
"fail",

# Formatting helpers
"SIG_END_MARKER",
Expand Down
46 changes: 46 additions & 0 deletions Tools/clinic/libclinic/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import dataclasses as dc
from typing import Literal, NoReturn, overload


@dc.dataclass
Expand All @@ -24,3 +25,48 @@ def report(self, *, warn_only: bool = False) -> str:

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)

0 comments on commit a18c985

Please sign in to comment.