| 1 |
import pytest |
| 2 |
from _pickle import Pickler, PicklingError, dumps, dump |
| 3 |
from _pickle import Unpickler, UnpicklingError, loads |
| 4 |
import pickle |
| 5 |
from pickle import _dumps as dumps_py, PickleBuffer |
| 6 |
from copyreg import dispatch_table |
| 7 |
import io |
| 8 |
import sys |
| 9 |
import datetime |
| 10 |
|
| 11 |
try: |
| 12 |
from copyreg import K |
| 13 |
except ImportError: |
| 14 |
|
| 15 |
class K(): |
| 16 |
def __init__(self, value): |
| 17 |
self.value = value |
| 18 |
|
| 19 |
def __reduce__(self): |
| 20 |
|
| 21 |
return K, (self.value,) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
protocols = range(5, -1, -1) |
| 26 |
|
| 27 |
DICT = { |
| 28 |
'ads_flags': 0, |
| 29 |
'age': 18, |
| 30 |
'birthday': datetime.date(1980, 5, 7), |
| 31 |
'bulletin_count': 0, |
| 32 |
'comment_count': 0, |
| 33 |
'country': 'BR', |
| 34 |
'encrypted_id': 'G9urXXAJwjE', |
| 35 |
'favorite_count': 9, |
| 36 |
'first_name': '', |
| 37 |
'flags': 412317970704, |
| 38 |
'friend_count': 0, |
| 39 |
'gender': 'm', |
| 40 |
'gender_for_display': 'Male', |
| 41 |
'id': 302935349, |
| 42 |
'is_custom_profile_icon': 0, |
| 43 |
'last_name': '', |
| 44 |
'locale_preference': 'pt_BR', |
| 45 |
'member': 0, |
| 46 |
'tags': ['a', 'b', 'c', 'd', 'e', 'f', 'g'], |
| 47 |
'profile_foo_id': 827119638, |
| 48 |
'secure_encrypted_id': 'Z_xxx2dYx3t4YAdnmfgyKw', |
| 49 |
'session_number': 2, |
| 50 |
'signup_id': '201-19225-223', |
| 51 |
'status': 'A', |
| 52 |
'theme': 1, |
| 53 |
'time_created': 1225237014, |
| 54 |
'time_updated': 1233134493, |
| 55 |
'unread_message_count': 0, |
| 56 |
'user_group': '0', |
| 57 |
'username': 'collinwinter', |
| 58 |
'play_count': 9, |
| 59 |
'view_count': 7, |
| 60 |
'zip': ''} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
def test_int(): |
| 65 |
for proto in protocols: |
| 66 |
n = sys.maxsize |
| 67 |
while n: |
| 68 |
for expected in (-n, n): |
| 69 |
s = dumps(expected, proto) |
| 70 |
s2 = pickle._dumps(expected, proto) |
| 71 |
assert s == s2 |
| 72 |
n2 = loads(s) |
| 73 |
assert expected == n2 |
| 74 |
n = n >> 1 |
| 75 |
|
| 76 |
def test_ints(): |
| 77 |
for proto in protocols: |
| 78 |
n = sys.maxsize |
| 79 |
while n: |
| 80 |
for expected in (-n, n): |
| 81 |
s1= dumps(expected, proto) |
| 82 |
s2 = dumps_py(expected, proto) |
| 83 |
assert s1 == s2 |
| 84 |
n2 = loads(s1) |
| 85 |
assert expected == n2, "expected %d got %d for protocol %d" %(expected, n2, proto) |
| 86 |
n = n >> 1 |
| 87 |
|
| 88 |
|
| 89 |
def test_long(): |
| 90 |
for proto in protocols: |
| 91 |
|
| 92 |
for nbits in 1, 8, 8*254, 8*255, 8*256, 8*257: |
| 93 |
nbase = 1 << nbits |
| 94 |
for npos in nbase-1, nbase, nbase+1: |
| 95 |
for n in npos, -npos: |
| 96 |
pickle = dumps(n, proto) |
| 97 |
got = loads(pickle) |
| 98 |
assert n == got |
| 99 |
|
| 100 |
|
| 101 |
nbase = int("deadbeeffeedface", 16) |
| 102 |
nbase += nbase << 1000000 |
| 103 |
for n in nbase, -nbase: |
| 104 |
p = dumps(n, 2) |
| 105 |
got = loads(p) |
| 106 |
assert n == got |
| 107 |
|
| 108 |
|
| 109 |
def test_none_true_false(): |
| 110 |
s = dumps(None) |
| 111 |
assert s == b'\x80\x04N.' |
| 112 |
assert loads(s) is None |
| 113 |
s = dumps(True) |
| 114 |
assert s == b'\x80\x04\x88.' |
| 115 |
assert loads(s) is True |
| 116 |
s = dumps(False) |
| 117 |
assert s == b'\x80\x04\x89.' |
| 118 |
assert loads(s) is False |
| 119 |
|
| 120 |
def test_bytes(): |
| 121 |
s = dumps(b'abc') |
| 122 |
assert s == b'\x80\x04\x95\x07\x00\x00\x00\x00\x00\x00\x00C\x03abc\x94.' |
| 123 |
assert loads(s) == b"abc" |
| 124 |
s = dumps(b'abc' * 1000) |
| 125 |
expected = b'\x80\x04\x95\xbf\x0b\x00\x00\x00\x00\x00\x00B\xb8\x0b\x00\x00abcabcabcab' |
| 126 |
assert s.startswith(expected) |
| 127 |
assert loads(s) == b"abc" * 1000 |
| 128 |
s = dumps(b'abc' * 1000000) |
| 129 |
assert s.startswith(b'\x80\x04B\xc0\xc6-\x00abcabcabcabc') |
| 130 |
assert loads(s) == b"abc" * 1000000 |
| 131 |
|
| 132 |
def test_unicode(): |
| 133 |
s = dumps(u'abc') |
| 134 |
assert s == b'\x80\x04\x95\x07\x00\x00\x00\x00\x00\x00\x00\x8c\x03abc\x94.' |
| 135 |
assert loads(s) == u"abc" |
| 136 |
s = dumps(u'abc' * 1000) |
| 137 |
assert s.startswith(b'\x80\x04\x95\xbf\x0b\x00\x00\x00\x00\x00\x00X\xb8\x0b\x00\x00abcabcabc') |
| 138 |
assert loads(s) == u"abc" * 1000 |
| 139 |
s = dumps(u'abc' * 100000) |
| 140 |
assert s.startswith(b'\x80\x04X\xe0\x93\x04\x00abcabcabcabcabc') |
| 141 |
assert loads(s) == u'abc' * 100000 |
| 142 |
|
| 143 |
def test_tuple(): |
| 144 |
s = dumps(()) |
| 145 |
assert s == b'\x80\x04).' |
| 146 |
assert loads(s) == () |
| 147 |
s = dumps((1, )) |
| 148 |
assert s.startswith(b'\x80\x04\x95\x05\x00\x00\x00\x00\x00\x00\x00K\x01\x85\x94.') |
| 149 |
assert loads(s) == (1,) |
| 150 |
s = dumps((1, ) * 1000) |
| 151 |
assert s.startswith(b'\x80\x04\x95\xd4\x07\x00\x00\x00\x00\x00\x00(K\x01K\x01K') |
| 152 |
assert loads(s) == (1, ) * 1000 |
| 153 |
|
| 154 |
def test_memo(): |
| 155 |
a = "abcdefghijkl" |
| 156 |
s = dumps((a, a)) |
| 157 |
assert s.count(b'abcdefghijkl') == 1 |
| 158 |
assert loads(s) == (a, a) |
| 159 |
a = (1, ) |
| 160 |
s = dumps((a, a)) |
| 161 |
assert s.count(b'K\x01\x85') == 1 |
| 162 |
assert loads(s) == (a, a) |
| 163 |
|
| 164 |
def test_list(): |
| 165 |
s = dumps([]) |
| 166 |
assert s == b'\x80\x04]\x94.' |
| 167 |
assert loads(s) == [] |
| 168 |
s = dumps([1]) |
| 169 |
assert s == b'\x80\x04\x95\x06\x00\x00\x00\x00\x00\x00\x00]\x94K\x01a.' |
| 170 |
assert loads(s) == [1] |
| 171 |
s = dumps([1] * 1000) |
| 172 |
assert s.startswith(b'\x80\x04\x95\xd5\x07\x00\x00\x00\x00\x00\x00]\x94(K\x01K\x01K\x01K\x01K\x01') |
| 173 |
assert loads(s) == [1] * 1000 |
| 174 |
|
| 175 |
def test_list_memo(): |
| 176 |
l = [] |
| 177 |
l.append(l) |
| 178 |
s = dumps(l) |
| 179 |
assert s == b'\x80\x04\x95\x06\x00\x00\x00\x00\x00\x00\x00]\x94h\x00a.' |
| 180 |
l_roundtrip = loads(s) |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
t = (1, 2, 3, [], "abc") |
| 185 |
t[3].append(t) |
| 186 |
s = dumps(t) |
| 187 |
assert s == b'\x80\x04\x95!\x00\x00\x00\x00\x00\x00\x00(K\x01K\x02K\x03]\x94(K\x01K\x02K\x03h\x00\x8c\x03abc\x94t\x94ah\x011h\x02.' |
| 188 |
t_roundtrip = loads(s) |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
def test_dict(): |
| 193 |
s = dumps({}) |
| 194 |
assert s == b'\x80\x04}\x94.' |
| 195 |
assert loads(s) == {} |
| 196 |
s = dumps({1: 2}) |
| 197 |
assert s == b'\x80\x04\x95\x08\x00\x00\x00\x00\x00\x00\x00}\x94K\x01K\x02s.' |
| 198 |
assert loads(s) == {1: 2} |
| 199 |
d = dict([(a, str(a)) for a in range(10000)]) |
| 200 |
s = dumps(d) |
| 201 |
assert s.startswith(b'\x80\x04\x95\x03\x00\x01\x00\x00\x00\x00\x00}\x94(K\x00\x8c\x010\x94K\x01\x8c') |
| 202 |
assert loads(s) == d |
| 203 |
s = dumps({1: 2}, 0) |
| 204 |
assert s == b'(dp0\nI1\nI2\ns.' |
| 205 |
assert loads(s) == {1: 2} |
| 206 |
for proto in [-1]: |
| 207 |
s1 = dumps(DICT, proto) |
| 208 |
s2 = dumps_py(DICT, proto) |
| 209 |
assert s1 == s2 |
| 210 |
val = loads(s1) |
| 211 |
assert val == DICT |
| 212 |
|
| 213 |
def test_reduce(): |
| 214 |
import sys |
| 215 |
class A: |
| 216 |
pass |
| 217 |
|
| 218 |
mod = type(sys)('fakemod') |
| 219 |
mod.A = A |
| 220 |
A.__module__ = 'fakemod' |
| 221 |
A.__qualname__ = 'A' |
| 222 |
A.__name__ = 'A' |
| 223 |
|
| 224 |
sys.modules['fakemod'] = mod |
| 225 |
try: |
| 226 |
|
| 227 |
a = A() |
| 228 |
a.x = 'abc' |
| 229 |
s = dumps(a) |
| 230 |
assert s == b'\x80\x04\x95"\x00\x00\x00\x00\x00\x00\x00\x8c\x07fakemod\x94\x8c\x01A\x94\x93\x94)\x81\x94}\x94\x8c\x01x\x94\x8c\x03abc\x94sb.' |
| 231 |
a_roundtrip = loads(s) |
| 232 |
assert type(a_roundtrip) == type(a) |
| 233 |
assert a_roundtrip.x == "abc" |
| 234 |
finally: |
| 235 |
del sys.modules['fakemod'] |
| 236 |
|
| 237 |
def test_globals(): |
| 238 |
s = dumps(dumps) |
| 239 |
assert s == b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00\x8c\x07_pickle\x94\x8c\x05dumps\x94\x93\x94.' |
| 240 |
assert loads(s) == dumps |
| 241 |
|
| 242 |
def test_float(): |
| 243 |
s = dumps(1.234) |
| 244 |
assert s == b'\x80\x04\x95\n\x00\x00\x00\x00\x00\x00\x00G?\xf3\xbev\xc8\xb49X.' |
| 245 |
assert loads(s) == 1.234 |
| 246 |
|
| 247 |
def test_frozenset(): |
| 248 |
f = frozenset((1, 2, 3)) |
| 249 |
s = dumps(f) |
| 250 |
assert pickle.FROZENSET in s |
| 251 |
f2 = loads(s) |
| 252 |
assert isinstance(f2, frozenset) |
| 253 |
assert f == f2 |
| 254 |
|
| 255 |
def test_bytearray(): |
| 256 |
for proto in protocols: |
| 257 |
for s in b'', b'xyz', b'xyz'*100: |
| 258 |
b = bytearray(s) |
| 259 |
p = dumps(b, proto) |
| 260 |
bb = loads(p) |
| 261 |
assert bb is not b |
| 262 |
assert b == bb |
| 263 |
if proto <= 3: |
| 264 |
|
| 265 |
assert b'bytearray' in p |
| 266 |
assert pickle.GLOBAL in p |
| 267 |
elif proto == 4: |
| 268 |
assert b'bytearray' in p |
| 269 |
assert pickle.STACK_GLOBAL in p |
| 270 |
elif proto == 5: |
| 271 |
assert b'bytearray' not in p |
| 272 |
assert pickle.BYTEARRAY8 in p |
| 273 |
|
| 274 |
def test_complex(): |
| 275 |
c = 1+2j |
| 276 |
s = dumps(c) |
| 277 |
c2 = loads(s) |
| 278 |
assert c2 == c |
| 279 |
|
| 280 |
def test_dispatch_table(): |
| 281 |
c = 1 + 2j |
| 282 |
f = io.BytesIO() |
| 283 |
p = Pickler(f, 4) |
| 284 |
p.dispatch_table = dispatch_table.copy() |
| 285 |
|
| 286 |
|
| 287 |
REDUCE_1 = 'reduce_1' |
| 288 |
def reduce_1(obj): |
| 289 |
return str, (REDUCE_1,) |
| 290 |
|
| 291 |
p.dispatch_table[complex] = reduce_1 |
| 292 |
p.dump(c) |
| 293 |
s = f.getvalue() |
| 294 |
assert s == b'\x80\x04\x95#\x00\x00\x00\x00\x00\x00\x00\x8c\x08builtins\x94\x8c\x03str\x94\x93\x94\x8c\x08reduce_1\x94\x85\x94R\x94.' |
| 295 |
c2 = loads(s) |
| 296 |
assert c2 == 'reduce_1' |
| 297 |
|
| 298 |
def test_bad_mark(): |
| 299 |
badpickles = [ |
| 300 |
b'N(.', |
| 301 |
b'N(2', |
| 302 |
b'cbuiltins\nlist\n)(R', |
| 303 |
b'cbuiltins\nlist\n()R', |
| 304 |
b']N(a', |
| 305 |
|
| 306 |
b'cbuiltins\nValueError\n)R}(b', |
| 307 |
b'cbuiltins\nValueError\n)R(}b', |
| 308 |
|
| 309 |
b'N(p1\n', |
| 310 |
b'N(q\x00', |
| 311 |
b'N(r\x00\x00\x00\x00', |
| 312 |
b'}NN(s', |
| 313 |
b'}N(Ns', |
| 314 |
b'}(NNs', |
| 315 |
b'}((u', |
| 316 |
b'cbuiltins\nlist\n)(\x81', |
| 317 |
b'cbuiltins\nlist\n()\x81', |
| 318 |
b'N(\x85', |
| 319 |
b'NN(\x86', |
| 320 |
b'N(N\x86', |
| 321 |
b'NNN(\x87', |
| 322 |
b'NN(N\x87', |
| 323 |
b'N(NN\x87', |
| 324 |
b']((\x90', |
| 325 |
|
| 326 |
b'cbuiltins\nlist\n)}(\x92', |
| 327 |
b'cbuiltins\nlist\n)(}\x92', |
| 328 |
b'cbuiltins\nlist\n()}\x92', |
| 329 |
|
| 330 |
b'Vbuiltins\n(Vlist\n\x93', |
| 331 |
b'Vbuiltins\nVlist\n(\x93', |
| 332 |
b'N(\x94', |
| 333 |
b'e', |
| 334 |
] |
| 335 |
for data in badpickles: |
| 336 |
try: |
| 337 |
loads(data) |
| 338 |
except (UnpicklingError, IndexError) as exc: |
| 339 |
pass |
| 340 |
|
| 341 |
def test_truncated(): |
| 342 |
badpickles = [ |
| 343 |
b'', |
| 344 |
b'B\x03\x00\x00', |
| 345 |
b'\x8e\x03\x00\x00\x00\x00\x00\x00', |
| 346 |
b'Np0', |
| 347 |
b'ibuiltins\nlist\n', |
| 348 |
b'jens:', |
| 349 |
] |
| 350 |
for data in badpickles: |
| 351 |
try: |
| 352 |
loads(data) |
| 353 |
except (UnpicklingError, EOFError) as e: |
| 354 |
pass |
| 355 |
|
| 356 |
def test_maxint64(): |
| 357 |
maxint64 = (1 << 63) - 1 |
| 358 |
data = b'I' + str(maxint64).encode("ascii") + b'\n.' |
| 359 |
got = loads(data) |
| 360 |
assert maxint64 == got |
| 361 |
|
| 362 |
|
| 363 |
data = b'I' + str(maxint64).encode("ascii") + b'JUNK\n.' |
| 364 |
try: |
| 365 |
loads(data) |
| 366 |
except ValueError as e: |
| 367 |
pass |
| 368 |
pass |
| 369 |
|
| 370 |
def test_find_class(): |
| 371 |
data = b'\x80\x02c__builtin__\nxrange\nK\x01K\x07K\x01\x87R.' |
| 372 |
got = loads(data) |
| 373 |
assert isinstance(got, range) |
| 374 |
|
| 375 |
def test_function(): |
| 376 |
for method, args in ( |
| 377 |
({1, 2}.__contains__, (2,)), |
| 378 |
(set.__contains__, ({1, 2}, 2)), |
| 379 |
): |
| 380 |
for proto in protocols: |
| 381 |
data = dumps(method, proto) |
| 382 |
pydata = dumps_py(method, proto) |
| 383 |
assert data == pydata |
| 384 |
got = loads(data) |
| 385 |
assert got(*args) == method(*args) |
| 386 |
|
| 387 |
def test_unseekable(): |
| 388 |
class UnseekableIO(io.BytesIO): |
| 389 |
def peek(self, *args): |
| 390 |
raise NotImplementedError |
| 391 |
|
| 392 |
def seekable(self): |
| 393 |
return False |
| 394 |
|
| 395 |
def seek(self, *args): |
| 396 |
raise io.UnsupportedOperation |
| 397 |
|
| 398 |
def tell(self): |
| 399 |
raise io.UnsupportedOperation |
| 400 |
|
| 401 |
data1 = [(x, str(x)) for x in range(2000)] + [b"abcde", len] |
| 402 |
for proto in range(5): |
| 403 |
f = UnseekableIO() |
| 404 |
pickler = Pickler(f, protocol=proto) |
| 405 |
pickler.dump(data1) |
| 406 |
pickled = f.getvalue() |
| 407 |
|
| 408 |
N = 5 |
| 409 |
f = UnseekableIO(pickled * N) |
| 410 |
unpickler = Unpickler(f) |
| 411 |
for i in range(N): |
| 412 |
assert unpickler.load() == data1 |
| 413 |
try: |
| 414 |
unpickler.load() |
| 415 |
except EOFError: |
| 416 |
pass |
| 417 |
|
| 418 |
def test_compat_pickle(): |
| 419 |
tests = [ |
| 420 |
(range(1, 7), '__builtin__', 'xrange'), |
| 421 |
(map(int, '123'), 'itertools', 'imap'), |
| 422 |
(Exception(), 'exceptions', 'Exception'), |
| 423 |
] |
| 424 |
for val, mod, name in tests: |
| 425 |
for proto in range(3): |
| 426 |
pickled = dumps(val, proto) |
| 427 |
assert('c%s\n%s' % (mod, name)).encode() in pickled |
| 428 |
val2 = loads(pickled) |
| 429 |
assert type(val2) is type(val) |
| 430 |
|
| 431 |
def test_bad_newobj_ex(): |
| 432 |
val = loads(b'cbuiltins\nint\n)}\x92.') |
| 433 |
assert val == 0 |
| 434 |
|
| 435 |
def test_recursive_set(): |
| 436 |
|
| 437 |
y = set() |
| 438 |
y.add(K(y)) |
| 439 |
for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
| 440 |
s = dumps(y, proto) |
| 441 |
s2 = dumps_py(y, proto) |
| 442 |
assert s == s2 |
| 443 |
x = loads(s) |
| 444 |
assert isinstance(x, set) |
| 445 |
assert len(x) == 1 |
| 446 |
assert isinstance(list(x)[0], K) |
| 447 |
assert list(x)[0].value is x |
| 448 |
|
| 449 |
|
| 450 |
y, = y |
| 451 |
for proto in range(4, pickle.HIGHEST_PROTOCOL + 1): |
| 452 |
s = dumps(y, proto) |
| 453 |
x = loads(s) |
| 454 |
assert isinstance(x, K) |
| 455 |
assert isinstance(x.value, set) |
| 456 |
assert len(x.value) == 1 |
| 457 |
assert list(x.value)[0] is x |
| 458 |
|
| 459 |
def test_picklebuffer(): |
| 460 |
pb = PickleBuffer(b'foobar') |
| 461 |
s1 = dumps(pb, 5) |
| 462 |
s2 = dumps_py(pb, 5) |
| 463 |
|
| 464 |
assert loads(s1) == b'foobar' |
| 465 |
|
| 466 |
def test_picklebuffer_multidim_contiguous(): |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
data = bytearray(range(12)) |
| 472 |
mv = memoryview(data).cast('B', [3, 4]) |
| 473 |
assert mv.c_contiguous |
| 474 |
pb = PickleBuffer(mv) |
| 475 |
s1 = dumps(pb, 5) |
| 476 |
s2 = dumps_py(pb, 5) |
| 477 |
expected = bytearray(range(12)) |
| 478 |
assert loads(s1) == expected |
| 479 |
assert loads(s2) == expected |
| 480 |
|
| 481 |
def test_buffers_error(): |
| 482 |
pb = PickleBuffer(b"foobar") |
| 483 |
data = dumps(pb, 5, buffer_callback=[].append) |
| 484 |
|
| 485 |
try: |
| 486 |
loads(data, buffers=object()) |
| 487 |
except TypeError: |
| 488 |
pass |
| 489 |
|
| 490 |
try: |
| 491 |
loads(data, buffers=[]) |
| 492 |
except UnpicklingError: |
| 493 |
pass |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
def test_load_int_true_false(): |
| 499 |
|
| 500 |
assert loads(b'I01\n.') is True |
| 501 |
assert loads(b'I00\n.') is False |
| 502 |
assert loads(b'I42\n.') == 42 |
| 503 |
|
| 504 |
def test_load_unicode_raw_escape(): |
| 505 |
|
| 506 |
assert loads(b'V\\u03c0\n.') == u'\u03c0' |
| 507 |
assert loads(b'Vhello\n.') == u'hello' |
| 508 |
|
| 509 |
assert loads(b'V\\u0000\n.') == u'\x00' |
| 510 |
|
| 511 |
def test_save_bytes_proto0(): |
| 512 |
|
| 513 |
for proto in (0, 1, 2): |
| 514 |
for b in (b'\x80', b'\xff', b'\x00\x80\xff'): |
| 515 |
assert loads(dumps(b, proto)) == b |
| 516 |
|
| 517 |
def test_save_str_proto0(): |
| 518 |
|
| 519 |
for proto in (0,): |
| 520 |
for s in (u'hello', u'a\nb', u'a\\b', u'\x00', u'\r', u'\x1a'): |
| 521 |
assert loads(dumps(s, proto)) == s |
| 522 |
|
| 523 |
assert loads(dumps(u'\u03c0', 0)) == u'\u03c0' |
| 524 |
|
| 525 |
def test_string_opcode(): |
| 526 |
|
| 527 |
assert loads(b"S'hello'\n.") == u'hello' |
| 528 |
assert loads(b'S"hello"\n.') == u'hello' |
| 529 |
assert loads(b"S'\\x41'\n.") == u'A' |
| 530 |
|
| 531 |
def test_binstring_opcode(): |
| 532 |
|
| 533 |
data = b'T\x05\x00\x00\x00hello.' |
| 534 |
assert loads(data) == u'hello' |
| 535 |
|
| 536 |
def test_short_binstring_opcode(): |
| 537 |
|
| 538 |
data = b'U\x05hello.' |
| 539 |
assert loads(data) == u'hello' |
| 540 |
|
| 541 |
def test_string_opcode_bytes_encoding(): |
| 542 |
|
| 543 |
import io |
| 544 |
f = io.BytesIO(b"S'hello'\n.") |
| 545 |
u = Unpickler(f, encoding='bytes') |
| 546 |
assert u.load() == b'hello' |
| 547 |
|
| 548 |
def test_file_write_none_return(): |
| 549 |
|
| 550 |
import io |
| 551 |
class NoneWriter: |
| 552 |
def __init__(self): |
| 553 |
self.buf = b'' |
| 554 |
def write(self, data): |
| 555 |
self.buf += data |
| 556 |
|
| 557 |
w = NoneWriter() |
| 558 |
p = Pickler(w, 4) |
| 559 |
p.dump(42) |
| 560 |
assert loads(w.buf) == 42 |
| 561 |
|
| 562 |
def test_buffers_as_list(): |
| 563 |
|
| 564 |
from pickle import PickleBuffer |
| 565 |
pb = PickleBuffer(b'hello') |
| 566 |
collected = [] |
| 567 |
data = dumps(pb, 5, buffer_callback=collected.append) |
| 568 |
result = loads(data, buffers=collected) |
| 569 |
assert result == b'hello' |
| 570 |
|
| 571 |
def test_buffers_exhausted_error(): |
| 572 |
|
| 573 |
from pickle import PickleBuffer |
| 574 |
pb = PickleBuffer(b'hello') |
| 575 |
data = dumps(pb, 5, buffer_callback=[].append) |
| 576 |
try: |
| 577 |
loads(data, buffers=[]) |
| 578 |
except UnpicklingError: |
| 579 |
pass |
| 580 |
else: |
| 581 |
assert False, "expected UnpicklingError" |
| 582 |
|
| 583 |
def test_reducer_override(): |
| 584 |
|
| 585 |
import io |
| 586 |
class MyPickler(Pickler): |
| 587 |
def reducer_override(self, obj): |
| 588 |
if isinstance(obj, int): |
| 589 |
return str, (str(obj),) |
| 590 |
return NotImplemented |
| 591 |
f = io.BytesIO() |
| 592 |
p = MyPickler(f, 4) |
| 593 |
p.dump(42) |
| 594 |
result = loads(f.getvalue()) |
| 595 |
assert result == '42' |
| 596 |
|
| 597 |
def test_dispatch_table_on_instance(): |
| 598 |
|
| 599 |
import io |
| 600 |
f = io.BytesIO() |
| 601 |
p = Pickler(f, 4) |
| 602 |
p.dispatch_table = {complex: lambda c: (str, (str(c),))} |
| 603 |
p.dump(1+2j) |
| 604 |
assert loads(f.getvalue()) == '(1+2j)' |
| 605 |
|
| 606 |
def test_dispatch_table_on_class(): |
| 607 |
|
| 608 |
import io |
| 609 |
class MyPickler(Pickler): |
| 610 |
dispatch_table = {complex: lambda c: (str, (str(c),))} |
| 611 |
f = io.BytesIO() |
| 612 |
p = MyPickler(f, 4) |
| 613 |
p.dump(1+2j) |
| 614 |
assert loads(f.getvalue()) == '(1+2j)' |
| 615 |
|
| 616 |
def test_persistent_id(): |
| 617 |
|
| 618 |
import io |
| 619 |
class MyPickler(Pickler): |
| 620 |
def persistent_id(self, obj): |
| 621 |
if isinstance(obj, int): |
| 622 |
return 'int:%d' % obj |
| 623 |
return None |
| 624 |
class MyUnpickler(Unpickler): |
| 625 |
def persistent_load(self, pid): |
| 626 |
if pid.startswith('int:'): |
| 627 |
return int(pid[4:]) |
| 628 |
raise UnpicklingError("bad pid") |
| 629 |
for proto in range(6): |
| 630 |
f = io.BytesIO() |
| 631 |
p = MyPickler(f, proto) |
| 632 |
p.dump([1, 2, 'hello']) |
| 633 |
f.seek(0) |
| 634 |
u = MyUnpickler(f) |
| 635 |
result = u.load() |
| 636 |
assert result == [1, 2, 'hello'] |
| 637 |
|
| 638 |
def test_pickler_clear_memo(): |
| 639 |
|
| 640 |
import io |
| 641 |
f = io.BytesIO() |
| 642 |
p = Pickler(f, 4) |
| 643 |
p.dump('hello') |
| 644 |
assert len(p.memo) > 0 |
| 645 |
p.clear_memo() |
| 646 |
assert len(p.memo) == 0 |
| 647 |
|
| 648 |
def test_pickler_memo_property(): |
| 649 |
|
| 650 |
import io |
| 651 |
f1 = io.BytesIO() |
| 652 |
p1 = Pickler(f1, 4) |
| 653 |
p1.dump('hello') |
| 654 |
memo = p1.memo |
| 655 |
assert isinstance(memo, dict) |
| 656 |
|
| 657 |
|
| 658 |
f2 = io.BytesIO() |
| 659 |
p2 = Pickler(f2, 4) |
| 660 |
p2.memo = memo |
| 661 |
p2.dump('hello') |
| 662 |
|
| 663 |
assert len(f2.getvalue()) < len(f1.getvalue()) |
| 664 |
|
| 665 |
def test_unpickler_memo_property(): |
| 666 |
|
| 667 |
import io |
| 668 |
data = dumps('hello') |
| 669 |
f = io.BytesIO(data) |
| 670 |
u = Unpickler(f) |
| 671 |
u.load() |
| 672 |
memo = u.memo |
| 673 |
assert isinstance(memo, dict) |
| 674 |
|
| 675 |
def test_pickler_new_without_file(): |
| 676 |
|
| 677 |
class BadPickler(Pickler): |
| 678 |
def __init__(self): |
| 679 |
pass |
| 680 |
p = BadPickler() |
| 681 |
try: |
| 682 |
p.dump(42) |
| 683 |
except PicklingError: |
| 684 |
pass |
| 685 |
else: |
| 686 |
assert False, "expected PicklingError" |
| 687 |
|
| 688 |
def test_pickler_bad_file(): |
| 689 |
|
| 690 |
class NoWrite: |
| 691 |
pass |
| 692 |
try: |
| 693 |
Pickler(NoWrite(), 4) |
| 694 |
except TypeError: |
| 695 |
pass |
| 696 |
else: |
| 697 |
assert False, "expected TypeError" |
| 698 |
|
| 699 |
def test_load_setitems_odd(): |
| 700 |
|
| 701 |
|
| 702 |
try: |
| 703 |
loads(b'}(NNNu.') |
| 704 |
except (UnpicklingError, IndexError): |
| 705 |
pass |
| 706 |
else: |
| 707 |
assert False, "expected error" |
| 708 |
|
| 709 |
def test_frame_readline(): |
| 710 |
|
| 711 |
|
| 712 |
for proto in (4, 5): |
| 713 |
data = dumps(u'line1\nline2', proto) |
| 714 |
assert loads(data) == u'line1\nline2' |
| 715 |
|
| 716 |
data = dumps(u'hello', 0) |
| 717 |
assert loads(data) == u'hello' |
| 718 |
|
| 719 |
def test_newobj_ex_proto2(): |
| 720 |
|
| 721 |
|
| 722 |
import io |
| 723 |
import copyreg |
| 724 |
for proto in (2, 3): |
| 725 |
f = io.BytesIO() |
| 726 |
p = Pickler(f, proto) |
| 727 |
|
| 728 |
|
| 729 |
p.dispatch_table = {int: lambda x: (copyreg.__newobj_ex__, (int, (x,), {}))} |
| 730 |
p.dump(42) |
| 731 |
data = f.getvalue() |
| 732 |
result = loads(data) |
| 733 |
assert result == 42 |
| 734 |
|
| 735 |
assert b'\xd2' not in data, "NEWOBJ_EX opcode must not appear in proto < 4" |
| 736 |
|
| 737 |
def test_pickler_subclass_super_init(): |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
import io |
| 742 |
class MyPickler(Pickler): |
| 743 |
def __init__(self, file, protocol=None): |
| 744 |
super().__init__(file, protocol) |
| 745 |
f = io.BytesIO() |
| 746 |
p = MyPickler(f, 2) |
| 747 |
p.dump(42) |
| 748 |
assert loads(f.getvalue()) == 42 |
| 749 |
|
| 750 |
def test_memo_reuses_repeated_string(): |
| 751 |
|
| 752 |
import pickletools |
| 753 |
s = 'hello' |
| 754 |
data = [s, s, s] |
| 755 |
buf = io.BytesIO() |
| 756 |
Pickler(buf, 4).dump(data) |
| 757 |
blob = buf.getvalue() |
| 758 |
buf.seek(0) |
| 759 |
ops = [(op.name, arg) for op, arg, pos in pickletools.genops(buf)] |
| 760 |
gets = [op for op, arg in ops if 'GET' in op] |
| 761 |
shorts = [op for op, arg in ops if op == 'SHORT_BINUNICODE' and arg == 'hello'] |
| 762 |
assert len(shorts) == 1, "expected 1 full string, got %d: %r" % (len(shorts), ops) |
| 763 |
assert len(gets) == 2, "expected 2 GET ops, got %d: %r" % (len(gets), ops) |
| 764 |
|
| 765 |
def test_readonly_buffer(): |
| 766 |
|
| 767 |
from pickle import PickleBuffer |
| 768 |
import io |
| 769 |
bufs = [] |
| 770 |
pb = PickleBuffer(bytearray(b'mutable')) |
| 771 |
data = dumps(pb, 5, buffer_callback=bufs.append) |
| 772 |
|
| 773 |
readonly_buf = memoryview(bufs[0]).toreadonly() |
| 774 |
result = loads(data, buffers=[readonly_buf]) |
| 775 |
assert bytes(result) == b'mutable' |
| 776 |
|
| 777 |
def test_reducer_override_subclass(): |
| 778 |
|
| 779 |
|
| 780 |
import io |
| 781 |
class MyPickler(Pickler): |
| 782 |
def reducer_override(self, obj): |
| 783 |
if obj == 42: |
| 784 |
return int, (99,) |
| 785 |
return NotImplemented |
| 786 |
|
| 787 |
bio = io.BytesIO() |
| 788 |
p = MyPickler(bio, 2) |
| 789 |
p.dump(42) |
| 790 |
result = loads(bio.getvalue()) |
| 791 |
assert result == 99, "reducer_override was not called" |
| 792 |
|
| 793 |
|
| 794 |
bio2 = io.BytesIO() |
| 795 |
p2 = MyPickler(bio2, 2) |
| 796 |
p2.dump("hello") |
| 797 |
assert loads(bio2.getvalue()) == "hello" |
| 798 |
|
| 799 |
def test_pickle_dict_views(): |
| 800 |
|
| 801 |
d = {1: 10, "a": "ABC"} |
| 802 |
for proto in range(5 + 1): |
| 803 |
for view in (d.keys(), d.values(), d.items()): |
| 804 |
try: |
| 805 |
dumps(view, proto) |
| 806 |
except (TypeError, PicklingError): |
| 807 |
pass |
| 808 |
else: |
| 809 |
assert False, "expected TypeError or PicklingError for %r" % (view,) |
| 810 |
|
| 811 |
def test_pickle_empty_set(): |
| 812 |
|
| 813 |
|
| 814 |
for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 815 |
for obj in (set(), frozenset()): |
| 816 |
result = loads(dumps(obj, proto)) |
| 817 |
assert result == obj |
| 818 |
assert type(result) is type(obj) |
| 819 |
|
| 820 |
def test_unpickle_crash1(): |
| 821 |
b = b'\x98\x1f\x1c\x8f\xe1P\xf8n\xa7R\xe8\xc3\x8c\x9d\xc6' |
| 822 |
with raises(pickle.UnpicklingError): |
| 823 |
pickle.loads(b) |
| 824 |
|
| 825 |
def test_unpickle_crash2(): |
| 826 |
b = b'p\n' |
| 827 |
with raises(ValueError): |
| 828 |
pickle.loads(b) |
| 829 |
|
| 830 |
def test_unpickle_crash3(): |
| 831 |
b = b'F\n' |
| 832 |
with raises(ValueError): |
| 833 |
pickle.loads(b) |
| 834 |
|
| 835 |
def test_unpickle_crash4(): |
| 836 |
b = b'g\n' |
| 837 |
with raises(ValueError): |
| 838 |
pickle.loads(b) |
| 839 |
|
| 840 |
def test_large_32b_binbytes8(): |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
import pytest |
| 845 |
if sys.maxsize >= 2**32: |
| 846 |
pytest.skip("only relevant on 32-bit builds") |
| 847 |
dumped_bytes8 = b'\x80\x04\x8e\x04\x00\x00\x00\x01\x00\x00\x00\xe2\x82\xac\x00.' |
| 848 |
dumped_unicode8 = b'\x80\x04\x8d\x04\x00\x00\x00\x01\x00\x00\x00\xe2\x82\xac\x00.' |
| 849 |
dumped_barray8 = b'\x80\x05\x96\x04\x00\x00\x00\x01\x00\x00\x00\xe2\x82\xac\x00.' |
| 850 |
for dumped in (dumped_bytes8, dumped_unicode8, dumped_barray8): |
| 851 |
raises((UnpicklingError, OverflowError), loads, dumped) |
| 852 |
|
| 853 |
|
| 854 |
def test_memoize_and_binput_index_collision(): |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
pickled = (b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00' |
| 859 |
b']\x94(\x8c\x04spamq\x01\x8c\x03ham\x94h\x02e.') |
| 860 |
unpickled = pickle.loads(pickled) |
| 861 |
assert unpickled == ['spam', 'ham', 'ham'] |
| 862 |
assert unpickled[1] is unpickled[2] |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
pickled2 = (b'\x80\x04\x95\x12\x00\x00\x00\x00\x00\x00\x00' |
| 868 |
b'](\x8c\x04spam\x8c\x03ham\x94h\x00e.') |
| 869 |
unpickled2 = pickle.loads(pickled2) |
| 870 |
assert unpickled2 == ['spam', 'ham', 'ham'] |
| 871 |
assert unpickled2[1] is unpickled2[2] |