forked from prompt-toolkit/ptpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalidator.py
More file actions
54 lines (44 loc) · 1.81 KB
/
validator.py
File metadata and controls
54 lines (44 loc) · 1.81 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from prompt_toolkit.validation import ValidationError, Validator
__all__ = ["PythonValidator"]
class PythonValidator(Validator):
"""
Validation of Python input.
:param get_compiler_flags: Callable that returns the currently
active compiler flags.
"""
def __init__(self, get_compiler_flags=None):
self.get_compiler_flags = get_compiler_flags
def validate(self, document):
"""
Check input for Python syntax errors.
"""
text = document.text
# If the input is single line, remove leading whitespace.
# (This doesn't have to be a syntax error.)
if len(text.splitlines()) == 1:
text = text.strip()
# When the input starts with Ctrl-Z, always accept. This means EOF in a
# Python REPL.
if text.startswith("\x1a"):
return
try:
if self.get_compiler_flags:
flags = self.get_compiler_flags()
else:
flags = 0
compile(text, "<input>", "exec", flags=flags, dont_inherit=True)
except SyntaxError as e:
# Note, the 'or 1' for offset is required because Python 2.7
# gives `None` as offset in case of '4=4' as input. (Looks like
# fixed in Python 3.)
index = document.translate_row_col_to_index(
e.lineno - 1, (e.offset or 1) - 1
)
raise ValidationError(index, f"Syntax Error: {e}")
except TypeError as e:
# e.g. "compile() expected string without null bytes"
raise ValidationError(0, str(e))
except ValueError as e:
# In Python 2, compiling "\x9" (an invalid escape sequence) raises
# ValueError instead of SyntaxError.
raise ValidationError(0, "Syntax Error: %s" % e)