Debian Codegraph @

pypy/module/__builtin__/descriptor.py

raw symbols SCIP
1 from rpython.rlib import jit
2 from pypy.interpreter.baseobjspace import W_Root
3 from pypy.interpreter.error import OperationError, oefmt
4 from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
5 from pypy.interpreter.typedef import (
6 GetSetProperty, TypeDef, generic_new_descr, interp_attrproperty_w)
7 from pypy.objspace.descroperation import object_getattribute
8
9
10 class W_Super(W_Root):
11
12 def __init__(self, space):
13 self.w_starttype = None
14 self.w_objtype = None
15 self.w_self = None
16
17 def descr_init(self, space, args_w):
18 if len(args_w) > 2:
19 raise oefmt(space.w_TypeError,
20 "super() expected at most 2 arguments (%d given)",
21 len(args_w))
22 w_starttype = args_w[0] if len(args_w) >= 1 else space.w_None
23 w_obj_or_type = args_w[1] if len(args_w) >= 2 else space.w_None
24 if space.is_none(w_starttype):
25 frame = space.getexecutioncontext().gettopframe()
26 w_starttype, w_obj_or_type = _super_from_frame(space, frame)
27
28 if space.is_none(w_obj_or_type):
29 w_type = None # unbound super object
30 w_obj_or_type = None
31 else:
32 if not space.isinstance_w(w_starttype, space.w_type):
33 raise oefmt(space.w_TypeError, "super() argument 1 must be a type, not %T", w_starttype)
34 w_type = _super_check(space, w_starttype, w_obj_or_type)
35 self.w_starttype = w_starttype
36 self.w_objtype = w_type
37 self.w_self = w_obj_or_type # may be None
38
39 def descr_repr(self, space):
40 if self.w_objtype is not None:
41 objtype_name = "<%s object>" % self.w_objtype.getname(space)
42 else:
43 objtype_name = 'NULL'
44 if self.w_starttype is not None:
45 starttype_name = self.w_starttype.getname(space)
46 else:
47 starttype_name = 'NULL'
48 return space.newtext("<super: <class '%s'>, %s>" % (
49 starttype_name, objtype_name))
50
51 def get(self, space, w_obj, w_type=None):
52 if self.w_self is not None or space.is_w(w_obj, space.w_None):
53 return self
54 if self.w_starttype is None:
55 raise oefmt(space.w_TypeError,
56 "__get__(x) is invalid on an uninitialized instance of 'super'")
57 else:
58 # if type(self) is W_Super:
59 # XXX write a fast path for this common case
60 w_selftype = space.type(self)
61 return space.call_function(w_selftype, self.w_starttype, w_obj)
62
63 def getattribute(self, space, w_name):
64 name = space.text_w(w_name)
65 # only use a special logic for bound super objects and not for
66 # getting the __class__ of the super object itself.
67 if self.w_objtype is not None and name != '__class__':
68 assert self.w_starttype is not None
69 w_value = space.lookup_in_type_starting_at(self.w_objtype,
70 self.w_starttype,
71 name)
72 if w_value is not None:
73 w_get = space.lookup(w_value, '__get__')
74 if w_get is None:
75 return w_value
76 # Only pass 'obj' param if this is instance-mode super
77 # (see CPython sourceforge id #743627)
78 w_obj = self.w_self
79 if w_obj is None or w_obj is self.w_objtype:
80 w_obj = space.w_None
81 return space.get_and_call_function(w_get, w_value,
82 w_obj, self.w_objtype)
83 # fallback to object.__getattribute__()
84 return space.call_function(object_getattribute(space), self, w_name)
85
86 @jit.elidable
87 def _get_self_location(space, code):
88 if code.co_argcount == 0:
89 raise oefmt(space.w_RuntimeError, "super(): no arguments")
90 args_to_copy = code._args_as_cellvars
91 for i in range(len(args_to_copy)):
92 if args_to_copy[i] == 0:
93 self_cell = i
94 break
95 else:
96 self_cell = -1
97
98 for index, name in enumerate(code.co_freevars):
99 if name == '__class__':
100 break
101 else:
102 raise oefmt(space.w_RuntimeError, "super(): __class__ cell not found")
103 class_cell = len(code.co_cellvars) + index
104 return self_cell, class_cell
105
106 def _super_from_frame(space, frame):
107 """super() without args -- fill in from __class__ and first local
108 variable on the stack.
109 """
110 if frame is None:
111 raise oefmt(space.w_RuntimeError, "super(): no frame object")
112 self_cell, class_cell = _get_self_location(space, frame.getcode())
113 if self_cell < 0:
114 w_obj = frame.locals_cells_stack_w[0]
115 else:
116 w_obj = frame._getcell(self_cell).w_value
117 if not w_obj:
118 raise oefmt(space.w_RuntimeError, "super(): arg[0] deleted")
119
120 # a kind of LOAD_DEREF
121 w_starttype = frame._getcell(class_cell).w_value
122 if w_starttype is None:
123 raise oefmt(space.w_RuntimeError, "super(): empty __class__ cell")
124 return w_starttype, w_obj
125
126 def _super_check(space, w_starttype, w_obj_or_type):
127 """Check that the super() call makes sense. Returns a type"""
128 w_objtype = space.type(w_obj_or_type)
129
130 if (space.issubtype_w(w_objtype, space.w_type) and
131 space.issubtype_w(w_obj_or_type, w_starttype)):
132 # special case for class methods
133 return w_obj_or_type
134
135 if space.issubtype_w(w_objtype, w_starttype):
136 # normal case
137 return w_objtype
138
139 try:
140 w_type = space.getattr(w_obj_or_type, space.newtext('__class__'))
141 except OperationError as e:
142 if not e.match(space, space.w_AttributeError):
143 raise
144 w_type = w_objtype
145
146 if space.issubtype_w(w_type, w_starttype):
147 return w_type
148 raise oefmt(space.w_TypeError,
149 "super(type, obj): obj must be an instance or subtype of type")
150
151 W_Super.typedef = TypeDef(
152 'super',
153 __new__ = generic_new_descr(W_Super),
154 __init__ = interp2app(W_Super.descr_init),
155 __repr__ = interp2app(W_Super.descr_repr),
156 __thisclass__ = interp_attrproperty_w("w_starttype", W_Super),
157 __self__ = interp_attrproperty_w("w_self", W_Super),
158 __self_class__ = interp_attrproperty_w("w_objtype", W_Super),
159 __getattribute__ = interp2app(W_Super.getattribute),
160 __get__ = interp2app(W_Super.get),
161 __doc__ = """\
162 super(type) -> unbound super object
163 super(type, obj) -> bound super object; requires isinstance(obj, type)
164 super(type, type2) -> bound super object; requires issubclass(type2, type)
165
166 Typical use to call a cooperative superclass method:
167
168 class C(B):
169 def meth(self, arg):
170 super(C, self).meth(arg)"""
171 )
172
173
174 class W_Property(W_Root):
175 _immutable_fields_ = ["w_fget?", "w_fset?", "w_fdel?"]
176
177 def __init__(self, space):
178 self.w_fget = space.w_None
179 self.w_fset = space.w_None
180 self.w_fdel = space.w_None
181 self.w_doc = space.w_None
182 self.getter_doc = False
183 self.w_name = None
184
185 @unwrap_spec(w_fget=WrappedDefault(None),
186 w_fset=WrappedDefault(None),
187 w_fdel=WrappedDefault(None),
188 w_doc=WrappedDefault(None))
189 def init(self, space, w_fget=None, w_fset=None, w_fdel=None, w_doc=None):
190 self.w_fget = w_fget
191 self.w_fset = w_fset
192 self.w_fdel = w_fdel
193 self.w_doc = w_doc
194 self.getter_doc = False
195 # our __doc__ comes from the getter if we don't have an explicit one
196 if (space.is_w(self.w_doc, space.w_None) and
197 not space.is_w(self.w_fget, space.w_None)):
198 w_getter_doc = space.findattr(self.w_fget, space.newtext('__doc__'))
199 if w_getter_doc is not None:
200 if type(self) is W_Property:
201 self.w_doc = w_getter_doc
202 else:
203 space.setattr(self, space.newtext('__doc__'), w_getter_doc)
204 self.getter_doc = True
205
206 def _properror(self, space, w_obj, kind):
207 qualname = space.type(w_obj).qualname
208 if self.w_name is not None:
209 raise oefmt(
210 space.w_AttributeError,
211 "property %R of '%s' object has no %s",
212 self.w_name, qualname, kind)
213 else:
214 raise oefmt(
215 space.w_AttributeError,
216 "property of '%s' object has no %s",
217 qualname, kind)
218
219
220
221 def get(self, space, w_obj, w_objtype=None):
222 if space.is_w(w_obj, space.w_None):
223 return self
224 if space.is_w(self.w_fget, space.w_None):
225 self._properror(space, w_obj, 'getter')
226 return space.call_function(self.w_fget, w_obj)
227
228 def set(self, space, w_obj, w_value):
229 if space.is_w(self.w_fset, space.w_None):
230 self._properror(space, w_obj, 'setter')
231 space.call_function(self.w_fset, w_obj, w_value)
232 return space.w_None
233
234 def delete(self, space, w_obj):
235 if space.is_w(self.w_fdel, space.w_None):
236 self._properror(space, w_obj, 'deleter')
237 space.call_function(self.w_fdel, w_obj)
238 return space.w_None
239
240 def getter(self, space, w_getter):
241 return self._copy(space, w_getter=w_getter)
242
243 def setter(self, space, w_setter):
244 return self._copy(space, w_setter=w_setter)
245
246 def deleter(self, space, w_deleter):
247 return self._copy(space, w_deleter=w_deleter)
248
249 def get_doc(self, space):
250 return self.w_doc
251
252 def set_doc(self, space, w_doc):
253 self.w_doc = w_doc
254 self.getter_doc = False
255
256 def _copy(self, space, w_getter=None, w_setter=None, w_deleter=None):
257 if w_getter is None:
258 w_getter = self.w_fget
259 if w_setter is None:
260 w_setter = self.w_fset
261 if w_deleter is None:
262 w_deleter = self.w_fdel
263 if self.getter_doc and w_getter is not None:
264 w_doc = space.w_None
265 else:
266 w_doc = self.w_doc
267 w_type = self.getclass(space)
268 w_res = space.call_function(w_type, w_getter, w_setter, w_deleter,
269 w_doc)
270 if isinstance(w_res, W_Property):
271 w_res.w_name = self.w_name
272 return w_res
273
274 def set_name(self, space, w_type, w_name):
275 """ Method to set name of a property. """
276 self.w_name = w_name
277
278 def descr_isabstract(self, space):
279 return space.newbool(space.isabstractmethod_w(self.w_fget) or
280 space.isabstractmethod_w(self.w_fset) or
281 space.isabstractmethod_w(self.w_fdel))
282
283 def descr_reduce(self, space):
284 raise oefmt(space.w_TypeError, "cannot pickle 'property' object")
285
286 W_Property.typedef = TypeDef(
287 'property',
288 __doc__ = '''\
289 property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
290
291 fget is a function to be used for getting an attribute value, and likewise
292 fset is a function for setting, and fdel a function for deleting, an
293 attribute. Typical use is to define a managed attribute x:
294 class C(object):
295 def getx(self): return self.__x
296 def setx(self, value): self.__x = value
297 def delx(self): del self.__x
298 x = property(getx, setx, delx, "I am the 'x' property.")''',
299 __new__ = generic_new_descr(W_Property),
300 __init__ = interp2app(W_Property.init),
301 __get__ = interp2app(W_Property.get),
302 __set__ = interp2app(W_Property.set),
303 __delete__ = interp2app(W_Property.delete),
304 __isabstractmethod__ = GetSetProperty(W_Property.descr_isabstract),
305 __reduce__ = interp2app(W_Property.descr_reduce),
306 fdel = interp_attrproperty_w('w_fdel', W_Property),
307 fget = interp_attrproperty_w('w_fget', W_Property),
308 fset = interp_attrproperty_w('w_fset', W_Property),
309 getter = interp2app(W_Property.getter),
310 setter = interp2app(W_Property.setter),
311 deleter = interp2app(W_Property.deleter),
312 __set_name__ = interp2app(W_Property.set_name),
313 )
314 # This allows there to be a __doc__ of the property type and a __doc__
315 # descriptor for the instances.
316 W_Property.typedef.rawdict['__doc__'] = GetSetProperty(
317 W_Property.get_doc, W_Property.set_doc)