| 1 |
import operator |
| 2 |
from pypy.interpreter.error import OperationError, oefmt, oefmt_attribute_error |
| 3 |
from pypy.interpreter.error import enrich_attribute_error |
| 4 |
from pypy.interpreter.baseobjspace import ObjSpace |
| 5 |
from pypy.interpreter.function import Function, Method, FunctionWithFixedCode |
| 6 |
from pypy.interpreter.argument import Arguments |
| 7 |
from pypy.interpreter.typedef import default_identity_hash, use_special_method_shortcut |
| 8 |
from rpython.tool.sourcetools import compile2, func_with_new_name |
| 9 |
from rpython.rlib.objectmodel import specialize, always_inline |
| 10 |
from rpython.rlib import jit |
| 11 |
|
| 12 |
@specialize.memo() |
| 13 |
def object_getattribute(space): |
| 14 |
"Utility that returns the app-level descriptor object.__getattribute__." |
| 15 |
return space.lookup_in_type(space.w_object, '__getattribute__') |
| 16 |
|
| 17 |
@specialize.memo() |
| 18 |
def object_setattr(space): |
| 19 |
"Utility that returns the app-level descriptor object.__setattr__." |
| 20 |
return space.lookup_in_type(space.w_object, '__setattr__') |
| 21 |
|
| 22 |
@specialize.memo() |
| 23 |
def object_delattr(space): |
| 24 |
"Utility that returns the app-level descriptor object.__delattr__." |
| 25 |
return space.lookup_in_type(space.w_object, '__delattr__') |
| 26 |
|
| 27 |
@specialize.memo() |
| 28 |
def object_hash(space): |
| 29 |
"Utility that returns the app-level descriptor object.__hash__." |
| 30 |
return space.lookup_in_type(space.w_object, '__hash__') |
| 31 |
|
| 32 |
@specialize.memo() |
| 33 |
def type_eq(space): |
| 34 |
"Utility that returns the app-level descriptor type.__eq__." |
| 35 |
return space.lookup_in_type(space.w_type, '__eq__') |
| 36 |
|
| 37 |
@specialize.memo() |
| 38 |
def list_iter(space): |
| 39 |
"Utility that returns the app-level descriptor list.__iter__." |
| 40 |
return space.lookup_in_type(space.w_list, '__iter__') |
| 41 |
|
| 42 |
@specialize.memo() |
| 43 |
def tuple_iter(space): |
| 44 |
"Utility that returns the app-level descriptor tuple.__iter__." |
| 45 |
return space.lookup_in_type(space.w_tuple, '__iter__') |
| 46 |
|
| 47 |
@specialize.memo() |
| 48 |
def unicode_iter(space): |
| 49 |
"Utility that returns the app-level descriptor str.__iter__." |
| 50 |
return space.lookup_in_type(space.w_unicode, '__iter__') |
| 51 |
|
| 52 |
@specialize.memo() |
| 53 |
def dict_getitem(space): |
| 54 |
"Utility that returns the app-level descriptor dict.__getitem__." |
| 55 |
return space.lookup_in_type(space.w_dict, '__getitem__') |
| 56 |
|
| 57 |
|
| 58 |
def raiseattrerror(space, w_obj, w_name, w_descr=None): |
| 59 |
if w_descr is None: |
| 60 |
if space.isinstance_w(w_obj, space.w_type): |
| 61 |
raise oefmt_attribute_error( |
| 62 |
space, w_obj, w_name, "type object '%N' has no attribute %R") |
| 63 |
raise oefmt_attribute_error(space, |
| 64 |
w_obj, w_name, "'%T' object has no attribute %R") |
| 65 |
else: |
| 66 |
raise oefmt(space.w_AttributeError, |
| 67 |
"'%T' object attribute %R is read-only", w_obj, w_name) |
| 68 |
|
| 69 |
def get_attribute_name(space, w_obj, w_name): |
| 70 |
try: |
| 71 |
return space.text_w(w_name) |
| 72 |
except OperationError as e: |
| 73 |
if e.match(space, space.w_UnicodeEncodeError): |
| 74 |
raiseattrerror(space, w_obj, w_name) |
| 75 |
raise |
| 76 |
|
| 77 |
@always_inline |
| 78 |
def _same_class_w(space, w_obj1, w_obj2, w_typ1, w_typ2): |
| 79 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 80 |
|
| 81 |
|
| 82 |
assert isinstance(w_typ1, W_TypeObject) |
| 83 |
assert isinstance(w_typ2, W_TypeObject) |
| 84 |
return w_typ1.is_w(space, w_typ2) |
| 85 |
|
| 86 |
|
| 87 |
class Object(object): |
| 88 |
def descr__getattribute__(space, w_obj, w_name): |
| 89 |
name = get_attribute_name(space, w_obj, w_name) |
| 90 |
w_descr = space.lookup(w_obj, name) |
| 91 |
if w_descr is not None: |
| 92 |
if space.is_data_descr(w_descr): |
| 93 |
|
| 94 |
|
| 95 |
w_get = space.lookup(w_descr, "__get__") |
| 96 |
if w_get is not None: |
| 97 |
w_type = space.type(w_obj) |
| 98 |
return space.get_and_call_function(w_get, w_descr, w_obj, |
| 99 |
w_type) |
| 100 |
w_value = w_obj.getdictvalue(space, name) |
| 101 |
if w_value is not None: |
| 102 |
return w_value |
| 103 |
if w_descr is not None: |
| 104 |
typ = type(w_descr) |
| 105 |
if typ is Function or typ is FunctionWithFixedCode: |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
return Method(space, w_descr, w_obj) |
| 111 |
return space.get(w_descr, w_obj) |
| 112 |
raiseattrerror(space, w_obj, w_name) |
| 113 |
|
| 114 |
def descr__setattr__(space, w_obj, w_name, w_value): |
| 115 |
name = get_attribute_name(space, w_obj, w_name) |
| 116 |
w_descr = space.lookup(w_obj, name) |
| 117 |
if w_descr is not None: |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
w_set = space.lookup(w_descr, '__set__') |
| 122 |
if w_set is not None: |
| 123 |
return space.get_and_call_function(w_set, w_descr, w_obj, w_value) |
| 124 |
if space.lookup(w_descr, '__delete__') is not None: |
| 125 |
raise oefmt(space.w_AttributeError, |
| 126 |
"'%T' object is not a descriptor with set", w_descr) |
| 127 |
if w_obj.setdictvalue(space, name, w_value): |
| 128 |
return |
| 129 |
raiseattrerror(space, w_obj, w_name, w_descr) |
| 130 |
|
| 131 |
def descr__delattr__(space, w_obj, w_name): |
| 132 |
name = get_attribute_name(space, w_obj, w_name) |
| 133 |
w_descr = space.lookup(w_obj, name) |
| 134 |
if w_descr is not None: |
| 135 |
if space.is_data_descr(w_descr): |
| 136 |
space.delete(w_descr, w_obj) |
| 137 |
return |
| 138 |
if w_obj.deldictvalue(space, name): |
| 139 |
return |
| 140 |
raiseattrerror(space, w_obj, w_name, w_descr) |
| 141 |
|
| 142 |
def descr__init__(space, w_obj, __args__): |
| 143 |
pass |
| 144 |
|
| 145 |
def get_printable_location(itergreenkey, w_itemtype): |
| 146 |
return "DescrOperation.contains [%s, %s]" % ( |
| 147 |
itergreenkey.iterator_greenkey_printable(), |
| 148 |
w_itemtype.getname(w_itemtype.space)) |
| 149 |
|
| 150 |
contains_jitdriver = jit.JitDriver(name='contains', |
| 151 |
greens=['itergreenkey', 'w_itemtype'], reds='auto', |
| 152 |
get_printable_location=get_printable_location) |
| 153 |
|
| 154 |
class DescrOperation(object): |
| 155 |
|
| 156 |
|
| 157 |
def is_data_descr(space, w_obj): |
| 158 |
return (space.lookup(w_obj, '__set__') is not None or |
| 159 |
space.lookup(w_obj, '__delete__') is not None) |
| 160 |
|
| 161 |
def get_and_call_args(space, w_descr, w_obj, args): |
| 162 |
|
| 163 |
if isinstance(w_descr, Function): |
| 164 |
return w_descr.call_obj_args(w_obj, args) |
| 165 |
else: |
| 166 |
w_impl = space.get(w_descr, w_obj) |
| 167 |
return space.call_args(w_impl, args) |
| 168 |
|
| 169 |
def get_and_call_function(space, w_descr, w_obj, *args_w): |
| 170 |
typ = type(w_descr) |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
if typ is Function or typ is FunctionWithFixedCode: |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
return w_descr.funccall(w_obj, *args_w) |
| 184 |
else: |
| 185 |
args = Arguments(space, list(args_w)) |
| 186 |
w_impl = space.get(w_descr, w_obj) |
| 187 |
return space.call_args(w_impl, args) |
| 188 |
|
| 189 |
def call_args(space, w_obj, args): |
| 190 |
|
| 191 |
if isinstance(w_obj, Function): |
| 192 |
return w_obj.call_args(args) |
| 193 |
if isinstance(w_obj, Method): |
| 194 |
return w_obj.call_args(args) |
| 195 |
w_descr = space.lookup(w_obj, '__call__') |
| 196 |
if w_descr is None: |
| 197 |
raise oefmt(space.w_TypeError, |
| 198 |
"'%T' object is not callable", w_obj) |
| 199 |
return space.get_and_call_args(w_descr, w_obj, args) |
| 200 |
|
| 201 |
def get(space, w_descr, w_obj, w_type=None): |
| 202 |
w_get = space.lookup(w_descr, '__get__') |
| 203 |
if w_get is None: |
| 204 |
return w_descr |
| 205 |
if w_type is None: |
| 206 |
w_type = space.type(w_obj) |
| 207 |
|
| 208 |
|
| 209 |
return space.call_function(w_get, w_descr, w_obj, w_type) |
| 210 |
|
| 211 |
def set(space, w_descr, w_obj, w_val): |
| 212 |
w_set = space.lookup(w_descr, '__set__') |
| 213 |
if w_set is None: |
| 214 |
raise oefmt(space.w_AttributeError, |
| 215 |
"'%T' object is not a descriptor with set", w_descr) |
| 216 |
return space.get_and_call_function(w_set, w_descr, w_obj, w_val) |
| 217 |
|
| 218 |
def delete(space, w_descr, w_obj): |
| 219 |
w_delete = space.lookup(w_descr, '__delete__') |
| 220 |
if w_delete is None: |
| 221 |
raise oefmt(space.w_AttributeError, |
| 222 |
"'%T' object is not a descriptor with delete", w_descr) |
| 223 |
return space.get_and_call_function(w_delete, w_descr, w_obj) |
| 224 |
|
| 225 |
def getattr(space, w_obj, w_name): |
| 226 |
|
| 227 |
w_descr = space.lookup(w_obj, '__getattribute__') |
| 228 |
try: |
| 229 |
return space._handle_getattribute(w_descr, w_obj, w_name) |
| 230 |
except OperationError as e: |
| 231 |
enrich_attribute_error(space, e, w_obj, w_name) |
| 232 |
raise e |
| 233 |
|
| 234 |
def _handle_getattribute(space, w_descr, w_obj, w_name): |
| 235 |
try: |
| 236 |
if w_descr is None: |
| 237 |
raise OperationError(space.w_AttributeError, space.w_None) |
| 238 |
return space.get_and_call_function(w_descr, w_obj, w_name) |
| 239 |
except OperationError as e: |
| 240 |
if not e.match(space, space.w_AttributeError): |
| 241 |
raise |
| 242 |
w_descr = space.lookup(w_obj, '__getattr__') |
| 243 |
if w_descr is None: |
| 244 |
raise |
| 245 |
return space.get_and_call_function(w_descr, w_obj, w_name) |
| 246 |
|
| 247 |
def setattr(space, w_obj, w_name, w_val): |
| 248 |
|
| 249 |
w_descr = space.lookup(w_obj, '__setattr__') |
| 250 |
if w_descr is None: |
| 251 |
raise oefmt(space.w_AttributeError, |
| 252 |
"'%T' object is readonly", w_obj) |
| 253 |
return space.get_and_call_function(w_descr, w_obj, w_name, w_val) |
| 254 |
|
| 255 |
def delattr(space, w_obj, w_name): |
| 256 |
w_descr = space.lookup(w_obj, '__delattr__') |
| 257 |
if w_descr is None: |
| 258 |
raise oefmt(space.w_AttributeError, |
| 259 |
"'%T' object does not support attribute removal", |
| 260 |
w_obj) |
| 261 |
return space.get_and_call_function(w_descr, w_obj, w_name) |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
def is_true(space, w_obj): |
| 266 |
w_descr = space.lookup(w_obj, "__bool__") |
| 267 |
if w_descr is None: |
| 268 |
w_descr = space.lookup(w_obj, "__len__") |
| 269 |
if w_descr is None: |
| 270 |
return True |
| 271 |
|
| 272 |
w_res = space.get_and_call_function(w_descr, w_obj) |
| 273 |
return space._check_len_result(space.index(w_res)) != 0 |
| 274 |
|
| 275 |
w_res = space.get_and_call_function(w_descr, w_obj) |
| 276 |
|
| 277 |
if space.is_w(w_res, space.w_False): |
| 278 |
return False |
| 279 |
if space.is_w(w_res, space.w_True): |
| 280 |
return True |
| 281 |
w_restype = space.type(w_res) |
| 282 |
|
| 283 |
|
| 284 |
raise oefmt(space.w_TypeError, |
| 285 |
"__bool__ should return bool, returned %T", w_obj) |
| 286 |
|
| 287 |
def nonzero(space, w_obj): |
| 288 |
if space.is_true(w_obj): |
| 289 |
return space.w_True |
| 290 |
else: |
| 291 |
return space.w_False |
| 292 |
|
| 293 |
@use_special_method_shortcut('__len__') |
| 294 |
def _len(space, w_obj): |
| 295 |
w_descr = space.lookup(w_obj, '__len__') |
| 296 |
if w_descr is None: |
| 297 |
raise oefmt(space.w_TypeError, "object of type '%T' has no len()", w_obj) |
| 298 |
return space.get_and_call_function(w_descr, w_obj) |
| 299 |
|
| 300 |
def len_w(space, w_obj): |
| 301 |
w_res = space._len(w_obj) |
| 302 |
return space._check_len_result(space.index(w_res)) |
| 303 |
|
| 304 |
def len(space, w_obj): |
| 305 |
w_res = space.index(space._len(w_obj)) |
| 306 |
|
| 307 |
space._check_len_result(w_res) |
| 308 |
return w_res |
| 309 |
|
| 310 |
def _check_len_result(space, w_int): |
| 311 |
|
| 312 |
assert space.isinstance_w(w_int, space.w_int) |
| 313 |
if space.is_true(space.lt(w_int, space.newint(0))): |
| 314 |
raise oefmt(space.w_ValueError, "__len__() should return >= 0") |
| 315 |
result = space.getindex_w(w_int, space.w_OverflowError) |
| 316 |
assert result >= 0 |
| 317 |
return result |
| 318 |
|
| 319 |
def is_iterable(space, w_obj): |
| 320 |
w_descr = space.lookup(w_obj, '__iter__') |
| 321 |
if w_descr is None: |
| 322 |
if space.type(w_obj).flag_map_or_seq != 'M': |
| 323 |
w_descr = space.lookup(w_obj, '__getitem__') |
| 324 |
if w_descr is None: |
| 325 |
return False |
| 326 |
return True |
| 327 |
|
| 328 |
@use_special_method_shortcut('__iter__', |
| 329 |
lambda space, w_iter: space.lookup(w_iter, '__next__') is not None) |
| 330 |
def iter(space, w_obj): |
| 331 |
w_descr = space.lookup(w_obj, '__iter__') |
| 332 |
if w_descr is None: |
| 333 |
if space.type(w_obj).flag_map_or_seq != 'M': |
| 334 |
w_descr = space.lookup(w_obj, '__getitem__') |
| 335 |
if w_descr is None: |
| 336 |
raise oefmt(space.w_TypeError, |
| 337 |
"'%T' object is not iterable", w_obj) |
| 338 |
return space.newseqiter(w_obj) |
| 339 |
if space.is_w(w_descr, space.w_None): |
| 340 |
raise oefmt(space.w_TypeError, |
| 341 |
"'%T' object is not iterable", w_obj) |
| 342 |
w_iter = space.get_and_call_function(w_descr, w_obj) |
| 343 |
w_next = space.lookup(w_iter, '__next__') |
| 344 |
if w_next is None: |
| 345 |
raise oefmt(space.w_TypeError, "iter() returned non-iterator") |
| 346 |
return w_iter |
| 347 |
|
| 348 |
@use_special_method_shortcut('__next__') |
| 349 |
def next(space, w_obj): |
| 350 |
w_descr = space.lookup(w_obj, '__next__') |
| 351 |
if w_descr is None: |
| 352 |
raise oefmt(space.w_TypeError, |
| 353 |
"'%T' object is not an iterator", w_obj) |
| 354 |
return space.get_and_call_function(w_descr, w_obj) |
| 355 |
|
| 356 |
@use_special_method_shortcut('__getitem__') |
| 357 |
def getitem(space, w_obj, w_key): |
| 358 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 359 |
w_descr = space.lookup(w_obj, '__getitem__') |
| 360 |
if w_descr is None and space.isinstance_w(w_obj, space.w_type): |
| 361 |
|
| 362 |
if space.is_w(w_obj, space.w_type): |
| 363 |
from pypy.objspace.std.util import generic_alias_class_getitem |
| 364 |
return generic_alias_class_getitem(space, w_obj, w_key) |
| 365 |
try: |
| 366 |
w_descr = space.getattr(w_obj, space.newtext('__class_getitem__')) |
| 367 |
except OperationError as e: |
| 368 |
if e.match(space, space.w_AttributeError): |
| 369 |
w_descr = None |
| 370 |
else: |
| 371 |
raise e |
| 372 |
if space.is_none(w_descr): |
| 373 |
assert isinstance(w_obj, W_TypeObject) |
| 374 |
raise oefmt(space.w_TypeError, |
| 375 |
"type '%8' is not subscriptable (key %R)", |
| 376 |
w_obj.name, w_key) |
| 377 |
if w_descr is None: |
| 378 |
raise oefmt(space.w_TypeError, |
| 379 |
"'%T' object is not subscriptable (key %R)", |
| 380 |
w_obj, w_key) |
| 381 |
return space.get_and_call_function(w_descr, w_obj, w_key) |
| 382 |
|
| 383 |
@use_special_method_shortcut('__setitem__') |
| 384 |
def setitem(space, w_obj, w_key, w_val): |
| 385 |
w_descr = space.lookup(w_obj, '__setitem__') |
| 386 |
if w_descr is None: |
| 387 |
raise oefmt(space.w_TypeError, |
| 388 |
"'%T' object does not support item assignment", w_obj) |
| 389 |
return space.get_and_call_function(w_descr, w_obj, w_key, w_val) |
| 390 |
|
| 391 |
@use_special_method_shortcut('__delitem__') |
| 392 |
def delitem(space, w_obj, w_key): |
| 393 |
w_descr = space.lookup(w_obj, '__delitem__') |
| 394 |
if w_descr is None: |
| 395 |
raise oefmt(space.w_TypeError, |
| 396 |
"'%T' object does not support item deletion", w_obj) |
| 397 |
return space.get_and_call_function(w_descr, w_obj, w_key) |
| 398 |
|
| 399 |
def format(space, w_obj, w_format_spec): |
| 400 |
w_descr = space.lookup(w_obj, '__format__') |
| 401 |
if w_descr is None: |
| 402 |
raise oefmt(space.w_TypeError, |
| 403 |
"'%T' object does not define __format__", w_obj) |
| 404 |
w_res = space.get_and_call_function(w_descr, w_obj, w_format_spec) |
| 405 |
if not space.isinstance_w(w_res, space.w_unicode): |
| 406 |
raise oefmt(space.w_TypeError, |
| 407 |
"%T.__format__ must return string, not %T", |
| 408 |
w_obj, w_res) |
| 409 |
return w_res |
| 410 |
|
| 411 |
def _pow_find_impls(space, w_obj1, w_obj2): |
| 412 |
w_typ1 = space.type(w_obj1) |
| 413 |
w_typ2 = space.type(w_obj2) |
| 414 |
w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, '__pow__') |
| 415 |
if space.is_w(w_typ1, w_typ2): |
| 416 |
w_right_impl = None |
| 417 |
else: |
| 418 |
w_right_src, w_right_impl = space.lookup_in_type_where(w_typ2, '__rpow__') |
| 419 |
|
| 420 |
if (w_left_src is not w_right_src |
| 421 |
and space.issubtype_w(w_typ2, w_typ1)): |
| 422 |
if (w_left_src and w_right_src and |
| 423 |
not space.abstract_issubclass_w(w_left_src, w_right_src) and |
| 424 |
not space.abstract_issubclass_w(w_typ1, w_right_src)): |
| 425 |
w_obj1, w_obj2 = w_obj2, w_obj1 |
| 426 |
w_left_impl, w_right_impl = w_right_impl, w_left_impl |
| 427 |
return w_left_impl, w_right_impl, w_obj1, w_obj2 |
| 428 |
|
| 429 |
def pow_binary(space, w_obj1, w_obj2): |
| 430 |
w_left_impl, w_right_impl, w_obj1, w_obj2 = space._pow_find_impls(w_obj1, w_obj2) |
| 431 |
if w_left_impl is not None: |
| 432 |
w_res = space.get_and_call_function(w_left_impl, w_obj1, w_obj2) |
| 433 |
if _check_notimplemented(space, w_res): |
| 434 |
return w_res |
| 435 |
if w_right_impl is not None: |
| 436 |
w_res = space.get_and_call_function(w_right_impl, w_obj2, w_obj1) |
| 437 |
if _check_notimplemented(space, w_res): |
| 438 |
return w_res |
| 439 |
return None |
| 440 |
|
| 441 |
def pow(space, w_obj1, w_obj2, w_obj3): |
| 442 |
if space.is_w(w_obj3, space.w_None): |
| 443 |
w_res = space.pow_binary(w_obj1, w_obj2) |
| 444 |
if w_res is None: |
| 445 |
raise oefmt(space.w_TypeError, |
| 446 |
"unsupported operand type(s) for ** or pow(): '%T' and '%T'", |
| 447 |
w_obj1, w_obj2 |
| 448 |
) |
| 449 |
return w_res |
| 450 |
|
| 451 |
|
| 452 |
w_typ1 = space.type(w_obj1) |
| 453 |
w_typ2 = space.type(w_obj2) |
| 454 |
if w_typ2.is_cpytype(): |
| 455 |
w_res = space.pow3_bug_compat_cpyext(w_obj1, w_obj2, w_obj3) |
| 456 |
if w_res is not None: |
| 457 |
return w_res |
| 458 |
else: |
| 459 |
w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, '__pow__') |
| 460 |
if w_left_impl is not None: |
| 461 |
w_res = space.get_and_call_function(w_left_impl, w_obj1, w_obj2, w_obj3) |
| 462 |
if _check_notimplemented(space, w_res): |
| 463 |
return w_res |
| 464 |
if space.type(w_obj3).is_cpytype() and space.lookup(w_obj3, '__pow__') is not None: |
| 465 |
raise oefmt(space.w_TypeError, |
| 466 |
"unsupported operand type(s) for pow(): '%T', '%T', '%T' (This case is unsupported in PyPy. Please comment here if you hit it: https://github.com/pypy/pypy/issues/5207 )", |
| 467 |
w_obj1, w_obj2, w_obj3 |
| 468 |
) |
| 469 |
raise oefmt(space.w_TypeError, |
| 470 |
"unsupported operand type(s) for pow(): '%T', '%T', '%T'", |
| 471 |
w_obj1, w_obj2, w_obj3 |
| 472 |
) |
| 473 |
|
| 474 |
def pow3_bug_compat_cpyext(space, w_obj1, w_obj2, w_obj3): |
| 475 |
w_left_impl, w_right_impl, w_obj1, w_obj2 = space._pow_find_impls(w_obj1, w_obj2) |
| 476 |
if w_left_impl is not None: |
| 477 |
w_res = space.get_and_call_function(w_left_impl, w_obj1, w_obj2, w_obj3) |
| 478 |
if _check_notimplemented(space, w_res): |
| 479 |
return w_res |
| 480 |
if w_right_impl is not None: |
| 481 |
w_res = space.get_and_call_function(w_right_impl, w_obj2, w_obj1, w_obj3) |
| 482 |
if _check_notimplemented(space, w_res): |
| 483 |
return w_res |
| 484 |
return None |
| 485 |
|
| 486 |
def inplace_pow(space, w_lhs, w_rhs): |
| 487 |
w_impl = space.lookup(w_lhs, '__ipow__') |
| 488 |
if w_impl is not None: |
| 489 |
w_res = space.get_and_call_function(w_impl, w_lhs, w_rhs) |
| 490 |
if _check_notimplemented(space, w_res): |
| 491 |
return w_res |
| 492 |
w_res = space.pow_binary(w_lhs, w_rhs) |
| 493 |
if w_res is None: |
| 494 |
raise oefmt( |
| 495 |
space.w_TypeError, |
| 496 |
"unsupported operand type(s) for **=: '%T' and '%T'", |
| 497 |
w_lhs, w_rhs |
| 498 |
) |
| 499 |
return w_res |
| 500 |
|
| 501 |
@use_special_method_shortcut('__contains__', |
| 502 |
lambda space, w_res: space.is_w(space.type(w_res), space.w_bool)) |
| 503 |
def contains(space, w_container, w_item): |
| 504 |
w_descr = space.lookup(w_container, '__contains__') |
| 505 |
if w_descr is not None: |
| 506 |
w_result = space.get_and_call_function(w_descr, w_container, w_item) |
| 507 |
return space.nonzero(w_result) |
| 508 |
return space.sequence_contains(w_container, w_item) |
| 509 |
|
| 510 |
def sequence_contains(space, w_container, w_item): |
| 511 |
w_iter = space.iter(w_container) |
| 512 |
itergreenkey = space.iterator_greenkey(w_iter) |
| 513 |
w_itemtype = space.type(w_item) |
| 514 |
while 1: |
| 515 |
contains_jitdriver.jit_merge_point(itergreenkey=itergreenkey, w_itemtype=w_itemtype) |
| 516 |
try: |
| 517 |
w_next = space.next(w_iter) |
| 518 |
except OperationError as e: |
| 519 |
if not e.match(space, space.w_StopIteration): |
| 520 |
raise |
| 521 |
return space.w_False |
| 522 |
if space.eq_w(w_next, w_item): |
| 523 |
return space.w_True |
| 524 |
|
| 525 |
def sequence_count(space, w_container, w_item): |
| 526 |
w_iter = space.iter(w_container) |
| 527 |
count = 0 |
| 528 |
while 1: |
| 529 |
try: |
| 530 |
w_next = space.next(w_iter) |
| 531 |
except OperationError as e: |
| 532 |
if not e.match(space, space.w_StopIteration): |
| 533 |
raise |
| 534 |
return space.newint(count) |
| 535 |
if space.eq_w(w_next, w_item): |
| 536 |
count += 1 |
| 537 |
|
| 538 |
def sequence_index(space, w_container, w_item): |
| 539 |
w_iter = space.iter(w_container) |
| 540 |
index = 0 |
| 541 |
while 1: |
| 542 |
try: |
| 543 |
w_next = space.next(w_iter) |
| 544 |
except OperationError as e: |
| 545 |
if not e.match(space, space.w_StopIteration): |
| 546 |
raise |
| 547 |
raise oefmt(space.w_ValueError, |
| 548 |
"sequence.index(x): x not in sequence") |
| 549 |
if space.eq_w(w_next, w_item): |
| 550 |
return space.newint(index) |
| 551 |
index += 1 |
| 552 |
|
| 553 |
def hash_w(space, w_obj): |
| 554 |
"""compute the unwrapped hash of w_obj""" |
| 555 |
w_hash = space.lookup(w_obj, '__hash__') |
| 556 |
if w_hash is None: |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
return space.int_w(default_identity_hash(space, w_obj)) |
| 562 |
if space.is_w(w_hash, space.w_None): |
| 563 |
raise oefmt(space.w_TypeError, |
| 564 |
"unhashable type: '%T'", w_obj) |
| 565 |
w_result = space.get_and_call_function(w_hash, w_obj) |
| 566 |
if not space.isinstance_w(w_result, space.w_int): |
| 567 |
raise oefmt(space.w_TypeError, |
| 568 |
"__hash__ method should return an integer not '%T'", w_result) |
| 569 |
|
| 570 |
from pypy.objspace.std.intobject import ( |
| 571 |
W_AbstractIntObject, W_IntObject) |
| 572 |
if not isinstance(w_result, W_IntObject): |
| 573 |
|
| 574 |
assert isinstance(w_result, W_AbstractIntObject) |
| 575 |
w_result = w_result.descr_hash(space) |
| 576 |
result = space.int_w(w_result) |
| 577 |
|
| 578 |
|
| 579 |
result -= (result == -1) |
| 580 |
return result |
| 581 |
|
| 582 |
def hash(space, w_obj): |
| 583 |
return space.newint(space.hash_w(w_obj)) |
| 584 |
|
| 585 |
def issubtype_w(space, w_sub, w_type): |
| 586 |
return space._type_issubtype(w_sub, w_type) |
| 587 |
|
| 588 |
def issubtype(space, w_sub, w_type): |
| 589 |
return space.newbool(space._type_issubtype(w_sub, w_type)) |
| 590 |
|
| 591 |
@specialize.arg_or_var(2) |
| 592 |
def isinstance_w(space, w_inst, w_type): |
| 593 |
return space._type_isinstance(w_inst, w_type) |
| 594 |
|
| 595 |
@specialize.arg_or_var(2) |
| 596 |
def isinstance(space, w_inst, w_type): |
| 597 |
return space.newbool(space.isinstance_w(w_inst, w_type)) |
| 598 |
|
| 599 |
def _index(space, w_obj): |
| 600 |
if space.isinstance_w(w_obj, space.w_int): |
| 601 |
return w_obj |
| 602 |
w_impl = space.lookup(w_obj, '__index__') |
| 603 |
if w_impl is None: |
| 604 |
raise oefmt(space.w_TypeError, |
| 605 |
"'%T' object cannot be interpreted as an integer", |
| 606 |
w_obj) |
| 607 |
w_result = space.get_and_call_function(w_impl, w_obj) |
| 608 |
|
| 609 |
if space.is_w(space.type(w_result), space.w_int): |
| 610 |
return w_result |
| 611 |
if not space.isinstance_w(w_result, space.w_int): |
| 612 |
raise oefmt(space.w_TypeError, |
| 613 |
"__index__ returned non-int (type %T)", w_result) |
| 614 |
tp = space.type(w_result).name |
| 615 |
space.warn(space.newtext( |
| 616 |
"__index__ returned non-int (type %s). " |
| 617 |
"The ability to return an instance of a strict subclass of int " |
| 618 |
"is deprecated, and may be removed in a future version of " |
| 619 |
"Python." % (tp,)), space.w_DeprecationWarning) |
| 620 |
return w_result |
| 621 |
|
| 622 |
def index(space, w_obj): |
| 623 |
w_result = space._index(w_obj) |
| 624 |
if not space.is_w(space.type(w_result), space.w_int): |
| 625 |
result = space.bigint_w(w_result) |
| 626 |
w_result = space.newlong_from_rbigint(result) |
| 627 |
return w_result |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
def _check_notimplemented(space, w_obj): |
| 632 |
return not space.is_w(w_obj, space.w_NotImplemented) |
| 633 |
|
| 634 |
def _invoke_binop(space, w_impl, w_obj1, w_obj2): |
| 635 |
if w_impl is not None: |
| 636 |
w_res = space.get_and_call_function(w_impl, w_obj1, w_obj2) |
| 637 |
if _check_notimplemented(space, w_res): |
| 638 |
return w_res |
| 639 |
return None |
| 640 |
|
| 641 |
class PrintCache(object): |
| 642 |
def __init__(self, space): |
| 643 |
self.w_print = space.getattr(space.builtin, space.newtext("print")) |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
@specialize.arg(3, 4) |
| 648 |
def _call_binop_impl(space, w_obj1, w_obj2, left, right, seq_bug_compat): |
| 649 |
w_typ1 = space.type(w_obj1) |
| 650 |
w_typ2 = space.type(w_obj2) |
| 651 |
w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, left) |
| 652 |
if space.is_w(w_typ1, w_typ2): |
| 653 |
w_right_impl = None |
| 654 |
else: |
| 655 |
w_right_src, w_right_impl = space.lookup_in_type_where(w_typ2, right) |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
if w_right_src and (w_left_src is not w_right_src) and w_left_src: |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
if ((seq_bug_compat and w_typ1.flag_sequence_bug_compat |
| 668 |
and not w_typ2.flag_sequence_bug_compat) |
| 669 |
|
| 670 |
or space.issubtype_w(w_typ2, w_typ1)): |
| 671 |
if (not space.abstract_issubclass_w(w_left_src, w_right_src) and |
| 672 |
not space.abstract_issubclass_w(w_typ1, w_right_src)): |
| 673 |
w_obj1, w_obj2 = w_obj2, w_obj1 |
| 674 |
w_left_impl, w_right_impl = w_right_impl, w_left_impl |
| 675 |
|
| 676 |
w_res = _invoke_binop(space, w_left_impl, w_obj1, w_obj2) |
| 677 |
if w_res is not None: |
| 678 |
return w_res |
| 679 |
return _invoke_binop(space, w_right_impl, w_obj2, w_obj1) |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
def _make_binop_impl(symbol, specialnames): |
| 684 |
left, right = specialnames |
| 685 |
errormsg = "unsupported operand type(s) for %s: '%%T' and '%%T'" % ( |
| 686 |
symbol.replace('%', '%%'),) |
| 687 |
seq_bug_compat = (symbol == '+' or symbol == '*') |
| 688 |
|
| 689 |
printerrormsg = None |
| 690 |
if symbol == ">>": |
| 691 |
printerrormsg = errormsg + '. Did you mean "print(<message>, file=<output_stream>)"?' |
| 692 |
if symbol == "-": |
| 693 |
printerrormsg = errormsg + '. Did you mean "print(<-number>)"?' |
| 694 |
|
| 695 |
@use_special_method_shortcut(left) |
| 696 |
def shortcut_binop(space, w_obj1, w_obj2): |
| 697 |
|
| 698 |
|
| 699 |
w_impl = space.lookup(w_obj1, left) |
| 700 |
if w_impl is not None: |
| 701 |
w_res = space.get_and_call_function(w_impl, w_obj1, w_obj2) |
| 702 |
if _check_notimplemented(space, w_res): |
| 703 |
return w_res |
| 704 |
raise oefmt(space.w_TypeError, errormsg, w_obj1, w_obj2) |
| 705 |
|
| 706 |
def binop_impl(space, w_obj1, w_obj2): |
| 707 |
|
| 708 |
if type(w_obj1) is type(w_obj2) and not w_obj1.user_overridden_class: |
| 709 |
w_res = shortcut_binop(space, w_obj1, w_obj2) |
| 710 |
if _check_notimplemented(space, w_res): |
| 711 |
return w_res |
| 712 |
w_res = _call_binop_impl(space, w_obj1, w_obj2, left, right, seq_bug_compat) |
| 713 |
if w_res is not None: |
| 714 |
return w_res |
| 715 |
if printerrormsg is not None and w_obj1 is space.fromcache(PrintCache).w_print: |
| 716 |
raise oefmt(space.w_TypeError, printerrormsg, w_obj1, w_obj2) |
| 717 |
raise oefmt(space.w_TypeError, errormsg, w_obj1, w_obj2) |
| 718 |
|
| 719 |
return func_with_new_name(binop_impl, "binop_%s_impl"%left.strip('_')) |
| 720 |
|
| 721 |
def _invoke_comparison(space, w_descr, w_obj1, w_obj2): |
| 722 |
if w_descr is not None: |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
typ = type(w_descr) |
| 728 |
if typ is Function or typ is FunctionWithFixedCode: |
| 729 |
w_res = w_descr.funccall(w_obj1, w_obj2) |
| 730 |
else: |
| 731 |
try: |
| 732 |
w_impl = space.get(w_descr, w_obj1) |
| 733 |
except OperationError as e: |
| 734 |
|
| 735 |
|
| 736 |
if not e.match(space, space.w_AttributeError): |
| 737 |
raise |
| 738 |
return None |
| 739 |
else: |
| 740 |
w_res = space.call_function(w_impl, w_obj2) |
| 741 |
if _check_notimplemented(space, w_res): |
| 742 |
return w_res |
| 743 |
return None |
| 744 |
|
| 745 |
def _make_comparison_impl(symbol, specialnames): |
| 746 |
left, right = specialnames |
| 747 |
op = getattr(operator, left) |
| 748 |
|
| 749 |
@use_special_method_shortcut(left) |
| 750 |
def shortcut_comparison(space, w_obj1, w_obj2): |
| 751 |
|
| 752 |
w_impl = space.lookup(w_obj1, left) |
| 753 |
if w_impl is not None: |
| 754 |
w_res = space.get_and_call_function(w_impl, w_obj1, w_obj2) |
| 755 |
if _check_notimplemented(space, w_res): |
| 756 |
return w_res |
| 757 |
return _no_special_method_found_cmp(space, w_obj1, w_obj2, w_obj1, w_obj2) |
| 758 |
|
| 759 |
|
| 760 |
def comparison_impl(space, w_obj1, w_obj2): |
| 761 |
w_orig_obj1 = w_obj1 |
| 762 |
w_orig_obj2 = w_obj2 |
| 763 |
|
| 764 |
if left == right and type(w_obj1) is type(w_obj2) and not w_obj1.user_overridden_class: |
| 765 |
w_res = shortcut_comparison(space, w_obj1, w_obj2) |
| 766 |
if _check_notimplemented(space, w_res): |
| 767 |
return w_res |
| 768 |
w_typ1 = space.type(w_obj1) |
| 769 |
w_typ2 = space.type(w_obj2) |
| 770 |
w_left_src, w_left_impl = space.lookup_in_type_where(w_typ1, left) |
| 771 |
w_first = w_obj1 |
| 772 |
w_second = w_obj2 |
| 773 |
|
| 774 |
w_right_src, w_right_impl = space.lookup_in_type_where(w_typ2,right) |
| 775 |
if space.is_w(w_typ1, w_typ2): |
| 776 |
|
| 777 |
|
| 778 |
pass |
| 779 |
elif space.issubtype_w(w_typ2, w_typ1): |
| 780 |
|
| 781 |
w_obj1, w_obj2 = w_obj2, w_obj1 |
| 782 |
w_left_impl, w_right_impl = w_right_impl, w_left_impl |
| 783 |
|
| 784 |
w_res = _invoke_comparison(space, w_left_impl, w_obj1, w_obj2) |
| 785 |
if w_res is not None: |
| 786 |
return w_res |
| 787 |
w_res = _invoke_comparison(space, w_right_impl, w_obj2, w_obj1) |
| 788 |
if w_res is not None: |
| 789 |
return w_res |
| 790 |
return _no_special_method_found_cmp(space, w_obj1, w_obj2, w_orig_obj1, w_orig_obj2) |
| 791 |
|
| 792 |
def _no_special_method_found_cmp(space, w_obj1, w_obj2, w_orig_obj1, w_orig_obj2): |
| 793 |
|
| 794 |
|
| 795 |
if left == '__eq__': |
| 796 |
if space.is_w(w_obj1, w_obj2): |
| 797 |
return space.w_True |
| 798 |
else: |
| 799 |
return space.w_False |
| 800 |
elif left == '__ne__': |
| 801 |
if space.is_w(w_obj1, w_obj2): |
| 802 |
return space.w_False |
| 803 |
else: |
| 804 |
return space.w_True |
| 805 |
|
| 806 |
|
| 807 |
raise oefmt(space.w_TypeError, |
| 808 |
"'%s' not supported between instances of '%T' and '%T'", |
| 809 |
symbol, w_orig_obj1, w_orig_obj2) |
| 810 |
|
| 811 |
return func_with_new_name(comparison_impl, 'comparison_%s_impl'%left.strip('_')) |
| 812 |
|
| 813 |
def _make_inplace_impl(symbol, specialnames): |
| 814 |
specialname, = specialnames |
| 815 |
assert specialname.startswith('__i') and specialname.endswith('__') |
| 816 |
noninplacespacemethod = specialname[3:-2] |
| 817 |
if noninplacespacemethod in ['or', 'and']: |
| 818 |
noninplacespacemethod += '_' |
| 819 |
seq_bug_compat = (symbol == '+=' or symbol == '*=') |
| 820 |
rhs_method = '__r' + specialname[3:] |
| 821 |
lhs_method = '__' + specialname[3:] |
| 822 |
errormsg = "unsupported operand type(s) for %s: '%%T' and '%%T'" % ( |
| 823 |
symbol.replace('%', '%%'),) |
| 824 |
|
| 825 |
def inplace_impl(space, w_lhs, w_rhs): |
| 826 |
w_impl = space.lookup(w_lhs, specialname) |
| 827 |
if w_impl is not None: |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
if (seq_bug_compat and space.type(w_lhs).flag_sequence_bug_compat |
| 832 |
and not space.type(w_rhs).flag_sequence_bug_compat): |
| 833 |
w_res = _invoke_binop(space, space.lookup(w_rhs, rhs_method), |
| 834 |
w_rhs, w_lhs) |
| 835 |
if w_res is not None: |
| 836 |
return w_res |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
w_res = space.get_and_call_function(w_impl, w_lhs, w_rhs) |
| 843 |
if _check_notimplemented(space, w_res): |
| 844 |
return w_res |
| 845 |
|
| 846 |
w_res = _call_binop_impl(space, w_lhs, w_rhs, lhs_method, |
| 847 |
rhs_method, seq_bug_compat) |
| 848 |
if w_res is not None: |
| 849 |
return w_res |
| 850 |
|
| 851 |
raise oefmt(space.w_TypeError, errormsg, w_lhs, w_rhs) |
| 852 |
|
| 853 |
return func_with_new_name(inplace_impl, 'inplace_%s_impl'%specialname.strip('_')) |
| 854 |
|
| 855 |
def _make_unaryop_impl(symbol, specialnames): |
| 856 |
specialname, = specialnames |
| 857 |
errormsg = "unsupported operand type for unary %s: '%%T'" % symbol |
| 858 |
@use_special_method_shortcut(specialname) |
| 859 |
def unaryop_impl(space, w_obj): |
| 860 |
w_impl = space.lookup(w_obj, specialname) |
| 861 |
if w_impl is None: |
| 862 |
raise oefmt(space.w_TypeError, errormsg, w_obj) |
| 863 |
return space.get_and_call_function(w_impl, w_obj) |
| 864 |
return func_with_new_name(unaryop_impl, 'unaryop_%s_impl'%specialname.strip('_')) |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
for targetname, specialname, checkerspec in [ |
| 871 |
('float', '__float__', ("space.w_float",))]: |
| 872 |
|
| 873 |
l = ["space.isinstance_w(w_result, %s)" % x |
| 874 |
for x in checkerspec] |
| 875 |
checker = " or ".join(l) |
| 876 |
msg = "must be real number, not %%T" |
| 877 |
msg = msg % locals() |
| 878 |
source = """if 1: |
| 879 |
@use_special_method_shortcut(%(specialname)r, |
| 880 |
lambda space, w_result: %(checker)s) |
| 881 |
def %(targetname)s(space, w_obj): |
| 882 |
w_impl = space.lookup(w_obj, %(specialname)r) |
| 883 |
if w_impl is None: |
| 884 |
raise oefmt(space.w_TypeError, |
| 885 |
%(msg)r, |
| 886 |
w_obj) |
| 887 |
w_result = space.get_and_call_function(w_impl, w_obj) |
| 888 |
|
| 889 |
if %(checker)s: |
| 890 |
return w_result |
| 891 |
raise oefmt(space.w_TypeError, |
| 892 |
"%(specialname)s returned non-%(targetname)s (type " |
| 893 |
"'%%T')", w_result) |
| 894 |
assert not hasattr(DescrOperation, %(targetname)r) |
| 895 |
DescrOperation.%(targetname)s = %(targetname)s |
| 896 |
del %(targetname)s |
| 897 |
\n""" % locals() |
| 898 |
exec compile2(source) |
| 899 |
|
| 900 |
for targetname, specialname in [ |
| 901 |
('str', '__str__'), |
| 902 |
('repr', '__repr__')]: |
| 903 |
|
| 904 |
source = """if 1: |
| 905 |
@use_special_method_shortcut(%(specialname)r, |
| 906 |
lambda space, w_res: space.isinstance_w(w_res, space.w_text) |
| 907 |
) |
| 908 |
def %(targetname)s(space, w_obj): |
| 909 |
w_impl = space.lookup(w_obj, %(specialname)r) |
| 910 |
if w_impl is None: |
| 911 |
raise oefmt(space.w_TypeError, |
| 912 |
"unsupported operand type for %(targetname)s(): " |
| 913 |
"'%%T'", w_obj) |
| 914 |
w_result = space.get_and_call_function(w_impl, w_obj) |
| 915 |
if space.isinstance_w(w_result, space.w_unicode): |
| 916 |
return w_result |
| 917 |
|
| 918 |
raise oefmt(space.w_TypeError, |
| 919 |
"%(specialname)s returned non-string (type " |
| 920 |
"'%%T')", w_result) |
| 921 |
assert not hasattr(DescrOperation, %(targetname)r) |
| 922 |
DescrOperation.%(targetname)s = %(targetname)s |
| 923 |
del %(targetname)s |
| 924 |
\n""" % locals() |
| 925 |
exec compile2(source) |
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
for _name, _symbol, _arity, _specialnames in ObjSpace.MethodTable: |
| 930 |
if not hasattr(DescrOperation, _name): |
| 931 |
_impl_maker = None |
| 932 |
if _arity == 2 and _name in ['lt', 'le', 'gt', 'ge', 'ne', 'eq']: |
| 933 |
|
| 934 |
_impl_maker = _make_comparison_impl |
| 935 |
elif _arity == 2 and _name.startswith('inplace_'): |
| 936 |
|
| 937 |
_impl_maker = _make_inplace_impl |
| 938 |
elif _arity == 2 and len(_specialnames) == 2: |
| 939 |
|
| 940 |
_impl_maker = _make_binop_impl |
| 941 |
elif _arity == 1 and len(_specialnames) == 1 and _name != 'int': |
| 942 |
|
| 943 |
_impl_maker = _make_unaryop_impl |
| 944 |
if _impl_maker: |
| 945 |
setattr(DescrOperation,_name,_impl_maker(_symbol,_specialnames)) |
| 946 |
elif _name not in ['is_', 'id','type','issubtype', 'int', |
| 947 |
|
| 948 |
'ord', 'unichr', 'unicode']: |
| 949 |
raise Exception("missing def for operation %s" % _name) |