| 1 |
from rpython.rlib.rarithmetic import LONG_BIT, r_longlong, r_uint |
| 2 |
from rpython.rlib.rstring import assert_str0 |
| 3 |
from rpython.rlib.mutbuffer import MutableStringBuffer |
| 4 |
from rpython.rlib.rstruct import ieee |
| 5 |
from rpython.rlib.unroll import unrolling_iterable |
| 6 |
from rpython.rlib import objectmodel |
| 7 |
|
| 8 |
from pypy.interpreter.error import OperationError, oefmt |
| 9 |
from pypy.interpreter.special import Ellipsis |
| 10 |
from pypy.interpreter.pycode import PyCode |
| 11 |
from pypy.interpreter import unicodehelper |
| 12 |
from pypy.objspace.std.boolobject import W_BoolObject |
| 13 |
from pypy.objspace.std.bytesobject import W_BytesObject |
| 14 |
from pypy.objspace.std.complexobject import W_ComplexObject |
| 15 |
from pypy.objspace.std.dictmultiobject import W_DictMultiObject |
| 16 |
from pypy.objspace.std.intobject import W_IntObject |
| 17 |
from pypy.objspace.std.floatobject import W_FloatObject |
| 18 |
from pypy.objspace.std.listobject import W_ListObject |
| 19 |
from pypy.objspace.std.longobject import W_AbstractLongObject |
| 20 |
from pypy.objspace.std.noneobject import W_NoneObject |
| 21 |
from pypy.objspace.std.setobject import W_FrozensetObject, W_SetObject |
| 22 |
from pypy.objspace.std.tupleobject import W_AbstractTupleObject |
| 23 |
from pypy.objspace.std.typeobject import W_TypeObject |
| 24 |
from pypy.objspace.std.unicodeobject import W_UnicodeObject |
| 25 |
|
| 26 |
|
| 27 |
TYPE_NULL = '0' |
| 28 |
TYPE_NONE = 'N' |
| 29 |
TYPE_FALSE = 'F' |
| 30 |
TYPE_TRUE = 'T' |
| 31 |
TYPE_STOPITER = 'S' |
| 32 |
TYPE_ELLIPSIS = '.' |
| 33 |
TYPE_INT = 'i' |
| 34 |
TYPE_FLOAT = 'f' |
| 35 |
TYPE_BINARY_FLOAT = 'g' |
| 36 |
TYPE_COMPLEX = 'x' |
| 37 |
TYPE_BINARY_COMPLEX = 'y' |
| 38 |
TYPE_LONG = 'l' |
| 39 |
TYPE_STRING = 's' |
| 40 |
TYPE_INTERNED = 't' |
| 41 |
TYPE_REF = 'r' |
| 42 |
TYPE_TUPLE = '(' |
| 43 |
TYPE_LIST = '[' |
| 44 |
TYPE_DICT = '{' |
| 45 |
TYPE_CODE = 'c' |
| 46 |
TYPE_UNICODE = 'u' |
| 47 |
TYPE_UNKNOWN = '?' |
| 48 |
TYPE_SET = '<' |
| 49 |
TYPE_FROZENSET = '>' |
| 50 |
FLAG_REF = 0x80 |
| 51 |
FLAG_DONE = '\x00' |
| 52 |
|
| 53 |
TYPE_INT64 = 'I' |
| 54 |
|
| 55 |
|
| 56 |
TYPE_ASCII = 'a' |
| 57 |
TYPE_ASCII_INTERNED = 'A' |
| 58 |
TYPE_SMALL_TUPLE = ')' |
| 59 |
TYPE_SHORT_ASCII = 'z' |
| 60 |
TYPE_SHORT_ASCII_INTERNED = 'Z' |
| 61 |
|
| 62 |
|
| 63 |
_marshallers = [] |
| 64 |
_unmarshallers = [] |
| 65 |
|
| 66 |
def marshaller(type): |
| 67 |
def _decorator(f): |
| 68 |
_marshallers.append((type, f)) |
| 69 |
return f |
| 70 |
return _decorator |
| 71 |
|
| 72 |
def unmarshaller(tc, save_ref=False): |
| 73 |
def _decorator(f): |
| 74 |
assert tc < '\x80' |
| 75 |
_unmarshallers.append((tc, f, save_ref)) |
| 76 |
return f |
| 77 |
return _decorator |
| 78 |
|
| 79 |
def write_ref(typecode, w_obj, m): |
| 80 |
if m.version < 3: |
| 81 |
return typecode |
| 82 |
try: |
| 83 |
index = m.all_refs[w_obj] |
| 84 |
except KeyError: |
| 85 |
|
| 86 |
index = len(m.all_refs) |
| 87 |
if index >= 0x7fffffff: |
| 88 |
return typecode |
| 89 |
m.all_refs[w_obj] = index |
| 90 |
return chr(ord(typecode) + FLAG_REF) |
| 91 |
else: |
| 92 |
|
| 93 |
m.atom_int(TYPE_REF, index) |
| 94 |
return FLAG_DONE |
| 95 |
|
| 96 |
def marshal(space, w_obj, m): |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if not space.type(w_obj).is_heaptype(): |
| 101 |
for type, func in _marshallers_unroll: |
| 102 |
if isinstance(w_obj, type): |
| 103 |
func(space, w_obj, m) |
| 104 |
return |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
try: |
| 109 |
s = space.readbuf_w(w_obj) |
| 110 |
except OperationError as e: |
| 111 |
if e.match(space, space.w_TypeError): |
| 112 |
raise oefmt(space.w_ValueError, "unmarshallable object") |
| 113 |
raise |
| 114 |
typecode = write_ref(TYPE_STRING, w_obj, m) |
| 115 |
if typecode != FLAG_DONE: |
| 116 |
m.atom_str(typecode, s.as_str()) |
| 117 |
|
| 118 |
|
| 119 |
@marshaller(W_NoneObject) |
| 120 |
def marshal_none(space, w_none, m): |
| 121 |
m.atom(TYPE_NONE) |
| 122 |
|
| 123 |
@unmarshaller(TYPE_NONE) |
| 124 |
def unmarshal_none(space, u, tc): |
| 125 |
return space.w_None |
| 126 |
|
| 127 |
|
| 128 |
@marshaller(W_BoolObject) |
| 129 |
def marshal_bool(space, w_bool, m): |
| 130 |
m.atom(TYPE_TRUE if w_bool.intval else TYPE_FALSE) |
| 131 |
|
| 132 |
@unmarshaller(TYPE_TRUE) |
| 133 |
def unmarshal_bool(space, u, tc): |
| 134 |
return space.w_True |
| 135 |
|
| 136 |
@unmarshaller(TYPE_FALSE) |
| 137 |
def unmarshal_false(space, u, tc): |
| 138 |
return space.w_False |
| 139 |
|
| 140 |
|
| 141 |
@marshaller(W_TypeObject) |
| 142 |
def marshal_stopiter(space, w_type, m): |
| 143 |
if not space.is_w(w_type, space.w_StopIteration): |
| 144 |
raise oefmt(space.w_ValueError, "unmarshallable object") |
| 145 |
m.atom(TYPE_STOPITER) |
| 146 |
|
| 147 |
@unmarshaller(TYPE_STOPITER) |
| 148 |
def unmarshal_stopiter(space, u, tc): |
| 149 |
return space.w_StopIteration |
| 150 |
|
| 151 |
|
| 152 |
@marshaller(Ellipsis) |
| 153 |
def marshal_ellipsis(space, w_ellipsis, m): |
| 154 |
m.atom(TYPE_ELLIPSIS) |
| 155 |
|
| 156 |
@unmarshaller(TYPE_ELLIPSIS) |
| 157 |
def unmarshal_ellipsis(space, u, tc): |
| 158 |
return space.w_Ellipsis |
| 159 |
|
| 160 |
|
| 161 |
@marshaller(W_IntObject) |
| 162 |
def marshal_int(space, w_int, m): |
| 163 |
y = w_int.intval >> 31 |
| 164 |
if y and y != -1: |
| 165 |
marshal_long(space, w_int, m) |
| 166 |
else: |
| 167 |
m.atom_int(TYPE_INT, w_int.intval) |
| 168 |
|
| 169 |
@unmarshaller(TYPE_INT) |
| 170 |
def unmarshal_int(space, u, tc): |
| 171 |
return space.newint(u.get_int()) |
| 172 |
|
| 173 |
@unmarshaller(TYPE_INT64) |
| 174 |
def unmarshal_int64(space, u, tc): |
| 175 |
from rpython.rlib.rbigint import rbigint |
| 176 |
|
| 177 |
lo = u.get_int() |
| 178 |
hi = u.get_int() |
| 179 |
if LONG_BIT >= 64: |
| 180 |
x = (hi << 32) | (lo & (2**32-1)) |
| 181 |
return space.newint(x) |
| 182 |
else: |
| 183 |
x = (r_longlong(hi) << 32) | r_longlong(r_uint(lo)) |
| 184 |
result = rbigint.fromrarith_int(x) |
| 185 |
return space.newlong_from_rbigint(result) |
| 186 |
|
| 187 |
|
| 188 |
@marshaller(W_AbstractLongObject) |
| 189 |
def marshal_long(space, w_long, m): |
| 190 |
from rpython.rlib.rarithmetic import r_ulonglong |
| 191 |
typecode = write_ref(TYPE_LONG, w_long, m) |
| 192 |
if typecode == FLAG_DONE: |
| 193 |
return |
| 194 |
m.start(typecode) |
| 195 |
SHIFT = 15 |
| 196 |
MASK = (1 << SHIFT) - 1 |
| 197 |
num = space.bigint_w(w_long) |
| 198 |
sign = num.get_sign() |
| 199 |
num = num.abs() |
| 200 |
total_length = (num.bit_length() + (SHIFT - 1)) / SHIFT |
| 201 |
m.put_int(total_length * sign) |
| 202 |
bigshiftcount = r_ulonglong(0) |
| 203 |
for i in range(total_length): |
| 204 |
next = num.abs_rshift_and_mask(bigshiftcount, MASK) |
| 205 |
m.put_short(next) |
| 206 |
bigshiftcount += SHIFT |
| 207 |
|
| 208 |
@unmarshaller(TYPE_LONG) |
| 209 |
def unmarshal_long(space, u, tc): |
| 210 |
from rpython.rlib.rbigint import rbigint |
| 211 |
lng = u.get_int() |
| 212 |
if lng < 0: |
| 213 |
negative = True |
| 214 |
lng = -lng |
| 215 |
else: |
| 216 |
negative = False |
| 217 |
digits = [u.get_short() for i in range(lng)] |
| 218 |
result = rbigint.from_list_n_bits(digits, 15) |
| 219 |
if lng and not result.tobool(): |
| 220 |
raise oefmt(space.w_ValueError, "bad marshal data") |
| 221 |
if negative: |
| 222 |
result = result.neg() |
| 223 |
|
| 224 |
try: |
| 225 |
return space.newint(result.toint()) |
| 226 |
except OverflowError: |
| 227 |
return space.newlong_from_rbigint(result) |
| 228 |
|
| 229 |
|
| 230 |
def pack_float(f): |
| 231 |
buf = MutableStringBuffer(8) |
| 232 |
ieee.pack_float(buf, 0, f, 8, False) |
| 233 |
return buf.finish() |
| 234 |
|
| 235 |
def unpack_float(s): |
| 236 |
return ieee.unpack_float(s, False) |
| 237 |
|
| 238 |
@marshaller(W_FloatObject) |
| 239 |
def marshal_float(space, w_float, m): |
| 240 |
if m.version > 1: |
| 241 |
m.start(TYPE_BINARY_FLOAT) |
| 242 |
m.put(pack_float(w_float.floatval)) |
| 243 |
else: |
| 244 |
m.start(TYPE_FLOAT) |
| 245 |
m.put_pascal(space.text_w(space.repr(w_float))) |
| 246 |
|
| 247 |
@unmarshaller(TYPE_FLOAT) |
| 248 |
def unmarshal_float(space, u, tc): |
| 249 |
return space.call_function(space.builtin.get('float'), |
| 250 |
space.newtext(u.get_pascal())) |
| 251 |
|
| 252 |
@unmarshaller(TYPE_BINARY_FLOAT) |
| 253 |
def unmarshal_float_bin(space, u, tc): |
| 254 |
return space.newfloat(unpack_float(u.get(8))) |
| 255 |
|
| 256 |
|
| 257 |
@marshaller(W_ComplexObject) |
| 258 |
def marshal_complex(space, w_complex, m): |
| 259 |
if m.version > 1: |
| 260 |
m.start(TYPE_BINARY_COMPLEX) |
| 261 |
m.put(pack_float(w_complex.realval)) |
| 262 |
m.put(pack_float(w_complex.imagval)) |
| 263 |
else: |
| 264 |
w_real = space.newfloat(w_complex.realval) |
| 265 |
w_imag = space.newfloat(w_complex.imagval) |
| 266 |
m.start(TYPE_COMPLEX) |
| 267 |
m.put_pascal(space.text_w(space.repr(w_real))) |
| 268 |
m.put_pascal(space.text_w(space.repr(w_imag))) |
| 269 |
|
| 270 |
@unmarshaller(TYPE_COMPLEX) |
| 271 |
def unmarshal_complex(space, u, tc): |
| 272 |
w_real = space.call_function(space.builtin.get('float'), |
| 273 |
space.newtext(u.get_pascal())) |
| 274 |
w_imag = space.call_function(space.builtin.get('float'), |
| 275 |
space.newtext(u.get_pascal())) |
| 276 |
w_t = space.builtin.get('complex') |
| 277 |
return space.call_function(w_t, w_real, w_imag) |
| 278 |
|
| 279 |
@unmarshaller(TYPE_BINARY_COMPLEX) |
| 280 |
def unmarshal_complex_bin(space, u, tc): |
| 281 |
real = unpack_float(u.get(8)) |
| 282 |
imag = unpack_float(u.get(8)) |
| 283 |
return space.newcomplex(real, imag) |
| 284 |
|
| 285 |
|
| 286 |
@marshaller(W_BytesObject) |
| 287 |
def marshal_bytes(space, w_str, m): |
| 288 |
typecode = write_ref(TYPE_STRING, w_str, m) |
| 289 |
if typecode != FLAG_DONE: |
| 290 |
s = space.bytes_w(w_str) |
| 291 |
m.atom_str(typecode, s) |
| 292 |
|
| 293 |
@unmarshaller(TYPE_STRING) |
| 294 |
def unmarshal_bytes(space, u, tc): |
| 295 |
return space.newbytes(u.get_str()) |
| 296 |
|
| 297 |
|
| 298 |
def _tuple_typecode(tuple_w, m): |
| 299 |
if m.version >= 4 and len(tuple_w) < 256: |
| 300 |
return TYPE_SMALL_TUPLE, True |
| 301 |
return TYPE_TUPLE, False |
| 302 |
|
| 303 |
def _marshal_tuple(space, tuple_w, m): |
| 304 |
"""Marshal a list of wrapped objects as a tuple (for code object internals).""" |
| 305 |
typecode, single_byte_size = _tuple_typecode(tuple_w, m) |
| 306 |
m.put_tuple_w(typecode, tuple_w, single_byte_size=single_byte_size) |
| 307 |
|
| 308 |
@marshaller(W_AbstractTupleObject) |
| 309 |
def marshal_tuple(space, w_tuple, m): |
| 310 |
tuple_w = w_tuple.tolist() |
| 311 |
typecode, single_byte_size = _tuple_typecode(tuple_w, m) |
| 312 |
typecode = write_ref(typecode, w_tuple, m) |
| 313 |
if typecode == FLAG_DONE: |
| 314 |
return |
| 315 |
m.put_tuple_w(typecode, tuple_w, single_byte_size=single_byte_size) |
| 316 |
|
| 317 |
@unmarshaller(TYPE_TUPLE) |
| 318 |
def unmarshal_tuple(space, u, tc): |
| 319 |
items_w = u.get_tuple_w() |
| 320 |
return space.newtuple(items_w) |
| 321 |
|
| 322 |
@unmarshaller(TYPE_SMALL_TUPLE) |
| 323 |
def unmarshal_tuple(space, u, tc): |
| 324 |
items_w = u.get_tuple_w(single_byte_size=True) |
| 325 |
return space.newtuple(items_w) |
| 326 |
|
| 327 |
|
| 328 |
@marshaller(W_ListObject) |
| 329 |
def marshal_list(space, w_list, m): |
| 330 |
typecode = write_ref(TYPE_LIST, w_list, m) |
| 331 |
if typecode != FLAG_DONE: |
| 332 |
items = w_list.getitems()[:] |
| 333 |
m.put_tuple_w(typecode, items) |
| 334 |
|
| 335 |
@unmarshaller(TYPE_LIST, save_ref=True) |
| 336 |
def unmarshal_list(space, u, tc): |
| 337 |
w_obj = space.newlist([]) |
| 338 |
u.save_ref(tc, w_obj) |
| 339 |
for w_item in u.get_tuple_w(): |
| 340 |
w_obj.append(w_item) |
| 341 |
return w_obj |
| 342 |
|
| 343 |
|
| 344 |
@marshaller(W_DictMultiObject) |
| 345 |
def marshal_dict(space, w_dict, m): |
| 346 |
typecode = write_ref(TYPE_DICT, w_dict, m) |
| 347 |
if typecode == FLAG_DONE: |
| 348 |
return |
| 349 |
m.start(typecode) |
| 350 |
for w_tuple in w_dict.items(): |
| 351 |
w_key, w_value = space.fixedview(w_tuple, 2) |
| 352 |
m.put_w_obj(w_key) |
| 353 |
m.put_w_obj(w_value) |
| 354 |
m.atom(TYPE_NULL) |
| 355 |
|
| 356 |
@unmarshaller(TYPE_DICT, save_ref=True) |
| 357 |
def unmarshal_dict(space, u, tc): |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
w_dic = space.newdict() |
| 362 |
u.save_ref(tc, w_dic) |
| 363 |
while 1: |
| 364 |
w_key = u.load_w_obj(allow_null=True) |
| 365 |
if w_key is None: |
| 366 |
break |
| 367 |
w_value = u.load_w_obj() |
| 368 |
space.setitem(w_dic, w_key, w_value) |
| 369 |
return w_dic |
| 370 |
|
| 371 |
@unmarshaller(TYPE_NULL) |
| 372 |
def unmarshal_NULL(self, u, tc): |
| 373 |
return None |
| 374 |
|
| 375 |
|
| 376 |
@marshaller(PyCode) |
| 377 |
def marshal_pycode(space, w_pycode, m): |
| 378 |
typecode = write_ref(TYPE_CODE, w_pycode, m) |
| 379 |
if typecode == FLAG_DONE: |
| 380 |
return |
| 381 |
m.start(typecode) |
| 382 |
|
| 383 |
x = space.interp_w(PyCode, w_pycode) |
| 384 |
m.put_int(x.co_argcount) |
| 385 |
m.put_int(x.co_posonlyargcount) |
| 386 |
m.put_int(x.co_kwonlyargcount) |
| 387 |
m.put_int(x.co_nlocals) |
| 388 |
m.put_int(x.co_stacksize) |
| 389 |
m.put_int(x.co_flags) |
| 390 |
m.atom_str(TYPE_STRING, x.co_code) |
| 391 |
_marshal_tuple(space, x.co_consts_w, m) |
| 392 |
_marshal_tuple(space, x.co_names_w, m) |
| 393 |
co_varnames_w = [space.newtext(*_decode_utf8(space, s)) for s in x.co_varnames] |
| 394 |
co_freevars_w = [space.newtext(*_decode_utf8(space, s)) for s in x.co_freevars] |
| 395 |
co_cellvars_w = [space.newtext(*_decode_utf8(space, s)) for s in x.co_cellvars] |
| 396 |
_marshal_tuple(space, co_varnames_w, m) |
| 397 |
_marshal_tuple(space, co_freevars_w, m) |
| 398 |
_marshal_tuple(space, co_cellvars_w, m) |
| 399 |
marshal(space, x.w_filename, m) |
| 400 |
_marshal_unicode(space, x.co_name, m) |
| 401 |
_marshal_unicode(space, x.co_qualname, m) |
| 402 |
m.put_int(x.co_firstlineno) |
| 403 |
m.atom_str(TYPE_STRING, x.co_linetable) |
| 404 |
m.atom_str(TYPE_STRING, x.co_exceptiontable) |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
def _unmarshal_strlist(u): |
| 410 |
items_w = _unmarshal_tuple_w(u) |
| 411 |
return [u.space.utf8_w(w_item) for w_item in items_w] |
| 412 |
|
| 413 |
def _unmarshal_tuple_w(u): |
| 414 |
w_obj = u.load_w_obj() |
| 415 |
try: |
| 416 |
return u.space.fixedview(w_obj) |
| 417 |
except OperationError as e: |
| 418 |
if e.match(u.space, u.space.w_TypeError): |
| 419 |
u.raise_exc('invalid marshal data for code object') |
| 420 |
raise |
| 421 |
|
| 422 |
@unmarshaller(TYPE_CODE, save_ref=True) |
| 423 |
def unmarshal_pycode(space, u, tc): |
| 424 |
w_codeobj = objectmodel.instantiate(PyCode) |
| 425 |
u.save_ref(tc, w_codeobj) |
| 426 |
argcount = u.get_int() |
| 427 |
posonlyargcount = u.get_int() |
| 428 |
kwonlyargcount = u.get_int() |
| 429 |
nlocals = u.get_int() |
| 430 |
stacksize = u.get_int() |
| 431 |
flags = u.get_int() |
| 432 |
code = space.bytes_w(u.load_w_obj()) |
| 433 |
consts_w = _unmarshal_tuple_w(u) |
| 434 |
names = _unmarshal_strlist(u) |
| 435 |
varnames = _unmarshal_strlist(u) |
| 436 |
freevars = _unmarshal_strlist(u) |
| 437 |
cellvars = _unmarshal_strlist(u) |
| 438 |
w_fn = u.load_w_obj() |
| 439 |
filename = space.bytes0_w(space.fsencode(w_fn)) |
| 440 |
|
| 441 |
name = space.utf8_w(u.load_w_obj()) |
| 442 |
qualname = space.utf8_w(u.load_w_obj()) |
| 443 |
firstlineno = u.get_int() |
| 444 |
position_info = space.bytes_w(u.load_w_obj()) |
| 445 |
exceptiontable = space.bytes_w(u.load_w_obj()) |
| 446 |
filename = assert_str0(filename) |
| 447 |
PyCode.__init__(w_codeobj, |
| 448 |
space, argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, |
| 449 |
code, consts_w[:], names, varnames, filename, |
| 450 |
name, qualname, firstlineno, position_info, freevars, cellvars, |
| 451 |
exceptiontable=exceptiontable, |
| 452 |
hidden_applevel=u.hidden_applevel) |
| 453 |
return w_codeobj |
| 454 |
|
| 455 |
def _marshal_ascii_unicode(space, s, m, w_unicode, w_interned): |
| 456 |
|
| 457 |
if len(s) < 256: |
| 458 |
is_short = True |
| 459 |
if w_interned is not None: |
| 460 |
typecode = TYPE_SHORT_ASCII_INTERNED |
| 461 |
else: |
| 462 |
typecode = TYPE_SHORT_ASCII |
| 463 |
else: |
| 464 |
is_short = False |
| 465 |
if w_interned is not None: |
| 466 |
typecode = TYPE_ASCII_INTERNED |
| 467 |
else: |
| 468 |
typecode = TYPE_ASCII |
| 469 |
|
| 470 |
if w_unicode is not None: |
| 471 |
typecode = write_ref(typecode, w_unicode, m) |
| 472 |
if typecode == FLAG_DONE: |
| 473 |
return |
| 474 |
|
| 475 |
m.start(typecode) |
| 476 |
if is_short: |
| 477 |
m.put(chr(len(s))) |
| 478 |
else: |
| 479 |
m.put_int(len(s)) |
| 480 |
m.put(s) |
| 481 |
|
| 482 |
def _marshal_unicode(space, s, m, w_unicode=None): |
| 483 |
from rpython.rlib import rutf8 |
| 484 |
if m.version >= 3: |
| 485 |
w_interned = space.get_interned_str(s) |
| 486 |
else: |
| 487 |
w_interned = None |
| 488 |
if w_interned is not None: |
| 489 |
w_unicode = w_interned |
| 490 |
typecode = TYPE_INTERNED |
| 491 |
else: |
| 492 |
typecode = TYPE_UNICODE |
| 493 |
if m.version >= 4: |
| 494 |
|
| 495 |
if w_unicode is not None: |
| 496 |
is_ascii = w_unicode.is_ascii() |
| 497 |
else: |
| 498 |
is_ascii = len(s) == rutf8.check_utf8(s, True) |
| 499 |
if is_ascii: |
| 500 |
_marshal_ascii_unicode(space, s, m, w_unicode, w_interned) |
| 501 |
return |
| 502 |
|
| 503 |
if w_unicode is not None: |
| 504 |
typecode = write_ref(typecode, w_unicode, m) |
| 505 |
if typecode == FLAG_DONE: |
| 506 |
return |
| 507 |
m.atom_str(typecode, s) |
| 508 |
|
| 509 |
|
| 510 |
_decode_utf8 = unicodehelper.decode_utf8sp |
| 511 |
|
| 512 |
@marshaller(W_UnicodeObject) |
| 513 |
def marshal_unicode(space, w_unicode, m): |
| 514 |
s = space.utf8_w(w_unicode) |
| 515 |
_marshal_unicode(space, s, m, w_unicode=w_unicode) |
| 516 |
|
| 517 |
@unmarshaller(TYPE_UNICODE) |
| 518 |
def unmarshal_unicode(space, u, tc): |
| 519 |
uc = _decode_utf8(space, u.get_str()) |
| 520 |
return space.newtext(*uc) |
| 521 |
|
| 522 |
@unmarshaller(TYPE_INTERNED) |
| 523 |
def unmarshal_interned(space, u, tc): |
| 524 |
w_ret = unmarshal_unicode(space, u, tc) |
| 525 |
return u.space.new_interned_w_str(w_ret) |
| 526 |
|
| 527 |
def _unmarshal_ascii(space, u, short_length, interned): |
| 528 |
from rpython.rlib import rutf8 |
| 529 |
if short_length: |
| 530 |
lng = ord(u.get1()) |
| 531 |
else: |
| 532 |
lng = u.get_lng() |
| 533 |
s = u.get(lng) |
| 534 |
try: |
| 535 |
rutf8.check_ascii(s) |
| 536 |
except rutf8.CheckError: |
| 537 |
raise oefmt(space.w_ValueError, "bad marshal data (string is not ascii)") |
| 538 |
w_u = u.space.newtext(s, len(s)) |
| 539 |
if interned: |
| 540 |
w_u = u.space.new_interned_w_str(w_u) |
| 541 |
return w_u |
| 542 |
|
| 543 |
@unmarshaller(TYPE_ASCII) |
| 544 |
def unmarshal_ascii(space, u, tc): |
| 545 |
return _unmarshal_ascii(space, u, False, False) |
| 546 |
@unmarshaller(TYPE_ASCII_INTERNED) |
| 547 |
def unmarshal_ascii(space, u, tc): |
| 548 |
return _unmarshal_ascii(space, u, False, True) |
| 549 |
@unmarshaller(TYPE_SHORT_ASCII) |
| 550 |
def unmarshal_ascii(space, u, tc): |
| 551 |
return _unmarshal_ascii(space, u, True, False) |
| 552 |
@unmarshaller(TYPE_SHORT_ASCII_INTERNED) |
| 553 |
def unmarshal_ascii(space, u, tc): |
| 554 |
return _unmarshal_ascii(space, u, True, True) |
| 555 |
|
| 556 |
|
| 557 |
@marshaller(W_SetObject) |
| 558 |
def marshal_set(space, w_set, m): |
| 559 |
typecode = write_ref(TYPE_SET, w_set, m) |
| 560 |
if typecode != FLAG_DONE: |
| 561 |
lis_w = space.fixedview(w_set) |
| 562 |
m.put_tuple_w(typecode, lis_w) |
| 563 |
|
| 564 |
@unmarshaller(TYPE_SET, save_ref=True) |
| 565 |
def unmarshal_set(space, u, tc): |
| 566 |
w_set = space.call_function(space.w_set) |
| 567 |
u.save_ref(tc, w_set) |
| 568 |
_unmarshal_set_frozenset(space, u, w_set) |
| 569 |
return w_set |
| 570 |
|
| 571 |
|
| 572 |
@marshaller(W_FrozensetObject) |
| 573 |
def marshal_frozenset(space, w_frozenset, m): |
| 574 |
typecode = write_ref(TYPE_FROZENSET, w_frozenset, m) |
| 575 |
if typecode != FLAG_DONE: |
| 576 |
lis_w = space.fixedview(w_frozenset) |
| 577 |
m.put_tuple_w(typecode, lis_w) |
| 578 |
|
| 579 |
def _unmarshal_set_frozenset(space, u, w_set): |
| 580 |
lng = u.get_lng() |
| 581 |
for i in xrange(lng): |
| 582 |
w_obj = u.load_w_obj() |
| 583 |
space.call_method(w_set, "add", w_obj) |
| 584 |
|
| 585 |
@unmarshaller(TYPE_FROZENSET) |
| 586 |
def unmarshal_frozenset(space, u, tc): |
| 587 |
w_set = space.call_function(space.w_set) |
| 588 |
_unmarshal_set_frozenset(space, u, w_set) |
| 589 |
return space.call_function(space.w_frozenset, w_set) |
| 590 |
|
| 591 |
|
| 592 |
@unmarshaller(TYPE_REF) |
| 593 |
def unmarshal_ref(space, u, tc): |
| 594 |
index = u.get_lng() |
| 595 |
if 0 <= index < len(u.refs_w): |
| 596 |
w_obj = u.refs_w[index] |
| 597 |
else: |
| 598 |
w_obj = None |
| 599 |
if w_obj is None: |
| 600 |
raise oefmt(space.w_ValueError, "bad marshal data (invalid reference)") |
| 601 |
return w_obj |
| 602 |
|
| 603 |
|
| 604 |
_marshallers_unroll = unrolling_iterable(_marshallers) |
| 605 |
_unmarshallers_unroll = unrolling_iterable(_unmarshallers) |