This repository was archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_quickle.py
1034 lines (805 loc) · 27 KB
/
test_quickle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
import enum
import gc
import itertools
import pickle
import pickletools
import string
import sys
import uuid
from distutils.version import StrictVersion
import pytest
import quickle
BATCHSIZE = 1000
def test_picklebuffer_is_shared():
assert pickle.PickleBuffer is quickle.PickleBuffer
def test_module_version():
StrictVersion(quickle.__version__)
def check(obj, sol=None):
if sol is None:
sol = obj
quick_res = quickle.dumps(obj)
obj2 = quickle.loads(quick_res)
assert obj2 == sol
assert type(obj2) is type(sol)
obj3 = pickle.loads(quick_res)
assert obj3 == sol
assert type(obj3) is type(sol)
pickle_res = pickle.dumps(obj, protocol=5)
obj4 = quickle.loads(pickle_res)
assert obj4 == sol
assert type(obj4) is type(sol)
def test_pickle_none():
check(None)
@pytest.mark.parametrize("value", [True, False])
def test_pickle_bool(value):
check(value)
@pytest.mark.parametrize("nbytes", [1, 2, 4, 8, 254, 255, 256, 257])
@pytest.mark.parametrize("negative", [False, True])
def test_pickle_int(nbytes, negative):
value = 2 ** (nbytes * 8 - 6)
if negative:
value *= -1
check(value)
@pytest.mark.parametrize(
"value",
[
0.0,
4.94e-324,
1e-310,
7e-308,
6.626e-34,
0.1,
0.5,
3.14,
263.44582062374053,
6.022e23,
1e30,
],
)
@pytest.mark.parametrize("negative", [False, True])
def test_pickle_float(value, negative):
if negative:
value *= -1
check(value)
@pytest.mark.parametrize("nbytes", [0, 10, 512])
def test_pickle_bytes(nbytes):
value = b"y" * nbytes
check(value)
@pytest.mark.parametrize("nbytes", [0, 10, 512])
def test_pickle_bytearray(nbytes):
value = bytearray(b"y" * nbytes)
check(value)
@pytest.mark.parametrize("nbytes", [0, 10, 512])
def test_pickle_unicode(nbytes):
value = "y" * nbytes
check(value)
@pytest.mark.parametrize(
"value",
["<\\u>", "<\\\u1234>", "<\n>", "<\\>", "\U00012345", "<\\\U00012345>", "<\udc80>"],
)
def test_pickle_unicode_edgecases(value):
check(value)
@pytest.mark.parametrize("n", [0, 1, 5, 100, BATCHSIZE + 10])
def test_pickle_set(n):
check(set(range(n)))
@pytest.mark.parametrize("n", [0, 1, 5, 100, BATCHSIZE + 10])
def test_pickle_frozenset(n):
check(frozenset(range(n)))
@pytest.mark.parametrize("n", [0, 1, 2, 3, 100, BATCHSIZE + 10])
def test_pickle_tuple(n):
check(tuple(range(n)))
def test_pickle_recursive_tuple():
obj = ([None],)
obj[0][0] = obj
quick_res = quickle.dumps(obj)
for loads in [quickle.loads, pickle.loads]:
obj2 = loads(quick_res)
assert isinstance(obj2, tuple)
assert obj2[0][0] is obj2
# Fix the cycle so `==` works, then test
obj2[0][0] = None
assert obj2 == ([None],)
@pytest.mark.parametrize("n", [0, 1, 5, 100, BATCHSIZE + 10])
def test_pickle_list(n):
check(list(range(n)))
def test_pickle_recursive_list():
# self referential
obj = []
obj.append(obj)
quick_res = quickle.dumps(obj)
for loads in [quickle.loads, pickle.loads]:
obj2 = loads(quick_res)
assert isinstance(obj2, list)
assert obj2[0] is obj2
assert len(obj2) == 1
# one level removed
obj = [[None]]
obj[0][0] = obj
quick_res = quickle.dumps(obj)
for loads in [quickle.loads, pickle.loads]:
obj2 = loads(quick_res)
assert isinstance(obj2, list)
assert obj2[0][0] is obj2
# Fix the cycle so `==` works, then test
obj2[0][0] = None
assert obj2 == [[None]]
@pytest.mark.parametrize("n", [0, 1, 5, 100, BATCHSIZE + 10])
def test_pickle_dict(n):
value = dict(
zip(itertools.product(string.ascii_letters, string.ascii_letters), range(n))
)
check(value)
def test_pickle_recursive_dict():
# self referential
obj = {}
obj[0] = obj
quick_res = quickle.dumps(obj)
for loads in [quickle.loads, pickle.loads]:
obj2 = loads(quick_res)
assert isinstance(obj2, dict)
assert obj2[0] is obj2
assert len(obj2) == 1
# one level removed
obj = {0: []}
obj[0].append(obj)
quick_res = quickle.dumps(obj)
for loads in [quickle.loads, pickle.loads]:
obj2 = loads(quick_res)
assert isinstance(obj2, dict)
assert obj2[0][0] is obj2
# Fix the cycle so `==` works, then test
obj2[0].pop()
assert obj2 == {0: []}
def test_pickle_highly_nested_list():
obj = []
for _ in range(66):
obj = [obj]
check(obj)
def test_pickle_large_memo():
obj = [[1, 2, 3] for _ in range(2000)]
check(obj)
def test_pickle_a_little_bit_of_everything():
obj = [
1,
1.5,
True,
False,
None,
"hello",
b"hello",
bytearray(b"hello"),
(1, 2, 3),
[1, 2, 3],
{"hello": "world"},
{1, 2, 3},
frozenset([1, 2, 3]),
]
check(obj)
def opcode_in_pickle(code, pickle):
for op, _, _ in pickletools.genops(pickle):
if op.code == code.decode("latin-1"):
return True
return False
@pytest.mark.parametrize("memoize", [True, False])
def test_pickle_memoize_class_setting(memoize):
obj = [[1], [2]]
enc = quickle.Encoder(memoize=memoize)
assert enc.memoize == memoize
# immutable
with pytest.raises(AttributeError):
enc.memoize = not memoize
assert enc.memoize == memoize
# default taken from class
res = enc.dumps(obj)
assert opcode_in_pickle(pickle.MEMOIZE, res) == memoize
assert enc.memoize == memoize
# specify None, no change
res = enc.dumps(obj, memoize=None)
assert opcode_in_pickle(pickle.MEMOIZE, res) == memoize
assert enc.memoize == memoize
# specify same, no change
res = enc.dumps(obj, memoize=memoize)
assert opcode_in_pickle(pickle.MEMOIZE, res) == memoize
assert enc.memoize == memoize
# overridden by opposite value
res = enc.dumps(obj, memoize=(not memoize))
assert opcode_in_pickle(pickle.MEMOIZE, res) != memoize
assert enc.memoize == memoize
@pytest.mark.parametrize("memoize", [True, False])
def test_pickle_memoize_function_settings(memoize):
obj = [[1], [2]]
res = quickle.dumps(obj, memoize=memoize)
assert opcode_in_pickle(pickle.MEMOIZE, res) == memoize
obj2 = quickle.loads(res)
assert obj == obj2
obj = [[]] * 2
res = quickle.dumps(obj, memoize=memoize)
assert opcode_in_pickle(pickle.MEMOIZE, res) == memoize
obj2 = quickle.loads(res)
assert obj == obj2
assert (obj2[0] is not obj2[1]) == (not memoize)
def test_pickle_memoize_false_recursion_error():
obj = []
obj.append(obj)
with pytest.raises(RecursionError):
quickle.dumps(obj, memoize=False)
@pytest.mark.parametrize("cls", [bytes, bytearray])
def test_pickle_picklebuffer_no_callback(cls):
sol = cls(b"hello")
obj = quickle.PickleBuffer(sol)
check(obj, sol)
@pytest.mark.parametrize("cls", [bytes, bytearray])
def test_pickler_collect_buffers_true(cls):
data = cls(b"hello")
pbuf = quickle.PickleBuffer(data)
enc = quickle.Encoder(collect_buffers=True)
assert enc.collect_buffers
with pytest.raises(AttributeError):
enc.collect_buffers = False
# No buffers present returns None
res, buffers = enc.dumps(data)
assert buffers is None
assert quickle.loads(res) == data
# Buffers are collected and returned
res, buffers = enc.dumps(pbuf)
assert buffers == [pbuf]
assert quickle.loads(res, buffers=buffers) is pbuf
# Override None uses default
res, buffers = enc.dumps(pbuf, collect_buffers=None)
assert buffers == [pbuf]
assert quickle.loads(res, buffers=buffers) is pbuf
# Override True is same as default
res, buffers = enc.dumps(pbuf, collect_buffers=True)
assert buffers == [pbuf]
assert quickle.loads(res, buffers=buffers) is pbuf
# Override False disables buffer collecting
res = enc.dumps(pbuf, collect_buffers=False)
assert quickle.loads(res) == data
# Override doesn't persist
res, buffers = enc.dumps(pbuf)
assert buffers == [pbuf]
assert quickle.loads(res, buffers=buffers) is pbuf
@pytest.mark.parametrize("cls", [bytes, bytearray])
def test_pickler_collect_buffers_false(cls):
data = cls(b"hello")
pbuf = quickle.PickleBuffer(data)
enc = quickle.Encoder(collect_buffers=False)
assert not enc.collect_buffers
with pytest.raises(AttributeError):
enc.collect_buffers = True
# By default buffers are serialized in-band
res = enc.dumps(pbuf)
assert quickle.loads(res) == data
# Override None uses default
res = enc.dumps(pbuf, collect_buffers=None)
assert quickle.loads(res) == data
# Override False is the same as default
res = enc.dumps(pbuf, collect_buffers=False)
assert quickle.loads(res) == data
# Override True works
res, buffers = enc.dumps(pbuf, collect_buffers=True)
assert buffers == [pbuf]
assert quickle.loads(res, buffers=buffers) is pbuf
# If no buffers present, output is None
res, buffers = enc.dumps(data, collect_buffers=True)
assert buffers is None
assert quickle.loads(res, buffers=buffers) == data
# Override doesn't persist
res = enc.dumps(pbuf)
assert quickle.loads(res) == data
@pytest.mark.parametrize("cls", [bytes, bytearray])
def test_quickle_pickle_collect_buffers_true_compatibility(cls):
data = cls(b"hello")
pbuf = quickle.PickleBuffer(data)
# quickle -> pickle
quick_res, quick_buffers = quickle.dumps(pbuf, collect_buffers=True)
obj = pickle.loads(quick_res, buffers=quick_buffers)
assert obj is pbuf
# pickle -> quickle
pickle_buffers = []
pickle_res = pickle.dumps(pbuf, buffer_callback=pickle_buffers.append, protocol=5)
obj = quickle.loads(pickle_res, buffers=pickle_buffers)
assert obj is pbuf
@pytest.mark.parametrize("cls", [bytes, bytearray])
def test_quickle_pickle_collect_buffers_false_compatibility(cls):
data = cls(b"hello")
pbuf = quickle.PickleBuffer(data)
# quickle -> pickle
quick_res = quickle.dumps(pbuf)
obj = pickle.loads(quick_res)
assert obj == data
# pickle -> quickle
pickle_res = pickle.dumps(pbuf, protocol=5)
obj = quickle.loads(pickle_res)
assert obj == data
def test_loads_buffers_errors():
obj = quickle.PickleBuffer(b"hello")
res, _ = quickle.dumps(obj, collect_buffers=True)
with pytest.raises(TypeError):
quickle.loads(res, buffers=object())
with pytest.raises(quickle.DecodingError):
quickle.loads(res, buffers=[])
@pytest.mark.parametrize("value", [object(), object, sum, itertools.count])
def test_dumps_and_loads_unpickleable_types(value):
with pytest.raises(TypeError):
quickle.dumps(value)
o = pickle.dumps(value, protocol=5)
with pytest.raises(quickle.DecodingError):
quickle.loads(o)
def test_loads_truncated_input():
data = quickle.dumps([1, 2, 3])
with pytest.raises(quickle.DecodingError):
quickle.loads(data[:-2])
def test_loads_bad_pickle():
with pytest.raises(quickle.DecodingError):
quickle.loads(b"this isn't valid at all")
def test_getsizeof():
a = sys.getsizeof(quickle.Encoder(write_buffer_size=64))
b = sys.getsizeof(quickle.Encoder(write_buffer_size=128))
assert b > a
# Smoketest
sys.getsizeof(quickle.Decoder())
@pytest.mark.parametrize(
"enc",
[
# bad stacks
b".", # STOP
b"0", # POP
b"1", # POP_MARK
b"a", # APPEND
b"Na",
b"e", # APPENDS
b"(e",
b"s", # SETITEM
b"Ns",
b"NNs",
b"t", # TUPLE
b"u", # SETITEMS
b"(u",
b"}(Nu",
b"\x85", # TUPLE1
b"\x86", # TUPLE2
b"N\x86",
b"\x87", # TUPLE3
b"N\x87",
b"NN\x87",
b"\x90", # ADDITEMS
b"(\x90",
b"\x91", # FROZENSET
b"\x94", # MEMOIZE
# bad marks
b"N(.", # STOP
b"]N(a", # APPEND
b"}NN(s", # SETITEM
b"}N(Ns",
b"}(NNs",
b"}((u", # SETITEMS
b"N(\x85", # TUPLE1
b"NN(\x86", # TUPLE2
b"N(N\x86",
b"NNN(\x87", # TUPLE3
b"NN(N\x87",
b"N(NN\x87",
b"]((\x90", # ADDITEMS
b"N(\x94", # MEMOIZE
],
)
def test_bad_stack_or_mark(enc):
with pytest.raises(quickle.DecodingError):
quickle.loads(enc)
@pytest.mark.parametrize(
"enc",
[
b"B", # BINBYTES
b"B\x03\x00\x00",
b"B\x03\x00\x00\x00",
b"B\x03\x00\x00\x00ab",
b"C", # SHORT_BINBYTES
b"C\x03",
b"C\x03ab",
b"G", # BINFLOAT
b"G\x00\x00\x00\x00\x00\x00\x00",
b"J", # BININT
b"J\x00\x00\x00",
b"K", # BININT1
b"M", # BININT2
b"M\x00",
b"T", # BINSTRING
b"T\x03\x00\x00",
b"T\x03\x00\x00\x00",
b"T\x03\x00\x00\x00ab",
b"U", # SHORT_BINSTRING
b"U\x03",
b"U\x03ab",
b"X", # BINUNICODE
b"X\x03\x00\x00",
b"X\x03\x00\x00\x00",
b"X\x03\x00\x00\x00ab",
b"Nh", # BINGET
b"Nj", # LONG_BINGET
b"Nj\x00\x00\x00",
b"Nr\x00\x00\x00",
b"\x80", # PROTO
b"\x8a", # LONG1
b"\x8b", # LONG4
b"\x8b\x00\x00\x00",
b"\x8c", # SHORT_BINUNICODE
b"\x8c\x03",
b"\x8c\x03ab",
b"\x8d", # BINUNICODE8
b"\x8d\x03\x00\x00\x00\x00\x00\x00",
b"\x8d\x03\x00\x00\x00\x00\x00\x00\x00",
b"\x8d\x03\x00\x00\x00\x00\x00\x00\x00ab",
b"\x8e", # BINBYTES8
b"\x8e\x03\x00\x00\x00\x00\x00\x00",
b"\x8e\x03\x00\x00\x00\x00\x00\x00\x00",
b"\x8e\x03\x00\x00\x00\x00\x00\x00\x00ab",
b"\x96", # BYTEARRAY8
b"\x96\x03\x00\x00\x00\x00\x00\x00",
b"\x96\x03\x00\x00\x00\x00\x00\x00\x00",
b"\x96\x03\x00\x00\x00\x00\x00\x00\x00ab",
b"\x95", # FRAME
b"\x95\x02\x00\x00\x00\x00\x00\x00",
b"\x95\x02\x00\x00\x00\x00\x00\x00\x00",
b"\x95\x02\x00\x00\x00\x00\x00\x00\x00N",
],
)
def test_truncated_data(enc):
with pytest.raises(quickle.DecodingError):
quickle.loads(enc)
class MyStruct(quickle.Struct):
x: object
y: object
class MyStruct2(quickle.Struct):
x: object
y: object = 1
z: object = []
z2: object = 3
class MyStruct3(quickle.Struct):
x: object
y: object
z: object
def test_pickler_unpickler_registry_kwarg_errors():
with pytest.raises(TypeError, match="registry must be a list or a dict"):
quickle.Encoder(registry="bad")
with pytest.raises(TypeError, match="an integer is required"):
quickle.Encoder(registry={MyStruct: 1.0})
with pytest.raises(ValueError, match="registry values must be between"):
quickle.Encoder(registry={MyStruct: -1})
with pytest.raises(TypeError, match="registry must be a list or a dict"):
quickle.Decoder(registry="bad")
@pytest.mark.parametrize("registry_type", ["list", "dict"])
@pytest.mark.parametrize("use_functions", [True, False])
def test_pickle_struct(registry_type, use_functions):
if registry_type == "list":
p_registry = u_registry = [MyStruct]
else:
p_registry = {MyStruct: 0}
u_registry = {0: MyStruct}
x = MyStruct(1, 2)
if use_functions:
s = quickle.dumps(x, registry=p_registry)
x2 = quickle.loads(s, registry=u_registry)
else:
enc = quickle.Encoder(registry=p_registry)
dec = quickle.Decoder(registry=u_registry)
s = enc.dumps(x)
x2 = dec.loads(s)
assert x == x2
@pytest.mark.parametrize("code", [0, 2 ** 8 - 1, 2 ** 16 - 1, 2 ** 31 - 1])
def test_pickle_struct_codes(code):
x = MyStruct(1, 2)
p_registry = {MyStruct: code}
u_registry = {code: MyStruct}
s = quickle.dumps(x, registry=p_registry)
x2 = quickle.loads(s, registry=u_registry)
assert x2 == x
def test_pickle_struct_code_out_of_range():
x = MyStruct(1, 2)
with pytest.raises(Exception) as exc:
quickle.dumps(x, registry={MyStruct: 2 ** 32})
if isinstance(exc.value, ValueError):
assert "registry values must be between" in str(exc.value)
else:
assert isinstance(exc.value, OverflowError)
def test_pickle_struct_recursive():
x = MyStruct(1, None)
x.y = x
s = quickle.dumps(x, registry=[MyStruct])
x2 = quickle.loads(s, registry=[MyStruct])
assert x2.x == 1
assert x2.y is x2
assert type(x) is MyStruct
@pytest.mark.parametrize("registry", ["missing", None, [], {}, {1: MyStruct}])
def test_pickle_errors_struct_missing_from_registry(registry):
x = MyStruct(1, 2)
s = quickle.dumps(x, registry=[MyStruct])
kwargs = {} if registry == "missing" else {"registry": registry}
with pytest.raises(ValueError, match="Typecode"):
quickle.loads(s, **kwargs)
@pytest.mark.parametrize(
"registry", ["missing", None, [], [MyStruct2], {}, {MyStruct2: 0}]
)
def test_unpickle_errors_struct_typecode_missing_from_registry(registry):
kwargs = {} if registry == "missing" else {"registry": registry}
x = MyStruct(1, 2)
with pytest.raises(TypeError, match="Type MyStruct isn't in type registry"):
quickle.dumps(x, **kwargs)
def test_unpickle_errors_obj_in_registry_is_not_struct_type():
class Foo(object):
pass
x = MyStruct(1, 2)
s = quickle.dumps(x, registry=[MyStruct])
with pytest.raises(TypeError, match="Value for typecode"):
quickle.loads(s, registry=[Foo])
def test_unpickle_errors_buildstruct_on_non_struct_object():
s = b"\x80\x05K\x00\x94(K\x01K\x02\xb0."
with pytest.raises(quickle.DecodingError, match="BUILDSTRUCT"):
quickle.loads(s, registry=[MyStruct])
def test_struct_registry_mismatch_fewer_args_no_defaults_errors():
x = MyStruct(1, 2)
s = quickle.dumps(x, registry=[MyStruct])
with pytest.raises(TypeError, match="Missing required argument 'z'"):
quickle.loads(s, registry=[MyStruct3])
def test_struct_registry_mismatch_fewer_args_default_parameters_respected():
"""Unpickling a struct with a newer version that has additional default
parameters at the end works (the defaults are used)."""
x = MyStruct(1, 2)
s = quickle.dumps(x, registry=[MyStruct])
x2 = quickle.loads(s, registry=[MyStruct2])
assert isinstance(x2, MyStruct2)
assert x2.x == x.x
assert x2.y == x.y
assert x2.z == []
assert x2.z2 == 3
def test_struct_registry_mismatch_extra_args_are_ignored():
"""Unpickling a struct with an older version that has fewer parameters
works (the extra args are ignored)."""
x = MyStruct2(1, 2)
s = quickle.dumps(x, registry=[MyStruct2])
x2 = quickle.loads(s, registry=[MyStruct])
assert x2.x == 1
assert x2.y == 2
class Fruit(enum.IntEnum):
APPLE = 1
BANANA = 2
ORANGE = 3
class PyObjects(enum.Enum):
LIST = []
STRING = ""
OBJECT = object()
@pytest.mark.parametrize("x", list(Fruit))
def test_pickle_intenum(x):
s = quickle.dumps(x, registry=[Fruit])
x2 = quickle.loads(s, registry=[Fruit])
assert x2 == x
@pytest.mark.parametrize("x", list(PyObjects))
def test_pickle_enum(x):
s = quickle.dumps(x, registry=[PyObjects])
assert x.name.encode() in s
x2 = quickle.loads(s, registry=[PyObjects])
assert x2 == x
@pytest.mark.parametrize("code", [0, 2 ** 8 - 1, 2 ** 16 - 1, 2 ** 31 - 1])
def test_pickle_enum_codes(code):
p_registry = {Fruit: code}
u_registry = {code: Fruit}
s = quickle.dumps(Fruit.APPLE, registry=p_registry)
x2 = quickle.loads(s, registry=u_registry)
assert x2 == Fruit.APPLE
def test_pickle_enum_code_out_of_range():
class Fruit(enum.IntEnum):
APPLE = 1
with pytest.raises(Exception) as exc:
quickle.dumps(Fruit.APPLE, registry={Fruit: 2 ** 32})
if isinstance(exc.value, ValueError):
assert "registry values must be between" in str(exc.value)
else:
assert isinstance(exc.value, OverflowError)
@pytest.mark.parametrize("registry", [None, [], {1: PyObjects}])
def test_pickle_errors_enum_missing_from_registry(registry):
s = quickle.dumps(Fruit.APPLE, registry=[Fruit])
with pytest.raises(ValueError, match="Typecode"):
quickle.loads(s, registry=registry)
@pytest.mark.parametrize("registry", [None, [PyObjects], {PyObjects: 1}])
def test_unpickle_errors_enum_typecode_missing_from_registry(registry):
with pytest.raises(TypeError, match="Type Fruit isn't in type registry"):
quickle.dumps(Fruit.APPLE, registry=registry)
def test_unpickle_errors_obj_in_registry_is_not_enum_type():
s = quickle.dumps(Fruit.APPLE, registry=[Fruit])
with pytest.raises(TypeError, match="Value for typecode"):
quickle.loads(s, registry=[MyStruct])
def test_unpickle_errors_intenum_missing_value():
class Fruit2(enum.IntEnum):
APPLE = 1
s = quickle.dumps(Fruit.ORANGE, registry=[Fruit])
with pytest.raises(ValueError, match="Fruit2"):
quickle.loads(s, registry=[Fruit2])
def test_unpickle_errors_enum_missing_attribute():
class PyObjects2(enum.Enum):
LIST = []
s = quickle.dumps(PyObjects.OBJECT, registry=[PyObjects])
with pytest.raises(AttributeError, match="OBJECT"):
quickle.loads(s, registry=[PyObjects2])
@pytest.mark.parametrize("x", [0j, 1j, 1 + 0j, 1 + 1j, 1e-9 - 2.5e9j])
def test_pickle_complex(x):
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
TIMEDELTA_MAX_DAYS = 999999999
@pytest.mark.parametrize(
"x",
[
datetime.timedelta(),
datetime.timedelta(days=TIMEDELTA_MAX_DAYS),
datetime.timedelta(days=-TIMEDELTA_MAX_DAYS),
datetime.timedelta(days=1234, seconds=56, microseconds=78),
datetime.timedelta(seconds=24 * 3600 - 1),
datetime.timedelta(microseconds=1000000 - 1),
],
)
def test_timedelta(x):
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
@pytest.mark.parametrize("positive", [True, False])
def test_loads_timedelta_out_of_range(positive):
s = quickle.dumps(datetime.timedelta(days=1234))
days = TIMEDELTA_MAX_DAYS + 1
if not positive:
days = -days
key = (1234).to_bytes(4, "little", signed=True)
bad = s.replace(key, days.to_bytes(4, "little", signed=True))
with pytest.raises(OverflowError):
quickle.loads(bad)
@pytest.mark.parametrize("x", [datetime.date(2020, 1, 1), datetime.date(9999, 12, 31)])
def test_date(x):
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
def test_loads_date_out_of_range():
s = quickle.dumps(datetime.date(9999, 12, 31))
bad = s.replace((9999).to_bytes(2, "little"), (10000).to_bytes(2, "little"))
with pytest.raises(ValueError):
quickle.loads(bad)
@pytest.mark.parametrize(
"x",
[
datetime.time(hour=5, minute=30, second=25),
datetime.time(hour=23, minute=59, second=59, microsecond=999999, fold=0),
datetime.time(hour=23, minute=59, second=59, microsecond=999999, fold=1),
datetime.time(hour=5, tzinfo=datetime.timezone.utc),
datetime.time(hour=5, tzinfo=datetime.timezone(datetime.timedelta(0, 1, 2))),
],
)
def test_time(x):
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
@pytest.mark.parametrize(
"x",
[
datetime.datetime.now(),
datetime.datetime(
year=9999,
month=12,
day=31,
hour=23,
minute=59,
second=59,
microsecond=999999,
fold=0,
),
datetime.datetime(
year=9999,
month=12,
day=31,
hour=23,
minute=59,
second=59,
microsecond=999999,
fold=1,
),
datetime.datetime.now(datetime.timezone.utc),
datetime.datetime.now(datetime.timezone(datetime.timedelta(0, 1, 2))),
],
)
def test_datetime(x):
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
def test_timezone_utc():
s = quickle.dumps(datetime.timezone.utc)
x = quickle.loads(s)
assert x == datetime.timezone.utc
@pytest.mark.parametrize(
"offset",
[
datetime.timedelta(hours=23, minutes=59, seconds=59, microseconds=999999),
datetime.timedelta(hours=1, minutes=2, seconds=3, microseconds=4),
datetime.timedelta(microseconds=1),
datetime.timedelta(microseconds=-1),
datetime.timedelta(hours=-1, minutes=-2, seconds=-3, microseconds=-4),
datetime.timedelta(hours=-23, minutes=-59, seconds=-59, microseconds=-999999),
],
)
def test_timezone(offset):
x = datetime.timezone(offset)
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
@pytest.fixture
def zoneinfo_parts():
zoneinfo = pytest.importorskip("zoneinfo")
a, b = sorted(zoneinfo.available_timezones())[:2]
za = zoneinfo.ZoneInfo(a)
zb = zoneinfo.ZoneInfo(b)
objs = [
za,
zb,
datetime.datetime.now(za),
datetime.datetime.now(zb),
datetime.time(hour=4, tzinfo=za),
datetime.time(hour=5, tzinfo=zb),
]
return objs
@pytest.mark.parametrize("ind", range(6))
def test_zoneinfo(zoneinfo_parts, ind):
x = zoneinfo_parts[ind]
s = quickle.dumps(x)
x2 = quickle.loads(s)
assert x == x2
def test_zoneinfo_not_found():
try:
import zoneinfo # noqa
pytest.skip("zoneinfo successfully imported")
except ImportError:
pass
with pytest.raises(quickle.DecodingError, match="zoneinfo"):
quickle.loads(b"\x8c\x0fAmerica/Chicago\xc0.")
def test_objects_with_only_one_refcount_arent_memoized():
class Test(quickle.Struct):
x: list
y: str
def rstr():
return str(uuid.uuid4().hex)
data = [
(rstr(),),
(rstr(), rstr(), rstr(), rstr(), rstr()),
([[[rstr()]]],),
[rstr()],
{rstr()},
frozenset([rstr()]),
{rstr(): rstr()},
rstr(),
rstr().encode(),
bytearray(rstr().encode()),
Test([rstr()], rstr()),