Debian Codegraph @

pypy/interpreter/nestedscope.py

raw symbols SCIP
1 from rpython.tool.uid import uid
2 from rpython.rlib import jit
3
4 from pypy.interpreter.baseobjspace import W_Root
5 from pypy.interpreter.error import oefmt
6 from pypy.interpreter.mixedmodule import MixedModule
7
8
9 def make_cell_cmp(op):
10 import operator
11 cmp = getattr(operator, op)
12 def descr_cell_(self, space, w_other):
13 if not isinstance(w_other, Cell):
14 return space.w_NotImplemented
15 if self.w_value is None or w_other.w_value is None:
16 return space.newbool(cmp(self._cmp_one_empty(w_other), 0))
17 return getattr(space, op)(self.w_value, w_other.w_value)
18 descr_cell_.func_name += op
19 return descr_cell_
20
21
22 class Cell(W_Root):
23 "A simple container for a wrapped value."
24
25 _immutable_fields_ = ['family']
26
27 def __init__(self, w_value, family):
28 self.w_value = w_value
29 self.family = family
30
31 def get(self):
32 if jit.isconstant(self):
33 # ever_mutated is False if we never see a transition from not-None to
34 # not-None. That means _elidable_get might return an out-of-date
35 # None, and by now the cell was written to, with a not-None. So if
36 # we see a None, we don't return that and instead read self.w_value
37 # in the code below.
38 if not self.family.ever_mutated:
39 w_res = self._elidable_get()
40 if w_res is not None:
41 return w_res
42 if self.w_value is None:
43 raise ValueError("get() from an empty cell")
44 return self.w_value
45
46 def empty(self):
47 return self.w_value is None
48
49 @jit.elidable
50 def _elidable_get(self):
51 return self.w_value
52
53 def set(self, w_value):
54 if not self.family.ever_mutated and self.w_value is not None:
55 self.family.ever_mutated = True
56 self.w_value = w_value
57
58 def delete(self):
59 if not self.family.ever_mutated:
60 self.family.ever_mutated = True
61 if self.w_value is None:
62 raise ValueError("delete() on an empty cell")
63 self.w_value = None
64
65 def _cmp_one_empty(self, w_other):
66 if self.w_value is None:
67 if w_other.w_value is None:
68 return 0
69 return -1
70 else:
71 assert w_other.w_value is None
72 return 1
73
74 descr_eq = make_cell_cmp("eq")
75 descr_ne = make_cell_cmp("ne")
76 descr_lt = make_cell_cmp("lt")
77 descr_gt = make_cell_cmp("gt")
78 descr_ge = make_cell_cmp("ge")
79 descr_le = make_cell_cmp("le")
80
81 def descr__reduce__(self, space):
82 w_mod = space.getbuiltinmodule('_pickle_support')
83 mod = space.interp_w(MixedModule, w_mod)
84 new_inst = mod.get('cell_new')
85 if self.w_value is None: # when would this happen?
86 return space.newtuple2(new_inst, space.newtuple([]))
87 tup = [self.w_value]
88 return space.newtuple([new_inst, space.newtuple([]),
89 space.newtuple(tup)])
90
91 def descr__setstate__(self, space, w_state):
92 self.w_value = space.getitem(w_state, space.newint(0))
93
94 def __repr__(self):
95 """ representation for debugging purposes """
96 if self.w_value is None:
97 content = ""
98 else:
99 content = repr(self.w_value)
100 return "<%s(%s) at 0x%x>" % (self.__class__.__name__,
101 content, uid(self))
102
103 def descr__repr__(self, space):
104 if self.w_value is None:
105 content = "empty"
106 else:
107 content = "%s object at 0x%s" % (space.type(self.w_value).name,
108 self.w_value.getaddrstring(space))
109 s = "<cell at 0x%s: %s>" % (self.getaddrstring(space), content)
110 return space.newtext(s)
111
112 def descr__cell_contents(self, space):
113 try:
114 return self.get()
115 except ValueError:
116 raise oefmt(space.w_ValueError, "Cell is empty")
117
118 def descr_set_cell_contents(self, space, w_value):
119 return self.set(w_value)
120
121 def descr_del_cell_contents(self, space):
122 try:
123 return self.delete()
124 except ValueError:
125 pass # CPython ignores it
126
127
128 def descr_new_cell(space, w_type, w_obj=None):
129 """ Create and return a new cell. If an argument is given, it is used as
130 the cell_contents of the cell, otherwise the cell is empty. """
131 return Cell(w_obj, DUMMY_FAMILY)
132
133
134 class CellFamily(object):
135 _immutable_fields_ = ['ever_mutated?']
136
137 def __init__(self, name):
138 self.name = name
139 self.ever_mutated = False
140
141 DUMMY_FAMILY = CellFamily("<dummy>")
142 DUMMY_FAMILY.ever_mutated = True