-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patherrors.py
38 lines (28 loc) · 1.13 KB
/
errors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import re
class FatalError(Exception):
"""Class for throwing fatal errors."""
def __str__(self):
return " ".join(map(str, self.args))
class BadSyntax(FatalError):
"""Unrecoverable syntax error, e.g. starting with a binary operator."""
pass
class IncompleteSyntax(FatalError):
"""Recoverable syntax error, e.g. unmatched open parenthesis."""
pass
class ErrorReporter:
"""Class for reporting error messages."""
def __init__(self, warnings=False):
# The warnings parameter determines whether to print nonfatal
# errors to stderr or suppress them
self.warnings = warnings
def warn(self, *message):
"""Print a nonfatal error if warnings are turned on."""
if self.warnings:
print(*map(rewritePtypes, message), file=sys.stderr)
def die(self, *message, errorClass=FatalError):
"""Raise a fatal error."""
raise errorClass(*map(rewritePtypes, message))
def rewritePtypes(message):
"""Convert references to Pip types in a message to just their names."""
return re.sub(r"<class 'ptypes\.(\w+)'>", r"\1", str(message))