Debian Codegraph @

pypy/module/_pickle_support/maker.py

raw symbols SCIP
1 from pypy.interpreter.error import oefmt
2 from pypy.interpreter.nestedscope import Cell
3 from pypy.interpreter.pycode import PyCode
4 from pypy.interpreter.function import Function, Method
5 from pypy.interpreter.module import Module
6 from pypy.interpreter.pytraceback import PyTraceback
7 from pypy.interpreter.generator import GeneratorIterator, Coroutine
8 from rpython.rlib.objectmodel import instantiate
9 from pypy.interpreter.gateway import unwrap_spec
10 from pypy.objspace.std.iterobject import W_SeqIterObject, W_ReverseSeqIterObject
11
12
13 #note: for now we don't use the actual value when creating the Cell.
14 # (i.e. we assume it will be handled by __setstate__)
15 # Stackless does use this so it might be needed here as well.
16
17 def cell_new(space):
18 return instantiate(Cell)
19
20 def code_new(space, __args__):
21 w_type = space.gettypeobject(PyCode.typedef)
22 return space.call_args(w_type, __args__)
23
24 def func_new(space):
25 fu = instantiate(Function)
26 fu.w_func_dict = space.newdict()
27 return fu
28
29 def module_new(space, w_name, w_dict):
30 new_mod = Module(space, w_name, w_dict)
31 return new_mod
32
33 def method_new(space, __args__):
34 w_type = space.gettypeobject(Method.typedef)
35 return space.call_args(w_type, __args__)
36
37 def builtin_method_new(space, w_instance, w_name):
38 return space.getattr(w_instance, w_name)
39
40 def dictiter_surrogate_new(space, w_lis):
41 # we got a listobject.
42 # simply create an iterator and that's it.
43 return space.iter(w_lis)
44
45 def frame_new(space):
46 new_frame = instantiate(space.FrameClass) # XXX fish
47 return new_frame
48
49 def traceback_new(space):
50 tb = instantiate(PyTraceback)
51 return tb
52
53 def generator_new(space):
54 new_generator = instantiate(GeneratorIterator)
55 return new_generator
56
57 def coroutine_new(space):
58 new_coroutine = instantiate(Coroutine)
59 return new_coroutine
60
61 def longrangeiter_new(space, w_start, w_step, w_len, w_index):
62 from pypy.module.__builtin__.functional import W_LongRangeIterator
63 new_iter = W_LongRangeIterator(space, w_start, w_step, w_len, w_index)
64 return new_iter
65
66 @unwrap_spec(current=int, remaining=int, step=int)
67 def intrangeiter_new(space, current, remaining, step):
68 from pypy.module.__builtin__.functional import W_IntRangeIterator
69 new_iter = W_IntRangeIterator(space, current, remaining, step)
70 return new_iter
71
72 @unwrap_spec(identifier='text')
73 def builtin_code(space, identifier):
74 from pypy.interpreter import gateway
75 try:
76 return gateway.BuiltinCode.find(space, identifier)
77 except KeyError:
78 raise oefmt(space.w_RuntimeError,
79 "cannot unpickle builtin code: %s", identifier)
80
81 @unwrap_spec(identifier='text')
82 def builtin_function(space, identifier):
83 from pypy.interpreter import function
84 try:
85 return function.Function.find(space, identifier)
86 except KeyError:
87 raise oefmt(space.w_RuntimeError,
88 "cannot unpickle builtin function: %s", identifier)
89
90
91 # ___________________________________________________________________
92 # Helper functions for internal use
93
94 # adopted from prickelpit.c (but almost completely different)
95
96 def slp_into_tuple_with_nulls(space, seq_w):
97 """
98 create a tuple with the object and store
99 a tuple with the positions of NULLs as first element.
100 """
101 tup = [None] * (len(seq_w) + 1)
102 num = 1
103 nulls = [None for i in seq_w if i is None]
104 null_num = 0
105 for w_obj in seq_w:
106 if w_obj is None:
107 nulls[null_num] = space.newint(num - 1)
108 null_num += 1
109 w_obj = space.w_None
110 tup[num] = w_obj
111 num += 1
112 tup[0] = space.newtuple(nulls)
113 return space.newtuple(tup)
114
115 def slp_from_tuple_with_nulls(space, w_tup):
116 tup_w = space.unpackiterable(w_tup)
117 nulls = space.unpackiterable(tup_w[0])
118 tup_w = tup_w[1:]
119 for w_p in nulls:
120 p = space.int_w(w_p)
121 tup_w[p] = None
122 return tup_w