Debian Codegraph @

pypy/interpreter/pyparser/error.py

raw symbols SCIP
1 from rpython.rlib.objectmodel import specialize
2 from pypy.interpreter.unicodehelper import _str_decode_utf8_slowpath
3
4 def wrap_pos(space, num):
5 if num == 0:
6 return space.w_None
7 return space.newint(num)
8
9 @specialize.memo()
10 def make_replace_error_handler(space):
11 def replace_error_handler(errors, encoding, msg, w_s, startpos, endpos):
12 return b'\xef\xbf\xbd', endpos, 'b', space.bytes_w(w_s), w_s
13 return replace_error_handler
14
15 def _adjust_offset(space, offset, text, unilength):
16 if offset > len(text):
17 offset = unilength
18 elif offset >= 1:
19 offset = offset - 1 # 1-based to 0-based
20 assert offset >= 0
21 # slightly inefficient, call the decoder for text[:offset] too
22 s = text[:offset]
23 w_s = space.newbytes(s)
24 _, offset, _ = _str_decode_utf8_slowpath(space,
25 s, w_s, 'replace', False, make_replace_error_handler(space),
26 True)
27 offset += 1 # convert to 1-based
28 else:
29 offset = 0
30 return offset
31
32 class SyntaxError(Exception):
33 """Base class for exceptions raised by the parser."""
34
35 def __init__(self, msg, lineno=0, offset=0, text=None, filename=None,
36 end_lineno=0, end_offset=0):
37 self.msg = msg
38 self.lineno = lineno
39 # NB: offset and end_offset are 1-based indexes into the bytes source
40 self.offset = offset
41 self.text = text
42 self.filename = filename
43 self.end_lineno = end_lineno
44 self.end_offset = end_offset
45
46 @staticmethod
47 def fromast(msg, node, filename=None):
48 return SyntaxError(msg, node.lineno, node.col_offset + 1,
49 filename=filename,
50 end_lineno=node.end_lineno,
51 end_offset=node.end_col_offset + 1)
52
53 def find_sourceline_and_wrap_info(self, space, source=None, filename=None):
54 """ search for the line of input that caused the error and then return
55 a wrapped tuple that can be used to construct a wrapped SyntaxError.
56 Optionally pass source, to get better error messages for the case where
57 this instance was constructed without a source line (.text
58 attribute)"""
59 text = self.text
60 if text is None and source is not None and self.lineno:
61 lines = source.splitlines(True)
62 text = lines[self.lineno - 1]
63 w_text = w_filename = space.w_None
64 w_lineno = space.newint(self.lineno)
65 if filename is None:
66 filename = self.filename
67 if filename is not None:
68 w_filename = space.newfilename(filename)
69 if text is None:
70 w_text = space.appexec([w_filename, w_lineno],
71 """(filename, lineno):
72 try:
73 with open(filename) as f:
74 for _ in range(lineno - 1):
75 f.readline()
76 return f.readline()
77 except: # we can't allow any exceptions here!
78 return None""")
79 offset = self.offset
80 end_offset = self.end_offset
81 if text is not None:
82 # text may not be UTF-8 in case of decoding errors.
83 # adjust the encoded text offset to a decoded offset
84 # XXX do the right thing about continuation lines, which
85 # XXX are their own fun, sometimes giving offset >
86 # XXX len(text) for example (right now, avoid crashing)
87
88 # this also converts the byte-based self.offset to a
89 # codepoint-based index into the decoded unicode-version of
90 # self.text
91
92 replacedtext, unilength, _ = _str_decode_utf8_slowpath(space,
93 text, space.newbytes(text), 'replace', False, make_replace_error_handler(space), True)
94 offset = _adjust_offset(space, offset, text, unilength)
95 # XXX this is wrong if end_lineno != lineno
96 end_offset = _adjust_offset(space, end_offset, text, unilength)
97 w_text = space.newutf8(replacedtext, unilength)
98 return space.newtuple([
99 space.newtext(self.msg),
100 space.newtuple([
101 w_filename, w_lineno, wrap_pos(space, offset),
102 w_text, wrap_pos(space, self.end_lineno),
103 wrap_pos(space, end_offset)])])
104
105 def __str__(self):
106 return "%s at pos (%d, %d) in %r" % (
107 self.__class__.__name__, self.lineno, self.offset, self.text)
108
109 class IndentationError(SyntaxError):
110 pass
111
112 class TabError(IndentationError):
113 def __init__(self, lineno=0, offset=0, text=None, filename=None,
114 end_lineno=0, end_offset=0):
115 msg = "inconsistent use of tabs and spaces in indentation"
116 IndentationError.__init__(
117 self, msg, lineno, offset, text, filename, end_lineno, end_offset)
118
119 class ASTError(Exception):
120 def __init__(self, msg, ast_node):
121 self.msg = msg
122 self.ast_node = ast_node
123
124
125 class TokenError(SyntaxError):
126
127 def __init__(self, msg, line, lineno, column, tokens, end_lineno=0, end_offset=0):
128 SyntaxError.__init__(self, msg, lineno, column, line,
129 end_lineno=end_lineno, end_offset=end_offset)
130 self.tokens = tokens
131
132 class TokenIndentationError(IndentationError):
133
134 def __init__(self, msg, line, lineno, column, tokens):
135 SyntaxError.__init__(self, msg, lineno, column, line)
136 self.tokens = tokens