| 1 |
from rpython.rlib.rstring import StringBuilder, ParseStringError |
| 2 |
from rpython.rlib.mutbuffer import MutableStringBuffer |
| 3 |
from rpython.rlib.rstruct import ieee |
| 4 |
from rpython.rlib.rstruct.ieee import float_pack |
| 5 |
from rpython.rlib import objectmodel, jit, rarithmetic |
| 6 |
|
| 7 |
from pypy.interpreter.baseobjspace import W_Root |
| 8 |
from pypy.interpreter.argument import Arguments |
| 9 |
from pypy.interpreter.typedef import TypeDef, make_weakref_descr, GetSetProperty |
| 10 |
from pypy.interpreter.error import oefmt, oefmt_attribute_error, OperationError |
| 11 |
from pypy.interpreter.unicodehelper import decode_utf8sp |
| 12 |
|
| 13 |
from pypy.interpreter.gateway import interp2app, applevel, unwrap_spec, WrappedDefault |
| 14 |
from pypy.module._pickle.state import State |
| 15 |
from pypy.module.__pypy__.interp_buffer import W_PickleBuffer |
| 16 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 17 |
|
| 18 |
|
| 19 |
class _W_AsciiListUnpickler(W_Root): |
| 20 |
"""Sentinel pushed onto the stack when GLOBAL 'pypy._builtin' '_ascii_list_unpickle' |
| 21 |
is encountered. load_reduce checks for this type and calls the RPython fast path |
| 22 |
instead of going through the generic Python call mechanism.""" |
| 23 |
pass |
| 24 |
|
| 25 |
_ascii_list_unpickler_sentinel = _W_AsciiListUnpickler() |
| 26 |
|
| 27 |
|
| 28 |
class _W_BytesListUnpickler(W_Root): |
| 29 |
"""Sentinel for GLOBAL 'pypy._builtin' '_bytes_list_unpickle'.""" |
| 30 |
pass |
| 31 |
|
| 32 |
_bytes_list_unpickler_sentinel = _W_BytesListUnpickler() |
| 33 |
|
| 34 |
|
| 35 |
def _ascii_list_from_packed(space, w_args): |
| 36 |
"""Unpack the PyPy ASCII-list blob (a 1-tuple of bytes) into an AsciiList. |
| 37 |
|
| 38 |
Packed format: 4-byte LE count N + N x (1-byte length + raw ASCII bytes). |
| 39 |
Called by load_reduce when the sentinel is on the stack; never goes through |
| 40 |
the Python call mechanism. |
| 41 |
""" |
| 42 |
w_packed = space.getitem(w_args, space.newint(0)) |
| 43 |
data = space.bytes_w(w_packed) |
| 44 |
if len(data) < 4: |
| 45 |
raise oefmt(space.w_ValueError, "truncated ascii list pickle data") |
| 46 |
n = (ord(data[0]) |
| 47 |
| (ord(data[1]) << 8) |
| 48 |
| (ord(data[2]) << 16) |
| 49 |
| (ord(data[3]) << 24)) |
| 50 |
strs = [''] * n |
| 51 |
i = 4 |
| 52 |
for j in range(n): |
| 53 |
if i >= len(data): |
| 54 |
raise oefmt(space.w_ValueError, "truncated ascii list pickle data") |
| 55 |
length = ord(data[i]) |
| 56 |
i += 1 |
| 57 |
strs[j] = data[i:i + length] |
| 58 |
i += length |
| 59 |
return space.newlist_utf8(strs, True) |
| 60 |
|
| 61 |
|
| 62 |
def _bytes_list_from_packed(space, w_args): |
| 63 |
"""Unpack a PyPy bytes-list blob (1-tuple of bytes) into a BytesListStrategy list. |
| 64 |
|
| 65 |
Packed format: 4-byte LE count N + N x (1-byte length + raw bytes). |
| 66 |
""" |
| 67 |
w_packed = space.getitem(w_args, space.newint(0)) |
| 68 |
data = space.bytes_w(w_packed) |
| 69 |
if len(data) < 4: |
| 70 |
raise oefmt(space.w_ValueError, "truncated bytes list pickle data") |
| 71 |
n = (ord(data[0]) |
| 72 |
| (ord(data[1]) << 8) |
| 73 |
| (ord(data[2]) << 16) |
| 74 |
| (ord(data[3]) << 24)) |
| 75 |
items = [''] * n |
| 76 |
i = 4 |
| 77 |
for j in range(n): |
| 78 |
if i >= len(data): |
| 79 |
raise oefmt(space.w_ValueError, "truncated bytes list pickle data") |
| 80 |
length = ord(data[i]) |
| 81 |
i += 1 |
| 82 |
items[j] = data[i:i + length] |
| 83 |
i += length |
| 84 |
return space.newlist_bytes(items) |
| 85 |
|
| 86 |
|
| 87 |
import sys |
| 88 |
maxsize = sys.maxint |
| 89 |
|
| 90 |
HIGHEST_PROTOCOL = 5 |
| 91 |
DEFAULT_PROTOCOL = 4 |
| 92 |
|
| 93 |
|
| 94 |
def _get_printable_location(typ): |
| 95 |
return 'Pickler.dump [%s]' % ( |
| 96 |
typ, |
| 97 |
) |
| 98 |
jitdriver_dump = jit.JitDriver(name="Pickler.dump", greens=["typ"], reds="auto", |
| 99 |
get_printable_location=_get_printable_location) |
| 100 |
|
| 101 |
def _get_printable_location(typ, greenkey): |
| 102 |
return 'Pickler._batch_appends [%s, %s]' % ( |
| 103 |
typ, |
| 104 |
greenkey.iterator_greenkey_printable() |
| 105 |
) |
| 106 |
jitdriver_appends = jit.JitDriver(name="Pickler._batch_appends", greens=["typ", "greenkey"], reds="auto", |
| 107 |
get_printable_location=_get_printable_location) |
| 108 |
|
| 109 |
def _get_printable_location(typ): |
| 110 |
return 'Pickler.save_tuple [%s]' % ( |
| 111 |
typ, |
| 112 |
) |
| 113 |
jitdriver_tuple = jit.JitDriver(name="Pickler.save_tuple", greens=["typ"], reds="auto", |
| 114 |
get_printable_location=_get_printable_location) |
| 115 |
|
| 116 |
def _get_printable_location(typ): |
| 117 |
return 'Pickler.save_frozenset [%s]' % ( |
| 118 |
typ, |
| 119 |
) |
| 120 |
jitdriver_frozenset = jit.JitDriver(name="Pickler.save_frozenset", greens=["typ"], reds="auto", |
| 121 |
get_printable_location=_get_printable_location) |
| 122 |
|
| 123 |
def _get_printable_location(typ, greenkey): |
| 124 |
return 'Pickler._batch_setitems [%s, %s]' % ( |
| 125 |
typ, |
| 126 |
greenkey.iterator_greenkey_printable() |
| 127 |
) |
| 128 |
jitdriver_setitems = jit.JitDriver(name="Pickler._batch_setitems", greens=["typ", "greenkey"], reds="auto", |
| 129 |
get_printable_location=_get_printable_location) |
| 130 |
|
| 131 |
def _get_printable_location(opcode): |
| 132 |
return 'Unpickler.load opcode=%d' % opcode |
| 133 |
jitdriver_load = jit.JitDriver(name="Unpickler.load", greens=["opcode"], reds="auto", |
| 134 |
get_printable_location=_get_printable_location) |
| 135 |
|
| 136 |
def _get_printable_location(typ1, typ2, greenkey): |
| 137 |
return 'Unpickler.load_setitems [%s, %s, %s]' % ( |
| 138 |
typ1, typ2, |
| 139 |
greenkey.iterator_greenkey_printable() |
| 140 |
) |
| 141 |
jitdriver_load_setitems = jit.JitDriver(name="Unpickler.load_setitems", |
| 142 |
greens=["typ1", "typ2", "greenkey"], reds="auto", |
| 143 |
get_printable_location=_get_printable_location) |
| 144 |
|
| 145 |
class Opcodes(object): |
| 146 |
MARK = b'(' |
| 147 |
STOP = b'.' |
| 148 |
POP = b'0' |
| 149 |
POP_MARK = b'1' |
| 150 |
DUP = b'2' |
| 151 |
FLOAT = b'F' |
| 152 |
INT = b'I' |
| 153 |
BININT = b'J' |
| 154 |
BININT1 = b'K' |
| 155 |
LONG = b'L' |
| 156 |
BININT2 = b'M' |
| 157 |
NONE = b'N' |
| 158 |
PERSID = b'P' |
| 159 |
BINPERSID = b'Q' |
| 160 |
REDUCE = b'R' |
| 161 |
STRING = b'S' |
| 162 |
BINSTRING = b'T' |
| 163 |
SHORT_BINSTRING= b'U' |
| 164 |
UNICODE = b'V' |
| 165 |
BINUNICODE = b'X' |
| 166 |
APPEND = b'a' |
| 167 |
BUILD = b'b' |
| 168 |
GLOBAL = b'c' |
| 169 |
DICT = b'd' |
| 170 |
EMPTY_DICT = b'}' |
| 171 |
APPENDS = b'e' |
| 172 |
GET = b'g' |
| 173 |
BINGET = b'h' |
| 174 |
INST = b'i' |
| 175 |
LONG_BINGET = b'j' |
| 176 |
LIST = b'l' |
| 177 |
EMPTY_LIST = b']' |
| 178 |
OBJ = b'o' |
| 179 |
PUT = b'p' |
| 180 |
BINPUT = b'q' |
| 181 |
LONG_BINPUT = b'r' |
| 182 |
SETITEM = b's' |
| 183 |
TUPLE = b't' |
| 184 |
EMPTY_TUPLE = b')' |
| 185 |
SETITEMS = b'u' |
| 186 |
BINFLOAT = b'G' |
| 187 |
|
| 188 |
TRUE = b'I01\n' |
| 189 |
FALSE = b'I00\n' |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
PROTO = b'\x80' |
| 194 |
NEWOBJ = b'\x81' |
| 195 |
EXT1 = b'\x82' |
| 196 |
EXT2 = b'\x83' |
| 197 |
EXT4 = b'\x84' |
| 198 |
TUPLE1 = b'\x85' |
| 199 |
TUPLE2 = b'\x86' |
| 200 |
TUPLE3 = b'\x87' |
| 201 |
NEWTRUE = b'\x88' |
| 202 |
NEWFALSE = b'\x89' |
| 203 |
LONG1 = b'\x8a' |
| 204 |
LONG4 = b'\x8b' |
| 205 |
|
| 206 |
_tuplesize2code = [EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3] |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
BINBYTES = b'B' |
| 211 |
SHORT_BINBYTES = b'C' |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
SHORT_BINUNICODE = b'\x8c' |
| 216 |
BINUNICODE8 = b'\x8d' |
| 217 |
BINBYTES8 = b'\x8e' |
| 218 |
EMPTY_SET = b'\x8f' |
| 219 |
ADDITEMS = b'\x90' |
| 220 |
FROZENSET = b'\x91' |
| 221 |
NEWOBJ_EX = b'\x92' |
| 222 |
STACK_GLOBAL = b'\x93' |
| 223 |
MEMOIZE = b'\x94' |
| 224 |
FRAME = b'\x95' |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
BYTEARRAY8 = b'\x96' |
| 229 |
NEXT_BUFFER = b'\x97' |
| 230 |
READONLY_BUFFER = b'\x98' |
| 231 |
|
| 232 |
op = Opcodes() |
| 233 |
|
| 234 |
def packQ(opcode, val): |
| 235 |
res = StringBuilder(9) |
| 236 |
res.append(opcode) |
| 237 |
for i in range(8): |
| 238 |
res.append(chr(val & 0xff)) |
| 239 |
val >>= 8 |
| 240 |
return res.build() |
| 241 |
|
| 242 |
def packH(opcode, val): |
| 243 |
res = StringBuilder(3) |
| 244 |
res.append(opcode) |
| 245 |
for i in range(2): |
| 246 |
res.append(chr(val & 0xff)) |
| 247 |
val >>= 8 |
| 248 |
return res.build() |
| 249 |
|
| 250 |
def packi(opcode, val): |
| 251 |
res = StringBuilder(5) |
| 252 |
res.append(opcode) |
| 253 |
for i in range(4): |
| 254 |
res.append(chr(val & 0xff)) |
| 255 |
val >>= 8 |
| 256 |
return res.build() |
| 257 |
|
| 258 |
def unpacki(s, index=0): |
| 259 |
val = 0 |
| 260 |
for i in range(4): |
| 261 |
x = ord(s[index]) |
| 262 |
if i == 3 and x >= 128: |
| 263 |
x -= 256 |
| 264 |
val += x * (1 << 8 * i) |
| 265 |
index += 1 |
| 266 |
return val |
| 267 |
|
| 268 |
def unpackI(s, index=0): |
| 269 |
val = 0 |
| 270 |
for i in range(4): |
| 271 |
x = ord(s[index]) |
| 272 |
val += x * (1 << 8 * i) |
| 273 |
index += 1 |
| 274 |
return val |
| 275 |
|
| 276 |
def unpackQ(s, index=0): |
| 277 |
from rpython.rlib.rarithmetic import r_ulonglong, intmask |
| 278 |
val = r_ulonglong(0) |
| 279 |
shift = r_ulonglong(0) |
| 280 |
for i in range(8): |
| 281 |
val |= r_ulonglong(ord(s[index])) << shift |
| 282 |
shift += r_ulonglong(8) |
| 283 |
index += 1 |
| 284 |
if val > r_ulonglong(maxsize): |
| 285 |
raise OverflowError("Q value exceeds system's maximum size") |
| 286 |
return intmask(val) |
| 287 |
|
| 288 |
|
| 289 |
def packI(opcode, val): |
| 290 |
assert val >= 0 |
| 291 |
return packi(opcode, val) |
| 292 |
|
| 293 |
def packB(opcode, val): |
| 294 |
return opcode + chr(val) |
| 295 |
|
| 296 |
def append_pack_int(builder, opcode, val, length, extra=None): |
| 297 |
builder.append(opcode) |
| 298 |
for i in range(length): |
| 299 |
builder.append(chr(val & 0xff)) |
| 300 |
val >>= 8 |
| 301 |
if extra: |
| 302 |
builder.append(extra) |
| 303 |
|
| 304 |
|
| 305 |
def pack_float(f): |
| 306 |
|
| 307 |
buf = MutableStringBuffer(8) |
| 308 |
ieee.pack_float(buf, 0, f, 8, True) |
| 309 |
return buf.finish() |
| 310 |
|
| 311 |
def unpack_float(s): |
| 312 |
return ieee.unpack_float(s, True) |
| 313 |
|
| 314 |
def pickling_error(space): |
| 315 |
w_module = space.getbuiltinmodule('_pickle') |
| 316 |
return space.getattr(w_module, space.newtext('PicklingError')) |
| 317 |
|
| 318 |
def unpickling_error(space): |
| 319 |
w_module = space.getbuiltinmodule('_pickle') |
| 320 |
return space.getattr(w_module, space.newtext('UnpicklingError')) |
| 321 |
|
| 322 |
def data_pop(space, stack): |
| 323 |
if len(stack) >= 1: |
| 324 |
return stack.pop() |
| 325 |
raise oefmt(unpickling_error(space), |
| 326 |
"unexpected MARK found") |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
class _Stop(Exception): |
| 331 |
def __init__(self, value): |
| 332 |
self.value = value |
| 333 |
|
| 334 |
class _Framer(object): |
| 335 |
|
| 336 |
_FRAME_SIZE_MIN = 4 |
| 337 |
_FRAME_SIZE_TARGET = 64 * 1024 |
| 338 |
|
| 339 |
def __init__(self, space, w_file): |
| 340 |
self.w_file = w_file |
| 341 |
self.space = space |
| 342 |
self.current_frame = None |
| 343 |
|
| 344 |
def start_framing(self): |
| 345 |
self.current_frame = StringBuilder() |
| 346 |
|
| 347 |
def end_framing(self): |
| 348 |
if self.current_frame and self.current_frame.getlength() > 0: |
| 349 |
self.commit_frame(force=True) |
| 350 |
self.current_frame = None |
| 351 |
|
| 352 |
def file_write(self, data): |
| 353 |
self.space.call_method(self.w_file, 'write', self.space.newbytes(data)) |
| 354 |
return len(data) |
| 355 |
|
| 356 |
def commit_frame(self, force=False): |
| 357 |
if self.current_frame is not None: |
| 358 |
f = self.current_frame |
| 359 |
if f.getlength() >= self._FRAME_SIZE_TARGET or force: |
| 360 |
data = f.build() |
| 361 |
write = self.file_write |
| 362 |
if len(data) >= self._FRAME_SIZE_MIN: |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
write(packQ(op.FRAME, len(data))) |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
write(data) |
| 373 |
|
| 374 |
self.current_frame = StringBuilder() |
| 375 |
|
| 376 |
def write(self, data): |
| 377 |
if self.current_frame is not None: |
| 378 |
self.current_frame.append(data) |
| 379 |
return len(data) |
| 380 |
else: |
| 381 |
return self.file_write(data) |
| 382 |
|
| 383 |
def write_large_bytes(self, header, payload): |
| 384 |
write = self.file_write |
| 385 |
if self.current_frame: |
| 386 |
|
| 387 |
self.commit_frame(force=True) |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
write(header) |
| 396 |
write(payload) |
| 397 |
|
| 398 |
def write_packB(self, opcode, val, extra=None): |
| 399 |
if self.current_frame is not None: |
| 400 |
self.current_frame.append(opcode) |
| 401 |
self.current_frame.append(chr(val)) |
| 402 |
res = 2 |
| 403 |
if extra is not None: |
| 404 |
self.current_frame.append(extra) |
| 405 |
res += len(extra) |
| 406 |
return res |
| 407 |
else: |
| 408 |
data = packB(opcode, val) |
| 409 |
if extra: |
| 410 |
data += extra |
| 411 |
return self.file_write(data) |
| 412 |
|
| 413 |
def write_packH(self, opcode, val, extra=None): |
| 414 |
if self.current_frame is not None: |
| 415 |
self.current_frame.append(opcode) |
| 416 |
self.current_frame.append(chr(val & 0xff)) |
| 417 |
self.current_frame.append(chr((val >> 8) & 0xff)) |
| 418 |
res = 3 |
| 419 |
if extra is not None: |
| 420 |
self.current_frame.append(extra) |
| 421 |
res += len(extra) |
| 422 |
return res |
| 423 |
else: |
| 424 |
data = packH(opcode, val) |
| 425 |
if extra: |
| 426 |
data += extra |
| 427 |
return self.file_write(data) |
| 428 |
|
| 429 |
@jit.unroll_safe |
| 430 |
def write_binfloat(self, f): |
| 431 |
"""Write the BINFLOAT opcode + 8-byte big-endian IEEE 754 double. |
| 432 |
Avoids MutableStringBuffer + finish() + string concat from pack_float(). |
| 433 |
""" |
| 434 |
unsigned = float_pack(f, 8) |
| 435 |
value = rarithmetic.longlongmask(unsigned) |
| 436 |
if self.current_frame is not None: |
| 437 |
self.current_frame.append(op.BINFLOAT) |
| 438 |
for i in range(7, -1, -1): |
| 439 |
self.current_frame.append(chr((value >> (i * 8)) & 0xFF)) |
| 440 |
else: |
| 441 |
buf = StringBuilder(9) |
| 442 |
buf.append(op.BINFLOAT) |
| 443 |
for i in range(7, -1, -1): |
| 444 |
buf.append(chr((value >> (i * 8)) & 0xFF)) |
| 445 |
self.file_write(buf.build()) |
| 446 |
|
| 447 |
def write_packi(self, opcode, val, extra=None): |
| 448 |
if self.current_frame is not None: |
| 449 |
append_pack_int(self.current_frame, opcode, val, 4, extra) |
| 450 |
else: |
| 451 |
data = packi(opcode, val) |
| 452 |
if extra: |
| 453 |
data += extra |
| 454 |
self.file_write(data) |
| 455 |
|
| 456 |
def write_packI(self, opcode, val, extra=None): |
| 457 |
if self.current_frame is not None: |
| 458 |
append_pack_int(self.current_frame, opcode, val, 4, extra) |
| 459 |
else: |
| 460 |
data = packI(opcode, val) |
| 461 |
if extra: |
| 462 |
data += extra |
| 463 |
self.file_write(data) |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
def spacenext(space, w_it): |
| 468 |
try: |
| 469 |
return space.next(w_it) |
| 470 |
except OperationError as e: |
| 471 |
if not e.match(space, space.w_StopIteration): |
| 472 |
raise |
| 473 |
return None |
| 474 |
|
| 475 |
class DispatchCache(object): |
| 476 |
def __init__(self, space): |
| 477 |
self.dispatch = {} |
| 478 |
self.dispatch[space.w_NoneType] = save_none |
| 479 |
self.dispatch[space.w_bool] = save_bool |
| 480 |
self.dispatch[space.w_int] = save_long |
| 481 |
self.dispatch[space.w_float] = save_float |
| 482 |
self.dispatch[space.w_unicode] = save_str |
| 483 |
self.dispatch[space.w_bytes] = save_bytes |
| 484 |
self.dispatch[space.w_tuple] = save_tuple |
| 485 |
self.dispatch[space.w_list] = save_list |
| 486 |
self.dispatch[space.w_dict] = save_dict |
| 487 |
self.dispatch[space.w_bytearray] = save_bytearray |
| 488 |
w_function = space.type(space.getattr(space.w_text, space.newtext("count"))) |
| 489 |
self.dispatch[w_function] = save_global |
| 490 |
self.dispatch4 = self.dispatch.copy() |
| 491 |
self.dispatch4[space.w_frozenset] = save_frozenset |
| 492 |
|
| 493 |
class W_Pickler(W_Root): |
| 494 |
""" |
| 495 |
This takes a binary file for writing a pickle data stream. |
| 496 |
|
| 497 |
The optional *protocol* argument tells the pickler to use the given |
| 498 |
protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default |
| 499 |
protocol is 4. It was introduced in Python 3.4, and is incompatible |
| 500 |
with previous versions. |
| 501 |
|
| 502 |
Specifying a negative protocol version selects the highest protocol |
| 503 |
version supported. The higher the protocol used, the more recent the |
| 504 |
version of Python needed to read the pickle produced. |
| 505 |
|
| 506 |
The *file* argument must have a write() method that accepts a single |
| 507 |
bytes argument. It can thus be a file object opened for binary |
| 508 |
writing, an io.BytesIO instance, or any other custom object that meets |
| 509 |
this interface. |
| 510 |
|
| 511 |
If *fix_imports* is True and protocol is less than 3, pickle will try |
| 512 |
to map the new Python 3 names to the old module names used in Python |
| 513 |
2, so that the pickle data stream is readable with Python 2. |
| 514 |
|
| 515 |
If *buffer_callback* is None (the default), buffer views are |
| 516 |
serialized into *file* as part of the pickle stream. |
| 517 |
|
| 518 |
If *buffer_callback* is not None, then it can be called any number |
| 519 |
of times with a buffer view. If the callback returns a false value |
| 520 |
(such as None), the given buffer is out-of-band; otherwise the |
| 521 |
buffer is serialized in-band, i.e. inside the pickle stream. |
| 522 |
|
| 523 |
It is an error if *buffer_callback* is not None and *protocol* |
| 524 |
is None or smaller than 5. |
| 525 |
""" |
| 526 |
def __init__(self, space, w_file=None, protocol=4, fix_imports=1, |
| 527 |
buffer_callback=None): |
| 528 |
self.space = space |
| 529 |
self.w_file = w_file |
| 530 |
self.protocol = protocol |
| 531 |
self.fix_imports = fix_imports |
| 532 |
if buffer_callback is None or space.is_none(buffer_callback): |
| 533 |
self.buffer_callback = None |
| 534 |
else: |
| 535 |
self.buffer_callback = buffer_callback |
| 536 |
self.memo = {} |
| 537 |
self.str_memo = {} |
| 538 |
self.bytes_memo = {} |
| 539 |
self.memo_size = 0 |
| 540 |
self.proto = protocol |
| 541 |
self.bin = protocol >= 1 |
| 542 |
self.fast = 0 |
| 543 |
self.pypy_extensions = False |
| 544 |
self.pers_func = None |
| 545 |
self.w_dispatch_table = None |
| 546 |
self.w_reducer_override = None |
| 547 |
self.w_function = space.type(space.getattr(space.w_text, space.newtext("count"))) |
| 548 |
cache = space.fromcache(DispatchCache) |
| 549 |
self.dispatch = cache.dispatch |
| 550 |
if self.proto >= 4: |
| 551 |
self.dispatch = cache.dispatch4 |
| 552 |
if w_file is not None and not space.is_none(w_file): |
| 553 |
try: |
| 554 |
space.getattr(w_file, space.newtext('write')) |
| 555 |
except OperationError as e: |
| 556 |
if e.match(space, space.w_AttributeError): |
| 557 |
raise oefmt(space.w_TypeError, "file must have a 'write' attribute") |
| 558 |
raise |
| 559 |
self.framer = _Framer(space, w_file) |
| 560 |
else: |
| 561 |
self.framer = None |
| 562 |
|
| 563 |
w_pers = space.findattr(self, space.newtext('persistent_id')) |
| 564 |
if w_pers is not None: |
| 565 |
self.pers_func = w_pers |
| 566 |
w_ro = space.findattr(self, space.newtext('reducer_override')) |
| 567 |
if w_ro is not None: |
| 568 |
self.w_reducer_override = w_ro |
| 569 |
|
| 570 |
def write(self, data): |
| 571 |
return self.framer.write(data) |
| 572 |
|
| 573 |
def write_packB(self, opcode, val, extra=None): |
| 574 |
return self.framer.write_packB(opcode, val, extra) |
| 575 |
|
| 576 |
def write_packH(self, opcode, val, extra=None): |
| 577 |
return self.framer.write_packH(opcode, val, extra) |
| 578 |
|
| 579 |
def write_binfloat(self, f): |
| 580 |
self.framer.write_binfloat(f) |
| 581 |
|
| 582 |
def write_packi(self, opcode, val, extra=None): |
| 583 |
self.framer.write_packi(opcode, val, extra) |
| 584 |
|
| 585 |
def write_packI(self, opcode, val, extra=None): |
| 586 |
self.framer.write_packI(opcode, val, extra) |
| 587 |
|
| 588 |
def _write_large_bytes(self, arg0, arg1): |
| 589 |
return self.framer.write_large_bytes(arg0, arg1) |
| 590 |
|
| 591 |
def dump(self, w_obj): |
| 592 |
"""Write a pickled representation of obj to the open file.""" |
| 593 |
if self.framer is None: |
| 594 |
raise oefmt(pickling_error(self.space), "Pickler.__init__() was not called") |
| 595 |
jitdriver_dump.jit_merge_point(typ=type(w_obj)) |
| 596 |
jit.promote(self.proto) |
| 597 |
jit.promote(self.bin) |
| 598 |
space = self.space |
| 599 |
if self.proto >= 2: |
| 600 |
self.write_packB(op.PROTO, self.proto) |
| 601 |
if self.proto >= 4: |
| 602 |
self.framer.start_framing() |
| 603 |
self.save(w_obj) |
| 604 |
self.write(op.STOP) |
| 605 |
self.framer.end_framing() |
| 606 |
|
| 607 |
def save(self, w_obj, save_persistent_id=True): |
| 608 |
space = self.space |
| 609 |
self.framer.commit_frame() |
| 610 |
|
| 611 |
if save_persistent_id and self.pers_func is not None: |
| 612 |
w_pid = space.call_function(self.pers_func, w_obj) |
| 613 |
if not space.is_none(w_pid): |
| 614 |
self.save_pers(w_pid) |
| 615 |
return |
| 616 |
|
| 617 |
w_type = space.type(w_obj) |
| 618 |
|
| 619 |
w_reduce = space.w_None |
| 620 |
w_rv = space.w_NotImplemented |
| 621 |
if self.w_reducer_override: |
| 622 |
w_reduce = self.w_reducer_override |
| 623 |
w_rv = space.call_function(w_reduce, w_obj) |
| 624 |
|
| 625 |
if w_rv is space.w_NotImplemented: |
| 626 |
|
| 627 |
if w_type is space.w_None: |
| 628 |
return save_none(self, w_obj) |
| 629 |
elif w_type is space.w_bool: |
| 630 |
return save_bool(self, w_obj) |
| 631 |
elif w_type is space.w_int: |
| 632 |
return save_long(self, w_obj) |
| 633 |
elif w_type is space.w_float: |
| 634 |
return save_float(self, w_obj) |
| 635 |
|
| 636 |
if w_rv is space.w_NotImplemented: |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
if w_type is space.w_text: |
| 641 |
x = self.str_memo.get(space.utf8_w(w_obj), -1) |
| 642 |
if x >= 0: |
| 643 |
self.write_get(x) |
| 644 |
return |
| 645 |
elif w_type is space.w_bytes: |
| 646 |
x = self.bytes_memo.get(space.bytes_w(w_obj), -1) |
| 647 |
if x >= 0: |
| 648 |
self.write_get(x) |
| 649 |
return |
| 650 |
|
| 651 |
|
| 652 |
x = self.memo.get(w_obj, -1) |
| 653 |
if x >= 0: |
| 654 |
self.write_get(x) |
| 655 |
return |
| 656 |
|
| 657 |
if w_type is space.w_bytes: |
| 658 |
return save_bytes(self, w_obj) |
| 659 |
elif w_type is space.w_text: |
| 660 |
return save_str(self, w_obj) |
| 661 |
|
| 662 |
|
| 663 |
if w_type is space.w_dict: |
| 664 |
return save_dict(self, w_obj) |
| 665 |
elif w_type is space.w_set: |
| 666 |
return save_set(self, w_obj) |
| 667 |
elif w_type is space.w_frozenset and self.proto >= 4: |
| 668 |
return save_frozenset(self, w_obj) |
| 669 |
elif w_type is space.w_list: |
| 670 |
return save_list(self, w_obj) |
| 671 |
elif w_type is space.w_tuple: |
| 672 |
return save_tuple(self, w_obj) |
| 673 |
elif w_type is space.w_bytearray: |
| 674 |
return save_bytearray(self, w_obj) |
| 675 |
elif isinstance(w_obj, W_PickleBuffer): |
| 676 |
return save_picklebuffer(self, w_obj) |
| 677 |
|
| 678 |
if w_rv is space.w_NotImplemented: |
| 679 |
|
| 680 |
f = self.dispatch.get(w_type) |
| 681 |
if f is not None: |
| 682 |
|
| 683 |
f(self, w_obj) |
| 684 |
return |
| 685 |
|
| 686 |
w_dispatch_table = self.w_dispatch_table |
| 687 |
if not w_dispatch_table: |
| 688 |
w_dispatch_table = space.findattr(self, space.newtext('dispatch_table')) |
| 689 |
if not w_dispatch_table: |
| 690 |
w_dispatch_table = space.fromcache(State).w_dispatch_table |
| 691 |
w_reduce = space.finditem(w_dispatch_table, w_type) |
| 692 |
if w_reduce: |
| 693 |
w_rv = space.call_function(w_reduce, w_obj) |
| 694 |
else: |
| 695 |
|
| 696 |
|
| 697 |
if space.issubtype_w(w_type, space.w_type): |
| 698 |
|
| 699 |
if space.is_w(w_obj, space.type(space.w_None)): |
| 700 |
self.save_reduce(space.w_type, |
| 701 |
space.newtuple([space.w_None]), w_obj=w_obj) |
| 702 |
elif space.is_w(w_obj, space.type(space.w_NotImplemented)): |
| 703 |
self.save_reduce(space.w_type, |
| 704 |
space.newtuple([space.w_NotImplemented]), w_obj=w_obj) |
| 705 |
elif space.is_w(w_obj, space.type(space.w_Ellipsis)): |
| 706 |
self.save_reduce(space.w_type, |
| 707 |
space.newtuple([space.w_Ellipsis]), w_obj=w_obj) |
| 708 |
else: |
| 709 |
self.save_global2(w_obj, None) |
| 710 |
return |
| 711 |
|
| 712 |
|
| 713 |
if w_type is self.w_function: |
| 714 |
save_global(self, w_obj) |
| 715 |
return |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
w_reduce = space.findattr(w_obj, space.newtext("__reduce_ex__")) |
| 720 |
if w_reduce is not None: |
| 721 |
w_rv = space.call_function(w_reduce, space.newint(self.proto)) |
| 722 |
else: |
| 723 |
w_reduce = space.findattr(w_obj, space.newtext("__reduce__")) |
| 724 |
if w_reduce is not None: |
| 725 |
w_rv = space.call_function(w_reduce) |
| 726 |
else: |
| 727 |
raise oefmt(pickling_error(space), "Can't pickle %T object: %R", w_obj, w_obj) |
| 728 |
|
| 729 |
if space.isinstance_w(w_rv, space.w_unicode): |
| 730 |
self.save_global2(w_obj, w_rv) |
| 731 |
return |
| 732 |
|
| 733 |
|
| 734 |
if not space.isinstance_w(w_rv, space.w_tuple): |
| 735 |
raise oefmt(pickling_error(space), "%S must return string or tuple", w_reduce) |
| 736 |
|
| 737 |
|
| 738 |
rv_w = space.fixedview(w_rv) |
| 739 |
length = len(rv_w) |
| 740 |
if not (2 <= length <= 6): |
| 741 |
raise oefmt(pickling_error(space), "Tuple returned by %S must have " |
| 742 |
"two to six elements", w_reduce) |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
if length == 2: |
| 748 |
self.save_reduce(rv_w[0], rv_w[1], w_obj=w_obj) |
| 749 |
if length == 3: |
| 750 |
self.save_reduce(rv_w[0], rv_w[1], rv_w[2], w_obj=w_obj) |
| 751 |
if length == 4: |
| 752 |
self.save_reduce(rv_w[0], rv_w[1], rv_w[2], rv_w[3], w_obj=w_obj) |
| 753 |
if length == 5: |
| 754 |
self.save_reduce(rv_w[0], rv_w[1], rv_w[2], rv_w[3], rv_w[4], w_obj=w_obj) |
| 755 |
if length == 6: |
| 756 |
self.save_reduce(rv_w[0], rv_w[1], rv_w[2], rv_w[3], rv_w[4], rv_w[5], w_obj=w_obj) |
| 757 |
|
| 758 |
_BATCHSIZE = 1000 |
| 759 |
|
| 760 |
def _batch_appends_raw_str(self, items, value_memo, write_item): |
| 761 |
"""Fast path for list strategies storing raw RPython strings. |
| 762 |
|
| 763 |
items - list of RPython str from listview_ascii / listview_bytes |
| 764 |
value_memo - self.str_memo or self.bytes_memo |
| 765 |
write_item - callable(self, s) that writes the pickle opcode + data |
| 766 |
""" |
| 767 |
write = self.write |
| 768 |
n_items = len(items) |
| 769 |
if n_items == 0: |
| 770 |
return |
| 771 |
if n_items == 1: |
| 772 |
s = items[0] |
| 773 |
x = value_memo.get(s, -1) |
| 774 |
if x >= 0: |
| 775 |
self.write_get(x) |
| 776 |
else: |
| 777 |
write_item(self, s) |
| 778 |
value_memo[s] = self.memo_size |
| 779 |
self.memo_size += 1 |
| 780 |
write(op.MEMOIZE) |
| 781 |
write(op.APPEND) |
| 782 |
return |
| 783 |
i = 0 |
| 784 |
while i < n_items: |
| 785 |
batch_end = min(i + self._BATCHSIZE, n_items) |
| 786 |
write(op.MARK) |
| 787 |
while i < batch_end: |
| 788 |
s = items[i] |
| 789 |
x = value_memo.get(s, -1) |
| 790 |
if x >= 0: |
| 791 |
self.write_get(x) |
| 792 |
else: |
| 793 |
write_item(self, s) |
| 794 |
value_memo[s] = self.memo_size |
| 795 |
self.memo_size += 1 |
| 796 |
write(op.MEMOIZE) |
| 797 |
i += 1 |
| 798 |
write(op.APPENDS) |
| 799 |
|
| 800 |
def _batch_appends(self, w_list): |
| 801 |
space = self.space |
| 802 |
|
| 803 |
save = self.save |
| 804 |
write = self.write |
| 805 |
|
| 806 |
if not self.bin: |
| 807 |
for w_x in space.listview(w_list): |
| 808 |
save(w_x) |
| 809 |
write(op.APPEND) |
| 810 |
return |
| 811 |
|
| 812 |
|
| 813 |
ascii_items = space.listview_ascii(w_list) |
| 814 |
if ascii_items is not None: |
| 815 |
self._batch_appends_raw_str(ascii_items, self.str_memo, |
| 816 |
_write_ascii_item) |
| 817 |
return |
| 818 |
|
| 819 |
|
| 820 |
if self.proto >= 3: |
| 821 |
bytes_items = space.listview_bytes(w_list) |
| 822 |
if bytes_items is not None: |
| 823 |
self._batch_appends_raw_str(bytes_items, self.bytes_memo, |
| 824 |
_write_bytes_item) |
| 825 |
return |
| 826 |
|
| 827 |
w_it = space.iter(w_list) |
| 828 |
while True: |
| 829 |
w_firstitem = spacenext(space, w_it) |
| 830 |
if w_firstitem is None: |
| 831 |
return |
| 832 |
w_item = spacenext(space, w_it) |
| 833 |
if w_item is None: |
| 834 |
|
| 835 |
self.save(w_firstitem) |
| 836 |
self.write(op.APPEND) |
| 837 |
return |
| 838 |
|
| 839 |
|
| 840 |
self.write(op.MARK) |
| 841 |
n = 1 |
| 842 |
self.save(w_firstitem) |
| 843 |
greenkey = space.iterator_greenkey(w_list) |
| 844 |
while 1: |
| 845 |
self.save(w_item) |
| 846 |
n += 1 |
| 847 |
if n == self._BATCHSIZE: |
| 848 |
break |
| 849 |
jitdriver_appends.jit_merge_point(typ=type(w_item), greenkey=greenkey) |
| 850 |
w_item = spacenext(space, w_it) |
| 851 |
if not w_item: |
| 852 |
break |
| 853 |
self.write(op.APPENDS) |
| 854 |
|
| 855 |
def _batch_setitems(self, w_items): |
| 856 |
def savetup2(self, w_tup): |
| 857 |
space = self.space |
| 858 |
w_k, w_v = space.unpackiterable(w_tup, 2) |
| 859 |
self.save(w_k) |
| 860 |
self.save(w_v) |
| 861 |
return w_k |
| 862 |
|
| 863 |
|
| 864 |
space = self.space |
| 865 |
save = self.save |
| 866 |
write = self.write |
| 867 |
|
| 868 |
w_it = space.iter(w_items) |
| 869 |
if not self.bin: |
| 870 |
w_item = spacenext(space, w_it) |
| 871 |
if w_item is None: |
| 872 |
return |
| 873 |
while True: |
| 874 |
w_k, w_v = space.unpackiterable(w_item, 2) |
| 875 |
save(w_k) |
| 876 |
save(w_v) |
| 877 |
write(op.SETITEM) |
| 878 |
w_item = spacenext(space, w_it) |
| 879 |
if not w_item: |
| 880 |
break |
| 881 |
return |
| 882 |
|
| 883 |
while True: |
| 884 |
w_firstitem = spacenext(space, w_it) |
| 885 |
if w_firstitem is None: |
| 886 |
return |
| 887 |
w_item = spacenext(space, w_it) |
| 888 |
if w_item is None: |
| 889 |
savetup2(self, w_firstitem) |
| 890 |
self.write(op.SETITEM) |
| 891 |
return |
| 892 |
|
| 893 |
|
| 894 |
self.write(op.MARK) |
| 895 |
n = 1 |
| 896 |
savetup2(self, w_firstitem) |
| 897 |
greenkey = space.iterator_greenkey(w_items) |
| 898 |
while 1: |
| 899 |
w_k = savetup2(self, w_item) |
| 900 |
n += 1 |
| 901 |
if n == self._BATCHSIZE: |
| 902 |
break |
| 903 |
jitdriver_setitems.jit_merge_point(typ=type(w_k), greenkey=greenkey) |
| 904 |
w_item = spacenext(space, w_it) |
| 905 |
if not w_item: |
| 906 |
break |
| 907 |
self.write(op.SETITEMS) |
| 908 |
|
| 909 |
def save_reduce(self, w_func, w_args, w_state=None, w_listitems=None, |
| 910 |
w_dictitems=None, w_state_setter=None, __kwargs__=None, w_obj=None): |
| 911 |
space = self.space |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
if not space.isinstance_w(w_args, space.w_tuple): |
| 916 |
raise oefmt(pickling_error(space), "args from save_reduce() must be a tuple") |
| 917 |
if not space.callable_w(w_func): |
| 918 |
raise oefmt(pickling_error(space), "func from save_reduce() must be callable") |
| 919 |
|
| 920 |
save = self.save |
| 921 |
write = self.write |
| 922 |
|
| 923 |
w_func_name = space.findattr(w_func, space.newtext("__name__")) |
| 924 |
if self.proto >= 2 and w_func_name and space.eq_w(w_func_name, space.newtext("__newobj_ex__")): |
| 925 |
w_cls, w_args, w_kwargs = space.unpackiterable(w_args, 3) |
| 926 |
w_new = space.findattr(w_cls, space.newtext("__new__")) |
| 927 |
if not w_new: |
| 928 |
raise oefmt(pickling_error(space), "args[0] from %S args has no __new__", w_func_name) |
| 929 |
if w_obj is not None and not space.is_w(w_cls, space.getattr(w_obj, space.newtext('__class__'))): |
| 930 |
raise oefmt(pickling_error(space), "args[0] from %S args has the wrong class", w_func_name) |
| 931 |
if self.proto >= 4: |
| 932 |
save(w_cls) |
| 933 |
save(w_args) |
| 934 |
save(w_kwargs) |
| 935 |
write(op.NEWOBJ_EX) |
| 936 |
else: |
| 937 |
args_w = space.listview(w_args) |
| 938 |
w_full_args = space.newtuple([w_cls] + args_w) |
| 939 |
if space.len_w(w_kwargs) > 0: |
| 940 |
w_partial = space.fromcache(State).w_partial |
| 941 |
w_partial_args_list = space.newlist([w_new]) |
| 942 |
w_func_with_kwargs = space.call(w_partial, w_partial_args_list, w_kwargs) |
| 943 |
save(w_func_with_kwargs) |
| 944 |
else: |
| 945 |
save(w_new) |
| 946 |
save(w_full_args) |
| 947 |
write(op.REDUCE) |
| 948 |
elif self.proto >= 2 and w_func_name and space.eq_w(w_func_name, space.newtext("__newobj__")): |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
args_w = space.fixedview(w_args) |
| 976 |
if not args_w: |
| 977 |
raise oefmt(pickling_error(space), "__newobj__ requires at least one arg") |
| 978 |
w_cls = args_w[0] |
| 979 |
w_new = space.findattr(w_cls, space.newtext("__new__")) |
| 980 |
if not w_new: |
| 981 |
raise oefmt(pickling_error(space), |
| 982 |
"args[0] from __newobj__ args has no __new__") |
| 983 |
if w_obj is not None and not space.is_w(w_cls, space.getattr(w_obj, space.newtext('__class__'))): |
| 984 |
raise oefmt(pickling_error(space), |
| 985 |
"args[0] from __newobj__ args has the wrong class") |
| 986 |
w_newargs = space.newtuple(args_w[1:]) |
| 987 |
self.save(w_cls) |
| 988 |
self.save(w_newargs) |
| 989 |
self.write(op.NEWOBJ) |
| 990 |
else: |
| 991 |
save(w_func) |
| 992 |
save(w_args) |
| 993 |
write(op.REDUCE) |
| 994 |
|
| 995 |
if w_obj is not None: |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
if w_obj in self.memo: |
| 1000 |
write(op.POP + self.get(self.memo[w_obj])) |
| 1001 |
else: |
| 1002 |
self.memoize(w_obj) |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
if not space.is_none(w_listitems): |
| 1010 |
self._batch_appends(w_listitems) |
| 1011 |
|
| 1012 |
if not space.is_none(w_dictitems): |
| 1013 |
self._batch_setitems(w_dictitems) |
| 1014 |
|
| 1015 |
if not space.is_none(w_state): |
| 1016 |
if w_state_setter is None: |
| 1017 |
save(w_state) |
| 1018 |
write(op.BUILD) |
| 1019 |
else: |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
save(w_state_setter) |
| 1025 |
save(w_obj) |
| 1026 |
save(w_state) |
| 1027 |
write(op.TUPLE2) |
| 1028 |
|
| 1029 |
write(op.REDUCE) |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
write(op.POP) |
| 1035 |
|
| 1036 |
def save_global2(self, w_obj, w_name): |
| 1037 |
space = self.space |
| 1038 |
write = self.write |
| 1039 |
memo = self.memo |
| 1040 |
|
| 1041 |
if space.is_none(w_name): |
| 1042 |
w_name = space.findattr(w_obj, space.newtext('__qualname__')) |
| 1043 |
if space.is_none(w_name): |
| 1044 |
w_name = space.getattr(w_obj, space.newtext('__name__')) |
| 1045 |
|
| 1046 |
w_module_name = whichmodule(space, w_obj, w_name) |
| 1047 |
try: |
| 1048 |
w_import = space.getattr(space.builtin, space.newtext("__import__")) |
| 1049 |
space.call_function(w_import, w_module_name) |
| 1050 |
w_module = space.getitem(space.getattr(space.sys, space.newtext('modules')), w_module_name) |
| 1051 |
w_obj2, w_parent = space.unpackiterable(_getattribute(space, w_module, w_name), 2) |
| 1052 |
except OperationError as e: |
| 1053 |
if (not e.match(space, space.w_ImportError) and |
| 1054 |
not e.match(space, space.w_KeyError) and |
| 1055 |
not e.match(space, space.w_AttributeError)): |
| 1056 |
raise |
| 1057 |
raise oefmt(pickling_error(space), |
| 1058 |
"Can't pickle %R: it's not found as %S.%S", |
| 1059 |
w_obj, w_module_name, w_name) |
| 1060 |
else: |
| 1061 |
if not space.is_w(w_obj2, w_obj): |
| 1062 |
raise oefmt(pickling_error(space), |
| 1063 |
"Can't pickle %R: it's not the same object as %S.%S", |
| 1064 |
w_obj, w_module_name, w_name) |
| 1065 |
|
| 1066 |
if self.proto >= 2: |
| 1067 |
w_mod_and_name = space.newtuple([w_module_name, w_name]) |
| 1068 |
state = space.fromcache(State) |
| 1069 |
w_code = space.finditem(state.w_extension_registry, w_mod_and_name) |
| 1070 |
if not space.is_none(w_code): |
| 1071 |
code = space.int_w(w_code) |
| 1072 |
assert code > 0 |
| 1073 |
if code <= 0xff: |
| 1074 |
self.write_packB(op.EXT1, code) |
| 1075 |
elif code <= 0xffff: |
| 1076 |
self.write_packH(op.EXT2, code) |
| 1077 |
else: |
| 1078 |
self.write_packi(op.EXT4, code) |
| 1079 |
return |
| 1080 |
name = space.utf8_w(w_name) |
| 1081 |
lastname = name.split('.')[-1] |
| 1082 |
if space.is_w(w_parent, w_module): |
| 1083 |
w_name = space.newtext(lastname) |
| 1084 |
name = lastname |
| 1085 |
|
| 1086 |
if self.proto >= 4: |
| 1087 |
self.save(w_module_name) |
| 1088 |
self.save(w_name) |
| 1089 |
write(op.STACK_GLOBAL) |
| 1090 |
elif not space.is_w(w_parent, w_module): |
| 1091 |
self.save_reduce(space.getattr(space.builtin, space.newtext('getattr')), |
| 1092 |
space.newtuple2(w_parent, space.newtext(lastname))) |
| 1093 |
elif self.proto >= 3: |
| 1094 |
module_name = space.utf8_w(w_module_name) |
| 1095 |
write(op.GLOBAL + module_name + b'\n' + |
| 1096 |
name + b'\n') |
| 1097 |
else: |
| 1098 |
if self.fix_imports: |
| 1099 |
w_modname_and_name = space.newtuple([w_module_name, w_name]) |
| 1100 |
w_r_NAME_MAPPING = space.fromcache(State).w_REVERSE_NAME_MAPPING |
| 1101 |
w_r_IMPORT_MAPPING = space.fromcache(State).w_REVERSE_IMPORT_MAPPING |
| 1102 |
w_1 = space.finditem(w_r_NAME_MAPPING, w_modname_and_name) |
| 1103 |
w_2 = space.finditem(w_r_IMPORT_MAPPING, w_module_name) |
| 1104 |
if w_1: |
| 1105 |
w_module_name, w_name = space.listview(w_1) |
| 1106 |
elif w_2: |
| 1107 |
w_module_name = w_2 |
| 1108 |
try: |
| 1109 |
module_name = space.utf8_w(w_module_name) |
| 1110 |
name = space.utf8_w(w_name) |
| 1111 |
write(op.GLOBAL + module_name + b'\n' + |
| 1112 |
name + b'\n') |
| 1113 |
except UnicodeEncodeError: |
| 1114 |
raise oefmt(pickling_error(space), |
| 1115 |
"can't pickle global identifier '%S.%S' using " |
| 1116 |
"pickle protocol %d", w_module, w_name, self.proto) |
| 1117 |
self.memoize(w_obj) |
| 1118 |
|
| 1119 |
def memoize(self, w_obj): |
| 1120 |
"""Store an object in the memo.""" |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
if self.fast: |
| 1131 |
return |
| 1132 |
assert w_obj not in self.memo |
| 1133 |
idx = self.memo_size |
| 1134 |
self.memo_size += 1 |
| 1135 |
self.memo[w_obj] = idx |
| 1136 |
space = self.space |
| 1137 |
w_type = space.type(w_obj) |
| 1138 |
if w_type is space.w_text: |
| 1139 |
self.str_memo[space.utf8_w(w_obj)] = idx |
| 1140 |
elif w_type is space.w_bytes: |
| 1141 |
self.bytes_memo[space.bytes_w(w_obj)] = idx |
| 1142 |
|
| 1143 |
if self.proto >= 4: |
| 1144 |
self.write(op.MEMOIZE) |
| 1145 |
elif self.bin: |
| 1146 |
if idx < 256: |
| 1147 |
self.write_packB(op.BINPUT, idx) |
| 1148 |
else: |
| 1149 |
self.write_packI(op.LONG_BINPUT, idx) |
| 1150 |
else: |
| 1151 |
self.write("%s%d\n" % (op.PUT, idx)) |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
def put(self, idx): |
| 1156 |
if self.proto >= 4: |
| 1157 |
return op.MEMOIZE |
| 1158 |
elif self.bin: |
| 1159 |
if idx < 256: |
| 1160 |
return packB(op.BINPUT, idx) |
| 1161 |
else: |
| 1162 |
return packI(op.LONG_BINPUT, idx) |
| 1163 |
else: |
| 1164 |
return "%s%d\n" %(op.PUT, idx) |
| 1165 |
|
| 1166 |
|
| 1167 |
def get(self, idx): |
| 1168 |
if self.bin: |
| 1169 |
if idx < 256: |
| 1170 |
return packB(op.BINGET, idx) |
| 1171 |
else: |
| 1172 |
return packI(op.LONG_BINGET, idx) |
| 1173 |
|
| 1174 |
return "%s%d\n" %(op.GET, idx) |
| 1175 |
|
| 1176 |
def write_get(self, idx): |
| 1177 |
"""Write a GET opcode directly, avoiding an intermediate bytes object.""" |
| 1178 |
if self.bin: |
| 1179 |
if idx < 256: |
| 1180 |
self.write_packB(op.BINGET, idx) |
| 1181 |
else: |
| 1182 |
self.write_packI(op.LONG_BINGET, idx) |
| 1183 |
else: |
| 1184 |
self.write("%s%d\n" % (op.GET, idx)) |
| 1185 |
|
| 1186 |
def get_pers_func_w(self, space): |
| 1187 |
if self.pers_func is None: |
| 1188 |
raise oefmt_attribute_error(space, self, space.newtext('persistent_id'), |
| 1189 |
"'%T' object has no attribute %R") |
| 1190 |
return self.pers_func |
| 1191 |
|
| 1192 |
def set_pers_func_w(self, space, w_obj): |
| 1193 |
self.pers_func = w_obj |
| 1194 |
|
| 1195 |
def del_pers_func_w(self, space): |
| 1196 |
self.pers_func = None |
| 1197 |
|
| 1198 |
def get_reducer_override_w(self, space): |
| 1199 |
if self.w_reducer_override is None: |
| 1200 |
raise oefmt_attribute_error(space, self, space.newtext('reducer_override'), |
| 1201 |
"'%T' object has no attribute %R") |
| 1202 |
return self.w_reducer_override |
| 1203 |
|
| 1204 |
def set_reducer_override_w(self, space, w_obj): |
| 1205 |
self.w_reducer_override = w_obj |
| 1206 |
|
| 1207 |
def del_reducer_override_w(self, space): |
| 1208 |
self.w_reducer_override = None |
| 1209 |
|
| 1210 |
def set_dispatch_table_w(self, space, w_obj): |
| 1211 |
self.w_dispatch_table = w_obj |
| 1212 |
|
| 1213 |
def get_dispatch_table_w(self, space): |
| 1214 |
if self.w_dispatch_table: |
| 1215 |
return self.w_dispatch_table |
| 1216 |
raise oefmt_attribute_error(space, self, space.newtext('dispatch_table'), |
| 1217 |
"'%T' object has no attribute %R") |
| 1218 |
|
| 1219 |
def set_fast_w(self, space, w_val): |
| 1220 |
self.fast = space.int_w(w_val) |
| 1221 |
|
| 1222 |
def get_fast_w(self, space): |
| 1223 |
return space.newint(self.fast) |
| 1224 |
|
| 1225 |
def set_pypy_extensions_w(self, space, w_val): |
| 1226 |
self.pypy_extensions = space.is_true(w_val) |
| 1227 |
|
| 1228 |
def get_pypy_extensions_w(self, space): |
| 1229 |
return space.newbool(self.pypy_extensions) |
| 1230 |
|
| 1231 |
def save_pers(self, w_pid): |
| 1232 |
space = self.space |
| 1233 |
if self.bin: |
| 1234 |
self.save(w_pid, save_persistent_id=False) |
| 1235 |
self.write(op.BINPERSID) |
| 1236 |
else: |
| 1237 |
try: |
| 1238 |
pid_bytes = space.utf8_w(space.str(w_pid)) |
| 1239 |
except OperationError: |
| 1240 |
raise oefmt(pickling_error(space), |
| 1241 |
"persistent IDs in protocol 0 must be ASCII strings") |
| 1242 |
for c in pid_bytes: |
| 1243 |
if ord(c) > 127: |
| 1244 |
raise oefmt(pickling_error(space), |
| 1245 |
"persistent IDs in protocol 0 must be ASCII strings") |
| 1246 |
self.write(op.PERSID + pid_bytes + b'\n') |
| 1247 |
|
| 1248 |
def clear_memo(self): |
| 1249 |
self.memo.clear() |
| 1250 |
self.str_memo.clear() |
| 1251 |
self.bytes_memo.clear() |
| 1252 |
self.memo_size = 0 |
| 1253 |
|
| 1254 |
def get_memo_w(self, space): |
| 1255 |
|
| 1256 |
w_d = space.newdict() |
| 1257 |
for w_obj, idx in self.memo.items(): |
| 1258 |
space.setitem(w_d, space.newint(idx), w_obj) |
| 1259 |
return w_d |
| 1260 |
|
| 1261 |
def set_memo_w(self, space, w_memo): |
| 1262 |
self.memo = {} |
| 1263 |
self.str_memo = {} |
| 1264 |
self.bytes_memo = {} |
| 1265 |
w_items = space.call_method(w_memo, 'items') |
| 1266 |
w_iter = space.iter(w_items) |
| 1267 |
while True: |
| 1268 |
try: |
| 1269 |
w_item = space.next(w_iter) |
| 1270 |
except OperationError as e: |
| 1271 |
if not e.match(space, space.w_StopIteration): |
| 1272 |
raise |
| 1273 |
break |
| 1274 |
w_key, w_val = space.unpackiterable(w_item, 2) |
| 1275 |
idx = space.int_w(w_key) |
| 1276 |
self.memo[w_val] = idx |
| 1277 |
w_type = space.type(w_val) |
| 1278 |
if w_type is space.w_text: |
| 1279 |
self.str_memo[space.utf8_w(w_val)] = idx |
| 1280 |
elif w_type is space.w_bytes: |
| 1281 |
self.bytes_memo[space.bytes_w(w_val)] = idx |
| 1282 |
self.memo_size = len(self.memo) |
| 1283 |
|
| 1284 |
def save_global(self, w_obj): |
| 1285 |
return self.save_global2(w_obj, None) |
| 1286 |
|
| 1287 |
def save_none(self, w_obj): |
| 1288 |
self.write(op.NONE) |
| 1289 |
|
| 1290 |
def save_bool(self, w_obj): |
| 1291 |
space = self.space |
| 1292 |
if self.proto >= 2: |
| 1293 |
self.write(op.NEWTRUE if space.is_w(space.w_True, w_obj) else op.NEWFALSE) |
| 1294 |
else: |
| 1295 |
self.write(op.TRUE if space.is_w(space.w_True, w_obj) else op.FALSE) |
| 1296 |
|
| 1297 |
|
| 1298 |
def save_long(self, w_obj): |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
space = self.space |
| 1304 |
try: |
| 1305 |
obj = space.int_w(w_obj) |
| 1306 |
except OperationError as e: |
| 1307 |
if not e.match(space, space.w_OverflowError): |
| 1308 |
raise |
| 1309 |
else: |
| 1310 |
if self.bin: |
| 1311 |
if obj >= 0: |
| 1312 |
if obj <= 0xff: |
| 1313 |
self.write_packB(op.BININT1, obj) |
| 1314 |
return |
| 1315 |
if obj <= 0xffff: |
| 1316 |
self.write_packH(op.BININT2 , obj) |
| 1317 |
return |
| 1318 |
|
| 1319 |
|
| 1320 |
if -0x80000000 <= obj <= 0x7fffffff: |
| 1321 |
self.write_packi(op.BININT, obj) |
| 1322 |
return |
| 1323 |
if self.proto < 2: |
| 1324 |
as_ascii = space.utf8_w(space.repr(w_obj)) |
| 1325 |
if -0x80000000 <= obj <= 0x7fffffff: |
| 1326 |
self.write(op.INT + as_ascii + b'\n') |
| 1327 |
else: |
| 1328 |
self.write(op.LONG + as_ascii + b'L\n') |
| 1329 |
return |
| 1330 |
encoded = encode_long(space, w_obj) |
| 1331 |
n = len(encoded) |
| 1332 |
if n < 256: |
| 1333 |
self.write_packB(op.LONG1, n, encoded) |
| 1334 |
else: |
| 1335 |
self.write_packi(op.LONG4, n, encoded) |
| 1336 |
|
| 1337 |
def save_float(self, w_obj): |
| 1338 |
space = self.space |
| 1339 |
if self.bin: |
| 1340 |
obj = space.float_w(w_obj) |
| 1341 |
self.write_binfloat(obj) |
| 1342 |
else: |
| 1343 |
as_ascii = space.utf8_w(space.repr(w_obj)) |
| 1344 |
self.write(op.FLOAT + as_ascii + '\n') |
| 1345 |
|
| 1346 |
def save_bytes(self, w_obj): |
| 1347 |
space = self.space |
| 1348 |
n = space.len_w(w_obj) |
| 1349 |
if self.proto < 3: |
| 1350 |
if n < 1: |
| 1351 |
self.save_reduce(space.w_bytes, space.newtuple([]), w_obj=w_obj) |
| 1352 |
else: |
| 1353 |
w_import = space.getattr(space.builtin, space.newtext("__import__")) |
| 1354 |
w_codecs = space.call_function(w_import, space.newtext('codecs')) |
| 1355 |
w_encode = space.getattr(w_codecs, space.newtext('encode')) |
| 1356 |
w_decoded = space.call_method(w_obj, 'decode', space.newtext('latin1')) |
| 1357 |
self.save_reduce(w_encode, space.newtuple([w_decoded, space.newtext('latin1')]), w_obj=w_obj) |
| 1358 |
return |
| 1359 |
obj = space.bytes_w(w_obj) |
| 1360 |
save_raw_bytes(self, n, obj) |
| 1361 |
self.memoize(w_obj) |
| 1362 |
|
| 1363 |
def save_raw_bytes(self, n, obj): |
| 1364 |
if n <= 0xff: |
| 1365 |
self.write_packB(op.SHORT_BINBYTES, n, obj) |
| 1366 |
elif sys.maxint > 0x7fffffff and n > 0xffffffff and self.proto >= 4: |
| 1367 |
self._write_large_bytes(packQ(op.BINBYTES8, n), obj) |
| 1368 |
elif n >= self.framer._FRAME_SIZE_TARGET: |
| 1369 |
self._write_large_bytes(packI(op.BINBYTES, n), obj) |
| 1370 |
else: |
| 1371 |
self.write_packI(op.BINBYTES, n, obj) |
| 1372 |
|
| 1373 |
def _write_ascii_item(self, s): |
| 1374 |
"""Write a single ASCII/UTF-8 string item in the fast-path batch loop.""" |
| 1375 |
n = len(s) |
| 1376 |
if n <= 0xff and self.proto >= 4: |
| 1377 |
self.write_packB(op.SHORT_BINUNICODE, n, s) |
| 1378 |
elif sys.maxint > 0x7fffffff and n > 0xffffffff and self.proto >= 4: |
| 1379 |
self._write_large_bytes(packQ(op.BINUNICODE8, n), s) |
| 1380 |
elif n >= self.framer._FRAME_SIZE_TARGET: |
| 1381 |
self._write_large_bytes(packI(op.BINUNICODE, n), s) |
| 1382 |
else: |
| 1383 |
self.write_packI(op.BINUNICODE, n, s) |
| 1384 |
|
| 1385 |
def _write_bytes_item(self, s): |
| 1386 |
"""Write a single bytes item in the fast-path batch loop.""" |
| 1387 |
n = len(s) |
| 1388 |
if n <= 0xff: |
| 1389 |
self.write_packB(op.SHORT_BINBYTES, n, s) |
| 1390 |
elif sys.maxint > 0x7fffffff and n > 0xffffffff and self.proto >= 4: |
| 1391 |
self._write_large_bytes(packQ(op.BINBYTES8, n), s) |
| 1392 |
elif n >= self.framer._FRAME_SIZE_TARGET: |
| 1393 |
self._write_large_bytes(packI(op.BINBYTES, n), s) |
| 1394 |
else: |
| 1395 |
self.write_packI(op.BINBYTES, n, s) |
| 1396 |
|
| 1397 |
def save_str(self, w_obj): |
| 1398 |
space = self.space |
| 1399 |
if self.bin: |
| 1400 |
encoded = space.utf8_w(w_obj) |
| 1401 |
n = len(encoded) |
| 1402 |
if n <= 0xff and self.proto >= 4: |
| 1403 |
self.write_packB(op.SHORT_BINUNICODE, n, encoded) |
| 1404 |
elif sys.maxint > 0x7fffffff and n > 0xffffffff and self.proto >= 4: |
| 1405 |
self._write_large_bytes(packQ(op.BINUNICODE8, n), encoded) |
| 1406 |
elif n >= self.framer._FRAME_SIZE_TARGET: |
| 1407 |
self._write_large_bytes(packI(op.BINUNICODE, n), encoded) |
| 1408 |
else: |
| 1409 |
self.write_packI(op.BINUNICODE, n, encoded) |
| 1410 |
else: |
| 1411 |
|
| 1412 |
w_tmp = space.call_method(w_obj, "replace", space.newtext("\\"), space.newtext("\\u005c")) |
| 1413 |
w_tmp = space.call_method(w_tmp, "replace", space.newtext("\0"), space.newtext("\\u0000")) |
| 1414 |
w_tmp = space.call_method(w_tmp, "replace", space.newtext("\n"), space.newtext("\\u000a")) |
| 1415 |
w_tmp = space.call_method(w_tmp, "replace", space.newtext("\r"), space.newtext("\\u000d")) |
| 1416 |
|
| 1417 |
w_tmp = space.call_method(w_tmp, "replace", space.newtext("\x1a"), space.newtext("\\u001a")) |
| 1418 |
w_encoded = space.call_method(w_tmp, "encode", space.newtext('raw-unicode-escape')) |
| 1419 |
encoded = space.bytes_w(w_encoded) |
| 1420 |
self.write(op.UNICODE + encoded + b'\n') |
| 1421 |
|
| 1422 |
self.memoize(w_obj) |
| 1423 |
|
| 1424 |
def save_tuple(self, w_obj): |
| 1425 |
space = self.space |
| 1426 |
n = space.len_w(w_obj) |
| 1427 |
if not n: |
| 1428 |
if self.bin: |
| 1429 |
self.write(op.EMPTY_TUPLE) |
| 1430 |
else: |
| 1431 |
self.write(op.MARK + op.TUPLE) |
| 1432 |
return |
| 1433 |
|
| 1434 |
save = self.save |
| 1435 |
memo = self.memo |
| 1436 |
if n <= 3 and self.proto >= 2: |
| 1437 |
for element in space.unpackiterable(w_obj): |
| 1438 |
save(element) |
| 1439 |
|
| 1440 |
if w_obj in memo: |
| 1441 |
get = self.get(memo[w_obj]) |
| 1442 |
self.write(op.POP * n + get) |
| 1443 |
else: |
| 1444 |
self.write(op._tuplesize2code[n]) |
| 1445 |
self.memoize(w_obj) |
| 1446 |
return |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
write = self.write |
| 1451 |
write(op.MARK) |
| 1452 |
w_it = space.iter(w_obj) |
| 1453 |
while 1: |
| 1454 |
jitdriver_tuple.jit_merge_point(typ=type(w_obj)) |
| 1455 |
element = spacenext(space, w_it) |
| 1456 |
if element is None: |
| 1457 |
break |
| 1458 |
save(element) |
| 1459 |
|
| 1460 |
if w_obj in memo: |
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
get = self.get(memo[w_obj]) |
| 1469 |
if self.bin: |
| 1470 |
write(op.POP_MARK + get) |
| 1471 |
else: |
| 1472 |
write(op.POP * (n+1) + get) |
| 1473 |
return |
| 1474 |
|
| 1475 |
|
| 1476 |
write(op.TUPLE) |
| 1477 |
self.memoize(w_obj) |
| 1478 |
|
| 1479 |
def _save_ascii_list(self, w_obj, ascii_items): |
| 1480 |
"""Emit a PyPy-specific compact form for a list of pure-ASCII strings. |
| 1481 |
|
| 1482 |
Format on the wire: |
| 1483 |
GLOBAL 'pypy._builtin\\n_ascii_list_unpickle\\n' |
| 1484 |
SHORT_BINBYTES / BINBYTES <packed> |
| 1485 |
TUPLE1 |
| 1486 |
REDUCE |
| 1487 |
MEMOIZE / BINPUT / PUT |
| 1488 |
|
| 1489 |
packed = 4-byte LE count N + N x (1-byte length + raw UTF-8 bytes) |
| 1490 |
Strings longer than 255 bytes fall back to the regular path. |
| 1491 |
""" |
| 1492 |
n = len(ascii_items) |
| 1493 |
|
| 1494 |
for s in ascii_items: |
| 1495 |
if len(s) > 255: |
| 1496 |
|
| 1497 |
self.write(op.EMPTY_LIST) |
| 1498 |
self.memoize(w_obj) |
| 1499 |
self._batch_appends(w_obj) |
| 1500 |
return |
| 1501 |
|
| 1502 |
total = 4 + n |
| 1503 |
for s in ascii_items: |
| 1504 |
total += len(s) |
| 1505 |
buf = StringBuilder(total) |
| 1506 |
buf.append(chr(n & 0xff)) |
| 1507 |
buf.append(chr((n >> 8) & 0xff)) |
| 1508 |
buf.append(chr((n >> 16) & 0xff)) |
| 1509 |
buf.append(chr((n >> 24) & 0xff)) |
| 1510 |
for s in ascii_items: |
| 1511 |
buf.append(chr(len(s))) |
| 1512 |
buf.append(s) |
| 1513 |
packed = buf.build() |
| 1514 |
|
| 1515 |
self.write(op.GLOBAL + 'pypy._builtin\n_ascii_list_unpickle\n') |
| 1516 |
self.save(self.space.newbytes(packed)) |
| 1517 |
self.write(op.TUPLE1) |
| 1518 |
self.write(op.REDUCE) |
| 1519 |
self.memoize(w_obj) |
| 1520 |
|
| 1521 |
def _save_bytes_list(self, w_obj, bytes_items): |
| 1522 |
"""Emit a PyPy-specific compact form for a list of bytes objects. |
| 1523 |
|
| 1524 |
Format on the wire: |
| 1525 |
GLOBAL 'pypy._builtin\\n_bytes_list_unpickle\\n' |
| 1526 |
SHORT_BINBYTES / BINBYTES <packed> |
| 1527 |
TUPLE1 |
| 1528 |
REDUCE |
| 1529 |
MEMOIZE / BINPUT / PUT |
| 1530 |
|
| 1531 |
packed = 4-byte LE count N + N x (1-byte length + raw bytes) |
| 1532 |
Items longer than 255 bytes fall back to the regular path. |
| 1533 |
""" |
| 1534 |
n = len(bytes_items) |
| 1535 |
for s in bytes_items: |
| 1536 |
if len(s) > 255: |
| 1537 |
self.write(op.EMPTY_LIST) |
| 1538 |
self.memoize(w_obj) |
| 1539 |
self._batch_appends(w_obj) |
| 1540 |
return |
| 1541 |
total = 4 + n |
| 1542 |
for s in bytes_items: |
| 1543 |
total += len(s) |
| 1544 |
buf = StringBuilder(total) |
| 1545 |
buf.append(chr(n & 0xff)) |
| 1546 |
buf.append(chr((n >> 8) & 0xff)) |
| 1547 |
buf.append(chr((n >> 16) & 0xff)) |
| 1548 |
buf.append(chr((n >> 24) & 0xff)) |
| 1549 |
for s in bytes_items: |
| 1550 |
buf.append(chr(len(s))) |
| 1551 |
buf.append(s) |
| 1552 |
packed = buf.build() |
| 1553 |
self.write(op.GLOBAL + 'pypy._builtin\n_bytes_list_unpickle\n') |
| 1554 |
self.save(self.space.newbytes(packed)) |
| 1555 |
self.write(op.TUPLE1) |
| 1556 |
self.write(op.REDUCE) |
| 1557 |
self.memoize(w_obj) |
| 1558 |
|
| 1559 |
def save_list(self, w_obj): |
| 1560 |
if self.bin: |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
if self.pypy_extensions and self.proto >= 2: |
| 1566 |
ascii_items = self.space.listview_ascii(w_obj) |
| 1567 |
if ascii_items is not None: |
| 1568 |
_save_ascii_list(self, w_obj, ascii_items) |
| 1569 |
return |
| 1570 |
bytes_items = self.space.listview_bytes(w_obj) |
| 1571 |
if bytes_items is not None: |
| 1572 |
_save_bytes_list(self, w_obj, bytes_items) |
| 1573 |
return |
| 1574 |
self.write(op.EMPTY_LIST) |
| 1575 |
else: |
| 1576 |
self.write(op.MARK + op.LIST) |
| 1577 |
|
| 1578 |
self.memoize(w_obj) |
| 1579 |
self._batch_appends(w_obj) |
| 1580 |
|
| 1581 |
def save_dict(self, w_obj): |
| 1582 |
if self.bin: |
| 1583 |
self.write(op.EMPTY_DICT) |
| 1584 |
else: |
| 1585 |
self.write(op.MARK + op.DICT) |
| 1586 |
|
| 1587 |
self.memoize(w_obj) |
| 1588 |
w_items = self.space.call_method(w_obj, 'items') |
| 1589 |
self._batch_setitems(w_items) |
| 1590 |
|
| 1591 |
def save_set(self, w_obj): |
| 1592 |
save = self.save |
| 1593 |
write = self.write |
| 1594 |
space = self.space |
| 1595 |
|
| 1596 |
if self.proto < 4: |
| 1597 |
self.save_reduce(space.w_set, space.newtuple([space.call_function(space.w_list, w_obj)]), w_obj=w_obj) |
| 1598 |
return |
| 1599 |
|
| 1600 |
write(op.EMPTY_SET) |
| 1601 |
self.memoize(w_obj) |
| 1602 |
length = space.len_w(w_obj) |
| 1603 |
if length == 0: |
| 1604 |
return |
| 1605 |
w_iter = space.iter(w_obj) |
| 1606 |
write(op.MARK) |
| 1607 |
w_item = space.next(w_iter) |
| 1608 |
save(w_item) |
| 1609 |
for i in range(1, length - 1): |
| 1610 |
if i % self._BATCHSIZE == 0: |
| 1611 |
write(op.ADDITEMS) |
| 1612 |
write(op.MARK) |
| 1613 |
w_item = space.next(w_iter) |
| 1614 |
save(w_item) |
| 1615 |
if length > 1: |
| 1616 |
w_item = space.next(w_iter) |
| 1617 |
save(w_item) |
| 1618 |
write(op.ADDITEMS) |
| 1619 |
|
| 1620 |
def save_frozenset(self, w_obj): |
| 1621 |
self.write(op.MARK) |
| 1622 |
space = self.space |
| 1623 |
save = self.save |
| 1624 |
memo = self.memo |
| 1625 |
w_it = space.iter(w_obj) |
| 1626 |
while 1: |
| 1627 |
jitdriver_frozenset.jit_merge_point(typ=type(w_obj)) |
| 1628 |
element = spacenext(space, w_it) |
| 1629 |
if element is None: |
| 1630 |
break |
| 1631 |
save(element) |
| 1632 |
|
| 1633 |
if w_obj in memo: |
| 1634 |
get = self.get(memo[w_obj]) |
| 1635 |
self.write(op.POP + get) |
| 1636 |
else: |
| 1637 |
self.write(op.FROZENSET) |
| 1638 |
self.memoize(w_obj) |
| 1639 |
|
| 1640 |
def save_bytearray(self, w_obj): |
| 1641 |
space = self.space |
| 1642 |
n = space.len_w(w_obj) |
| 1643 |
if self.proto >= 5: |
| 1644 |
obj = space.buffer_w(w_obj, 0).as_str() |
| 1645 |
save_raw_bytearray(self, n, obj) |
| 1646 |
self.memoize(w_obj) |
| 1647 |
elif n == 0: |
| 1648 |
self.save_reduce(space.w_bytearray, space.newtuple([]), w_obj=w_obj) |
| 1649 |
else: |
| 1650 |
obj = space.buffer_w(w_obj, 0).as_str() |
| 1651 |
w_bytes = space.newbytes(obj) |
| 1652 |
self.save_reduce(space.w_bytearray, space.newtuple([w_bytes]), w_obj=w_obj) |
| 1653 |
|
| 1654 |
def save_raw_bytearray(self, n, obj): |
| 1655 |
if n >= self.framer._FRAME_SIZE_TARGET: |
| 1656 |
self._write_large_bytes(packQ(op.BYTEARRAY8, n), obj) |
| 1657 |
else: |
| 1658 |
self.write(packQ(op.BYTEARRAY8, n)) |
| 1659 |
self.write(obj) |
| 1660 |
|
| 1661 |
def iscontiguous(buf): |
| 1662 |
|
| 1663 |
ndim = buf.getndim() |
| 1664 |
shape = buf.getshape() |
| 1665 |
strides = buf.getstrides() |
| 1666 |
itemsize = buf.getitemsize() |
| 1667 |
if ndim == 0: |
| 1668 |
return 1 |
| 1669 |
if not strides: |
| 1670 |
return ndim == 1 |
| 1671 |
if ndim == 1: |
| 1672 |
return shape[0] == 1 or itemsize == strides[0] |
| 1673 |
sd = itemsize |
| 1674 |
for i in range(ndim -1, -1, -1): |
| 1675 |
|
| 1676 |
dim = shape[i] |
| 1677 |
if dim == 0: |
| 1678 |
return 1 |
| 1679 |
if strides[i] != sd: |
| 1680 |
|
| 1681 |
break |
| 1682 |
sd *= dim |
| 1683 |
else: |
| 1684 |
return 1 |
| 1685 |
sd = itemsize |
| 1686 |
for i in range(ndim): |
| 1687 |
dim = shape[i] |
| 1688 |
if dim == 0: |
| 1689 |
return 1 |
| 1690 |
if strides[i] != sd: |
| 1691 |
return 0 |
| 1692 |
sd *= dim |
| 1693 |
return 1 |
| 1694 |
|
| 1695 |
def save_picklebuffer(self, w_obj): |
| 1696 |
space = self.space |
| 1697 |
if self.proto < 5: |
| 1698 |
raise oefmt(pickling_error(space), |
| 1699 |
"PickleBuffer can only pickled with protocol >= 5") |
| 1700 |
buf = w_obj.buf |
| 1701 |
if not iscontiguous(buf): |
| 1702 |
raise oefmt(pickling_error(space), |
| 1703 |
"PickleBuffer can not be pickled when " |
| 1704 |
"pointing to a non-contiguous buffer") |
| 1705 |
in_band = True |
| 1706 |
if self.buffer_callback: |
| 1707 |
w_ret = space.call_function(self.buffer_callback, w_obj) |
| 1708 |
in_band = space.is_true(w_ret) |
| 1709 |
if in_band: |
| 1710 |
|
| 1711 |
raw = buf.as_str() |
| 1712 |
n = len(raw) |
| 1713 |
if buf.readonly: |
| 1714 |
save_raw_bytes(self, n, raw) |
| 1715 |
else: |
| 1716 |
save_raw_bytearray(self, n, raw) |
| 1717 |
else: |
| 1718 |
|
| 1719 |
self.write(op.NEXT_BUFFER) |
| 1720 |
if buf.readonly: |
| 1721 |
self.write(op.READONLY_BUFFER) |
| 1722 |
|
| 1723 |
|
| 1724 |
app = applevel(''' |
| 1725 |
def _getattribute(obj, name): |
| 1726 |
for subpath in name.split('.'): |
| 1727 |
if subpath == '<locals>': |
| 1728 |
raise AttributeError("Can't get local attribute {!r} on {!r}" |
| 1729 |
.format(name, obj)) |
| 1730 |
try: |
| 1731 |
parent = obj |
| 1732 |
obj = getattr(obj, subpath) |
| 1733 |
except AttributeError: |
| 1734 |
raise AttributeError("Can't get attribute {!r} on {!r}" |
| 1735 |
.format(name, obj)) from None |
| 1736 |
return obj, parent |
| 1737 |
|
| 1738 |
def whichmodule(obj, name): |
| 1739 |
"""Find the module an object belong to.""" |
| 1740 |
module_name = getattr(obj, '__module__', None) |
| 1741 |
if module_name is not None: |
| 1742 |
return module_name |
| 1743 |
# Protect the iteration by using a list copy of sys.modules against dynamic |
| 1744 |
# modules that trigger imports of other modules upon calls to getattr. |
| 1745 |
import sys |
| 1746 |
for module_name, module in sys.modules.copy().items(): |
| 1747 |
if (module_name == '__main__' |
| 1748 |
or module_name == '__mp_main__' # bpo-42406 |
| 1749 |
or module is None): |
| 1750 |
continue |
| 1751 |
try: |
| 1752 |
if _getattribute(module, name)[0] is obj: |
| 1753 |
return module_name |
| 1754 |
except AttributeError: |
| 1755 |
pass |
| 1756 |
return '__main__' |
| 1757 |
''', filename=__file__) |
| 1758 |
|
| 1759 |
_getattribute = app.interphook('_getattribute') |
| 1760 |
whichmodule = app.interphook('whichmodule') |
| 1761 |
|
| 1762 |
def encode_long(space, w_x): |
| 1763 |
r"""Encode a long to a two's complement little-endian binary string. |
| 1764 |
Note that 0 is a special case, returning an empty string, to save a |
| 1765 |
byte in the LONG1 pickling context. |
| 1766 |
|
| 1767 |
>>> encode_long(0) |
| 1768 |
b'' |
| 1769 |
>>> encode_long(255) |
| 1770 |
b'\xff\x00' |
| 1771 |
>>> encode_long(32767) |
| 1772 |
b'\xff\x7f' |
| 1773 |
>>> encode_long(-256) |
| 1774 |
b'\x00\xff' |
| 1775 |
>>> encode_long(-32768) |
| 1776 |
b'\x00\x80' |
| 1777 |
>>> encode_long(-128) |
| 1778 |
b'\x80' |
| 1779 |
>>> encode_long(127) |
| 1780 |
b'\x7f' |
| 1781 |
>>> |
| 1782 |
""" |
| 1783 |
from pypy.objspace.std import intobject |
| 1784 |
assert isinstance(w_x, intobject.W_AbstractIntObject) |
| 1785 |
if not space.is_true(w_x): |
| 1786 |
return b'' |
| 1787 |
nbytes = (space.int_w(w_x.descr_bit_length(space)) >> 3) + 1 |
| 1788 |
result = space.bytes_w(w_x.descr_to_bytes(space, nbytes, byteorder='little', signed=True)) |
| 1789 |
if space.is_true(space.lt(w_x, space.newint(0))) and nbytes > 1: |
| 1790 |
n = len(result) |
| 1791 |
stop = n - 1 |
| 1792 |
if stop >= 0 and result[stop] == b'\xff' and (ord(result[stop - 1]) & 0x080 != 0): |
| 1793 |
result = result[:stop] |
| 1794 |
return result |
| 1795 |
|
| 1796 |
def decode_long(space, data): |
| 1797 |
from pypy.objspace.std import intobject |
| 1798 |
return intobject.W_AbstractIntObject.descr_from_bytes( |
| 1799 |
space, space.w_int, space.newbytes(data), byteorder="little", |
| 1800 |
signed=True) |
| 1801 |
|
| 1802 |
|
| 1803 |
@unwrap_spec(fix_imports=int, w_file=WrappedDefault(None)) |
| 1804 |
def descr__new__(space, w_subtype, w_file=None, w_protocol=None, fix_imports=1, w_buffer_callback=None): |
| 1805 |
w_self = space.allocate_instance(W_Pickler, w_subtype) |
| 1806 |
if w_file is None or space.is_none(w_file): |
| 1807 |
|
| 1808 |
W_Pickler.__init__(w_self, space, None, HIGHEST_PROTOCOL, fix_imports, None) |
| 1809 |
return w_self |
| 1810 |
if space.is_none(w_protocol): |
| 1811 |
protocol = HIGHEST_PROTOCOL |
| 1812 |
else: |
| 1813 |
protocol = space.int_w(w_protocol) |
| 1814 |
if protocol < 0: |
| 1815 |
protocol = HIGHEST_PROTOCOL |
| 1816 |
elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
| 1817 |
raise oefmt(space.w_ValueError, "pickle protocol must be <= %d", HIGHEST_PROTOCOL) |
| 1818 |
if not space.is_none(w_buffer_callback) and protocol < 5: |
| 1819 |
raise oefmt(space.w_ValueError, "buffer_callback needs protocol >= 5") |
| 1820 |
W_Pickler.__init__(w_self, space, w_file, protocol, fix_imports, w_buffer_callback) |
| 1821 |
return w_self |
| 1822 |
|
| 1823 |
@unwrap_spec(fix_imports=int, w_file=WrappedDefault(None)) |
| 1824 |
def descr__init__(space, w_self, w_file=None, w_protocol=None, fix_imports=1, w_buffer_callback=None): |
| 1825 |
assert isinstance(w_self, W_Pickler) |
| 1826 |
if w_file is None or space.is_none(w_file): |
| 1827 |
return |
| 1828 |
if space.is_none(w_protocol): |
| 1829 |
protocol = HIGHEST_PROTOCOL |
| 1830 |
else: |
| 1831 |
protocol = space.int_w(w_protocol) |
| 1832 |
if protocol < 0: |
| 1833 |
protocol = HIGHEST_PROTOCOL |
| 1834 |
elif not 0 <= protocol <= HIGHEST_PROTOCOL: |
| 1835 |
raise oefmt(space.w_ValueError, "pickle protocol must be <= %d", HIGHEST_PROTOCOL) |
| 1836 |
if not space.is_none(w_buffer_callback) and protocol < 5: |
| 1837 |
raise oefmt(space.w_ValueError, "buffer_callback needs protocol >= 5") |
| 1838 |
W_Pickler.__init__(w_self, space, w_file, protocol, fix_imports, w_buffer_callback) |
| 1839 |
|
| 1840 |
W_Pickler.typedef = TypeDef("_pickle.Pickler", |
| 1841 |
__new__ = interp2app(descr__new__), |
| 1842 |
__init__ = interp2app(descr__init__), |
| 1843 |
dump = interp2app(W_Pickler.dump), |
| 1844 |
clear_memo = interp2app(W_Pickler.clear_memo), |
| 1845 |
memo = GetSetProperty(W_Pickler.get_memo_w, W_Pickler.set_memo_w), |
| 1846 |
dispatch_table = GetSetProperty(W_Pickler.get_dispatch_table_w, W_Pickler.set_dispatch_table_w), |
| 1847 |
fast = GetSetProperty(W_Pickler.get_fast_w, W_Pickler.set_fast_w), |
| 1848 |
pypy_extensions = GetSetProperty(W_Pickler.get_pypy_extensions_w, |
| 1849 |
W_Pickler.set_pypy_extensions_w), |
| 1850 |
persistent_id = GetSetProperty(W_Pickler.get_pers_func_w, W_Pickler.set_pers_func_w, |
| 1851 |
W_Pickler.del_pers_func_w), |
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
) |
| 1857 |
|
| 1858 |
|
| 1859 |
class _Unframer(object): |
| 1860 |
def __init__(self, space, w_file_read, w_file_readline): |
| 1861 |
self.space = space |
| 1862 |
self.w_file_read = w_file_read |
| 1863 |
self.w_file_readline = w_file_readline |
| 1864 |
self.current_frame = None |
| 1865 |
self.index = 0 |
| 1866 |
|
| 1867 |
def read(self, n): |
| 1868 |
assert isinstance(n, int) |
| 1869 |
assert n >= 0 |
| 1870 |
space = self.space |
| 1871 |
if self.current_frame: |
| 1872 |
data = self.current_frame |
| 1873 |
if self.index + n > len(data) and n > 0: |
| 1874 |
self.current_frame = None |
| 1875 |
self.index = 0 |
| 1876 |
w_ret = space.call_function(self.w_file_read, space.newint(n)) |
| 1877 |
ret = space.bytes_w(w_ret) |
| 1878 |
if len(ret) < n: |
| 1879 |
raise oefmt(unpickling_error(space), "pickle data was truncated") |
| 1880 |
return ret |
| 1881 |
if len(data) - self.index < n: |
| 1882 |
raise oefmt(unpickling_error(space), |
| 1883 |
"pickle exhausted before end of frame") |
| 1884 |
result = data[self.index:self.index + n] |
| 1885 |
self.index += n |
| 1886 |
return result |
| 1887 |
else: |
| 1888 |
w_ret = space.call_function(self.w_file_read, space.newint(n)) |
| 1889 |
ret = space.bytes_w(w_ret) |
| 1890 |
if len(ret) < n: |
| 1891 |
raise oefmt(unpickling_error(space), "pickle data was truncated, wanted %d got %d", n, len(ret)) |
| 1892 |
return ret |
| 1893 |
|
| 1894 |
def read1(self): |
| 1895 |
if self.current_frame is not None and self.index < len(self.current_frame): |
| 1896 |
res = self.current_frame[self.index] |
| 1897 |
self.index += 1 |
| 1898 |
return res |
| 1899 |
return self.read(1)[0] |
| 1900 |
|
| 1901 |
@objectmodel.always_inline |
| 1902 |
def read_with_offset(self, n): |
| 1903 |
|
| 1904 |
if self.current_frame is not None and self.index + n <= len(self.current_frame): |
| 1905 |
data = self.current_frame |
| 1906 |
index = self.index |
| 1907 |
self.index += n |
| 1908 |
else: |
| 1909 |
data = self.read(n) |
| 1910 |
index = 0 |
| 1911 |
return data, index |
| 1912 |
|
| 1913 |
def readline(self): |
| 1914 |
"""Read a line, stripping the trailing newline.""" |
| 1915 |
space = self.space |
| 1916 |
if self.current_frame is not None: |
| 1917 |
data = self.current_frame |
| 1918 |
start = self.index |
| 1919 |
|
| 1920 |
pos = start |
| 1921 |
while pos < len(data) and data[pos] != '\n': |
| 1922 |
pos += 1 |
| 1923 |
if pos < len(data): |
| 1924 |
|
| 1925 |
self.index = pos + 1 |
| 1926 |
return data[start:pos] |
| 1927 |
else: |
| 1928 |
|
| 1929 |
self.current_frame = None |
| 1930 |
self.index = 0 |
| 1931 |
w_ret = space.call_function(self.w_file_readline) |
| 1932 |
line = space.bytes_w(w_ret) |
| 1933 |
n = len(line) |
| 1934 |
stop = n - 1 |
| 1935 |
if stop < 0: |
| 1936 |
raise oefmt(unpickling_error(space), "pickle data was truncated") |
| 1937 |
if line[stop] == '\n': |
| 1938 |
return line[:stop] |
| 1939 |
return line |
| 1940 |
else: |
| 1941 |
w_ret = space.call_function(self.w_file_readline) |
| 1942 |
data = space.bytes_w(w_ret) |
| 1943 |
stop = len(data) - 1 |
| 1944 |
if stop < 0: |
| 1945 |
raise oefmt(unpickling_error(space), |
| 1946 |
"pickle data was truncated 2") |
| 1947 |
if data[stop] != b'\n'[0]: |
| 1948 |
raise oefmt(unpickling_error(space), |
| 1949 |
"pickle exhausted before end of frame") |
| 1950 |
return data[:stop] |
| 1951 |
|
| 1952 |
def load_frame(self, frame_size): |
| 1953 |
if self.current_frame and len(self.current_frame) > 0: |
| 1954 |
raise oefmt(unpickling_error(self.space), |
| 1955 |
"beginning of a new frame before end of current frame") |
| 1956 |
w_ret = self.space.call_function(self.w_file_read, self.space.newint(frame_size)) |
| 1957 |
ret = self.space.bytes_w(w_ret) |
| 1958 |
if len(ret) < frame_size: |
| 1959 |
raise oefmt(unpickling_error(self.space), "pickle data was truncated") |
| 1960 |
self.current_frame = ret |
| 1961 |
self.index = 0 |
| 1962 |
|
| 1963 |
|
| 1964 |
class W_Unpickler(W_Root): |
| 1965 |
"""This takes a binary file for reading a pickle data stream. |
| 1966 |
|
| 1967 |
The protocol version of the pickle is detected automatically, so |
| 1968 |
no proto argument is needed. |
| 1969 |
|
| 1970 |
The argument *file* must have two methods, a read() method that |
| 1971 |
takes an integer argument, and a readline() method that requires |
| 1972 |
no arguments. Both methods should return bytes. Thus *file* |
| 1973 |
can be a binary file object opened for reading, an io.BytesIO |
| 1974 |
object, or any other custom object that meets this interface. |
| 1975 |
|
| 1976 |
The file-like object must have two methods, a read() method |
| 1977 |
that takes an integer argument, and a readline() method that |
| 1978 |
requires no arguments. Both methods should return bytes. |
| 1979 |
Thus file-like object can be a binary file object opened for |
| 1980 |
reading, a BytesIO object, or any other custom object that |
| 1981 |
meets this interface. |
| 1982 |
|
| 1983 |
If *buffers* is not None, it should be an iterable of buffer-enabled |
| 1984 |
objects that is consumed each time the pickle stream references |
| 1985 |
an out-of-band buffer view. Such buffers have been given in order |
| 1986 |
to the *buffer_callback* of a Pickler object. |
| 1987 |
|
| 1988 |
If *buffers* is None (the default), then the buffers are taken |
| 1989 |
from the pickle stream, assuming they are serialized there. |
| 1990 |
It is an error for *buffers* to be None if the pickle stream |
| 1991 |
was produced with a non-None *buffer_callback*. |
| 1992 |
|
| 1993 |
Other optional arguments are *fix_imports*, *encoding* and |
| 1994 |
*errors*, which are used to control compatibility support for |
| 1995 |
pickle stream generated by Python 2. If *fix_imports* is True, |
| 1996 |
pickle will try to map the old Python 2 names to the new names |
| 1997 |
used in Python 3. The *encoding* and *errors* tell pickle how |
| 1998 |
to decode 8-bit string instances pickled by Python 2; these |
| 1999 |
default to 'ASCII' and 'strict', respectively. *encoding* can be |
| 2000 |
'bytes' to read these 8-bit string instances as bytes objects. |
| 2001 |
""" |
| 2002 |
|
| 2003 |
def wrap_bad_mark(i): |
| 2004 |
def load_bad_mark(self): |
| 2005 |
raise oefmt(unpickling_error(self.space), |
| 2006 |
"unkown mark %s", chr(i)) |
| 2007 |
return load_bad_mark |
| 2008 |
|
| 2009 |
dispatch = [wrap_bad_mark(i) for i in range(256)] |
| 2010 |
|
| 2011 |
def __init__(self, space, w_file, fix_imports, encoding, errors, w_buffers=None): |
| 2012 |
self.w_buffers = w_buffers |
| 2013 |
self.w_file_readline = space.getattr(w_file, space.newtext("readline")) |
| 2014 |
self.w_file_read = space.getattr(w_file, space.newtext("read")) |
| 2015 |
MEMO_SIZE = 32 |
| 2016 |
self.memo = [None] * MEMO_SIZE |
| 2017 |
self.memo_index = 0 |
| 2018 |
self.encoding = encoding |
| 2019 |
self.errors = errors |
| 2020 |
self.proto = 0 |
| 2021 |
self.fix_imports = fix_imports |
| 2022 |
self.space = space |
| 2023 |
|
| 2024 |
def load(self): |
| 2025 |
space = self.space |
| 2026 |
|
| 2027 |
if self.w_file_read is None or space.is_none(self.w_file_read): |
| 2028 |
w_read = space.findattr(self, space.newtext('read')) |
| 2029 |
w_readline = space.findattr(self, space.newtext('readline')) |
| 2030 |
if w_read is None or w_readline is None: |
| 2031 |
raise oefmt(unpickling_error(space), |
| 2032 |
"Unpickler.__init__() was not called") |
| 2033 |
self.w_file_read = w_read |
| 2034 |
self.w_file_readline = w_readline |
| 2035 |
self._unframer = _Unframer(self.space, self.w_file_read, self.w_file_readline) |
| 2036 |
self.read = self._unframer.read |
| 2037 |
self.readline = self._unframer.readline |
| 2038 |
self.metastack = [] |
| 2039 |
self.stack = [] |
| 2040 |
self.append = self.stack.append |
| 2041 |
self.proto = 0 |
| 2042 |
read = self.read |
| 2043 |
while True: |
| 2044 |
try: |
| 2045 |
key = self.read1() |
| 2046 |
except OperationError as e: |
| 2047 |
if e.match(space, unpickling_error(space)): |
| 2048 |
raise oefmt(space.w_EOFError, "Ran out of input") |
| 2049 |
raise |
| 2050 |
opcode = ord(key[0]) |
| 2051 |
if opcode == ord(op.STOP[0]): |
| 2052 |
return data_pop(self.space, self.stack) |
| 2053 |
jitdriver_load.jit_merge_point(opcode=opcode) |
| 2054 |
self.dispatch[opcode](self) |
| 2055 |
|
| 2056 |
def read1(self): |
| 2057 |
return self._unframer.read1() |
| 2058 |
|
| 2059 |
def read_unpackI(self): |
| 2060 |
d, index = self._unframer.read_with_offset(4) |
| 2061 |
return unpackI(d, index) |
| 2062 |
|
| 2063 |
def read_unpacki(self): |
| 2064 |
d, index = self._unframer.read_with_offset(4) |
| 2065 |
return unpacki(d, index) |
| 2066 |
|
| 2067 |
def _memo_put(self, i, w_val): |
| 2068 |
assert i >= 0 |
| 2069 |
while i >= len(self.memo): |
| 2070 |
self.memo += [None] * len(self.memo) |
| 2071 |
self.memo[i] = w_val |
| 2072 |
if i >= self.memo_index: |
| 2073 |
self.memo_index = i + 1 |
| 2074 |
|
| 2075 |
def _memo_append(self, w_val): |
| 2076 |
self._memo_put(self.memo_index, w_val) |
| 2077 |
|
| 2078 |
def _stack_top(self, opcode_name): |
| 2079 |
"""Return the top of the stack, raising an error if the stack is empty.""" |
| 2080 |
if len(self.stack) < 1: |
| 2081 |
raise oefmt(unpickling_error(self.space), |
| 2082 |
"stack empty in %s", opcode_name) |
| 2083 |
return self.stack[-1] |
| 2084 |
|
| 2085 |
|
| 2086 |
def pop_mark(self): |
| 2087 |
items = self.stack |
| 2088 |
if len(self.metastack) < 1: |
| 2089 |
raise oefmt(unpickling_error(self.space), |
| 2090 |
"no items on stack") |
| 2091 |
self.stack = self.metastack.pop() |
| 2092 |
self.append = self.stack.append |
| 2093 |
return items |
| 2094 |
|
| 2095 |
def load_proto(self): |
| 2096 |
proto = ord(self.read1()) |
| 2097 |
if not 0 <= proto <= HIGHEST_PROTOCOL: |
| 2098 |
raise oefmt(self.space.w_ValueError, |
| 2099 |
"unsupported pickle protocol: %d", proto) |
| 2100 |
self.proto = proto |
| 2101 |
dispatch[ord(op.PROTO[0])] = load_proto |
| 2102 |
|
| 2103 |
def load_frame(self): |
| 2104 |
try: |
| 2105 |
frame_size = unpackQ(self.read(8)) |
| 2106 |
except IndexError: |
| 2107 |
raise oefmt(unpickling_error(self.space), |
| 2108 |
'truncated data in FRAME') |
| 2109 |
if frame_size > maxsize: |
| 2110 |
raise oefmt(self.space.w_ValueError, |
| 2111 |
"frame size > maxsize: %d", frame_size) |
| 2112 |
self._unframer.load_frame(frame_size) |
| 2113 |
dispatch[ord(op.FRAME[0])] = load_frame |
| 2114 |
|
| 2115 |
def load_persid(self): |
| 2116 |
space = self.space |
| 2117 |
try: |
| 2118 |
pid = self.readline() |
| 2119 |
except UnicodeDecodeError: |
| 2120 |
raise oefmt(unpickling_error(space), |
| 2121 |
"persistent IDs in protocol 0 must be ASCII strings") |
| 2122 |
for c in pid: |
| 2123 |
if ord(c) > 127: |
| 2124 |
raise oefmt(unpickling_error(space), |
| 2125 |
"persistent id must be ASCII") |
| 2126 |
w_pers_load = space.findattr(self, space.newtext('persistent_load')) |
| 2127 |
if w_pers_load is None: |
| 2128 |
raise oefmt(unpickling_error(space), |
| 2129 |
"unsupported persistent id encountered") |
| 2130 |
self.append(space.call_function(w_pers_load, space.newtext(pid))) |
| 2131 |
dispatch[ord(op.PERSID[0])] = load_persid |
| 2132 |
|
| 2133 |
def load_binpersid(self): |
| 2134 |
space = self.space |
| 2135 |
w_pid = data_pop(space, self.stack) |
| 2136 |
w_pers_load = space.findattr(self, space.newtext('persistent_load')) |
| 2137 |
if w_pers_load is None: |
| 2138 |
raise oefmt(unpickling_error(space), |
| 2139 |
"unsupported persistent id encountered") |
| 2140 |
self.append(space.call_function(w_pers_load, w_pid)) |
| 2141 |
dispatch[ord(op.BINPERSID[0])] = load_binpersid |
| 2142 |
|
| 2143 |
def load_none(self): |
| 2144 |
self.append(self.space.w_None) |
| 2145 |
dispatch[ord(op.NONE[0])] = load_none |
| 2146 |
|
| 2147 |
def load_false(self): |
| 2148 |
self.append(self.space.w_False) |
| 2149 |
dispatch[ord(op.NEWFALSE[0])] = load_false |
| 2150 |
|
| 2151 |
def load_true(self): |
| 2152 |
self.append(self.space.w_True) |
| 2153 |
dispatch[ord(op.NEWTRUE[0])] = load_true |
| 2154 |
|
| 2155 |
def load_int(self): |
| 2156 |
space = self.space |
| 2157 |
data = self.readline() |
| 2158 |
if data == b'00': |
| 2159 |
w_val = space.w_False |
| 2160 |
elif data == b'01': |
| 2161 |
w_val = space.w_True |
| 2162 |
else: |
| 2163 |
from rpython.rlib.rbigint import rbigint |
| 2164 |
try: |
| 2165 |
bigint = rbigint.fromstr(data) |
| 2166 |
except ParseStringError: |
| 2167 |
raise oefmt(space.w_ValueError, "could not convert string to int") |
| 2168 |
try: |
| 2169 |
w_val = space.newint(bigint.toint()) |
| 2170 |
except OverflowError: |
| 2171 |
w_val = space.newlong_from_rbigint(bigint) |
| 2172 |
self.append(w_val) |
| 2173 |
dispatch[ord(op.INT[0])] = load_int |
| 2174 |
|
| 2175 |
def load_binint(self): |
| 2176 |
val = self.read_unpacki() |
| 2177 |
self.append(self.space.newint(val)) |
| 2178 |
dispatch[ord(op.BININT[0])] = load_binint |
| 2179 |
|
| 2180 |
def load_binint1(self): |
| 2181 |
self.append(self.space.newint(ord(self.read1()))) |
| 2182 |
dispatch[ord(op.BININT1[0])] = load_binint1 |
| 2183 |
|
| 2184 |
def load_binint2(self): |
| 2185 |
data, offset = self._unframer.read_with_offset(2) |
| 2186 |
|
| 2187 |
val = 256 * ord(data[offset + 1]) + ord(data[offset]) |
| 2188 |
self.append(self.space.newint(val)) |
| 2189 |
dispatch[ord(op.BININT2[0])] = load_binint2 |
| 2190 |
|
| 2191 |
def load_long(self): |
| 2192 |
data = self.readline() |
| 2193 |
|
| 2194 |
if data and data[-1] == '\n': |
| 2195 |
data = data[:-1] |
| 2196 |
if data and data[-1] == 'L': |
| 2197 |
data = data[:-1] |
| 2198 |
self.append(self.space.call_function( |
| 2199 |
self.space.w_int, self.space.newtext(data))) |
| 2200 |
dispatch[ord(op.LONG[0])] = load_long |
| 2201 |
|
| 2202 |
def load_long1(self): |
| 2203 |
n = ord(self.read1()) |
| 2204 |
data = self.read(n) |
| 2205 |
self.append(decode_long(self.space, data)) |
| 2206 |
dispatch[ord(op.LONG1[0])] = load_long1 |
| 2207 |
|
| 2208 |
def load_long4(self): |
| 2209 |
n = self.read_unpacki() |
| 2210 |
if n < 0: |
| 2211 |
|
| 2212 |
raise oefmt(unpickling_error(self.space), |
| 2213 |
"LONG pickle has negative byte count") |
| 2214 |
data = self.read(n) |
| 2215 |
self.append(decode_long(self.space, data)) |
| 2216 |
dispatch[ord(op.LONG4[0])] = load_long4 |
| 2217 |
|
| 2218 |
def load_float(self): |
| 2219 |
try: |
| 2220 |
f = float(self.readline()) |
| 2221 |
except ValueError: |
| 2222 |
raise oefmt(self.space.w_ValueError, "could not convert string to float") |
| 2223 |
self.append(self.space.newfloat(f)) |
| 2224 |
dispatch[ord(op.FLOAT[0])] = load_float |
| 2225 |
|
| 2226 |
def load_binfloat(self): |
| 2227 |
f = unpack_float(self.read(8)) |
| 2228 |
self.append(self.space.newfloat(f)) |
| 2229 |
dispatch[ord(op.BINFLOAT[0])] = load_binfloat |
| 2230 |
|
| 2231 |
def _decode_string(self, data): |
| 2232 |
space = self.space |
| 2233 |
if self.encoding == "bytes": |
| 2234 |
return space.newbytes(data) |
| 2235 |
else: |
| 2236 |
w_data = space.newbytes(data) |
| 2237 |
return space.call_method(w_data, 'decode', space.newtext(self.encoding), space.newtext(self.errors)) |
| 2238 |
|
| 2239 |
def load_string(self): |
| 2240 |
space = self.space |
| 2241 |
data = self.readline() |
| 2242 |
n = len(data) |
| 2243 |
stop = n - 1 |
| 2244 |
if stop >= 1 and data[0] == data[stop] and (data[0] == b'\''[0] or data[0] == b'"'[0]): |
| 2245 |
data = data[1:stop] |
| 2246 |
else: |
| 2247 |
raise oefmt(unpickling_error(space), |
| 2248 |
"the STRING opcode argument must be quoted") |
| 2249 |
w_codecs = space.getbuiltinmodule('_codecs') |
| 2250 |
w_result = space.call_method(w_codecs, 'escape_decode', space.newbytes(data)) |
| 2251 |
decoded = space.bytes_w(space.getitem(w_result, space.newint(0))) |
| 2252 |
self.append(self._decode_string(decoded)) |
| 2253 |
dispatch[ord(op.STRING[0])] = load_string |
| 2254 |
|
| 2255 |
def load_binstring(self): |
| 2256 |
|
| 2257 |
length = self.read_unpacki() |
| 2258 |
if length < 0: |
| 2259 |
raise oefmt(unpickling_error(self.space), |
| 2260 |
"BINSTRING pickle has negative byte count") |
| 2261 |
data = self.read(length) |
| 2262 |
self.append(self._decode_string(data)) |
| 2263 |
dispatch[ord(op.BINSTRING[0])] = load_binstring |
| 2264 |
|
| 2265 |
def load_binbytes(self): |
| 2266 |
length = self.read_unpackI() |
| 2267 |
if length < 0 or length > maxsize: |
| 2268 |
raise oefmt(unpickling_error(self.space), |
| 2269 |
"BINBYTES exceeds system's maximum size " |
| 2270 |
"of %d bytes", maxsize) |
| 2271 |
data = self.read(length) |
| 2272 |
if len(data) < length: |
| 2273 |
raise oefmt(unpickling_error(self.space), |
| 2274 |
"truncated data in BINBYTES") |
| 2275 |
self.append(self.space.newbytes(data)) |
| 2276 |
dispatch[ord(op.BINBYTES[0])] = load_binbytes |
| 2277 |
|
| 2278 |
def load_unicode(self): |
| 2279 |
space = self.space |
| 2280 |
data = self.readline() |
| 2281 |
w_bytes = space.newbytes(data) |
| 2282 |
w_uni = space.call_method(w_bytes, 'decode', space.newtext('raw-unicode-escape')) |
| 2283 |
self.append(w_uni) |
| 2284 |
dispatch[ord(op.UNICODE[0])] = load_unicode |
| 2285 |
|
| 2286 |
def load_binunicode(self): |
| 2287 |
length = self.read_unpackI() |
| 2288 |
if length < 0 or length > maxsize: |
| 2289 |
raise oefmt(unpickling_error(self.space), |
| 2290 |
"BINUNICODE exceeds system's maximum size " |
| 2291 |
"of %d bytes", maxsize) |
| 2292 |
data = self.read(length) |
| 2293 |
if len(data) < length: |
| 2294 |
raise oefmt(unpickling_error(self.space), |
| 2295 |
"truncated data in BINUNICODE") |
| 2296 |
self.append(self.space.newtext(*decode_utf8sp(self.space, data))) |
| 2297 |
dispatch[ord(op.BINUNICODE[0])] = load_binunicode |
| 2298 |
|
| 2299 |
def load_binunicode8(self): |
| 2300 |
try: |
| 2301 |
length = unpackQ(self.read(8)) |
| 2302 |
except IndexError: |
| 2303 |
raise oefmt(unpickling_error(self.space), |
| 2304 |
"truncated data in BINUNICODE8") |
| 2305 |
except OverflowError: |
| 2306 |
raise oefmt(unpickling_error(self.space), |
| 2307 |
"BINUNICODE8 exceeds system's maximum size") |
| 2308 |
if length > maxsize: |
| 2309 |
raise oefmt(unpickling_error(self.space), |
| 2310 |
"BINUNICODE8 exceeds system's maximum size " |
| 2311 |
"of %d bytes", maxsize) |
| 2312 |
data = self.read(length) |
| 2313 |
if len(data) < length: |
| 2314 |
raise oefmt(unpickling_error(self.space), |
| 2315 |
"truncated data in BINUNICODE8") |
| 2316 |
self.append(self.space.newtext(*decode_utf8sp(self.space, data))) |
| 2317 |
dispatch[ord(op.BINUNICODE8[0])] = load_binunicode8 |
| 2318 |
|
| 2319 |
def load_binbytes8(self): |
| 2320 |
try: |
| 2321 |
length = unpackQ(self.read(8)) |
| 2322 |
except IndexError: |
| 2323 |
raise oefmt(unpickling_error(self.space), |
| 2324 |
"truncated data in BINBYTES8") |
| 2325 |
except OverflowError: |
| 2326 |
raise oefmt(unpickling_error(self.space), |
| 2327 |
"BINBYTES8 exceeds system's maximum size") |
| 2328 |
if length > maxsize: |
| 2329 |
raise oefmt(unpickling_error(self.space), |
| 2330 |
"BINBYTES8 exceeds system's maximum size " |
| 2331 |
"of %d bytes", maxsize) |
| 2332 |
data = self.read(length) |
| 2333 |
if len(data) < length: |
| 2334 |
raise oefmt(unpickling_error(self.space), |
| 2335 |
"truncated data in BINBYTES8") |
| 2336 |
self.append(self.space.newbytes(data)) |
| 2337 |
dispatch[ord(op.BINBYTES8[0])] = load_binbytes8 |
| 2338 |
|
| 2339 |
def load_bytearray8(self): |
| 2340 |
try: |
| 2341 |
length = unpackQ(self.read(8)) |
| 2342 |
except IndexError: |
| 2343 |
raise oefmt(unpickling_error(self.space), |
| 2344 |
"truncated data in BYTEARRAY8") |
| 2345 |
except OverflowError: |
| 2346 |
raise oefmt(unpickling_error(self.space), |
| 2347 |
"BYTEARRAY8 exceeds system's maximum size") |
| 2348 |
if length > maxsize: |
| 2349 |
raise oefmt(unpickling_error(self.space), |
| 2350 |
"BYTEARRAY8 exceeds system's maximum size " |
| 2351 |
"of %d bytes", maxsize) |
| 2352 |
data = self.read(length) |
| 2353 |
if len(data) < length: |
| 2354 |
raise oefmt(unpickling_error(self.space), |
| 2355 |
"truncated data in BYTEARRAY8") |
| 2356 |
self.append(self.space.newbytearray([c for c in data])) |
| 2357 |
dispatch[ord(op.BYTEARRAY8[0])] = load_bytearray8 |
| 2358 |
|
| 2359 |
def load_next_buffer(self): |
| 2360 |
space = self.space |
| 2361 |
if space.is_none(self.w_buffers): |
| 2362 |
raise oefmt(unpickling_error(space), |
| 2363 |
"pickle stream refers to out-of-band data " |
| 2364 |
"but no *buffers* argument was given") |
| 2365 |
try: |
| 2366 |
w_buf = space.next(self.w_buffers) |
| 2367 |
except OperationError as e: |
| 2368 |
if not e.match(space, space.w_StopIteration): |
| 2369 |
raise |
| 2370 |
raise oefmt(unpickling_error(space), |
| 2371 |
"not enough out-of-band buffers") |
| 2372 |
self.append(w_buf) |
| 2373 |
dispatch[ord(op.NEXT_BUFFER[0])] = load_next_buffer |
| 2374 |
|
| 2375 |
def load_readonly_buffer(self): |
| 2376 |
space = self.space |
| 2377 |
w_buf = self._stack_top("READONLY_BUFFER") |
| 2378 |
w_mv = space.call_function(space.w_memoryview, w_buf) |
| 2379 |
w_readonly = space.call_method(w_mv, 'toreadonly') |
| 2380 |
self.stack[-1] = w_readonly |
| 2381 |
dispatch[ord(op.READONLY_BUFFER[0])] = load_readonly_buffer |
| 2382 |
|
| 2383 |
def load_short_binstring(self): |
| 2384 |
length = ord(self.read1()) |
| 2385 |
data = self.read(length) |
| 2386 |
self.append(self._decode_string(data)) |
| 2387 |
dispatch[ord(op.SHORT_BINSTRING[0])] = load_short_binstring |
| 2388 |
|
| 2389 |
def load_short_binbytes(self): |
| 2390 |
length = ord(self.read1()) |
| 2391 |
data = self.read(length) |
| 2392 |
if len(data) < length: |
| 2393 |
raise oefmt(unpickling_error(self.space), |
| 2394 |
"truncated data in SHORT_BINBYTES") |
| 2395 |
self.append(self.space.newbytes(data)) |
| 2396 |
dispatch[ord(op.SHORT_BINBYTES[0])] = load_short_binbytes |
| 2397 |
|
| 2398 |
def load_short_binunicode(self): |
| 2399 |
length = ord(self.read1()) |
| 2400 |
data = self.read(length) |
| 2401 |
if len(data) < length: |
| 2402 |
raise oefmt(unpickling_error(self.space), |
| 2403 |
"truncated data in SHORT_BINUNICODE") |
| 2404 |
self.append(self.space.newtext(data)) |
| 2405 |
dispatch[ord(op.SHORT_BINUNICODE[0])] = load_short_binunicode |
| 2406 |
|
| 2407 |
def load_tuple(self): |
| 2408 |
items = self.pop_mark() |
| 2409 |
items_w = [w_obj for w_obj in items] |
| 2410 |
self.append(self.space.newtuple(items_w)) |
| 2411 |
dispatch[ord(op.TUPLE[0])] = load_tuple |
| 2412 |
|
| 2413 |
def load_empty_tuple(self): |
| 2414 |
self.append(self.space.newtuple([])) |
| 2415 |
dispatch[ord(op.EMPTY_TUPLE[0])] = load_empty_tuple |
| 2416 |
|
| 2417 |
def load_tuple1(self): |
| 2418 |
n1 = len(self.stack) - 1 |
| 2419 |
if n1 < 0: |
| 2420 |
raise oefmt(unpickling_error(self.space), |
| 2421 |
"stack empty in TUPLE") |
| 2422 |
self.stack[n1] = self.space.newtuple([self.stack[n1]]) |
| 2423 |
dispatch[ord(op.TUPLE1[0])] = load_tuple1 |
| 2424 |
|
| 2425 |
def load_tuple2(self): |
| 2426 |
n = len(self.stack) |
| 2427 |
n2 = n - 2 |
| 2428 |
if n2 < 0: |
| 2429 |
raise oefmt(unpickling_error(self.space), |
| 2430 |
"stack empty in TUPLE2") |
| 2431 |
self.stack[n2:n] = [self.space.newtuple([self.stack[n2], self.stack[n2+1]])] |
| 2432 |
dispatch[ord(op.TUPLE2[0])] = load_tuple2 |
| 2433 |
|
| 2434 |
def load_tuple3(self): |
| 2435 |
n = len(self.stack) |
| 2436 |
n3 = n - 3 |
| 2437 |
if n3 < 0: |
| 2438 |
raise oefmt(unpickling_error(self.space), |
| 2439 |
"stack empty in TUPLE3") |
| 2440 |
self.stack[n3:n] = [self.space.newtuple([self.stack[n3], self.stack[n3+1], self.stack[n3+2]])] |
| 2441 |
dispatch[ord(op.TUPLE3[0])] = load_tuple3 |
| 2442 |
|
| 2443 |
def load_empty_list(self): |
| 2444 |
self.append(self.space.newlist([])) |
| 2445 |
dispatch[ord(op.EMPTY_LIST[0])] = load_empty_list |
| 2446 |
|
| 2447 |
def load_empty_dictionary(self): |
| 2448 |
self.append(self.space.newdict()) |
| 2449 |
dispatch[ord(op.EMPTY_DICT[0])] = load_empty_dictionary |
| 2450 |
|
| 2451 |
def load_empty_set(self): |
| 2452 |
self.append(self.space.newset([])) |
| 2453 |
dispatch[ord(op.EMPTY_SET[0])] = load_empty_set |
| 2454 |
|
| 2455 |
def load_frozenset(self): |
| 2456 |
items = self.pop_mark() |
| 2457 |
items_copy_w = items[:] |
| 2458 |
w_frozenset = self.space.newfrozenset(items_copy_w) |
| 2459 |
self.append(w_frozenset) |
| 2460 |
dispatch[ord(op.FROZENSET[0])] = load_frozenset |
| 2461 |
|
| 2462 |
def load_list(self): |
| 2463 |
items = self.pop_mark() |
| 2464 |
self.append(self.space.newlist(items)) |
| 2465 |
dispatch[ord(op.LIST[0])] = load_list |
| 2466 |
|
| 2467 |
def load_dict(self): |
| 2468 |
space = self.space |
| 2469 |
items = self.pop_mark() |
| 2470 |
if (len(items) % 2) != 0: |
| 2471 |
raise oefmt(unpickling_error(space), |
| 2472 |
"odd number of items for DICT") |
| 2473 |
w_d = space.newdict() |
| 2474 |
for i in range(0, len(items), 2): |
| 2475 |
space.setitem(w_d, items[i], items[i+1]) |
| 2476 |
self.append(w_d) |
| 2477 |
dispatch[ord(op.DICT[0])] = load_dict |
| 2478 |
|
| 2479 |
def _instantiate(self, w_klass, w_args): |
| 2480 |
space = self.space |
| 2481 |
has_getinitargs = space.findattr(w_klass, space.newtext("__getinitargs__")) is not None |
| 2482 |
if space.len_w(w_args) > 0 or not space.isinstance_w(w_klass, space.w_type) or has_getinitargs: |
| 2483 |
w_obj = space.call(w_klass, w_args) |
| 2484 |
else: |
| 2485 |
w_new = space.getattr(w_klass, space.newtext("__new__")) |
| 2486 |
w_obj = space.call_function(w_new, w_klass) |
| 2487 |
self.append(w_obj) |
| 2488 |
|
| 2489 |
def load_inst(self): |
| 2490 |
space = self.space |
| 2491 |
w_module = space.newtext(self.readline()) |
| 2492 |
w_name = space.newtext(self.readline()) |
| 2493 |
w_klass = self.find_class(w_module, w_name) |
| 2494 |
w_args = space.newlist(self.pop_mark()) |
| 2495 |
self._instantiate(w_klass, w_args) |
| 2496 |
dispatch[ord(op.INST[0])] = load_inst |
| 2497 |
|
| 2498 |
def load_obj(self): |
| 2499 |
space = self.space |
| 2500 |
|
| 2501 |
args = self.pop_mark() |
| 2502 |
if not args: |
| 2503 |
raise oefmt(unpickling_error(space), "OBJ opcode with empty stack") |
| 2504 |
w_klass = args[0] |
| 2505 |
w_args = space.newlist(args[1:]) |
| 2506 |
self._instantiate(w_klass, w_args) |
| 2507 |
dispatch[ord(op.OBJ[0])] = load_obj |
| 2508 |
|
| 2509 |
def load_newobj(self): |
| 2510 |
space = self.space |
| 2511 |
w_args = data_pop(self.space, self.stack) |
| 2512 |
w_cls = data_pop(self.space, self.stack) |
| 2513 |
w_new = space.getattr(w_cls, space.newtext("__new__")) |
| 2514 |
if space.len_w(w_args) > 0: |
| 2515 |
w_arguments = Arguments(space, [w_cls], w_stararg = w_args) |
| 2516 |
w_obj = space.call_args(w_new, w_arguments) |
| 2517 |
else: |
| 2518 |
w_obj = space.call_function(w_new, w_cls) |
| 2519 |
self.append(w_obj) |
| 2520 |
dispatch[ord(op.NEWOBJ[0])] = load_newobj |
| 2521 |
|
| 2522 |
def load_newobj_ex(self): |
| 2523 |
space = self.space |
| 2524 |
w_kwargs = data_pop(self.space, self.stack) |
| 2525 |
w_args = data_pop(self.space, self.stack) |
| 2526 |
w_cls = data_pop(self.space, self.stack) |
| 2527 |
w_new = space.getattr(w_cls, space.newtext("__new__")) |
| 2528 |
w_arguments = Arguments(self.space, [w_cls], w_stararg = w_args, w_starstararg=w_kwargs) |
| 2529 |
w_obj = self.space.call_args(w_new, w_arguments) |
| 2530 |
self.append(w_obj) |
| 2531 |
dispatch[ord(op.NEWOBJ_EX[0])] = load_newobj_ex |
| 2532 |
|
| 2533 |
def load_global(self): |
| 2534 |
module = self.readline() |
| 2535 |
name = self.readline() |
| 2536 |
if module == 'pypy._builtin': |
| 2537 |
if name == '_ascii_list_unpickle': |
| 2538 |
self.append(_ascii_list_unpickler_sentinel) |
| 2539 |
return |
| 2540 |
if name == '_bytes_list_unpickle': |
| 2541 |
self.append(_bytes_list_unpickler_sentinel) |
| 2542 |
return |
| 2543 |
w_klass = self.find_class(self.space.newtext(module), |
| 2544 |
self.space.newtext(name)) |
| 2545 |
self.append(w_klass) |
| 2546 |
dispatch[ord(op.GLOBAL[0])] = load_global |
| 2547 |
|
| 2548 |
def load_stack_global(self): |
| 2549 |
space = self.space |
| 2550 |
w_name = data_pop(self.space, self.stack) |
| 2551 |
w_module = data_pop(self.space, self.stack) |
| 2552 |
if space.isinstance_w(w_name, space.w_text) and space.isinstance_w(w_module, space.w_text): |
| 2553 |
self.append(self.find_class(w_module, w_name)) |
| 2554 |
else: |
| 2555 |
raise oefmt(unpickling_error(self.space), |
| 2556 |
"STACK_GLOBAL requires str") |
| 2557 |
dispatch[ord(op.STACK_GLOBAL[0])] = load_stack_global |
| 2558 |
|
| 2559 |
def load_ext1(self): |
| 2560 |
code = ord(self.read1()) |
| 2561 |
self.get_extension(code) |
| 2562 |
dispatch[ord(op.EXT1[0])] = load_ext1 |
| 2563 |
|
| 2564 |
def load_ext2(self): |
| 2565 |
data = self.read(2) |
| 2566 |
|
| 2567 |
if len(data) < 2: |
| 2568 |
raise oefmt(unpickling_error(self.space), |
| 2569 |
'truncated data in EXT4') |
| 2570 |
code = 256 * ord(data[1]) + ord(data[0]) |
| 2571 |
self.get_extension(code) |
| 2572 |
dispatch[ord(op.EXT2[0])] = load_ext2 |
| 2573 |
|
| 2574 |
def load_ext4(self): |
| 2575 |
code = self.read_unpacki() |
| 2576 |
self.get_extension(code) |
| 2577 |
dispatch[ord(op.EXT4[0])] = load_ext4 |
| 2578 |
|
| 2579 |
def get_extension(self, code): |
| 2580 |
space = self.space |
| 2581 |
w_code = space.newint(code) |
| 2582 |
state = space.fromcache(State) |
| 2583 |
w_obj = space.finditem(state.w_extension_cache, w_code) |
| 2584 |
if not space.is_none(w_obj): |
| 2585 |
self.append(w_obj) |
| 2586 |
return |
| 2587 |
w_key = space.finditem(state.w_inverted_registry, w_code) |
| 2588 |
if space.is_none(w_key): |
| 2589 |
if code <= 0: |
| 2590 |
|
| 2591 |
raise oefmt(unpickling_error(self.space), "EXT specifies code <= 0") |
| 2592 |
raise oefmt(self.space.w_ValueError, "unregistered extension code %d", code) |
| 2593 |
w_module_name, w_name = space.listview(w_key) |
| 2594 |
w_obj = self.find_class(w_module_name, w_name) |
| 2595 |
space.setitem(state.w_extension_cache, w_code, w_obj) |
| 2596 |
self.append(w_obj) |
| 2597 |
|
| 2598 |
def find_class(self, w_module_name, w_name): |
| 2599 |
|
| 2600 |
space = self.space |
| 2601 |
space.audit('pickle.find_class', [w_module_name, w_name]) |
| 2602 |
if self.proto < 3 and self.fix_imports: |
| 2603 |
w_modname_and_name = space.newtuple([w_module_name, w_name]) |
| 2604 |
w_NAME_MAPPING = space.fromcache(State).w_NAME_MAPPING |
| 2605 |
w_IMPORT_MAPPING = space.fromcache(State).w_IMPORT_MAPPING |
| 2606 |
w_1 = space.finditem(w_NAME_MAPPING, w_modname_and_name) |
| 2607 |
w_2 = space.finditem(w_IMPORT_MAPPING, w_module_name) |
| 2608 |
if w_1: |
| 2609 |
w_module_name, w_name = space.listview(w_1) |
| 2610 |
elif w_2: |
| 2611 |
w_module_name = w_2 |
| 2612 |
w_import = space.getattr(space.builtin, space.newtext("__import__")) |
| 2613 |
space.call_function(w_import, w_module_name) |
| 2614 |
w_module = space.getitem(space.getattr(space.sys, space.newtext('modules')), w_module_name) |
| 2615 |
if self.proto >= 4: |
| 2616 |
retval = _getattribute(space, w_module, w_name) |
| 2617 |
return space.listview(retval)[0] |
| 2618 |
else: |
| 2619 |
return space.getattr(w_module, w_name) |
| 2620 |
|
| 2621 |
def load_reduce(self): |
| 2622 |
w_args = data_pop(self.space, self.stack) |
| 2623 |
w_func = self._stack_top("REDUCE") |
| 2624 |
if type(w_func) is _W_AsciiListUnpickler: |
| 2625 |
self.stack[-1] = _ascii_list_from_packed(self.space, w_args) |
| 2626 |
return |
| 2627 |
if type(w_func) is _W_BytesListUnpickler: |
| 2628 |
self.stack[-1] = _bytes_list_from_packed(self.space, w_args) |
| 2629 |
return |
| 2630 |
w_arguments = Arguments(self.space, [], w_stararg = w_args) |
| 2631 |
w_obj = self.space.call_args(w_func, w_arguments) |
| 2632 |
self.stack[-1] = w_obj |
| 2633 |
dispatch[ord(op.REDUCE[0])] = load_reduce |
| 2634 |
|
| 2635 |
def load_pop(self): |
| 2636 |
if self.stack: |
| 2637 |
del self.stack[-1] |
| 2638 |
else: |
| 2639 |
self.pop_mark() |
| 2640 |
dispatch[ord(op.POP[0])] = load_pop |
| 2641 |
|
| 2642 |
def load_pop_mark(self): |
| 2643 |
self.pop_mark() |
| 2644 |
dispatch[ord(op.POP_MARK[0])] = load_pop_mark |
| 2645 |
|
| 2646 |
def load_dup(self): |
| 2647 |
w_val = self._stack_top("DUP") |
| 2648 |
self.append(w_val) |
| 2649 |
dispatch[ord(op.DUP[0])] = load_dup |
| 2650 |
|
| 2651 |
def load_get(self): |
| 2652 |
data = self.readline() |
| 2653 |
try: |
| 2654 |
i = int(data) |
| 2655 |
except ValueError: |
| 2656 |
raise oefmt(self.space.w_ValueError, "invalid int literal %s", data) |
| 2657 |
try: |
| 2658 |
self.append(self.memo[i]) |
| 2659 |
except IndexError: |
| 2660 |
raise oefmt(unpickling_error(self.space), |
| 2661 |
'Memo value not found at index %d', i) |
| 2662 |
dispatch[ord(op.GET[0])] = load_get |
| 2663 |
|
| 2664 |
def load_binget(self): |
| 2665 |
i = ord(self.read1()) |
| 2666 |
try: |
| 2667 |
self.append(self.memo[i]) |
| 2668 |
except IndexError as exc: |
| 2669 |
raise oefmt(unpickling_error(self.space), |
| 2670 |
'Memo value not found at index %d', i) |
| 2671 |
dispatch[ord(op.BINGET[0])] = load_binget |
| 2672 |
|
| 2673 |
def load_long_binget(self): |
| 2674 |
i = self.read_unpackI() |
| 2675 |
if i < 0: |
| 2676 |
raise oefmt(unpickling_error(self.space), |
| 2677 |
'Memo value not found at index %d', i) |
| 2678 |
try: |
| 2679 |
self.append(self.memo[i]) |
| 2680 |
except IndexError as exc: |
| 2681 |
raise oefmt(unpickling_error(self.space), |
| 2682 |
'Memo value not found at index %d', i) |
| 2683 |
dispatch[ord(op.LONG_BINGET[0])] = load_long_binget |
| 2684 |
|
| 2685 |
def load_put(self): |
| 2686 |
data = self.readline() |
| 2687 |
try: |
| 2688 |
i = int(data) |
| 2689 |
except ValueError: |
| 2690 |
raise oefmt(self.space.w_ValueError, "invalid int literal %s", data) |
| 2691 |
if i < 0: |
| 2692 |
raise oefmt(self.space.w_ValueError, "negative PUT argument") |
| 2693 |
w_val = self._stack_top("PUT") |
| 2694 |
self._memo_put(i, w_val) |
| 2695 |
dispatch[ord(op.PUT[0])] = load_put |
| 2696 |
|
| 2697 |
def load_binput(self): |
| 2698 |
i = ord(self.read1()) |
| 2699 |
if i < 0: |
| 2700 |
raise oefmt(self.space.w_ValueError, "negative BINPUT argument") |
| 2701 |
w_val = self._stack_top("BINPUT") |
| 2702 |
self._memo_put(i, w_val) |
| 2703 |
dispatch[ord(op.BINPUT[0])] = load_binput |
| 2704 |
|
| 2705 |
def load_long_binput(self): |
| 2706 |
i = self.read_unpackI() |
| 2707 |
if i < 0 or i > maxsize: |
| 2708 |
raise oefmt(self.space.w_ValueError, "negative LONG_BINPUT argument") |
| 2709 |
w_val = self._stack_top("LONG_BINPUT") |
| 2710 |
self._memo_put(i, w_val) |
| 2711 |
dispatch[ord(op.LONG_BINPUT[0])] = load_long_binput |
| 2712 |
|
| 2713 |
def load_memoize(self): |
| 2714 |
w_val = self._stack_top("MEMOIZE") |
| 2715 |
self._memo_append(w_val) |
| 2716 |
dispatch[ord(op.MEMOIZE[0])] = load_memoize |
| 2717 |
|
| 2718 |
def load_append(self): |
| 2719 |
w_value = data_pop(self.space, self.stack) |
| 2720 |
w_list = self._stack_top("APPEND") |
| 2721 |
self.space.call_method(w_list, "append", w_value) |
| 2722 |
dispatch[ord(op.APPEND[0])] = load_append |
| 2723 |
|
| 2724 |
def load_appends(self): |
| 2725 |
space = self.space |
| 2726 |
w_items = self.pop_mark() |
| 2727 |
w_list_obj = self._stack_top("APPENDS") |
| 2728 |
w_extend = space.lookup(w_list_obj, "extend") |
| 2729 |
if w_extend: |
| 2730 |
|
| 2731 |
|
| 2732 |
|
| 2733 |
n = len(w_items) |
| 2734 |
if n > 0 and type(w_items[0]) is W_UnicodeObject: |
| 2735 |
ascii_strs = [''] * n |
| 2736 |
i = 0 |
| 2737 |
for w in w_items: |
| 2738 |
if type(w) is not W_UnicodeObject: |
| 2739 |
ascii_strs = None |
| 2740 |
break |
| 2741 |
utf8 = w._utf8 |
| 2742 |
if not w.is_ascii(): |
| 2743 |
ascii_strs = None |
| 2744 |
break |
| 2745 |
ascii_strs[i] = utf8 |
| 2746 |
i += 1 |
| 2747 |
if ascii_strs is not None: |
| 2748 |
space.call_method(w_list_obj, "extend", |
| 2749 |
space.newlist_utf8(ascii_strs, True)) |
| 2750 |
return |
| 2751 |
space.call_method(w_list_obj, "extend", space.newlist(w_items)) |
| 2752 |
return |
| 2753 |
|
| 2754 |
|
| 2755 |
|
| 2756 |
for w_item in w_items: |
| 2757 |
space.call_method(w_list_obj, "append", w_item) |
| 2758 |
dispatch[ord(op.APPENDS[0])] = load_appends |
| 2759 |
|
| 2760 |
def load_setitem(self): |
| 2761 |
w_value = data_pop(self.space, self.stack) |
| 2762 |
w_key = data_pop(self.space, self.stack) |
| 2763 |
w_dict = self._stack_top("SETITEM") |
| 2764 |
self.space.setitem(w_dict, w_key, w_value) |
| 2765 |
dispatch[ord(op.SETITEM[0])] = load_setitem |
| 2766 |
|
| 2767 |
def load_setitems(self): |
| 2768 |
items_w = self.pop_mark() |
| 2769 |
if len(items_w) % 2 != 0: |
| 2770 |
raise oefmt(unpickling_error(self.space), |
| 2771 |
"odd number of items for SETITEMS") |
| 2772 |
w_dict = self._stack_top("SETITEMS") |
| 2773 |
greenkey = self.space.iterator_greenkey(w_dict) |
| 2774 |
for i in range(0, len(items_w), 2): |
| 2775 |
jitdriver_load_setitems.jit_merge_point( |
| 2776 |
typ1=type(items_w[i]), typ2=type(items_w[i+1]), |
| 2777 |
greenkey=greenkey |
| 2778 |
) |
| 2779 |
self.space.setitem(w_dict, items_w[i], items_w[i + 1]) |
| 2780 |
dispatch[ord(op.SETITEMS[0])] = load_setitems |
| 2781 |
|
| 2782 |
def load_additems(self): |
| 2783 |
space = self.space |
| 2784 |
items = self.pop_mark() |
| 2785 |
w_set_obj = self._stack_top("ADDITEMS") |
| 2786 |
if space.isinstance_w(w_set_obj, space.w_set): |
| 2787 |
w_items = space.newtuple([w_i for w_i in items]) |
| 2788 |
space.call_method(w_set_obj,"update", w_items) |
| 2789 |
else: |
| 2790 |
for w_item in items: |
| 2791 |
space.call_method(w_set_obj,"add", w_item) |
| 2792 |
dispatch[ord(op.ADDITEMS[0])] = load_additems |
| 2793 |
|
| 2794 |
def load_build(self): |
| 2795 |
space = self.space |
| 2796 |
w_state = data_pop(self.space, self.stack) |
| 2797 |
w_inst = self._stack_top("BUILD") |
| 2798 |
w_setstate = space.findattr(w_inst, space.newtext("__setstate__")) |
| 2799 |
if not space.is_none(w_setstate): |
| 2800 |
space.call_function(w_setstate , w_state) |
| 2801 |
return |
| 2802 |
w_slotstate = space.w_None |
| 2803 |
if space.isinstance_w(w_state, space.w_tuple) and space.len_w(w_state) == 2: |
| 2804 |
w_state, w_slotstate = space.listview(w_state) |
| 2805 |
if not space.is_none(w_state): |
| 2806 |
w_inst_dict = w_inst.getdict(space) |
| 2807 |
space.call_method(w_inst_dict, "update", w_state) |
| 2808 |
if not space.is_none(w_slotstate): |
| 2809 |
w_iter = space.iter(space.call_method(w_slotstate, "items")) |
| 2810 |
while True: |
| 2811 |
try: |
| 2812 |
w_item = space.next(w_iter) |
| 2813 |
except OperationError as e: |
| 2814 |
if not e.match(space, space.w_StopIteration): |
| 2815 |
raise |
| 2816 |
break |
| 2817 |
w_key, w_value = space.unpackiterable(w_item, 2) |
| 2818 |
space.setattr(w_inst, w_key, w_value) |
| 2819 |
dispatch[ord(op.BUILD[0])] = load_build |
| 2820 |
|
| 2821 |
def load_mark(self): |
| 2822 |
self.metastack.append(self.stack) |
| 2823 |
self.stack = [] |
| 2824 |
self.append = self.stack.append |
| 2825 |
dispatch[ord(op.MARK[0])] = load_mark |
| 2826 |
|
| 2827 |
def get_memo_w(self, space): |
| 2828 |
w_d = space.newdict() |
| 2829 |
for i in range(len(self.memo)): |
| 2830 |
w_obj = self.memo[i] |
| 2831 |
if w_obj is not None: |
| 2832 |
space.setitem(w_d, space.newint(i), w_obj) |
| 2833 |
return w_d |
| 2834 |
|
| 2835 |
def set_memo_w(self, space, w_memo): |
| 2836 |
if not space.isinstance_w(w_memo, space.w_dict): |
| 2837 |
raise oefmt(space.w_TypeError, |
| 2838 |
"Unpickler.memo must be a mapping with integer keys") |
| 2839 |
self.memo = [None] * 32 |
| 2840 |
self.memo_index = 0 |
| 2841 |
w_iter = space.iter(space.call_method(w_memo, 'items')) |
| 2842 |
while True: |
| 2843 |
try: |
| 2844 |
w_item = space.next(w_iter) |
| 2845 |
except OperationError as e: |
| 2846 |
if not e.match(space, space.w_StopIteration): |
| 2847 |
raise |
| 2848 |
break |
| 2849 |
w_key, w_val = space.unpackiterable(w_item, 2) |
| 2850 |
idx = space.int_w(w_key) |
| 2851 |
if idx < 0: |
| 2852 |
raise oefmt(space.w_ValueError, |
| 2853 |
"Unpickler.memo key must be a non-negative integer") |
| 2854 |
self._memo_put(idx, w_val) |
| 2855 |
if idx >= self.memo_index: |
| 2856 |
self.memo_index = idx + 1 |
| 2857 |
|
| 2858 |
@unwrap_spec(fix_imports=bool, encoding="text", errors="text", w_buffers=WrappedDefault(None), w_file=WrappedDefault(None)) |
| 2859 |
def descr__new__unpickler(space, w_subtype, w_file=None, __kwonly__=None, fix_imports=True, encoding="ASCII", errors="strict", w_buffers=None): |
| 2860 |
w_self = space.allocate_instance(W_Unpickler, w_subtype) |
| 2861 |
if w_file is None or space.is_none(w_file): |
| 2862 |
|
| 2863 |
w_self.w_file_read = space.w_None |
| 2864 |
w_self.w_file_readline = space.w_None |
| 2865 |
w_self.fix_imports = fix_imports |
| 2866 |
w_self.encoding = encoding |
| 2867 |
w_self.errors = errors |
| 2868 |
w_self.w_buffers = None |
| 2869 |
w_self.memo = [None] * 32 |
| 2870 |
w_self.memo_index = 0 |
| 2871 |
w_self.space = space |
| 2872 |
return w_self |
| 2873 |
if not space.is_none(w_buffers): |
| 2874 |
w_buffers = space.iter(w_buffers) |
| 2875 |
W_Unpickler.__init__(w_self, space, w_file, fix_imports, encoding, errors, w_buffers) |
| 2876 |
return w_self |
| 2877 |
|
| 2878 |
W_Unpickler.typedef = TypeDef("_pickle.Unpickler", |
| 2879 |
__new__ = interp2app(descr__new__unpickler), |
| 2880 |
load = interp2app(W_Unpickler.load), |
| 2881 |
memo = GetSetProperty(W_Unpickler.get_memo_w, W_Unpickler.set_memo_w), |
| 2882 |
) |
| 2883 |
|
| 2884 |
|