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

Share more code between the warn() and fail() branches #49

Merged
Show file tree
Hide file tree
Changes from all 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: 6 additions & 2 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,8 +1446,12 @@ def test_non_ascii_character_in_docstring(self):
self.parse(block)
# The line numbers are off; this is a known limitation.
expected = dedent("""\
Warning on line 0: Non-ascii characters are not allowed in docstrings: 'á'
Warning on line 0: Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß'
Warning on line 0:
Non-ascii characters are not allowed in docstrings: 'á'

Warning on line 0:
Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß'

""")
self.assertEqual(stdout.getvalue(), expected)

Expand Down
48 changes: 22 additions & 26 deletions Tools/clinic/clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,25 @@ def text_accumulator() -> TextAccumulator:
return TextAccumulator(append, output)


@dc.dataclass
class ClinicError(Exception):
def __init__(
self,
message: str,
/,
*,
lineno: int | None = None,
filename: str | None = None
) -> None:
super().__init__(message)
self.lineno = lineno
self.filename = filename
message: str
_: dc.KW_ONLY
lineno: int | None = None
filename: str | None = None

def __post_init__(self) -> None:
super().__init__(self.message)

def report(self, *, warn_only: bool = False) -> str:
msg = "Warning" if warn_only else "Error"
if self.filename is not None:
msg += f" in file {self.filename!r}"
if self.lineno is not None:
msg += f" on line {self.lineno}"
msg += ":\n"
msg += f"{self.message}\n"
return msg


@overload
Expand Down Expand Up @@ -179,16 +186,11 @@ def warn_or_fail(
filename = clinic.filename
if getattr(clinic, 'block_parser', None) and (line_number is None):
line_number = clinic.block_parser.line_number
error = ClinicError(joined, filename=filename, lineno=line_number)
if fail:
raise ClinicError(joined, lineno=line_number, filename=filename)
raise error
else:
msg = "Warning"
if filename is not None:
msg += f" in file {filename!r}"
if line_number is not None:
msg += f" on line {line_number}"
msg += f": {joined}"
print(msg)
print(error.report(warn_only=True))


def warn(
Expand Down Expand Up @@ -5734,13 +5736,7 @@ def main(argv: list[str] | None = None) -> NoReturn:
try:
run_clinic(parser, args)
except ClinicError as exc:
sys.stderr.write("Error")
if exc.filename is not None:
sys.stderr.write(f" in file {exc.filename!r}")
if exc.lineno is not None:
sys.stderr.write(f" on line {exc.lineno}")
sys.stderr.write(":\n")
sys.stderr.write(f"{exc}\n")
sys.stderr.write(exc.report())
sys.exit(1)
else:
sys.exit(0)
Expand Down