| 1 |
import __builtin__ |
| 2 |
from pypy.interpreter import special |
| 3 |
from pypy.interpreter.baseobjspace import ObjSpace, W_Root |
| 4 |
from pypy.interpreter.error import OperationError, oefmt, enrich_attribute_error |
| 5 |
from pypy.interpreter.function import Function, Method, FunctionWithFixedCode |
| 6 |
from pypy.interpreter.typedef import get_unique_interplevel_subclass |
| 7 |
from pypy.interpreter.unicodehelper import decode_utf8sp |
| 8 |
from pypy.objspace.descroperation import ( |
| 9 |
DescrOperation, get_attribute_name, raiseattrerror) |
| 10 |
from pypy.objspace.std import frame, transparent, callmethod |
| 11 |
from rpython.rlib.objectmodel import instantiate, specialize, is_annotation_constant |
| 12 |
from rpython.rlib.debug import make_sure_not_resized |
| 13 |
from rpython.rlib.rarithmetic import base_int, widen, is_valid_int, intmask |
| 14 |
from rpython.rlib.objectmodel import import_from_mixin, we_are_translated |
| 15 |
from rpython.rlib.objectmodel import not_rpython |
| 16 |
from rpython.rlib import jit, rutf8, types |
| 17 |
from rpython.rlib.signature import signature, finishsigs |
| 18 |
|
| 19 |
|
| 20 |
from pypy.objspace.std.boolobject import W_BoolObject |
| 21 |
from pypy.objspace.std.bytearrayobject import W_BytearrayObject |
| 22 |
from pypy.objspace.std.bytesobject import W_BytesObject |
| 23 |
from pypy.objspace.std.complexobject import W_ComplexObject |
| 24 |
from pypy.objspace.std.dictmultiobject import W_DictMultiObject, W_DictObject |
| 25 |
from pypy.objspace.std.floatobject import W_FloatObject |
| 26 |
from pypy.objspace.std.intobject import ( |
| 27 |
W_AbstractIntObject, W_IntObject, setup_prebuilt, wrapint) |
| 28 |
from pypy.objspace.std.iterobject import W_AbstractSeqIterObject, W_SeqIterObject |
| 29 |
from pypy.objspace.std.iterobject import W_FastUnicodeIterObject |
| 30 |
from pypy.objspace.std.listobject import W_ListObject |
| 31 |
from pypy.objspace.std.longobject import W_LongObject, newlong |
| 32 |
from pypy.objspace.std.memoryobject import W_MemoryView |
| 33 |
from pypy.objspace.std.noneobject import W_NoneObject |
| 34 |
from pypy.objspace.std.objectobject import W_ObjectObject |
| 35 |
from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject |
| 36 |
from pypy.objspace.std.sliceobject import W_SliceObject |
| 37 |
from pypy.objspace.std.tupleobject import W_AbstractTupleObject, W_TupleObject |
| 38 |
from pypy.objspace.std.typeobject import W_TypeObject, TypeCache |
| 39 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 40 |
|
| 41 |
@finishsigs |
| 42 |
class StdObjSpace(ObjSpace): |
| 43 |
"""The standard object space, implementing a general-purpose object |
| 44 |
library in Restricted Python.""" |
| 45 |
import_from_mixin(DescrOperation) |
| 46 |
|
| 47 |
@not_rpython |
| 48 |
def initialize(self): |
| 49 |
"""only for initializing the space |
| 50 |
|
| 51 |
Setup all the object types and implementations. |
| 52 |
""" |
| 53 |
|
| 54 |
setup_prebuilt(self) |
| 55 |
self.FrameClass = frame.build_frame(self) |
| 56 |
self.StringObjectCls = W_BytesObject |
| 57 |
self.UnicodeObjectCls = W_UnicodeObject |
| 58 |
self.IntObjectCls = W_IntObject |
| 59 |
self.FloatObjectCls = W_FloatObject |
| 60 |
|
| 61 |
|
| 62 |
self.w_None = W_NoneObject.w_None |
| 63 |
self.w_False = W_BoolObject.w_False |
| 64 |
self.w_True = W_BoolObject.w_True |
| 65 |
self.w_NotImplemented = self.wrap(special.NotImplemented()) |
| 66 |
self.w_Ellipsis = self.wrap(special.Ellipsis()) |
| 67 |
self.w_DisallowNew = self.wrap(special.DisallowNew()) |
| 68 |
|
| 69 |
|
| 70 |
builtin_type_classes = { |
| 71 |
W_BoolObject.typedef: W_BoolObject, |
| 72 |
W_BytearrayObject.typedef: W_BytearrayObject, |
| 73 |
W_BytesObject.typedef: W_BytesObject, |
| 74 |
W_ComplexObject.typedef: W_ComplexObject, |
| 75 |
W_DictMultiObject.typedef: W_DictMultiObject, |
| 76 |
W_FloatObject.typedef: W_FloatObject, |
| 77 |
W_IntObject.typedef: W_AbstractIntObject, |
| 78 |
W_AbstractSeqIterObject.typedef: W_AbstractSeqIterObject, |
| 79 |
W_ListObject.typedef: W_ListObject, |
| 80 |
W_MemoryView.typedef: W_MemoryView, |
| 81 |
W_NoneObject.typedef: W_NoneObject, |
| 82 |
W_ObjectObject.typedef: W_ObjectObject, |
| 83 |
W_SetObject.typedef: W_SetObject, |
| 84 |
W_FrozensetObject.typedef: W_FrozensetObject, |
| 85 |
W_SliceObject.typedef: W_SliceObject, |
| 86 |
W_TupleObject.typedef: W_TupleObject, |
| 87 |
W_TypeObject.typedef: W_TypeObject, |
| 88 |
W_UnicodeObject.typedef: W_UnicodeObject, |
| 89 |
} |
| 90 |
self.builtin_types = {} |
| 91 |
self._interplevel_classes = {} |
| 92 |
for typedef, cls in builtin_type_classes.items(): |
| 93 |
w_type = self.gettypeobject(typedef) |
| 94 |
self.builtin_types[typedef.name] = w_type |
| 95 |
setattr(self, 'w_' + typedef.name, w_type) |
| 96 |
self._interplevel_classes[w_type] = cls |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
self.w_unicode = self.w_str |
| 101 |
self.w_text = self.w_str |
| 102 |
del self.w_str |
| 103 |
self.w_long = self.w_int |
| 104 |
self.w_dict.flag_map_or_seq = 'M' |
| 105 |
from pypy.objspace.std import dictproxyobject |
| 106 |
dictproxyobject._set_flag_map_or_seq(self) |
| 107 |
self.w_list.flag_map_or_seq = 'S' |
| 108 |
self.w_tuple.flag_map_or_seq = 'S' |
| 109 |
self.builtin_types['str'] = self.w_unicode |
| 110 |
self.builtin_types['bytes'] = self.w_bytes |
| 111 |
self.builtin_types["NotImplemented"] = self.w_NotImplemented |
| 112 |
self.builtin_types["Ellipsis"] = self.w_Ellipsis |
| 113 |
|
| 114 |
|
| 115 |
self.make_builtins() |
| 116 |
|
| 117 |
|
| 118 |
self.setup_builtin_modules() |
| 119 |
|
| 120 |
if self.config.objspace.std.withtproxy: |
| 121 |
transparent.setup(self) |
| 122 |
|
| 123 |
def get_builtin_types(self): |
| 124 |
return self.builtin_types |
| 125 |
|
| 126 |
def createexecutioncontext(self): |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
ec = ObjSpace.createexecutioncontext(self) |
| 131 |
ec._py_repr = None |
| 132 |
return ec |
| 133 |
|
| 134 |
def get_objects_in_repr(self): |
| 135 |
from pypy.module.__pypy__.interp_identitydict import W_IdentityDict |
| 136 |
ec = self.getexecutioncontext() |
| 137 |
w_currently_in_repr = ec._py_repr |
| 138 |
if w_currently_in_repr is None: |
| 139 |
w_currently_in_repr = ec._py_repr = W_IdentityDict(self) |
| 140 |
return w_currently_in_repr |
| 141 |
|
| 142 |
@specialize.memo() |
| 143 |
def gettypefor(self, cls): |
| 144 |
if not hasattr(cls, "typedef") or cls.typedef is None: |
| 145 |
return None |
| 146 |
return self.gettypeobject(cls.typedef) |
| 147 |
|
| 148 |
def gettypeobject(self, typedef): |
| 149 |
|
| 150 |
|
| 151 |
assert typedef is not None |
| 152 |
return self.fromcache(TypeCache).getorbuild(typedef) |
| 153 |
|
| 154 |
@not_rpython |
| 155 |
def wrap(self, x): |
| 156 |
""" Wraps the Python value 'x' into one of the wrapper classes. This |
| 157 |
should only be used for tests, in real code you need to use the |
| 158 |
explicit new* methods.""" |
| 159 |
if x is None: |
| 160 |
return self.w_None |
| 161 |
if isinstance(x, OperationError): |
| 162 |
raise TypeError("attempt to wrap already wrapped exception: %s"% |
| 163 |
(x,)) |
| 164 |
if isinstance(x, int): |
| 165 |
if isinstance(x, bool): |
| 166 |
return self.newbool(x) |
| 167 |
else: |
| 168 |
return self.newint(x) |
| 169 |
if isinstance(x, str): |
| 170 |
return self.newtext(x) |
| 171 |
if isinstance(x, unicode): |
| 172 |
x = x.encode('utf8') |
| 173 |
lgt = rutf8.check_utf8(x, True) |
| 174 |
return self.newutf8(x, lgt) |
| 175 |
if isinstance(x, float): |
| 176 |
return W_FloatObject(x) |
| 177 |
if isinstance(x, W_Root): |
| 178 |
w_result = x.spacebind(self) |
| 179 |
|
| 180 |
return w_result |
| 181 |
if isinstance(x, base_int): |
| 182 |
return self.newint(x) |
| 183 |
return self._wrap_not_rpython(x) |
| 184 |
|
| 185 |
def _wrap_string_old(self, x): |
| 186 |
|
| 187 |
print 'WARNING: space.wrap() called on a non-ascii byte string: %s' % ( |
| 188 |
self.text_w(self.repr(self.newbytes(x))),) |
| 189 |
lst = [] |
| 190 |
for ch in x: |
| 191 |
ch = ord(ch) |
| 192 |
if ch > 127: |
| 193 |
lst.append(u'\ufffd') |
| 194 |
else: |
| 195 |
lst.append(unichr(ch)) |
| 196 |
unicode_x = u''.join(lst) |
| 197 |
return self.newtext(unicode_x) |
| 198 |
|
| 199 |
@not_rpython |
| 200 |
def _wrap_not_rpython(self, x): |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if is_valid_int(x): |
| 206 |
return self.newint(x) |
| 207 |
|
| 208 |
return self._wrap_not_rpython(x) |
| 209 |
|
| 210 |
@not_rpython |
| 211 |
def _wrap_not_rpython(self, x): |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
if isinstance(x, dict): |
| 218 |
items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] |
| 219 |
r = self.newdict() |
| 220 |
r.initialize_content(items_w) |
| 221 |
return r |
| 222 |
if isinstance(x, tuple): |
| 223 |
wrappeditems = [self.wrap(item) for item in list(x)] |
| 224 |
return self.newtuple(wrappeditems) |
| 225 |
if isinstance(x, list): |
| 226 |
wrappeditems = [self.wrap(item) for item in x] |
| 227 |
return self.newlist(wrappeditems) |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if type(x) is long: |
| 232 |
return self.wraplong(x) |
| 233 |
if isinstance(x, slice): |
| 234 |
return W_SliceObject(self.wrap(x.start), |
| 235 |
self.wrap(x.stop), |
| 236 |
self.wrap(x.step)) |
| 237 |
if isinstance(x, complex): |
| 238 |
return W_ComplexObject(x.real, x.imag) |
| 239 |
|
| 240 |
if isinstance(x, set): |
| 241 |
res = W_SetObject(self, self.newlist([self.wrap(item) for item in x])) |
| 242 |
return res |
| 243 |
|
| 244 |
if isinstance(x, frozenset): |
| 245 |
wrappeditems = [self.wrap(item) for item in x] |
| 246 |
return W_FrozensetObject(self, wrappeditems) |
| 247 |
|
| 248 |
if x is __builtin__.Ellipsis: |
| 249 |
|
| 250 |
return self.w_Ellipsis |
| 251 |
|
| 252 |
raise OperationError(self.w_RuntimeError, |
| 253 |
self.wrap("refusing to wrap cpython value %r" % (x,)) |
| 254 |
) |
| 255 |
|
| 256 |
@not_rpython |
| 257 |
def wrap_exception_cls(self, x): |
| 258 |
if hasattr(self, 'w_' + x.__name__): |
| 259 |
w_result = getattr(self, 'w_' + x.__name__) |
| 260 |
return w_result |
| 261 |
return None |
| 262 |
|
| 263 |
@not_rpython |
| 264 |
def wraplong(self, x): |
| 265 |
if self.config.objspace.std.withsmalllong: |
| 266 |
from rpython.rlib.rarithmetic import r_longlong |
| 267 |
try: |
| 268 |
rx = r_longlong(x) |
| 269 |
except OverflowError: |
| 270 |
pass |
| 271 |
else: |
| 272 |
from pypy.objspace.std.smalllongobject import \ |
| 273 |
W_SmallLongObject |
| 274 |
return W_SmallLongObject(rx) |
| 275 |
return W_LongObject.fromlong(x) |
| 276 |
|
| 277 |
@not_rpython |
| 278 |
def unwrap(self, w_obj): |
| 279 |
|
| 280 |
if isinstance(w_obj, W_Root): |
| 281 |
return w_obj.unwrap(self) |
| 282 |
raise TypeError("cannot unwrap: %r" % w_obj) |
| 283 |
|
| 284 |
@specialize.argtype(1) |
| 285 |
def newint(self, intval): |
| 286 |
from rpython.rlib.rarithmetic import r_uint, maxint, intmask |
| 287 |
if self.config.objspace.std.withsmalllong and isinstance(intval, base_int): |
| 288 |
from pypy.objspace.std.smalllongobject import W_SmallLongObject |
| 289 |
from rpython.rlib.rarithmetic import r_longlong, r_ulonglong |
| 290 |
from rpython.rlib.rarithmetic import longlongmax |
| 291 |
if (not isinstance(intval, r_ulonglong) |
| 292 |
or intval <= r_ulonglong(longlongmax)): |
| 293 |
return W_SmallLongObject(r_longlong(intval)) |
| 294 |
intval = widen(intval) |
| 295 |
if not isinstance(intval, int): |
| 296 |
return W_LongObject.fromrarith_int(intval) |
| 297 |
return wrapint(self, intval) |
| 298 |
|
| 299 |
def newfloat(self, floatval): |
| 300 |
return W_FloatObject(floatval) |
| 301 |
|
| 302 |
def newcomplex(self, realval, imagval): |
| 303 |
return W_ComplexObject(realval, imagval) |
| 304 |
|
| 305 |
def unpackcomplex(self, w_complex): |
| 306 |
from pypy.objspace.std.complexobject import unpackcomplex |
| 307 |
return unpackcomplex(self, w_complex) |
| 308 |
|
| 309 |
def newlong(self, val): |
| 310 |
return wrapint(self, val) |
| 311 |
|
| 312 |
@specialize.argtype(1) |
| 313 |
def newlong_from_rarith_int(self, val): |
| 314 |
return W_LongObject.fromrarith_int(val) |
| 315 |
|
| 316 |
def newlong_from_rbigint(self, val): |
| 317 |
try: |
| 318 |
return self.newint(val.toint()) |
| 319 |
except OverflowError: |
| 320 |
return newlong(self, val) |
| 321 |
|
| 322 |
def newint_from_size_t(self, val): |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
if val >= 0: |
| 327 |
return self.newint(val) |
| 328 |
from rpython.rlib.rarithmetic import r_uint |
| 329 |
from rpython.rlib.rbigint import rbigint |
| 330 |
return self.newlong_from_rbigint(rbigint.fromrarith_int(r_uint(val))) |
| 331 |
|
| 332 |
def newtuple(self, list_w): |
| 333 |
from pypy.objspace.std.tupleobject import wraptuple |
| 334 |
assert isinstance(list_w, list) |
| 335 |
make_sure_not_resized(list_w) |
| 336 |
return wraptuple(self, list_w) |
| 337 |
|
| 338 |
def newtuple2(self, w_a, w_b): |
| 339 |
from pypy.objspace.std.tupleobject import wraptuple2 |
| 340 |
return wraptuple2(self, w_a, w_b) |
| 341 |
|
| 342 |
def newlist(self, list_w, sizehint=-1): |
| 343 |
assert not list_w or sizehint == -1 |
| 344 |
return W_ListObject(self, list_w, sizehint) |
| 345 |
|
| 346 |
def newlist_bytes(self, list_s): |
| 347 |
return W_ListObject.newlist_bytes(self, list_s) |
| 348 |
|
| 349 |
def newlist_text(self, list_t): |
| 350 |
return self.newlist_utf8([decode_utf8sp(self, s)[0] for s in list_t], False) |
| 351 |
|
| 352 |
def newlist_utf8(self, list_u, is_ascii): |
| 353 |
if is_ascii: |
| 354 |
return W_ListObject.newlist_ascii(self, list_u) |
| 355 |
return ObjSpace.newlist_utf8(self, list_u, False) |
| 356 |
|
| 357 |
|
| 358 |
def newlist_int(self, list_i): |
| 359 |
return W_ListObject.newlist_int(self, list_i) |
| 360 |
|
| 361 |
def newlist_float(self, list_f): |
| 362 |
return W_ListObject.newlist_float(self, list_f) |
| 363 |
|
| 364 |
def newdict(self, module=False, instance=False, kwargs=False, |
| 365 |
strdict=False): |
| 366 |
return W_DictMultiObject.allocate_and_init_instance( |
| 367 |
self, module=module, instance=instance, |
| 368 |
strdict=strdict, kwargs=kwargs) |
| 369 |
|
| 370 |
def newdictproxy(self, w_dict): |
| 371 |
|
| 372 |
from pypy.objspace.std.dictproxyobject import W_DictProxyObject |
| 373 |
return W_DictProxyObject(w_dict) |
| 374 |
|
| 375 |
def newset(self, iterable_w=None): |
| 376 |
if iterable_w is None: |
| 377 |
return W_SetObject(self, None) |
| 378 |
return W_SetObject(self, self.newtuple(iterable_w)) |
| 379 |
|
| 380 |
def newfrozenset(self, iterable_w=None): |
| 381 |
if iterable_w is None: |
| 382 |
return W_FrozensetObject(self, None) |
| 383 |
return W_FrozensetObject(self, self.newtuple(iterable_w)) |
| 384 |
|
| 385 |
def newslice(self, w_start, w_end, w_step): |
| 386 |
return W_SliceObject(w_start, w_end, w_step) |
| 387 |
|
| 388 |
def newseqiter(self, w_obj): |
| 389 |
return W_SeqIterObject(w_obj) |
| 390 |
|
| 391 |
def newmemoryview(self, w_obj): |
| 392 |
return W_MemoryView(w_obj) |
| 393 |
|
| 394 |
def newmemoryview(self, view): |
| 395 |
return W_MemoryView(view) |
| 396 |
|
| 397 |
def newbytes(self, s): |
| 398 |
assert isinstance(s, bytes) |
| 399 |
return W_BytesObject(s) |
| 400 |
|
| 401 |
def newbytearray(self, l): |
| 402 |
return W_BytearrayObject(l) |
| 403 |
|
| 404 |
@specialize.arg_or_var(1) |
| 405 |
def newtext(self, s, lgt=-1, unused=-1): |
| 406 |
|
| 407 |
|
| 408 |
if is_annotation_constant(s) and s is not None: |
| 409 |
return self._newtext_memo(s) |
| 410 |
assert isinstance(s, str) |
| 411 |
if lgt < 0: |
| 412 |
lgt = rutf8.codepoints_in_utf8(s) |
| 413 |
return W_UnicodeObject(s, lgt) |
| 414 |
|
| 415 |
def newtext_or_none(self, s, lgt=-1): |
| 416 |
if s is None: |
| 417 |
return self.w_None |
| 418 |
return self.newtext(s, lgt) |
| 419 |
|
| 420 |
@specialize.memo() |
| 421 |
def _newtext_memo(self, s): |
| 422 |
|
| 423 |
|
| 424 |
w_u = self.interned_strings.get(s) |
| 425 |
if w_u is not None: |
| 426 |
return w_u |
| 427 |
lgt = rutf8.codepoints_in_utf8(s) |
| 428 |
return W_UnicodeObject(s, lgt) |
| 429 |
|
| 430 |
def newutf8(self, utf8s, length): |
| 431 |
assert isinstance(utf8s, str) |
| 432 |
return W_UnicodeObject(utf8s, length) |
| 433 |
|
| 434 |
def newfilename(self, s): |
| 435 |
return self.fsdecode(self.newbytes(s)) |
| 436 |
|
| 437 |
def type(self, w_obj): |
| 438 |
jit.promote(w_obj.__class__) |
| 439 |
return w_obj.getclass(self) |
| 440 |
|
| 441 |
def lookup(self, w_obj, name): |
| 442 |
w_type = self.type(w_obj) |
| 443 |
return w_type.lookup(name) |
| 444 |
lookup._annspecialcase_ = 'specialize:lookup' |
| 445 |
|
| 446 |
def lookup_in_type(self, w_type, name): |
| 447 |
w_src, w_descr = self.lookup_in_type_where(w_type, name) |
| 448 |
return w_descr |
| 449 |
|
| 450 |
def lookup_in_type_where(self, w_type, name): |
| 451 |
return w_type.lookup_where(name) |
| 452 |
lookup_in_type_where._annspecialcase_ = 'specialize:lookup_in_type_where' |
| 453 |
|
| 454 |
def lookup_in_type_starting_at(self, w_type, w_starttype, name): |
| 455 |
""" Only supposed to be used to implement super, w_starttype |
| 456 |
and w_type are the same as for super(starttype, type) |
| 457 |
""" |
| 458 |
assert isinstance(w_type, W_TypeObject) |
| 459 |
assert isinstance(w_starttype, W_TypeObject) |
| 460 |
return w_type.lookup_starting_at(w_starttype, name) |
| 461 |
|
| 462 |
@specialize.arg(1) |
| 463 |
def allocate_instance(self, cls, w_subtype): |
| 464 |
"""Allocate the memory needed for an instance of an internal or |
| 465 |
user-defined type, without actually __init__ializing the instance.""" |
| 466 |
w_type = self.gettypeobject(cls.typedef) |
| 467 |
|
| 468 |
from pypy.module._hpy_universal.interp_type import W_HPyTypeObject |
| 469 |
if self.is_w(w_type, w_subtype) and not cls == W_HPyTypeObject: |
| 470 |
instance = instantiate(cls) |
| 471 |
elif cls.typedef.acceptable_as_base_class: |
| 472 |
|
| 473 |
|
| 474 |
w_subtype = w_type.check_user_subclass(w_subtype) |
| 475 |
if cls.typedef.applevel_subclasses_base is not None: |
| 476 |
cls = cls.typedef.applevel_subclasses_base |
| 477 |
|
| 478 |
subcls = get_unique_interplevel_subclass(self, cls) |
| 479 |
instance = instantiate(subcls) |
| 480 |
assert isinstance(instance, cls) |
| 481 |
instance.user_setup(self, w_subtype) |
| 482 |
if w_subtype.hasuserdel: |
| 483 |
self.finalizer_queue.register_finalizer(instance) |
| 484 |
else: |
| 485 |
raise oefmt(self.w_TypeError, |
| 486 |
"%N.__new__(%N): only for the type %N", |
| 487 |
w_type, w_subtype, w_type) |
| 488 |
return instance |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
def _wrap_expected_length(self, expected, got): |
| 495 |
if got > expected: |
| 496 |
raise oefmt(self.w_ValueError, |
| 497 |
"too many values to unpack (expected %d)", expected) |
| 498 |
else: |
| 499 |
raise oefmt(self.w_ValueError, |
| 500 |
"not enough values to unpack (expected %d, got %d)", |
| 501 |
expected, got) |
| 502 |
|
| 503 |
def unpackiterable(self, w_obj, expected_length=-1): |
| 504 |
if isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): |
| 505 |
t = w_obj.getitems_copy() |
| 506 |
elif type(w_obj) is W_ListObject: |
| 507 |
t = w_obj.getitems_copy() |
| 508 |
else: |
| 509 |
return ObjSpace.unpackiterable(self, w_obj, expected_length) |
| 510 |
if expected_length != -1 and len(t) != expected_length: |
| 511 |
raise self._wrap_expected_length(expected_length, len(t)) |
| 512 |
return t |
| 513 |
|
| 514 |
@specialize.arg(3) |
| 515 |
def fixedview(self, w_obj, expected_length=-1, unroll=False): |
| 516 |
""" Fast paths |
| 517 |
""" |
| 518 |
if isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): |
| 519 |
t = w_obj.tolist() |
| 520 |
elif type(w_obj) is W_ListObject: |
| 521 |
length = w_obj.length() |
| 522 |
if expected_length >= 0: |
| 523 |
if length != expected_length: |
| 524 |
raise self._wrap_expected_length(expected_length, length) |
| 525 |
if jit.isconstant(expected_length): |
| 526 |
jit.promote(length) |
| 527 |
if unroll: |
| 528 |
t = w_obj.getitems_unroll() |
| 529 |
else: |
| 530 |
t = w_obj.getitems_fixedsize() |
| 531 |
return make_sure_not_resized(t) |
| 532 |
else: |
| 533 |
if unroll: |
| 534 |
return make_sure_not_resized(ObjSpace.unpackiterable_unroll( |
| 535 |
self, w_obj, expected_length)) |
| 536 |
else: |
| 537 |
return make_sure_not_resized(ObjSpace.unpackiterable( |
| 538 |
self, w_obj, expected_length)[:]) |
| 539 |
if expected_length != -1 and len(t) != expected_length: |
| 540 |
raise self._wrap_expected_length(expected_length, len(t)) |
| 541 |
return make_sure_not_resized(t) |
| 542 |
|
| 543 |
def fixedview_unroll(self, w_obj, expected_length): |
| 544 |
assert expected_length >= 0 |
| 545 |
return self.fixedview(w_obj, expected_length, unroll=True) |
| 546 |
|
| 547 |
def listview_no_unpack(self, w_obj): |
| 548 |
if type(w_obj) is W_ListObject: |
| 549 |
return w_obj.getitems() |
| 550 |
elif isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): |
| 551 |
return w_obj.getitems_copy() |
| 552 |
elif isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj): |
| 553 |
return w_obj.getitems() |
| 554 |
else: |
| 555 |
return None |
| 556 |
|
| 557 |
def listview(self, w_obj, expected_length=-1): |
| 558 |
t = self.listview_no_unpack(w_obj) |
| 559 |
if t is None: |
| 560 |
return ObjSpace.unpackiterable(self, w_obj, expected_length) |
| 561 |
if expected_length != -1 and len(t) != expected_length: |
| 562 |
raise self._wrap_expected_length(expected_length, len(t)) |
| 563 |
return t |
| 564 |
|
| 565 |
def listview_bytes(self, w_obj): |
| 566 |
|
| 567 |
|
| 568 |
if type(w_obj) is W_ListObject: |
| 569 |
return w_obj.getitems_bytes() |
| 570 |
if type(w_obj) is W_DictObject: |
| 571 |
return w_obj.listview_bytes() |
| 572 |
if type(w_obj) is W_SetObject or type(w_obj) is W_FrozensetObject: |
| 573 |
return w_obj.listview_bytes() |
| 574 |
if isinstance(w_obj, W_BytesObject): |
| 575 |
|
| 576 |
return None |
| 577 |
if isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj): |
| 578 |
return w_obj.getitems_bytes() |
| 579 |
return None |
| 580 |
|
| 581 |
def listview_ascii(self, w_obj): |
| 582 |
|
| 583 |
|
| 584 |
if type(w_obj) is W_ListObject: |
| 585 |
return w_obj.getitems_ascii() |
| 586 |
if type(w_obj) is W_DictObject: |
| 587 |
return w_obj.listview_ascii() |
| 588 |
if type(w_obj) is W_SetObject or type(w_obj) is W_FrozensetObject: |
| 589 |
return w_obj.listview_ascii() |
| 590 |
if isinstance(w_obj, W_UnicodeObject) and self._uses_unicode_iter(w_obj): |
| 591 |
return w_obj.listview_ascii() |
| 592 |
if isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj): |
| 593 |
return w_obj.getitems_ascii() |
| 594 |
return None |
| 595 |
|
| 596 |
def listview_int(self, w_obj): |
| 597 |
if type(w_obj) is W_ListObject: |
| 598 |
return w_obj.getitems_int() |
| 599 |
if type(w_obj) is W_DictObject: |
| 600 |
return w_obj.listview_int() |
| 601 |
if type(w_obj) is W_SetObject or type(w_obj) is W_FrozensetObject: |
| 602 |
return w_obj.listview_int() |
| 603 |
if type(w_obj) is W_BytesObject: |
| 604 |
|
| 605 |
return w_obj.listview_int() |
| 606 |
if isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj): |
| 607 |
return w_obj.getitems_int() |
| 608 |
return None |
| 609 |
|
| 610 |
def listview_float(self, w_obj): |
| 611 |
if type(w_obj) is W_ListObject: |
| 612 |
return w_obj.getitems_float() |
| 613 |
|
| 614 |
|
| 615 |
if isinstance(w_obj, W_ListObject) and self._uses_list_iter(w_obj): |
| 616 |
return w_obj.getitems_float() |
| 617 |
return None |
| 618 |
|
| 619 |
def view_as_kwargs(self, w_dict): |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
if isinstance(w_dict, W_DictObject): |
| 626 |
return w_dict.view_as_kwargs() |
| 627 |
return (None, None) |
| 628 |
|
| 629 |
def _uses_list_iter(self, w_obj): |
| 630 |
from pypy.objspace.descroperation import list_iter |
| 631 |
return self.lookup(w_obj, '__iter__') is list_iter(self) |
| 632 |
|
| 633 |
def _uses_tuple_iter(self, w_obj): |
| 634 |
from pypy.objspace.descroperation import tuple_iter |
| 635 |
return self.lookup(w_obj, '__iter__') is tuple_iter(self) |
| 636 |
|
| 637 |
def _uses_unicode_iter(self, w_obj): |
| 638 |
from pypy.objspace.descroperation import unicode_iter |
| 639 |
return self.lookup(w_obj, '__iter__') is unicode_iter(self) |
| 640 |
|
| 641 |
def sliceindices(self, w_slice, w_length): |
| 642 |
if isinstance(w_slice, W_SliceObject): |
| 643 |
a, b, c = w_slice.indices3(self, self.int_w(w_length)) |
| 644 |
return (a, b, c) |
| 645 |
w_indices = self.getattr(w_slice, self.newtext('indices')) |
| 646 |
w_tup = self.call_function(w_indices, w_length) |
| 647 |
l_w = self.unpackiterable(w_tup) |
| 648 |
if not len(l_w) == 3: |
| 649 |
raise oefmt(self.w_ValueError, "Expected tuple of length 3") |
| 650 |
return self.int_w(l_w[0]), self.int_w(l_w[1]), self.int_w(l_w[2]) |
| 651 |
|
| 652 |
_DescrOperation_is_true = is_true |
| 653 |
|
| 654 |
def is_true(self, w_obj): |
| 655 |
|
| 656 |
if type(w_obj) is W_BoolObject: |
| 657 |
return bool(w_obj.intval) |
| 658 |
return self._DescrOperation_is_true(w_obj) |
| 659 |
|
| 660 |
def getattr(self, w_obj, w_name): |
| 661 |
|
| 662 |
|
| 663 |
w_type = self.type(w_obj) |
| 664 |
w_descr = w_type.getattribute_if_not_from_object() |
| 665 |
if w_descr is not None: |
| 666 |
return self._handle_getattribute(w_descr, w_obj, w_name) |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
name = get_attribute_name(self, w_obj, w_name) |
| 671 |
w_descr = w_type.lookup(name) |
| 672 |
e = None |
| 673 |
if w_descr is not None: |
| 674 |
w_get = None |
| 675 |
is_data = self.is_data_descr(w_descr) |
| 676 |
if is_data: |
| 677 |
w_get = self.lookup(w_descr, "__get__") |
| 678 |
if w_get is None: |
| 679 |
w_value = w_obj.getdictvalue(self, name) |
| 680 |
if w_value is not None: |
| 681 |
return w_value |
| 682 |
if not is_data: |
| 683 |
w_get = self.lookup(w_descr, "__get__") |
| 684 |
typ = type(w_descr) |
| 685 |
if typ is Function or typ is FunctionWithFixedCode: |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
return Method(self, w_descr, w_obj) |
| 691 |
if w_get is not None: |
| 692 |
|
| 693 |
|
| 694 |
try: |
| 695 |
return self.get_and_call_function(w_get, w_descr, w_obj, |
| 696 |
w_type) |
| 697 |
except OperationError as e: |
| 698 |
if not e.match(self, self.w_AttributeError): |
| 699 |
raise |
| 700 |
else: |
| 701 |
return w_descr |
| 702 |
else: |
| 703 |
w_value = w_obj.getdictvalue(self, name) |
| 704 |
if w_value is not None: |
| 705 |
return w_value |
| 706 |
|
| 707 |
w_descr = self.lookup(w_obj, '__getattr__') |
| 708 |
if w_descr is not None: |
| 709 |
try: |
| 710 |
return self.get_and_call_function(w_descr, w_obj, w_name) |
| 711 |
except OperationError as e: |
| 712 |
enrich_attribute_error(self, e, w_obj, w_name) |
| 713 |
raise |
| 714 |
elif e is not None: |
| 715 |
raise e |
| 716 |
else: |
| 717 |
raiseattrerror(self, w_obj, w_name) |
| 718 |
|
| 719 |
def setattr(space, w_obj, w_name, w_value): |
| 720 |
w_type = space.type(w_obj) |
| 721 |
w_descr = w_type.setattr_if_not_from_object() |
| 722 |
if w_descr is not None: |
| 723 |
return space.get_and_call_function(w_descr, w_obj, w_name, w_value) |
| 724 |
|
| 725 |
name = space.text_w(w_name) |
| 726 |
w_descr = w_type.lookup(name) |
| 727 |
if w_descr is not None: |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
w_set = space.lookup(w_descr, '__set__') |
| 732 |
if w_set is not None: |
| 733 |
return space.get_and_call_function(w_set, w_descr, w_obj, w_value) |
| 734 |
if space.lookup(w_descr, '__delete__') is not None: |
| 735 |
raise oefmt(space.w_AttributeError, |
| 736 |
"'%T' object is not a descriptor with set", w_descr) |
| 737 |
if w_obj.setdictvalue(space, name, w_value): |
| 738 |
return |
| 739 |
raiseattrerror(space, w_obj, w_name, w_descr) |
| 740 |
|
| 741 |
def finditem_str(self, w_obj, key): |
| 742 |
""" Perform a getitem on w_obj with key (string). Returns found |
| 743 |
element or None on element not found. |
| 744 |
|
| 745 |
performance shortcut to avoid creating the OperationError(KeyError) |
| 746 |
and allocating W_BytesObject |
| 747 |
""" |
| 748 |
if (isinstance(w_obj, W_DictMultiObject) and |
| 749 |
not w_obj.user_overridden_class): |
| 750 |
return w_obj.getitem_str(key) |
| 751 |
return ObjSpace.finditem_str(self, w_obj, key) |
| 752 |
|
| 753 |
def finditem(self, w_obj, w_key): |
| 754 |
""" Perform a getitem on w_obj with w_key (any object). Returns found |
| 755 |
element or None on element not found. |
| 756 |
|
| 757 |
performance shortcut to avoid creating the OperationError(KeyError). |
| 758 |
""" |
| 759 |
if (isinstance(w_obj, W_DictMultiObject) and |
| 760 |
not w_obj.user_overridden_class): |
| 761 |
return w_obj.getitem(w_key) |
| 762 |
return ObjSpace.finditem(self, w_obj, w_key) |
| 763 |
|
| 764 |
def setitem_str(self, w_obj, key, w_value): |
| 765 |
""" Same as setitem, but takes string instead of any wrapped object |
| 766 |
""" |
| 767 |
if (isinstance(w_obj, W_DictMultiObject) and |
| 768 |
not w_obj.user_overridden_class): |
| 769 |
w_obj.setitem_str(key, w_value) |
| 770 |
else: |
| 771 |
self.setitem(w_obj, self.newtext(key), w_value) |
| 772 |
|
| 773 |
def getindex_w(self, w_obj, w_exception, objdescr=None): |
| 774 |
if type(w_obj) is W_IntObject: |
| 775 |
return w_obj.intval |
| 776 |
return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr) |
| 777 |
|
| 778 |
def unicode_from_object(self, w_obj): |
| 779 |
from pypy.objspace.std.unicodeobject import unicode_from_object |
| 780 |
return unicode_from_object(self, w_obj) |
| 781 |
|
| 782 |
def encode_unicode_object(self, w_unicode, encoding, errors): |
| 783 |
from pypy.objspace.std.unicodeobject import encode_object |
| 784 |
return encode_object(self, w_unicode, encoding, errors) |
| 785 |
|
| 786 |
def call_method(self, w_obj, methname, *arg_w): |
| 787 |
return callmethod.call_method_opt(self, w_obj, methname, *arg_w) |
| 788 |
|
| 789 |
def _type_issubtype(self, w_sub, w_type): |
| 790 |
if isinstance(w_sub, W_TypeObject) and isinstance(w_type, W_TypeObject): |
| 791 |
return w_sub.issubtype(w_type) |
| 792 |
raise oefmt(self.w_TypeError, "need type objects") |
| 793 |
|
| 794 |
@specialize.arg_or_var(2) |
| 795 |
def _type_isinstance(self, w_inst, w_type): |
| 796 |
if not isinstance(w_type, W_TypeObject): |
| 797 |
raise oefmt(self.w_TypeError, "need type object") |
| 798 |
if is_annotation_constant(w_type): |
| 799 |
cls = self._get_interplevel_cls(w_type) |
| 800 |
if cls is not None: |
| 801 |
assert w_inst is not None |
| 802 |
if isinstance(w_inst, cls): |
| 803 |
return True |
| 804 |
return self.type(w_inst).issubtype(w_type) |
| 805 |
|
| 806 |
@specialize.memo() |
| 807 |
def _get_interplevel_cls(self, w_type): |
| 808 |
if not hasattr(self, "_interplevel_classes"): |
| 809 |
return None |
| 810 |
return self._interplevel_classes.get(w_type, None) |
| 811 |
|
| 812 |
@specialize.arg(2, 3) |
| 813 |
def is_overloaded(self, w_obj, tp, method): |
| 814 |
return (self.lookup(w_obj, method) is not |
| 815 |
self.lookup_in_type(tp, method)) |
| 816 |
|
| 817 |
def getfulltypename(self, w_obj): |
| 818 |
w_type = self.type(w_obj) |
| 819 |
if w_type.is_heaptype(): |
| 820 |
classname = w_type.getqualname(self) |
| 821 |
w_module = w_type.lookup("__module__") |
| 822 |
if w_module is not None: |
| 823 |
try: |
| 824 |
modulename = self.utf8_w(w_module) |
| 825 |
except OperationError as e: |
| 826 |
if not e.match(self, self.w_TypeError): |
| 827 |
raise |
| 828 |
else: |
| 829 |
classname = '%s.%s' % (modulename, classname) |
| 830 |
else: |
| 831 |
classname = w_type.name |
| 832 |
return classname |