Debian Codegraph @

pypy/module/_continuation/interp_pickle.py

raw symbols SCIP
1 from pypy.tool import stdlib_opcode as pythonopcode
2 from rpython.rlib import jit
3 from pypy.interpreter.error import OperationError
4 from pypy.interpreter.pyframe import PyFrame
5 from pypy.module._continuation.interp_continuation import (
6 State, global_state, build_sthread, pre_switch, post_switch,
7 get_result, geterror)
8
9
10 def getunpickle(space):
11 cs = space.fromcache(State)
12 return cs.w_unpickle
13
14
15 def reduce(self):
16 # xxx this is known to be not completely correct with respect
17 # to subclasses, e.g. no __slots__ support, no looking for a
18 # __getnewargs__ or __getstate__ defined in the subclass, etc.
19 # Doing the right thing looks involved, though...
20 space = self.space
21 w_frame = self.descr_get_frame(space)
22 w_continulet_type = space.type(self)
23 w_dict = self.getdict(space) or space.w_None
24 args = [getunpickle(space),
25 space.newtuple([w_continulet_type]),
26 space.newtuple2(w_frame, w_dict),
27 ]
28 return space.newtuple(args)
29
30 def setstate(self, w_args):
31 space = self.space
32 if self.sthread is not None:
33 raise geterror(space, "continulet.__setstate__() on an already-"
34 "initialized continulet")
35 w_frame, w_dict = space.fixedview(w_args, expected_length=2)
36 if not space.is_w(w_dict, space.w_None):
37 self.setdict(space, w_dict)
38 if space.is_w(w_frame, space.w_False):
39 return # not initialized
40 sthread = build_sthread(self.space)
41 self.sthread = sthread
42 self.bottomframe = space.interp_w(PyFrame, w_frame, can_be_None=True)
43 #
44 global_state.origin = self
45 if self.bottomframe is not None:
46 sthread.frame2continulet.set(self.bottomframe, self)
47 self.h = sthread.new(resume_trampoline_callback)
48 get_result() # propagate the eventual MemoryError
49
50 # ____________________________________________________________
51
52 def resume_trampoline_callback(h, arg):
53 self = global_state.origin
54 self.h = h
55 space = self.space
56 sthread = self.sthread
57 try:
58 global_state.clear()
59 if self.bottomframe is None:
60 w_result = space.w_None
61 else:
62 saved_exception = pre_switch(sthread)
63 h = sthread.switch(self.h)
64 try:
65 w_result = post_switch(sthread, h, saved_exception)
66 operr = None
67 except OperationError as e:
68 w_result = None
69 operr = e
70 #
71 while True:
72 ec = sthread.ec
73 frame = ec.topframeref()
74 assert frame is not None # XXX better error message
75 exit_continulet = sthread.frame2continulet.get(frame)
76 #
77 continue_after_call(frame)
78 #
79 # small hack: unlink frame out of the execution context,
80 # because execute_frame will add it there again
81 ec.topframeref = frame.f_backref
82 #
83 try:
84 w_result = frame.execute_frame(w_result, operr)
85 operr = None
86 except OperationError as e:
87 w_result = None
88 operr = e
89 if exit_continulet is not None:
90 self = exit_continulet
91 break
92 sthread.ec.topframeref = jit.vref_None
93 if operr:
94 raise operr
95 except Exception as e:
96 global_state.propagate_exception = e
97 else:
98 global_state.w_value = w_result
99 global_state.origin = self
100 global_state.destination = self
101 return self.h
102
103 def continue_after_call(frame):
104 code = frame.pycode.co_code
105 instr = frame.last_instr
106 opcode = ord(code[instr])
107 map = pythonopcode.opmap
108 call_ops = [map['CALL_FUNCTION'], map['CALL_FUNCTION_KW'],
109 map['CALL_FUNCTION_VAR'], map['CALL_FUNCTION_VAR_KW'],
110 map['CALL_METHOD']]
111 assert opcode in call_ops # XXX check better, and complain better
112 frame.last_instr = instr + 2 # continue after the call