Debian Codegraph @

pypy/interpreter/pytraceback.py

raw symbols SCIP
1 import sys
2
3 from pypy.interpreter.gateway import unwrap_spec
4 from pypy.interpreter import baseobjspace
5 from pypy.interpreter.error import oefmt, OperationError
6 from pypy.interpreter.location import offset2lineno
7
8 # for some strange reason CPython allows the setting of the lineno to be
9 # negative. I am not sure why that is useful, but let's use the most negative
10 # value as a sentinel to denote the default behaviour "please take the lineno
11 # from the frame and lasti"
12 LINENO_NOT_COMPUTED = -sys.maxint-1
13
14 """
15 """
16
17 class PyTraceback(baseobjspace.W_Root):
18 """Traceback object
19
20 Public app-level fields:
21 * 'tb_frame'
22 * 'tb_lasti'
23 * 'tb_lineno'
24 * 'tb_next'
25 """
26
27 def __init__(self, space, frame, lasti, next, lineno=LINENO_NOT_COMPUTED):
28 self.space = space
29 self.frame = frame
30 self.lasti = lasti
31 self.next = next
32 self.lineno = lineno
33
34 def get_lineno(self):
35 if self.lineno == LINENO_NOT_COMPUTED:
36 self.lineno = offset2lineno(self.frame.pycode, self.lasti)
37 return self.lineno
38
39 def descr_get_tb_lineno(self, space):
40 return space.newint(self.get_lineno())
41
42 def descr_set_tb_lineno(self, space, w_lineno):
43 self.lineno = space.int_w(w_lineno)
44
45 def descr_get_tb_lasti(self, space):
46 return space.newint(self.lasti)
47
48 def descr_set_tb_lasti(self, space, w_lasti):
49 self.lasti = space.int_w(w_lasti)
50
51 def descr_get_next(self, space):
52 return self.next
53
54 def descr_set_next(self, space, w_next):
55 newnext = space.interp_w(PyTraceback, w_next, can_be_None=True)
56 # check for loops
57 curr = newnext
58 while curr is not None and isinstance(curr, PyTraceback):
59 if curr is self:
60 raise oefmt(space.w_ValueError, 'traceback loop detected')
61 curr = curr.next
62 self.next = newnext
63
64 @staticmethod
65 @unwrap_spec(lasti=int, lineno=int)
66 def descr_new(space, w_subtype, w_next, w_frame, lasti, lineno):
67 from pypy.interpreter.pyframe import PyFrame
68 w_next = space.interp_w(PyTraceback, w_next, can_be_None=True)
69 w_frame = space.interp_w(PyFrame, w_frame)
70 traceback = space.allocate_instance(PyTraceback, w_subtype)
71 PyTraceback.__init__(traceback, space, w_frame, lasti, w_next, lineno)
72 return traceback
73
74 def descr__reduce__(self, space):
75 from pypy.interpreter.mixedmodule import MixedModule
76 w_mod = space.getbuiltinmodule('_pickle_support')
77 mod = space.interp_w(MixedModule, w_mod)
78 new_inst = mod.get('traceback_new')
79
80 tup_base = []
81 tup_state = [
82 self.frame,
83 space.newint(self.lasti),
84 self.next,
85 space.newint(self.lineno)
86 ]
87 nt = space.newtuple
88 return nt([new_inst, nt(tup_base), nt(tup_state)])
89
90 def descr__setstate__(self, space, w_args):
91 from pypy.interpreter.pyframe import PyFrame
92 args_w = space.unpackiterable(w_args, 4)
93 w_frame, w_lasti, w_next, w_lineno = args_w
94 self.frame = space.interp_w(PyFrame, w_frame)
95 self.lasti = space.int_w(w_lasti)
96 self.next = space.interp_w(PyTraceback, w_next, can_be_None=True)
97 self.lineno = space.int_w(w_lineno)
98
99 def descr__dir__(self, space):
100 return space.newlist([space.newtext(n) for n in
101 ['tb_frame', 'tb_next', 'tb_lasti', 'tb_lineno']])
102
103
104 def record_application_traceback(space, operror, frame, last_instruction):
105 if frame.pycode.hidden_applevel:
106 return
107 tb = operror.get_traceback()
108 tb = PyTraceback(space, frame, last_instruction, tb)
109 operror.set_traceback(tb)
110
111
112 def check_traceback(space, w_tb, msg):
113 if w_tb is None or not space.isinstance_w(w_tb, space.gettypeobject(PyTraceback.typedef)):
114 raise OperationError(space.w_TypeError, space.newtext(msg))
115 return w_tb