| 1 |
""" |
| 2 |
Function objects. |
| 3 |
|
| 4 |
In PyPy there is no difference between built-in and user-defined function |
| 5 |
objects; the difference lies in the code object found in their func_code |
| 6 |
attribute. |
| 7 |
""" |
| 8 |
|
| 9 |
from rpython.rlib.unroll import unrolling_iterable |
| 10 |
from pypy.interpreter.baseobjspace import W_Root |
| 11 |
from pypy.interpreter.error import OperationError, oefmt |
| 12 |
from pypy.interpreter.eval import Code |
| 13 |
from pypy.interpreter.argument import Arguments |
| 14 |
from rpython.rlib import jit |
| 15 |
|
| 16 |
from rpython.rlib.rarithmetic import LONG_BIT |
| 17 |
from rpython.rlib.rbigint import rbigint |
| 18 |
|
| 19 |
|
| 20 |
funccallunrolling = unrolling_iterable(range(4)) |
| 21 |
|
| 22 |
|
| 23 |
@jit.elidable_promote() |
| 24 |
def _get_immutable_code(func): |
| 25 |
assert not func.can_change_code |
| 26 |
return func.code |
| 27 |
|
| 28 |
class Function(W_Root): |
| 29 |
"""A function is a code object captured with some environment: |
| 30 |
an object space, a dictionary of globals, default arguments, |
| 31 |
and an arbitrary 'closure' passed to the code object.""" |
| 32 |
|
| 33 |
can_change_code = True |
| 34 |
_immutable_fields_ = ['code?', |
| 35 |
'w_func_globals?', |
| 36 |
'closure?[*]', |
| 37 |
'defs_w?[*]', |
| 38 |
'name?', |
| 39 |
'qualname?', |
| 40 |
'w_objclass?', |
| 41 |
'w_text_signature?', |
| 42 |
'w_kw_defs?'] |
| 43 |
|
| 44 |
w_kw_defs = None |
| 45 |
w_objclass = None |
| 46 |
w_text_signature = None |
| 47 |
_empty_defs = [] |
| 48 |
|
| 49 |
def __init__(self, space, code, w_globals=None, defs_w=[], kw_defs_w=None, |
| 50 |
closure=None, w_ann=None, forcename=None, qualname=None): |
| 51 |
self.space = space |
| 52 |
self.name = forcename or code.co_name |
| 53 |
self.qualname = qualname or self.name |
| 54 |
assert isinstance(self.qualname, str) |
| 55 |
self.w_doc = None |
| 56 |
self.code = code |
| 57 |
self.w_func_globals = w_globals |
| 58 |
self.closure = closure |
| 59 |
if not defs_w: |
| 60 |
|
| 61 |
|
| 62 |
defs_w = Function._empty_defs |
| 63 |
self.defs_w = defs_w |
| 64 |
self.w_func_dict = None |
| 65 |
self.w_module = None |
| 66 |
self.w_ann = w_ann |
| 67 |
|
| 68 |
if kw_defs_w is not None: |
| 69 |
self.init_kwdefaults_dict(kw_defs_w) |
| 70 |
|
| 71 |
def __repr__(self): |
| 72 |
|
| 73 |
|
| 74 |
name = getattr(self, 'name', None) |
| 75 |
if not isinstance(name, str): |
| 76 |
name = '?' |
| 77 |
return "<%s %s>" % (self.__class__.__name__, name) |
| 78 |
|
| 79 |
def call_args(self, args): |
| 80 |
|
| 81 |
w_res = self.getcode().funcrun(self, args) |
| 82 |
assert isinstance(w_res, W_Root) |
| 83 |
return w_res |
| 84 |
|
| 85 |
def call_obj_args(self, w_obj, args): |
| 86 |
|
| 87 |
w_res = self.getcode().funcrun_obj(self, w_obj, args) |
| 88 |
assert isinstance(w_res, W_Root) |
| 89 |
return w_res |
| 90 |
|
| 91 |
def getcode(self): |
| 92 |
if jit.we_are_jitted(): |
| 93 |
if not self.can_change_code: |
| 94 |
return _get_immutable_code(self) |
| 95 |
return jit.promote(self.code) |
| 96 |
return self.code |
| 97 |
|
| 98 |
def funccall(self, *args_w): |
| 99 |
from pypy.interpreter import gateway |
| 100 |
from pypy.interpreter.pycode import PyCode |
| 101 |
|
| 102 |
code = self.getcode() |
| 103 |
nargs = len(args_w) |
| 104 |
fast_natural_arity = code.fast_natural_arity |
| 105 |
if nargs == fast_natural_arity: |
| 106 |
if nargs == 0: |
| 107 |
assert isinstance(code, gateway.BuiltinCode0) |
| 108 |
return code.fastcall_0(self.space, self) |
| 109 |
elif nargs == 1: |
| 110 |
assert isinstance(code, gateway.BuiltinCode1) |
| 111 |
return code.fastcall_1(self.space, self, args_w[0]) |
| 112 |
elif nargs == 2: |
| 113 |
assert isinstance(code, gateway.BuiltinCode2) |
| 114 |
return code.fastcall_2(self.space, self, args_w[0], args_w[1]) |
| 115 |
elif nargs == 3: |
| 116 |
assert isinstance(code, gateway.BuiltinCode3) |
| 117 |
return code.fastcall_3(self.space, self, args_w[0], |
| 118 |
args_w[1], args_w[2]) |
| 119 |
elif nargs == 4: |
| 120 |
assert isinstance(code, gateway.BuiltinCode4) |
| 121 |
return code.fastcall_4(self.space, self, args_w[0], |
| 122 |
args_w[1], args_w[2], args_w[3]) |
| 123 |
elif (nargs | PyCode.FLATPYCALL) == fast_natural_arity: |
| 124 |
assert isinstance(code, PyCode) |
| 125 |
if nargs < 5: |
| 126 |
new_frame = self.space.createframe(code, self.w_func_globals, |
| 127 |
self) |
| 128 |
for i in funccallunrolling: |
| 129 |
if i < nargs: |
| 130 |
new_frame.locals_cells_stack_w[i] = args_w[i] |
| 131 |
return new_frame.run(self.name, self.qualname) |
| 132 |
elif nargs >= 1 and fast_natural_arity == Code.PASSTHROUGHARGS1: |
| 133 |
assert isinstance(code, gateway.BuiltinCodePassThroughArguments1) |
| 134 |
return code.funcrun_obj(self, args_w[0], |
| 135 |
Arguments(self.space, |
| 136 |
list(args_w[1:]))) |
| 137 |
return self.call_args(Arguments(self.space, list(args_w))) |
| 138 |
|
| 139 |
def funccall_valuestack(self, nargs, frame, dropvalues, methodcall=False): |
| 140 |
|
| 141 |
from pypy.interpreter import gateway |
| 142 |
from pypy.interpreter.pycode import PyCode |
| 143 |
|
| 144 |
code = self.getcode() |
| 145 |
|
| 146 |
if (jit.we_are_jitted() and code is self.space._code_of_sys_exc_info |
| 147 |
and nargs == 0): |
| 148 |
frame.dropvalues(dropvalues) |
| 149 |
from pypy.module.sys.vm import exc_info_direct |
| 150 |
return exc_info_direct(self.space, frame) |
| 151 |
|
| 152 |
fast_natural_arity = code.fast_natural_arity |
| 153 |
if nargs == fast_natural_arity: |
| 154 |
if nargs == 0: |
| 155 |
frame.dropvalues(dropvalues) |
| 156 |
assert isinstance(code, gateway.BuiltinCode0) |
| 157 |
return code.fastcall_0(self.space, self) |
| 158 |
elif nargs == 1: |
| 159 |
f_0 = frame.peekvalue(0) |
| 160 |
frame.dropvalues(dropvalues) |
| 161 |
assert isinstance(code, gateway.BuiltinCode1) |
| 162 |
return code.fastcall_1(self.space, self, f_0) |
| 163 |
elif nargs == 2: |
| 164 |
f_0 = frame.peekvalue(0) |
| 165 |
f_1 = frame.peekvalue(1) |
| 166 |
frame.dropvalues(dropvalues) |
| 167 |
assert isinstance(code, gateway.BuiltinCode2) |
| 168 |
return code.fastcall_2(self.space, self, f_1, f_0) |
| 169 |
elif nargs == 3: |
| 170 |
assert isinstance(code, gateway.BuiltinCode3) |
| 171 |
f_0 = frame.peekvalue(0) |
| 172 |
f_1 = frame.peekvalue(1) |
| 173 |
f_2 = frame.peekvalue(2) |
| 174 |
frame.dropvalues(dropvalues) |
| 175 |
return code.fastcall_3(self.space, self, f_2, |
| 176 |
f_1, f_0) |
| 177 |
elif nargs == 4: |
| 178 |
assert isinstance(code, gateway.BuiltinCode4) |
| 179 |
f_0 = frame.peekvalue(0) |
| 180 |
f_1 = frame.peekvalue(1) |
| 181 |
f_2 = frame.peekvalue(2) |
| 182 |
f_3 = frame.peekvalue(3) |
| 183 |
frame.dropvalues(dropvalues) |
| 184 |
return code.fastcall_4(self.space, self, f_3, f_2, f_1, f_0) |
| 185 |
elif (nargs | Code.FLATPYCALL) == fast_natural_arity: |
| 186 |
assert isinstance(code, PyCode) |
| 187 |
return self._flat_pycall(code, nargs, frame, dropvalues) |
| 188 |
elif fast_natural_arity & Code.FLATPYCALL: |
| 189 |
natural_arity = fast_natural_arity & 0xff |
| 190 |
if natural_arity > nargs >= natural_arity - len(self.defs_w): |
| 191 |
assert isinstance(code, PyCode) |
| 192 |
return self._flat_pycall_defaults(code, nargs, frame, |
| 193 |
natural_arity - nargs, dropvalues) |
| 194 |
elif fast_natural_arity == Code.PASSTHROUGHARGS1 and nargs >= 1: |
| 195 |
assert isinstance(code, gateway.BuiltinCodePassThroughArguments1) |
| 196 |
w_obj = frame.peekvalue(nargs-1) |
| 197 |
args = frame.make_arguments(nargs-1, w_function=self) |
| 198 |
frame.dropvalues(dropvalues) |
| 199 |
return code.funcrun_obj(self, w_obj, args) |
| 200 |
|
| 201 |
args = frame.make_arguments(nargs, methodcall=methodcall, w_function=self) |
| 202 |
frame.dropvalues(dropvalues) |
| 203 |
return self.call_args(args) |
| 204 |
|
| 205 |
@jit.unroll_safe |
| 206 |
def _flat_pycall(self, code, nargs, frame, dropvalues): |
| 207 |
|
| 208 |
new_frame = self.space.createframe(code, self.w_func_globals, |
| 209 |
self) |
| 210 |
for i in xrange(nargs): |
| 211 |
new_frame.locals_cells_stack_w[i] = frame.peekvalue(nargs-1-i) |
| 212 |
|
| 213 |
frame.dropvalues(dropvalues) |
| 214 |
return new_frame.run(self.name, self.qualname) |
| 215 |
|
| 216 |
@jit.unroll_safe |
| 217 |
def _flat_pycall_defaults(self, code, nargs, frame, defs_to_load, dropvalues): |
| 218 |
|
| 219 |
new_frame = self.space.createframe(code, self.w_func_globals, |
| 220 |
self) |
| 221 |
for i in xrange(nargs): |
| 222 |
new_frame.locals_cells_stack_w[i] = frame.peekvalue(nargs-1-i) |
| 223 |
|
| 224 |
ndefs = len(self.defs_w) |
| 225 |
start = ndefs - defs_to_load |
| 226 |
i = nargs |
| 227 |
for j in xrange(start, ndefs): |
| 228 |
new_frame.locals_cells_stack_w[i] = self.defs_w[j] |
| 229 |
i += 1 |
| 230 |
frame.dropvalues(dropvalues) |
| 231 |
return new_frame.run(self.name, self.qualname) |
| 232 |
|
| 233 |
def getdict(self, space): |
| 234 |
if self.w_func_dict is None: |
| 235 |
self.w_func_dict = space.newdict(instance=True) |
| 236 |
return self.w_func_dict |
| 237 |
|
| 238 |
def setdict(self, space, w_dict): |
| 239 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 240 |
raise oefmt(space.w_TypeError, |
| 241 |
"setting function's dictionary to a non-dict") |
| 242 |
self.w_func_dict = w_dict |
| 243 |
|
| 244 |
def descr_function__new__(space, w_subtype, w_code, w_globals, |
| 245 |
w_name=None, w_argdefs=None, w_closure=None): |
| 246 |
code = space.interp_w(Code, w_code) |
| 247 |
if not space.isinstance_w(w_globals, space.w_dict): |
| 248 |
raise oefmt(space.w_TypeError, "expected dict") |
| 249 |
if not space.is_none(w_name): |
| 250 |
name = space.text_w(w_name) |
| 251 |
else: |
| 252 |
name = None |
| 253 |
if not space.is_none(w_argdefs): |
| 254 |
defs_w = space.fixedview(w_argdefs) |
| 255 |
else: |
| 256 |
defs_w = [] |
| 257 |
nfreevars = 0 |
| 258 |
from pypy.interpreter.pycode import PyCode |
| 259 |
if isinstance(code, PyCode): |
| 260 |
nfreevars = len(code.co_freevars) |
| 261 |
if space.is_none(w_closure) and nfreevars == 0: |
| 262 |
closure = None |
| 263 |
elif not space.is_w(space.type(w_closure), space.w_tuple): |
| 264 |
raise oefmt(space.w_TypeError, "invalid closure") |
| 265 |
else: |
| 266 |
from pypy.interpreter.nestedscope import Cell |
| 267 |
closure_w = space.unpackiterable(w_closure) |
| 268 |
n = len(closure_w) |
| 269 |
if nfreevars == 0: |
| 270 |
raise oefmt(space.w_ValueError, "no closure needed") |
| 271 |
elif nfreevars != n: |
| 272 |
raise oefmt(space.w_ValueError, "closure is wrong size") |
| 273 |
closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w] |
| 274 |
func = space.allocate_instance(Function, w_subtype) |
| 275 |
Function.__init__(func, space, code, w_globals, defs_w, None, closure, |
| 276 |
None,name) |
| 277 |
return func |
| 278 |
|
| 279 |
def descr_function_call(self, __args__): |
| 280 |
return self.call_args(__args__) |
| 281 |
|
| 282 |
def descr_function_repr(self): |
| 283 |
return self.getrepr(self.space, 'function %s' % self.qualname) |
| 284 |
|
| 285 |
|
| 286 |
def _cleanup_(self): |
| 287 |
|
| 288 |
from pypy.interpreter.gateway import BuiltinCode |
| 289 |
if isinstance(self.code, BuiltinCode): |
| 290 |
|
| 291 |
|
| 292 |
identifier = self.code.identifier |
| 293 |
previous = self.space._builtin_functions_by_identifier.get(identifier, self) |
| 294 |
assert previous is self, ( |
| 295 |
"duplicate function ids with identifier=%r: %r and %r" % ( |
| 296 |
identifier, previous, self)) |
| 297 |
self.add_to_table() |
| 298 |
return False |
| 299 |
|
| 300 |
def add_to_table(self): |
| 301 |
self.space._builtin_functions_by_identifier[self.code.identifier] = self |
| 302 |
|
| 303 |
def find(space, identifier): |
| 304 |
return space._builtin_functions_by_identifier[identifier] |
| 305 |
find = staticmethod(find) |
| 306 |
|
| 307 |
def descr_function__reduce__(self, space): |
| 308 |
XXX |
| 309 |
from pypy.interpreter.gateway import BuiltinCode |
| 310 |
from pypy.interpreter.mixedmodule import MixedModule |
| 311 |
w_mod = space.getbuiltinmodule('_pickle_support') |
| 312 |
mod = space.interp_w(MixedModule, w_mod) |
| 313 |
code = self.code |
| 314 |
if isinstance(code, BuiltinCode): |
| 315 |
new_inst = mod.get('builtin_function') |
| 316 |
return space.newtuple2(new_inst, |
| 317 |
space.newtuple([space.newtext(code.identifier)])) |
| 318 |
|
| 319 |
new_inst = mod.get('func_new') |
| 320 |
if self.closure is None: |
| 321 |
w_closure = space.w_None |
| 322 |
else: |
| 323 |
w_closure = space.newtuple([cell for cell in self.closure]) |
| 324 |
if self.w_doc is None: |
| 325 |
w_doc = space.w_None |
| 326 |
else: |
| 327 |
w_doc = self.w_doc |
| 328 |
if self.w_func_globals is None: |
| 329 |
w_func_globals = space.w_None |
| 330 |
else: |
| 331 |
w_func_globals = self.w_func_globals |
| 332 |
if self.w_func_dict is None: |
| 333 |
w_func_dict = space.w_None |
| 334 |
else: |
| 335 |
w_func_dict = self.w_func_dict |
| 336 |
|
| 337 |
nt = space.newtuple |
| 338 |
tup_base = [] |
| 339 |
tup_state = [ |
| 340 |
space.newtext(self.name), |
| 341 |
space.newtext(self.qualname), |
| 342 |
w_doc, |
| 343 |
self.code, |
| 344 |
w_func_globals, |
| 345 |
w_closure, |
| 346 |
nt(self.defs_w), |
| 347 |
w_func_dict, |
| 348 |
self.w_module, |
| 349 |
] |
| 350 |
return nt([new_inst, nt(tup_base), nt(tup_state)]) |
| 351 |
|
| 352 |
def descr_function__setstate__(self, space, w_args): |
| 353 |
XXX |
| 354 |
args_w = space.unpackiterable(w_args) |
| 355 |
try: |
| 356 |
(w_name, w_qualname, w_doc, w_code, w_func_globals, w_closure, |
| 357 |
w_defs, w_func_dict, w_module) = args_w |
| 358 |
except ValueError: |
| 359 |
|
| 360 |
raise oefmt(space.w_ValueError, |
| 361 |
"Wrong arguments to function.__setstate__") |
| 362 |
|
| 363 |
self.space = space |
| 364 |
self.name = space.text_w(w_name) |
| 365 |
self.qualname = space.utf8_w(w_qualname) |
| 366 |
self.code = space.interp_w(Code, w_code) |
| 367 |
if not space.is_w(w_closure, space.w_None): |
| 368 |
from pypy.interpreter.nestedscope import Cell |
| 369 |
closure_w = space.unpackiterable(w_closure) |
| 370 |
self.closure = [space.interp_w(Cell, w_cell) for w_cell in closure_w] |
| 371 |
else: |
| 372 |
self.closure = None |
| 373 |
if space.is_w(w_doc, space.w_None): |
| 374 |
w_doc = None |
| 375 |
self.w_doc = w_doc |
| 376 |
if space.is_w(w_func_globals, space.w_None): |
| 377 |
w_func_globals = None |
| 378 |
self.w_func_globals = w_func_globals |
| 379 |
if space.is_w(w_func_dict, space.w_None): |
| 380 |
w_func_dict = None |
| 381 |
self.w_func_dict = w_func_dict |
| 382 |
self.defs_w = space.fixedview(w_defs) |
| 383 |
self.w_module = w_module |
| 384 |
|
| 385 |
def _check_code_mutable(self, attr): |
| 386 |
if not self.can_change_code: |
| 387 |
raise oefmt(self.space.w_AttributeError, |
| 388 |
"Cannot change %s attribute of builtin functions", attr) |
| 389 |
|
| 390 |
def fget_func_defaults(self, space): |
| 391 |
values_w = self.defs_w |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
if not values_w or None in values_w: |
| 396 |
return space.w_None |
| 397 |
return space.newtuple(values_w) |
| 398 |
|
| 399 |
def fget_defaults_count(self, space): |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
return space.newint(len(self.defs_w)) |
| 407 |
|
| 408 |
def fset_func_defaults(self, space, w_defaults): |
| 409 |
self._check_code_mutable("__defaults__") |
| 410 |
if space.is_w(w_defaults, space.w_None): |
| 411 |
self.defs_w = [] |
| 412 |
return |
| 413 |
if not space.isinstance_w(w_defaults, space.w_tuple): |
| 414 |
raise oefmt(space.w_TypeError, |
| 415 |
"__defaults__ must be set to a tuple object or None") |
| 416 |
self.defs_w = space.fixedview(w_defaults) |
| 417 |
|
| 418 |
def fdel_func_defaults(self, space): |
| 419 |
self._check_code_mutable("__defaults__") |
| 420 |
self.defs_w = [] |
| 421 |
|
| 422 |
def fget_func_kwdefaults(self, space): |
| 423 |
if self.w_kw_defs is None: |
| 424 |
return space.w_None |
| 425 |
return self.w_kw_defs |
| 426 |
|
| 427 |
def fset_func_kwdefaults(self, space, w_new): |
| 428 |
if space.is_w(w_new, space.w_None): |
| 429 |
self.w_kw_defs = None |
| 430 |
else: |
| 431 |
if not space.isinstance_w(w_new, space.w_dict): |
| 432 |
raise oefmt(space.w_TypeError, "__kwdefaults__ must be a dict") |
| 433 |
self.w_kw_defs = w_new |
| 434 |
|
| 435 |
def fdel_func_kwdefaults(self, space): |
| 436 |
self.w_kw_defs = None |
| 437 |
|
| 438 |
def init_kwdefaults_dict(self, kw_defs_w): |
| 439 |
|
| 440 |
space = self.space |
| 441 |
w_dict = space.newdict(module=True) |
| 442 |
for w_name, w_value in kw_defs_w: |
| 443 |
space.setitem(w_dict, w_name, w_value) |
| 444 |
self.w_kw_defs = w_dict |
| 445 |
|
| 446 |
def fget_func_doc(self, space): |
| 447 |
if self.w_doc is None: |
| 448 |
self.w_doc = self.code.getdocstring(space) |
| 449 |
return self.w_doc |
| 450 |
|
| 451 |
def fset_func_doc(self, space, w_doc): |
| 452 |
self._check_code_mutable("__doc__") |
| 453 |
self.w_doc = w_doc |
| 454 |
|
| 455 |
def fdel_func_doc(self, space): |
| 456 |
self._check_code_mutable("__doc__") |
| 457 |
self.w_doc = space.w_None |
| 458 |
|
| 459 |
def fget_func_name(self, space): |
| 460 |
return space.newtext(self.name) |
| 461 |
|
| 462 |
def fset_func_name(self, space, w_name): |
| 463 |
self._check_code_mutable("__name__") |
| 464 |
if space.isinstance_w(w_name, space.w_text): |
| 465 |
self.name = space.text_w(w_name) |
| 466 |
else: |
| 467 |
raise oefmt(space.w_TypeError, |
| 468 |
"__name__ must be set to a string object") |
| 469 |
|
| 470 |
def fget_func_qualname(self, space): |
| 471 |
return space.newtext(self.qualname) |
| 472 |
|
| 473 |
def set_qualname(self, qualname): |
| 474 |
self.qualname = qualname |
| 475 |
|
| 476 |
def fset_func_qualname(self, space, w_name): |
| 477 |
self._check_code_mutable("__qualname__") |
| 478 |
try: |
| 479 |
qualname = space.realutf8_w(w_name) |
| 480 |
except OperationError as e: |
| 481 |
if e.match(space, space.w_TypeError): |
| 482 |
raise oefmt(space.w_TypeError, |
| 483 |
"__qualname__ must be set to a string object") |
| 484 |
raise |
| 485 |
self.set_qualname(qualname) |
| 486 |
|
| 487 |
def fget_func_text_signature(self, space): |
| 488 |
if self.w_text_signature is None: |
| 489 |
raise oefmt(space.w_AttributeError, "__text_signature__") |
| 490 |
return self.w_text_signature |
| 491 |
|
| 492 |
def fset_func_text_signature(self, space, w_value): |
| 493 |
self.w_text_signature = w_value |
| 494 |
|
| 495 |
def fget_func_objclass(self, space): |
| 496 |
if self.w_objclass is None: |
| 497 |
raise oefmt(space.w_AttributeError, "__objclass__") |
| 498 |
return self.w_objclass |
| 499 |
|
| 500 |
def set_objclass(self, w_type): |
| 501 |
self.w_objclass = w_type |
| 502 |
|
| 503 |
def fget___module__(self, space): |
| 504 |
if self.w_module is None: |
| 505 |
if self.w_func_globals is not None and not space.is_w(self.w_func_globals, space.w_None): |
| 506 |
self.w_module = space.call_method(self.w_func_globals, "get", space.newtext("__name__")) |
| 507 |
else: |
| 508 |
self.w_module = space.w_None |
| 509 |
return self.w_module |
| 510 |
|
| 511 |
def fset___module__(self, space, w_module): |
| 512 |
self._check_code_mutable("__module__") |
| 513 |
self.w_module = w_module |
| 514 |
|
| 515 |
def fdel___module__(self, space): |
| 516 |
self._check_code_mutable("__module__") |
| 517 |
self.w_module = space.w_None |
| 518 |
|
| 519 |
def fget_func_code(self, space): |
| 520 |
return self.getcode() |
| 521 |
|
| 522 |
def fset_func_code(self, space, w_code): |
| 523 |
from pypy.interpreter.pycode import PyCode |
| 524 |
if not self.can_change_code: |
| 525 |
raise oefmt(space.w_AttributeError, |
| 526 |
"Cannot change code attribute of builtin functions") |
| 527 |
code = space.interp_w(Code, w_code) |
| 528 |
closure_len = 0 |
| 529 |
if self.closure: |
| 530 |
closure_len = len(self.closure) |
| 531 |
if isinstance(code, PyCode) and closure_len != len(code.co_freevars): |
| 532 |
raise oefmt(space.w_ValueError, |
| 533 |
"%N() requires a code object with %d free vars, not " |
| 534 |
"%d", self, closure_len, len(code.co_freevars)) |
| 535 |
self.fget_func_doc(space) |
| 536 |
self.code = code |
| 537 |
|
| 538 |
def fget_func_closure(self, space): |
| 539 |
if self.closure is not None: |
| 540 |
w_res = space.newtuple([cell for cell in self.closure]) |
| 541 |
else: |
| 542 |
w_res = space.w_None |
| 543 |
return w_res |
| 544 |
|
| 545 |
def fget_func_annotations(self, space): |
| 546 |
if self.w_ann is None: |
| 547 |
self.w_ann = space.newdict() |
| 548 |
return self.w_ann |
| 549 |
|
| 550 |
def fset_func_annotations(self, space, w_new): |
| 551 |
self._check_code_mutable("__annotations__") |
| 552 |
if space.is_w(w_new, space.w_None): |
| 553 |
w_new = None |
| 554 |
elif not space.isinstance_w(w_new, space.w_dict): |
| 555 |
raise oefmt(space.w_TypeError, "__annotations__ must be a dict") |
| 556 |
self.w_ann = w_new |
| 557 |
|
| 558 |
def fdel_func_annotations(self, space): |
| 559 |
self._check_code_mutable("__annotations__") |
| 560 |
self.w_ann = None |
| 561 |
|
| 562 |
|
| 563 |
class _Method(W_Root): |
| 564 |
_immutable_fields_ = ['w_function', 'w_instance'] |
| 565 |
|
| 566 |
def __init__(self, space, w_function, w_instance): |
| 567 |
self.space = space |
| 568 |
assert w_instance is not None |
| 569 |
self.w_function = w_function |
| 570 |
self.w_instance = w_instance |
| 571 |
|
| 572 |
def __repr__(self): |
| 573 |
return u"bound method %s" % (self.w_function.getname(self.space),) |
| 574 |
|
| 575 |
def call_args(self, args): |
| 576 |
space = self.space |
| 577 |
return space.call_obj_args(self.w_function, self.w_instance, args) |
| 578 |
|
| 579 |
def descr_method_get(self, w_instance, w_owner=None): |
| 580 |
return self |
| 581 |
|
| 582 |
def descr_method_call(self, __args__): |
| 583 |
return self.call_args(__args__) |
| 584 |
|
| 585 |
def descr_method_repr(self): |
| 586 |
space = self.space |
| 587 |
w_name = space.findattr(self.w_function, space.newtext('__qualname__')) |
| 588 |
if w_name is None: |
| 589 |
name = self.w_function.getname(self.space) |
| 590 |
else: |
| 591 |
try: |
| 592 |
name = space.utf8_w(w_name) |
| 593 |
except OperationError as e: |
| 594 |
if not e.match(space, space.w_TypeError): |
| 595 |
raise |
| 596 |
name = '?' |
| 597 |
objrepr = space.utf8_w(space.repr(self.w_instance)) |
| 598 |
s = b'<bound method %s of %s>' % (name, objrepr) |
| 599 |
return space.newtext(s) |
| 600 |
|
| 601 |
def descr_get_doc(self, space): |
| 602 |
return space.getattr(self.w_function, space.newtext('__doc__')) |
| 603 |
|
| 604 |
def descr_method_getattribute(self, w_attr): |
| 605 |
space = self.space |
| 606 |
if space.text_w(w_attr) != '__doc__': |
| 607 |
try: |
| 608 |
return space.call_method(space.w_object, '__getattribute__', |
| 609 |
self, w_attr) |
| 610 |
except OperationError as e: |
| 611 |
if not e.match(space, space.w_AttributeError): |
| 612 |
raise |
| 613 |
|
| 614 |
return space.getattr(self.w_function, w_attr) |
| 615 |
|
| 616 |
def descr_method_eq(self, w_other): |
| 617 |
space = self.space |
| 618 |
if not isinstance(w_other, _Method): |
| 619 |
return space.w_NotImplemented |
| 620 |
return space.newbool(self._eq(w_other)) |
| 621 |
|
| 622 |
def descr_method_ne(self, w_other): |
| 623 |
space = self.space |
| 624 |
if not isinstance(w_other, Method): |
| 625 |
return space.w_NotImplemented |
| 626 |
return space.newbool(not self._eq(w_other)) |
| 627 |
|
| 628 |
def _eq(self, w_other): |
| 629 |
space = self.space |
| 630 |
if not space.is_w(self.w_instance, w_other.w_instance): |
| 631 |
return False |
| 632 |
return space.is_w(self.w_function, w_other.w_function) |
| 633 |
|
| 634 |
|
| 635 |
def descr_method_hash(self): |
| 636 |
space = self.space |
| 637 |
w_result = space.hash(self.w_function) |
| 638 |
w_result = space.xor(w_result, space.id(self.w_instance)) |
| 639 |
return w_result |
| 640 |
|
| 641 |
def descr_method__reduce__(self, space): |
| 642 |
from pypy.interpreter.mixedmodule import MixedModule |
| 643 |
from pypy.interpreter.gateway import BuiltinCode |
| 644 |
w_mod = space.getbuiltinmodule('_pickle_support') |
| 645 |
mod = space.interp_w(MixedModule, w_mod) |
| 646 |
w_instance = self.w_instance |
| 647 |
w_function = self.w_function |
| 648 |
if (isinstance(w_function, Function) and |
| 649 |
isinstance(w_function.code, BuiltinCode)): |
| 650 |
new_inst = mod.get('builtin_method_new') |
| 651 |
tup = [w_instance, space.newtext(w_function.name)] |
| 652 |
else: |
| 653 |
w_builtins = space.getbuiltinmodule('builtins') |
| 654 |
new_inst = space.getattr(w_builtins, space.newtext('getattr')) |
| 655 |
tup = [w_instance, space.newtext(w_function.getname(space))] |
| 656 |
return space.newtuple2(new_inst, space.newtuple(tup)) |
| 657 |
|
| 658 |
class Method(_Method): |
| 659 |
"""Create a method object.""" |
| 660 |
|
| 661 |
def descr_method__new__(space, w_subtype, w_function, w_instance): |
| 662 |
if space.is_w(w_instance, space.w_None): |
| 663 |
w_instance = None |
| 664 |
if w_instance is None: |
| 665 |
raise oefmt(space.w_TypeError, "self must not be None") |
| 666 |
method = space.allocate_instance(Method, w_subtype) |
| 667 |
_Method.__init__(method, space, w_function, w_instance) |
| 668 |
return method |
| 669 |
|
| 670 |
|
| 671 |
class StaticMethod(W_Root): |
| 672 |
"""The staticmethod objects.""" |
| 673 |
_immutable_fields_ = ['w_function?'] |
| 674 |
|
| 675 |
def __init__(self, w_function): |
| 676 |
self.w_function = w_function |
| 677 |
self.w_dict = None |
| 678 |
|
| 679 |
def getdict(self, space): |
| 680 |
if self.w_dict is None: |
| 681 |
self.w_dict = space.newdict(instance=True) |
| 682 |
return self.w_dict |
| 683 |
|
| 684 |
def setdict(self, space, w_dict): |
| 685 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 686 |
raise oefmt(space.w_TypeError, |
| 687 |
"__dict__ must be set to a dictionary, not a %T", |
| 688 |
w_dict) |
| 689 |
self.w_dict = w_dict |
| 690 |
|
| 691 |
def descr_staticmethod_get(self, w_obj, w_cls=None): |
| 692 |
"""staticmethod(x).__get__(obj[, type]) -> x""" |
| 693 |
return self.w_function |
| 694 |
|
| 695 |
def descr_staticmethod__new__(space, w_subtype, w_function): |
| 696 |
instance = space.allocate_instance(StaticMethod, w_subtype) |
| 697 |
instance.__init__(space.w_None) |
| 698 |
return instance |
| 699 |
|
| 700 |
def descr_init(self, space, w_function): |
| 701 |
self.w_function = w_function |
| 702 |
w_dict = self.getdict(space) |
| 703 |
_copy_func_attrs(space, w_function, w_dict) |
| 704 |
|
| 705 |
def descr_isabstract(self, space): |
| 706 |
return space.newbool(space.isabstractmethod_w(self.w_function)) |
| 707 |
|
| 708 |
def descr_reduce_ex(self, space, w_proto): |
| 709 |
raise oefmt(space.w_TypeError, "cannot pickle '%N' object", self) |
| 710 |
|
| 711 |
def descr_call(self, space, __args__): |
| 712 |
return space.call_args(self.w_function, __args__) |
| 713 |
|
| 714 |
def descr_repr(self, space): |
| 715 |
return space.newtext("<staticmethod(%s)>" % (space.text_w(space.repr(self.w_function)), )) |
| 716 |
|
| 717 |
|
| 718 |
class ClassMethod(W_Root): |
| 719 |
"""The classmethod objects.""" |
| 720 |
_immutable_fields_ = ['w_function?'] |
| 721 |
|
| 722 |
def __init__(self, w_function): |
| 723 |
self.w_function = w_function |
| 724 |
self.w_dict = None |
| 725 |
|
| 726 |
def getdict(self, space): |
| 727 |
if self.w_dict is None: |
| 728 |
self.w_dict = space.newdict(instance=True) |
| 729 |
return self.w_dict |
| 730 |
|
| 731 |
def setdict(self, space, w_dict): |
| 732 |
if not space.isinstance_w(w_dict, space.w_dict): |
| 733 |
raise oefmt(space.w_TypeError, |
| 734 |
"__dict__ must be set to a dictionary, not a %T", |
| 735 |
w_dict) |
| 736 |
self.w_dict = w_dict |
| 737 |
|
| 738 |
def descr_classmethod_get(self, space, w_obj, w_klass=None): |
| 739 |
if space.is_none(w_klass): |
| 740 |
w_klass = space.type(w_obj) |
| 741 |
w_func = self.w_function |
| 742 |
|
| 743 |
w_bound = space.get(w_func, w_klass, w_klass) |
| 744 |
if w_bound is not w_func: |
| 745 |
return w_bound |
| 746 |
|
| 747 |
|
| 748 |
return Method(space, w_func, w_klass) |
| 749 |
|
| 750 |
def descr_classmethod__new__(space, w_subtype, w_function): |
| 751 |
instance = space.allocate_instance(ClassMethod, w_subtype) |
| 752 |
instance.__init__(space.w_None) |
| 753 |
return instance |
| 754 |
|
| 755 |
def descr_init(self, space, w_function): |
| 756 |
self.w_function = w_function |
| 757 |
w_dict = self.getdict(space) |
| 758 |
_copy_func_attrs(space, w_function, w_dict) |
| 759 |
|
| 760 |
def descr_isabstract(self, space): |
| 761 |
return space.newbool(space.isabstractmethod_w(self.w_function)) |
| 762 |
|
| 763 |
def descr_reduce_ex(self, space, w_proto): |
| 764 |
raise oefmt(space.w_TypeError, "cannot pickle '%N' object", self) |
| 765 |
|
| 766 |
def descr_repr(self, space): |
| 767 |
return space.newtext("<classmethod(%s)>" % (space.text_w(space.repr(self.w_function)), )) |
| 768 |
|
| 769 |
def _copy_func_attrs(space, w_function, w_dict): |
| 770 |
for name in ['__module__', '__name__', '__qualname__', '__doc__', '__annotations__']: |
| 771 |
w_name = space.newtext(name) |
| 772 |
try: |
| 773 |
w_attr = space.getattr(w_function, w_name) |
| 774 |
except OperationError as e: |
| 775 |
if e.match(space, space.w_AttributeError): |
| 776 |
continue |
| 777 |
raise |
| 778 |
space.setitem(w_dict, w_name, w_attr) |
| 779 |
|
| 780 |
class FunctionWithFixedCode(Function): |
| 781 |
can_change_code = False |
| 782 |
|
| 783 |
class BuiltinFunction(Function): |
| 784 |
can_change_code = False |
| 785 |
|
| 786 |
def __init__(self, func): |
| 787 |
assert isinstance(func, Function) |
| 788 |
Function.__init__(self, func.space, func.code, func.w_func_globals, |
| 789 |
func.defs_w, None, func.closure, |
| 790 |
None, func.name) |
| 791 |
self.w_doc = func.w_doc |
| 792 |
self.w_func_dict = func.w_func_dict |
| 793 |
self.w_module = func.w_module |
| 794 |
|
| 795 |
self.w_moduleobj = None |
| 796 |
self.w_kw_defs = func.w_kw_defs |
| 797 |
self.w_text_signature = func.w_text_signature |
| 798 |
|
| 799 |
def descr_builtinfunction__new__(space, w_subtype): |
| 800 |
raise oefmt(space.w_TypeError, |
| 801 |
"cannot create 'builtin_function' instances") |
| 802 |
|
| 803 |
def descr_function_repr(self): |
| 804 |
return self.space.newtext('<built-in function %s>' % (self.name,)) |
| 805 |
|
| 806 |
def descr__reduce__(self, space): |
| 807 |
return space.newtext(self.qualname) |
| 808 |
|
| 809 |
def descr_builtinfunction__self__(self, space): |
| 810 |
if self.w_moduleobj is not None: |
| 811 |
return self.w_moduleobj |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
w_modules = space.getattr(space.getbuiltinmodule('sys'), space.newtext('modules')) |
| 816 |
w_modstring = self.fget___module__(space) |
| 817 |
if not space.is_w(w_modstring, space.w_None): |
| 818 |
w_moduleobj = space.call_method(w_modules, 'get', w_modstring) |
| 819 |
else: |
| 820 |
w_moduleobj = space.w_None |
| 821 |
self.w_moduleobj = w_moduleobj |
| 822 |
return w_moduleobj |
| 823 |
|
| 824 |
|
| 825 |
def is_builtin_code(w_func): |
| 826 |
from pypy.interpreter.gateway import BuiltinCode |
| 827 |
if isinstance(w_func, _Method): |
| 828 |
w_func = w_func.w_function |
| 829 |
if isinstance(w_func, Function): |
| 830 |
code = w_func.getcode() |
| 831 |
else: |
| 832 |
code = None |
| 833 |
return isinstance(code, BuiltinCode) |