annotate Modules/_pickle.c @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents 7d6c27fa7f32
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1 #include "Python.h"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2 #include "structmember.h"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4 PyDoc_STRVAR(pickle_module_doc,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5 "Optimized C implementation for the Python pickle module.");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
8 module _pickle
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
9 class _pickle.Pickler
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
10 class _pickle.PicklerMemoProxy
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
11 class _pickle.Unpickler
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
12 class _pickle.UnpicklerMemoProxy
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
13 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
14 /*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
15
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
16 /*[python]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
17 class PicklerObject_converter(self_converter):
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
18 type = "PicklerObject *"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
19
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
20 class PicklerMemoProxyObject_converter(self_converter):
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
21 type = "PicklerMemoProxyObject *"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
22
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
23 class UnpicklerObject_converter(self_converter):
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
24 type = "UnpicklerObject *"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
25
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
26 class UnpicklerMemoProxyObject_converter(self_converter):
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
27 type = "UnpicklerMemoProxyObject *"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
28 [python]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
29 /*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
30
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
31 /* Bump this when new opcodes are added to the pickle protocol. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
32 enum {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
33 HIGHEST_PROTOCOL = 4,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
34 DEFAULT_PROTOCOL = 3
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
35 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
36
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
37 /* Pickle opcodes. These must be kept updated with pickle.py.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
38 Extensive docs are in pickletools.py. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
39 enum opcode {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
40 MARK = '(',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
41 STOP = '.',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
42 POP = '0',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
43 POP_MARK = '1',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
44 DUP = '2',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
45 FLOAT = 'F',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
46 INT = 'I',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
47 BININT = 'J',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
48 BININT1 = 'K',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
49 LONG = 'L',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
50 BININT2 = 'M',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
51 NONE = 'N',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
52 PERSID = 'P',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
53 BINPERSID = 'Q',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
54 REDUCE = 'R',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
55 STRING = 'S',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
56 BINSTRING = 'T',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
57 SHORT_BINSTRING = 'U',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
58 UNICODE = 'V',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
59 BINUNICODE = 'X',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
60 APPEND = 'a',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
61 BUILD = 'b',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
62 GLOBAL = 'c',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
63 DICT = 'd',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
64 EMPTY_DICT = '}',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
65 APPENDS = 'e',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
66 GET = 'g',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
67 BINGET = 'h',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
68 INST = 'i',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
69 LONG_BINGET = 'j',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
70 LIST = 'l',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
71 EMPTY_LIST = ']',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
72 OBJ = 'o',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
73 PUT = 'p',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
74 BINPUT = 'q',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
75 LONG_BINPUT = 'r',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
76 SETITEM = 's',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
77 TUPLE = 't',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
78 EMPTY_TUPLE = ')',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
79 SETITEMS = 'u',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
80 BINFLOAT = 'G',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
81
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
82 /* Protocol 2. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
83 PROTO = '\x80',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
84 NEWOBJ = '\x81',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
85 EXT1 = '\x82',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
86 EXT2 = '\x83',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
87 EXT4 = '\x84',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
88 TUPLE1 = '\x85',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
89 TUPLE2 = '\x86',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
90 TUPLE3 = '\x87',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
91 NEWTRUE = '\x88',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
92 NEWFALSE = '\x89',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
93 LONG1 = '\x8a',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
94 LONG4 = '\x8b',
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
95
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
96 /* Protocol 3 (Python 3.x) */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
97 BINBYTES = 'B',
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
98 SHORT_BINBYTES = 'C',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
99
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
100 /* Protocol 4 */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
101 SHORT_BINUNICODE = '\x8c',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
102 BINUNICODE8 = '\x8d',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
103 BINBYTES8 = '\x8e',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
104 EMPTY_SET = '\x8f',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
105 ADDITEMS = '\x90',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
106 FROZENSET = '\x91',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
107 NEWOBJ_EX = '\x92',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
108 STACK_GLOBAL = '\x93',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
109 MEMOIZE = '\x94',
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
110 FRAME = '\x95'
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
111 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
112
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
113 enum {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
114 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
115 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
116 break if this gets out of synch with pickle.py, but it's unclear that would
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
117 help anything either. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
118 BATCHSIZE = 1000,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
119
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
120 /* Nesting limit until Pickler, when running in "fast mode", starts
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
121 checking for self-referential data-structures. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
122 FAST_NESTING_LIMIT = 50,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
123
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
124 /* Initial size of the write buffer of Pickler. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
125 WRITE_BUF_SIZE = 4096,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
126
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
127 /* Prefetch size when unpickling (disabled on unpeekable streams) */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
128 PREFETCH = 8192 * 16,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
129
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
130 FRAME_SIZE_TARGET = 64 * 1024,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
131
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
132 FRAME_HEADER_SIZE = 9
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
133 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
134
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
135 /*************************************************************************/
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
136
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
137 /* State of the pickle module, per PEP 3121. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
138 typedef struct {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
139 /* Exception classes for pickle. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
140 PyObject *PickleError;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
141 PyObject *PicklingError;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
142 PyObject *UnpicklingError;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
143
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
144 /* copyreg.dispatch_table, {type_object: pickling_function} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
145 PyObject *dispatch_table;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
146
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
147 /* For the extension opcodes EXT1, EXT2 and EXT4. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
148
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
149 /* copyreg._extension_registry, {(module_name, function_name): code} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
150 PyObject *extension_registry;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
151 /* copyreg._extension_cache, {code: object} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
152 PyObject *extension_cache;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
153 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
154 PyObject *inverted_registry;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
155
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
156 /* Import mappings for compatibility with Python 2.x */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
157
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
158 /* _compat_pickle.NAME_MAPPING,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
159 {(oldmodule, oldname): (newmodule, newname)} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
160 PyObject *name_mapping_2to3;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
161 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
162 PyObject *import_mapping_2to3;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
163 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
164 PyObject *name_mapping_3to2;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
165 PyObject *import_mapping_3to2;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
166
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
167 /* codecs.encode, used for saving bytes in older protocols */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
168 PyObject *codecs_encode;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
169 } PickleState;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
170
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
171 /* Forward declaration of the _pickle module definition. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
172 static struct PyModuleDef _picklemodule;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
173
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
174 /* Given a module object, get its per-module state. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
175 static PickleState *
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
176 _Pickle_GetState(PyObject *module)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
177 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
178 return (PickleState *)PyModule_GetState(module);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
179 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
180
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
181 /* Find the module instance imported in the currently running sub-interpreter
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
182 and get its state. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
183 static PickleState *
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
184 _Pickle_GetGlobalState(void)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
185 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
186 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
187 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
188
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
189 /* Clear the given pickle module state. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
190 static void
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
191 _Pickle_ClearState(PickleState *st)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
192 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
193 Py_CLEAR(st->PickleError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
194 Py_CLEAR(st->PicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
195 Py_CLEAR(st->UnpicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
196 Py_CLEAR(st->dispatch_table);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
197 Py_CLEAR(st->extension_registry);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
198 Py_CLEAR(st->extension_cache);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
199 Py_CLEAR(st->inverted_registry);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
200 Py_CLEAR(st->name_mapping_2to3);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
201 Py_CLEAR(st->import_mapping_2to3);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
202 Py_CLEAR(st->name_mapping_3to2);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
203 Py_CLEAR(st->import_mapping_3to2);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
204 Py_CLEAR(st->codecs_encode);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
205 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
206
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
207 /* Initialize the given pickle module state. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
208 static int
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
209 _Pickle_InitState(PickleState *st)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
210 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
211 PyObject *copyreg = NULL;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
212 PyObject *compat_pickle = NULL;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
213 PyObject *codecs = NULL;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
214
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
215 copyreg = PyImport_ImportModule("copyreg");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
216 if (!copyreg)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
217 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
218 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
219 if (!st->dispatch_table)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
220 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
221 if (!PyDict_CheckExact(st->dispatch_table)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
222 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
223 "copyreg.dispatch_table should be a dict, not %.200s",
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
224 Py_TYPE(st->dispatch_table)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
225 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
226 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
227 st->extension_registry = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
228 PyObject_GetAttrString(copyreg, "_extension_registry");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
229 if (!st->extension_registry)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
230 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
231 if (!PyDict_CheckExact(st->extension_registry)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
232 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
233 "copyreg._extension_registry should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
234 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
235 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
236 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
237 st->inverted_registry = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
238 PyObject_GetAttrString(copyreg, "_inverted_registry");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
239 if (!st->inverted_registry)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
240 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
241 if (!PyDict_CheckExact(st->inverted_registry)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
242 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
243 "copyreg._inverted_registry should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
244 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
245 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
246 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
247 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
248 if (!st->extension_cache)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
249 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
250 if (!PyDict_CheckExact(st->extension_cache)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
251 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
252 "copyreg._extension_cache should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
253 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
254 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
255 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
256 Py_CLEAR(copyreg);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
257
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
258 /* Load the 2.x -> 3.x stdlib module mapping tables */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
259 compat_pickle = PyImport_ImportModule("_compat_pickle");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
260 if (!compat_pickle)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
261 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
262 st->name_mapping_2to3 = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
263 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
264 if (!st->name_mapping_2to3)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
265 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
266 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
267 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
268 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
269 Py_TYPE(st->name_mapping_2to3)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
270 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
271 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
272 st->import_mapping_2to3 = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
273 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
274 if (!st->import_mapping_2to3)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
275 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
276 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
277 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
278 "_compat_pickle.IMPORT_MAPPING should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
279 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
280 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
281 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
282 /* ... and the 3.x -> 2.x mapping tables */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
283 st->name_mapping_3to2 = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
284 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
285 if (!st->name_mapping_3to2)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
286 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
287 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
288 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
289 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
290 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
291 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
292 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
293 st->import_mapping_3to2 = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
294 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
295 if (!st->import_mapping_3to2)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
296 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
297 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
298 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
299 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
300 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
301 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
302 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
303 Py_CLEAR(compat_pickle);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
304
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
305 codecs = PyImport_ImportModule("codecs");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
306 if (codecs == NULL)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
307 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
308 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
309 if (st->codecs_encode == NULL) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
310 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
311 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
312 if (!PyCallable_Check(st->codecs_encode)) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
313 PyErr_Format(PyExc_RuntimeError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
314 "codecs.encode should be a callable, not %.200s",
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
315 Py_TYPE(st->codecs_encode)->tp_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
316 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
317 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
318 Py_CLEAR(codecs);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
319
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
320 return 0;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
321
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
322 error:
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
323 Py_CLEAR(copyreg);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
324 Py_CLEAR(compat_pickle);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
325 Py_CLEAR(codecs);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
326 _Pickle_ClearState(st);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
327 return -1;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
328 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
329
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
330 /* Helper for calling a function with a single argument quickly.
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
331
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
332 This function steals the reference of the given argument. */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
333 static PyObject *
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
334 _Pickle_FastCall(PyObject *func, PyObject *obj)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
335 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
336 PyObject *result;
87630
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
337 PyObject *arg_tuple = PyTuple_New(1);
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
338
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
339 /* Note: this function used to reuse the argument tuple. This used to give
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
340 a slight performance boost with older pickle implementations where many
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
341 unbuffered reads occurred (thus needing many function calls).
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
342
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
343 However, this optimization was removed because it was too complicated
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
344 to get right. It abused the C API for tuples to mutate them which led
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
345 to subtle reference counting and concurrency bugs. Furthermore, the
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
346 introduction of protocol 4 and the prefetching optimization via peek()
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
347 significantly reduced the number of function calls we do. Thus, the
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
348 benefits became marginal at best. */
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
349
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
350 if (arg_tuple == NULL) {
87630
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
351 Py_DECREF(obj);
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
352 return NULL;
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
353 }
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
354 PyTuple_SET_ITEM(arg_tuple, 0, obj);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
355 result = PyObject_Call(func, arg_tuple, NULL);
87630
dd51b72cfb52 Remove the tuple reuse optimization in _Pickle_FastCall.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87620
diff changeset
356 Py_CLEAR(arg_tuple);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
357 return result;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
358 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
359
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
360 /*************************************************************************/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
361
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
362 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
363 stack_underflow(void)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
364 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
365 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
366 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
367 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
368 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
369
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
370 /* Internal data type used as the unpickling stack. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
371 typedef struct {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
372 PyObject_VAR_HEAD
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
373 PyObject **data;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
374 Py_ssize_t allocated; /* number of slots in data allocated */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
375 } Pdata;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
376
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
377 static void
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
378 Pdata_dealloc(Pdata *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
379 {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
380 Py_ssize_t i = Py_SIZE(self);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
381 while (--i >= 0) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
382 Py_DECREF(self->data[i]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
383 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
384 PyMem_FREE(self->data);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
385 PyObject_Del(self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
386 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
387
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
388 static PyTypeObject Pdata_Type = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
389 PyVarObject_HEAD_INIT(NULL, 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
390 "_pickle.Pdata", /*tp_name*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
391 sizeof(Pdata), /*tp_basicsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
392 0, /*tp_itemsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
393 (destructor)Pdata_dealloc, /*tp_dealloc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
394 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
395
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
396 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
397 Pdata_New(void)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
398 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
399 Pdata *self;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
400
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
401 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
402 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
403 Py_SIZE(self) = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
404 self->allocated = 8;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
405 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
406 if (self->data)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
407 return (PyObject *)self;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
408 Py_DECREF(self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
409 return PyErr_NoMemory();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
410 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
411
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
412
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
413 /* Retain only the initial clearto items. If clearto >= the current
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
414 * number of items, this is a (non-erroneous) NOP.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
415 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
416 static int
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
417 Pdata_clear(Pdata *self, Py_ssize_t clearto)
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
418 {
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
419 Py_ssize_t i = Py_SIZE(self);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
420
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
421 if (clearto < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
422 return stack_underflow();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
423 if (clearto >= i)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
424 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
425
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
426 while (--i >= clearto) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
427 Py_CLEAR(self->data[i]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
428 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
429 Py_SIZE(self) = clearto;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
430 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
431 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
432
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
433 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
434 Pdata_grow(Pdata *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
435 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
436 PyObject **data = self->data;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
437 Py_ssize_t allocated = self->allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
438 Py_ssize_t new_allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
439
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
440 new_allocated = (allocated >> 3) + 6;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
441 /* check for integer overflow */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
442 if (new_allocated > PY_SSIZE_T_MAX - allocated)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
443 goto nomemory;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
444 new_allocated += allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
445 if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
446 goto nomemory;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
447 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
448 if (data == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
449 goto nomemory;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
450
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
451 self->data = data;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
452 self->allocated = new_allocated;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
453 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
454
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
455 nomemory:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
456 PyErr_NoMemory();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
457 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
458 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
459
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
460 /* D is a Pdata*. Pop the topmost element and store it into V, which
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
461 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
462 * is raised and V is set to NULL.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
463 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
464 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
465 Pdata_pop(Pdata *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
466 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
467 PickleState *st = _Pickle_GetGlobalState();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
468 if (Py_SIZE(self) == 0) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
469 PyErr_SetString(st->UnpicklingError, "bad pickle data");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
470 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
471 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
472 return self->data[--Py_SIZE(self)];
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
473 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
474 #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
475
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
476 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
477 Pdata_push(Pdata *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
478 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
479 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
480 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
481 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
482 self->data[Py_SIZE(self)++] = obj;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
483 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
484 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
485
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
486 /* Push an object on stack, transferring its ownership to the stack. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
487 #define PDATA_PUSH(D, O, ER) do { \
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
488 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
489
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
490 /* Push an object on stack, adding a new reference to the object. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
491 #define PDATA_APPEND(D, O, ER) do { \
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
492 Py_INCREF((O)); \
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
493 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
494
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
495 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
496 Pdata_poptuple(Pdata *self, Py_ssize_t start)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
497 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
498 PyObject *tuple;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
499 Py_ssize_t len, i, j;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
500
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
501 len = Py_SIZE(self) - start;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
502 tuple = PyTuple_New(len);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
503 if (tuple == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
504 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
505 for (i = start, j = 0; j < len; i++, j++)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
506 PyTuple_SET_ITEM(tuple, j, self->data[i]);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
507
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
508 Py_SIZE(self) = start;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
509 return tuple;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
510 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
511
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
512 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
513 Pdata_poplist(Pdata *self, Py_ssize_t start)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
514 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
515 PyObject *list;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
516 Py_ssize_t len, i, j;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
517
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
518 len = Py_SIZE(self) - start;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
519 list = PyList_New(len);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
520 if (list == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
521 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
522 for (i = start, j = 0; j < len; i++, j++)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
523 PyList_SET_ITEM(list, j, self->data[i]);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
524
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
525 Py_SIZE(self) = start;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
526 return list;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
527 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
528
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
529 typedef struct {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
530 PyObject *me_key;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
531 Py_ssize_t me_value;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
532 } PyMemoEntry;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
533
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
534 typedef struct {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
535 Py_ssize_t mt_mask;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
536 Py_ssize_t mt_used;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
537 Py_ssize_t mt_allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
538 PyMemoEntry *mt_table;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
539 } PyMemoTable;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
540
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
541 typedef struct PicklerObject {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
542 PyObject_HEAD
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
543 PyMemoTable *memo; /* Memo table, keep track of the seen
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
544 objects to support self-referential objects
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
545 pickling. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
546 PyObject *pers_func; /* persistent_id() method, can be NULL */
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
547 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
548
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
549 PyObject *write; /* write() method of the output stream. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
550 PyObject *output_buffer; /* Write into a local bytearray buffer before
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
551 flushing to the stream. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
552 Py_ssize_t output_len; /* Length of output_buffer. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
553 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
554 int proto; /* Pickle protocol number, >= 0 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
555 int bin; /* Boolean, true if proto > 0 */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
556 int framing; /* True when framing is enabled, proto >= 4 */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
557 Py_ssize_t frame_start; /* Position in output_buffer where the
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
558 where the current frame begins. -1 if there
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
559 is no frame currently open. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
560
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
561 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
562 int fast; /* Enable fast mode if set to a true value.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
563 The fast mode disable the usage of memo,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
564 therefore speeding the pickling process by
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
565 not generating superfluous PUT opcodes. It
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
566 should not be used if with self-referential
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
567 objects. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
568 int fast_nesting;
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
569 int fix_imports; /* Indicate whether Pickler should fix
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
570 the name of globals for Python 2.x. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
571 PyObject *fast_memo;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
572 } PicklerObject;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
573
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
574 typedef struct UnpicklerObject {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
575 PyObject_HEAD
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
576 Pdata *stack; /* Pickle data stack, store unpickled objects. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
577
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
578 /* The unpickler memo is just an array of PyObject *s. Using a dict
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
579 is unnecessary, since the keys are contiguous ints. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
580 PyObject **memo;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
581 Py_ssize_t memo_size; /* Capacity of the memo array */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
582 Py_ssize_t memo_len; /* Number of objects in the memo */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
583
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
584 PyObject *pers_func; /* persistent_load() method, can be NULL. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
585
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
586 Py_buffer buffer;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
587 char *input_buffer;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
588 char *input_line;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
589 Py_ssize_t input_len;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
590 Py_ssize_t next_read_idx;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
591 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
592
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
593 PyObject *read; /* read() method of the input stream. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
594 PyObject *readline; /* readline() method of the input stream. */
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
595 PyObject *peek; /* peek() method of the input stream, or NULL */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
596
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
597 char *encoding; /* Name of the encoding to be used for
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
598 decoding strings pickled using Python
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
599 2.x. The default value is "ASCII" */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
600 char *errors; /* Name of errors handling scheme to used when
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
601 decoding strings. The default value is
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
602 "strict". */
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
603 Py_ssize_t *marks; /* Mark stack, used for unpickling container
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
604 objects. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
605 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
606 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
607 int proto; /* Protocol of the pickle loaded. */
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
608 int fix_imports; /* Indicate whether Unpickler should fix
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
609 the name of globals pickled by Python 2.x. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
610 } UnpicklerObject;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
611
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
612 /* Forward declarations */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
613 static int save(PicklerObject *, PyObject *, int);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
614 static int save_reduce(PicklerObject *, PyObject *, PyObject *);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
615 static PyTypeObject Pickler_Type;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
616 static PyTypeObject Unpickler_Type;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
617
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
618
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
619 /*************************************************************************
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
620 A custom hashtable mapping void* to Python ints. This is used by the pickler
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
621 for memoization. Using a custom hashtable rather than PyDict allows us to skip
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
622 a bunch of unnecessary object creation. This makes a huge performance
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
623 difference. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
624
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
625 #define MT_MINSIZE 8
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
626 #define PERTURB_SHIFT 5
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
627
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
628
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
629 static PyMemoTable *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
630 PyMemoTable_New(void)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
631 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
632 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
633 if (memo == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
634 PyErr_NoMemory();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
635 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
636 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
637
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
638 memo->mt_used = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
639 memo->mt_allocated = MT_MINSIZE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
640 memo->mt_mask = MT_MINSIZE - 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
641 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
642 if (memo->mt_table == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
643 PyMem_FREE(memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
644 PyErr_NoMemory();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
645 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
646 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
647 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
648
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
649 return memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
650 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
651
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
652 static PyMemoTable *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
653 PyMemoTable_Copy(PyMemoTable *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
654 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
655 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
656 PyMemoTable *new = PyMemoTable_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
657 if (new == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
658 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
659
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
660 new->mt_used = self->mt_used;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
661 new->mt_allocated = self->mt_allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
662 new->mt_mask = self->mt_mask;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
663 /* The table we get from _New() is probably smaller than we wanted.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
664 Free it and allocate one that's the right size. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
665 PyMem_FREE(new->mt_table);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
666 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
667 if (new->mt_table == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
668 PyMem_FREE(new);
84577
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
669 PyErr_NoMemory();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
670 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
671 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
672 for (i = 0; i < self->mt_allocated; i++) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
673 Py_XINCREF(self->mt_table[i].me_key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
674 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
675 memcpy(new->mt_table, self->mt_table,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
676 sizeof(PyMemoEntry) * self->mt_allocated);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
677
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
678 return new;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
679 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
680
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
681 static Py_ssize_t
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
682 PyMemoTable_Size(PyMemoTable *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
683 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
684 return self->mt_used;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
685 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
686
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
687 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
688 PyMemoTable_Clear(PyMemoTable *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
689 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
690 Py_ssize_t i = self->mt_allocated;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
691
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
692 while (--i >= 0) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
693 Py_XDECREF(self->mt_table[i].me_key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
694 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
695 self->mt_used = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
696 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
697 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
698 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
699
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
700 static void
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
701 PyMemoTable_Del(PyMemoTable *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
702 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
703 if (self == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
704 return;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
705 PyMemoTable_Clear(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
706
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
707 PyMem_FREE(self->mt_table);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
708 PyMem_FREE(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
709 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
710
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
711 /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
712 can be considerably simpler than dictobject.c's lookdict(). */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
713 static PyMemoEntry *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
714 _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
715 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
716 size_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
717 size_t perturb;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
718 size_t mask = (size_t)self->mt_mask;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
719 PyMemoEntry *table = self->mt_table;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
720 PyMemoEntry *entry;
65494
e8969fb822fc make hashes always the size of pointers; introduce Py_hash_t #9778
Benjamin Peterson <benjamin@python.org>
parents: 65224
diff changeset
721 Py_hash_t hash = (Py_hash_t)key >> 3;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
722
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
723 i = hash & mask;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
724 entry = &table[i];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
725 if (entry->me_key == NULL || entry->me_key == key)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
726 return entry;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
727
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
728 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
729 i = (i << 2) + i + perturb + 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
730 entry = &table[i & mask];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
731 if (entry->me_key == NULL || entry->me_key == key)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
732 return entry;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
733 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
734 assert(0); /* Never reached */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
735 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
736 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
737
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
738 /* Returns -1 on failure, 0 on success. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
739 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
740 _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
741 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
742 PyMemoEntry *oldtable = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
743 PyMemoEntry *oldentry, *newentry;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
744 Py_ssize_t new_size = MT_MINSIZE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
745 Py_ssize_t to_process;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
746
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
747 assert(min_size > 0);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
748
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
749 /* Find the smallest valid table size >= min_size. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
750 while (new_size < min_size && new_size > 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
751 new_size <<= 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
752 if (new_size <= 0) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
753 PyErr_NoMemory();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
754 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
755 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
756 /* new_size needs to be a power of two. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
757 assert((new_size & (new_size - 1)) == 0);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
758
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
759 /* Allocate new table. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
760 oldtable = self->mt_table;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
761 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
762 if (self->mt_table == NULL) {
84576
f85fcbbbe8de Issue #18408: _PyMemoTable_ResizeTable() now restores the old table if
Victor Stinner <victor.stinner@gmail.com>
parents: 84572
diff changeset
763 self->mt_table = oldtable;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
764 PyErr_NoMemory();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
765 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
766 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
767 self->mt_allocated = new_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
768 self->mt_mask = new_size - 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
769 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
770
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
771 /* Copy entries from the old table. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
772 to_process = self->mt_used;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
773 for (oldentry = oldtable; to_process > 0; oldentry++) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
774 if (oldentry->me_key != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
775 to_process--;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
776 /* newentry is a pointer to a chunk of the new
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
777 mt_table, so we're setting the key:value pair
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
778 in-place. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
779 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
780 newentry->me_key = oldentry->me_key;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
781 newentry->me_value = oldentry->me_value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
782 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
783 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
784
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
785 /* Deallocate the old table. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
786 PyMem_FREE(oldtable);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
787 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
788 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
789
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
790 /* Returns NULL on failure, a pointer to the value otherwise. */
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
791 static Py_ssize_t *
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
792 PyMemoTable_Get(PyMemoTable *self, PyObject *key)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
793 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
794 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
795 if (entry->me_key == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
796 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
797 return &entry->me_value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
798 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
799
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
800 /* Returns -1 on failure, 0 on success. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
801 static int
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
802 PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
803 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
804 PyMemoEntry *entry;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
805
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
806 assert(key != NULL);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
807
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
808 entry = _PyMemoTable_Lookup(self, key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
809 if (entry->me_key != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
810 entry->me_value = value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
811 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
812 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
813 Py_INCREF(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
814 entry->me_key = key;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
815 entry->me_value = value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
816 self->mt_used++;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
817
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
818 /* If we added a key, we can safely resize. Otherwise just return!
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
819 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
820 *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
821 * Quadrupling the size improves average table sparseness
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
822 * (reducing collisions) at the cost of some memory. It also halves
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
823 * the number of expensive resize operations in a growing memo table.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
824 *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
825 * Very large memo tables (over 50K items) use doubling instead.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
826 * This may help applications with severe memory constraints.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
827 */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
828 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
829 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
830 return _PyMemoTable_ResizeTable(self,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
831 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
832 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
833
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
834 #undef MT_MINSIZE
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
835 #undef PERTURB_SHIFT
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
836
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
837 /*************************************************************************/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
838
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
839
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
840 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
841 _Pickler_ClearBuffer(PicklerObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
842 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
843 Py_CLEAR(self->output_buffer);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
844 self->output_buffer =
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
845 PyBytes_FromStringAndSize(NULL, self->max_output_len);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
846 if (self->output_buffer == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
847 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
848 self->output_len = 0;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
849 self->frame_start = -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
850 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
851 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
852
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
853 static void
87446
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
854 _write_size64(char *out, size_t value)
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
855 {
87560
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
856 int i;
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
857
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
858 assert(sizeof(size_t) <= 8);
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
859
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
860 for (i = 0; i < sizeof(size_t); i++) {
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
861 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
862 }
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
863 for (i = sizeof(size_t); i < 8; i++) {
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
864 out[i] = 0;
87540
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
865 }
87446
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
866 }
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
867
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
868 static void
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
869 _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
870 {
87446
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
871 qdata[0] = FRAME;
458340ed0606 Fix writing out 64-bit size fields on 32-bit builds
Antoine Pitrou <solipsis@pitrou.net>
parents: 87444
diff changeset
872 _write_size64(qdata + 1, frame_len);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
873 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
874
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
875 static int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
876 _Pickler_CommitFrame(PicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
877 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
878 size_t frame_len;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
879 char *qdata;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
880
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
881 if (!self->framing || self->frame_start == -1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
882 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
883 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
884 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
885 _Pickler_WriteFrameHeader(self, qdata, frame_len);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
886 self->frame_start = -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
887 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
888 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
889
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
890 static int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
891 _Pickler_OpcodeBoundary(PicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
892 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
893 Py_ssize_t frame_len;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
894
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
895 if (!self->framing || self->frame_start == -1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
896 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
897 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
898 if (frame_len >= FRAME_SIZE_TARGET)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
899 return _Pickler_CommitFrame(self);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
900 else
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
901 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
902 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
903
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
904 static PyObject *
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
905 _Pickler_GetString(PicklerObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
906 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
907 PyObject *output_buffer = self->output_buffer;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
908
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
909 assert(self->output_buffer != NULL);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
910
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
911 if (_Pickler_CommitFrame(self))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
912 return NULL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
913
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
914 self->output_buffer = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
915 /* Resize down to exact size */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
916 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
917 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
918 return output_buffer;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
919 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
920
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
921 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
922 _Pickler_FlushToFile(PicklerObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
923 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
924 PyObject *output, *result;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
925
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
926 assert(self->write != NULL);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
927
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
928 /* This will commit the frame first */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
929 output = _Pickler_GetString(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
930 if (output == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
931 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
932
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
933 result = _Pickle_FastCall(self->write, output);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
934 Py_XDECREF(result);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
935 return (result == NULL) ? -1 : 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
936 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
937
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
938 static Py_ssize_t
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
939 _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
940 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
941 Py_ssize_t i, n, required;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
942 char *buffer;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
943 int need_new_frame;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
944
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
945 assert(s != NULL);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
946 need_new_frame = (self->framing && self->frame_start == -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
947
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
948 if (need_new_frame)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
949 n = data_len + FRAME_HEADER_SIZE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
950 else
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
951 n = data_len;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
952
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
953 required = self->output_len + n;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
954 if (required > self->max_output_len) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
955 /* Make place in buffer for the pickle chunk */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
956 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
957 PyErr_NoMemory();
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
958 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
959 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
960 self->max_output_len = (self->output_len + n) / 2 * 3;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
961 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
962 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
963 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
964 buffer = PyBytes_AS_STRING(self->output_buffer);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
965 if (need_new_frame) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
966 /* Setup new frame */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
967 Py_ssize_t frame_start = self->output_len;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
968 self->frame_start = frame_start;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
969 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
970 /* Write an invalid value, for debugging */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
971 buffer[frame_start + i] = 0xFE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
972 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
973 self->output_len += FRAME_HEADER_SIZE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
974 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
975 if (data_len < 8) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
976 /* This is faster than memcpy when the string is short. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
977 for (i = 0; i < data_len; i++) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
978 buffer[self->output_len + i] = s[i];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
979 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
980 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
981 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
982 memcpy(buffer + self->output_len, s, data_len);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
983 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
984 self->output_len += data_len;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
985 return data_len;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
986 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
987
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
988 static PicklerObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
989 _Pickler_New(void)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
990 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
991 PicklerObject *self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
992
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
993 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
994 if (self == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
995 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
996
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
997 self->pers_func = NULL;
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
998 self->dispatch_table = NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
999 self->write = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1000 self->proto = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1001 self->bin = 0;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1002 self->framing = 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1003 self->frame_start = -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1004 self->fast = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1005 self->fast_nesting = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1006 self->fix_imports = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1007 self->fast_memo = NULL;
84565
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1008 self->max_output_len = WRITE_BUF_SIZE;
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1009 self->output_len = 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1010
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1011 self->memo = PyMemoTable_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1012 self->output_buffer = PyBytes_FromStringAndSize(NULL,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1013 self->max_output_len);
84565
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1014
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1015 if (self->memo == NULL || self->output_buffer == NULL) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1016 Py_DECREF(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1017 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1018 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1019 return self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1020 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1021
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1022 static int
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1023 _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1024 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1025 long proto;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1026
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1027 if (protocol == NULL || protocol == Py_None) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1028 proto = DEFAULT_PROTOCOL;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1029 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1030 else {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1031 proto = PyLong_AsLong(protocol);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1032 if (proto < 0) {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1033 if (proto == -1 && PyErr_Occurred())
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1034 return -1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1035 proto = HIGHEST_PROTOCOL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1036 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1037 else if (proto > HIGHEST_PROTOCOL) {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1038 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1039 HIGHEST_PROTOCOL);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1040 return -1;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1041 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1042 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
1043 self->proto = (int)proto;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1044 self->bin = proto > 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1045 self->fix_imports = fix_imports && proto < 3;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1046 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1047 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1048
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1049 /* Returns -1 (with an exception set) on failure, 0 on success. This may
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1050 be called once on a freshly created Pickler. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1051 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1052 _Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1053 {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
1054 _Py_IDENTIFIER(write);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1055 assert(file != NULL);
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1056 self->write = _PyObject_GetAttrId(file, &PyId_write);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1057 if (self->write == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1058 if (PyErr_ExceptionMatches(PyExc_AttributeError))
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1059 PyErr_SetString(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1060 "file must have a 'write' attribute");
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1061 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1062 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1063
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1064 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1065 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1066
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1067 /* Returns the size of the input on success, -1 on failure. This takes its
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1068 own reference to `input`. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1069 static Py_ssize_t
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1070 _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1071 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1072 if (self->buffer.buf != NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1073 PyBuffer_Release(&self->buffer);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1074 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
49546
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
1075 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1076 self->input_buffer = self->buffer.buf;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1077 self->input_len = self->buffer.len;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1078 self->next_read_idx = 0;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1079 self->prefetched_idx = self->input_len;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1080 return self->input_len;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1081 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1082
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1083 static int
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1084 _Unpickler_SkipConsumed(UnpicklerObject *self)
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1085 {
86796
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1086 Py_ssize_t consumed;
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1087 PyObject *r;
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1088
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1089 consumed = self->next_read_idx - self->prefetched_idx;
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1090 if (consumed <= 0)
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1091 return 0;
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1092
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1093 assert(self->peek); /* otherwise we did something wrong */
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1094 /* This makes an useless copy... */
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1095 r = PyObject_CallFunction(self->read, "n", consumed);
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1096 if (r == NULL)
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1097 return -1;
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1098 Py_DECREF(r);
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1099
78b35cf9c024 cleanup _Unpickler_SkipConsumed(): remove 1 level of indentation
Victor Stinner <victor.stinner@gmail.com>
parents: 85419
diff changeset
1100 self->prefetched_idx = self->next_read_idx;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1101 return 0;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1102 }
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1103
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1104 static const Py_ssize_t READ_WHOLE_LINE = -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1105
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1106 /* If reading from a file, we need to only pull the bytes we need, since there
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1107 may be multiple pickle objects arranged contiguously in the same input
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1108 buffer.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1109
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1110 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1111 bytes from the input stream/buffer.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1112
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1113 Update the unpickler's input buffer with the newly-read data. Returns -1 on
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1114 failure; on success, returns the number of bytes read from the file.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1115
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1116 On success, self->input_len will be 0; this is intentional so that when
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1117 unpickling from a file, the "we've run out of data" code paths will trigger,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1118 causing the Unpickler to go back to the file for more data. Use the returned
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1119 size to tell you how much data you can process. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1120 static Py_ssize_t
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1121 _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1122 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1123 PyObject *data;
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1124 Py_ssize_t read_size;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1125
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1126 assert(self->read != NULL);
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
1127
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1128 if (_Unpickler_SkipConsumed(self) < 0)
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1129 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1130
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1131 if (n == READ_WHOLE_LINE) {
87631
b44dbf07732e Remove explicit empty tuple reuse in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87630
diff changeset
1132 PyObject *empty_tuple = PyTuple_New(0);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1133 data = PyObject_Call(self->readline, empty_tuple, NULL);
87631
b44dbf07732e Remove explicit empty tuple reuse in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87630
diff changeset
1134 Py_DECREF(empty_tuple);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1135 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1136 else {
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1137 PyObject *len;
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1138 /* Prefetch some data without advancing the file pointer, if possible */
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1139 if (self->peek && n < PREFETCH) {
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1140 len = PyLong_FromSsize_t(PREFETCH);
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1141 if (len == NULL)
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1142 return -1;
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1143 data = _Pickle_FastCall(self->peek, len);
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1144 if (data == NULL) {
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1145 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1146 return -1;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1147 /* peek() is probably not supported by the given file object */
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1148 PyErr_Clear();
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1149 Py_CLEAR(self->peek);
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1150 }
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1151 else {
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1152 read_size = _Unpickler_SetStringInput(self, data);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1153 Py_DECREF(data);
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1154 self->prefetched_idx = 0;
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1155 if (n <= read_size)
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1156 return n;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1157 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1158 }
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1159 len = PyLong_FromSsize_t(n);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1160 if (len == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1161 return -1;
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
1162 data = _Pickle_FastCall(self->read, len);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1163 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1164 if (data == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1165 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1166
87654
d565310e7ae3 Issue #17897: Optimized unpickle prefetching.
Serhiy Storchaka <storchaka@gmail.com>
parents: 87632
diff changeset
1167 read_size = _Unpickler_SetStringInput(self, data);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1168 Py_DECREF(data);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1169 return read_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1170 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1171
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1172 /* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1173
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1174 This should be used for all data reads, rather than accessing the unpickler's
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1175 input buffer directly. This method deals correctly with reading from input
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1176 streams, which the input buffer doesn't deal with.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1177
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1178 Note that when reading from a file-like object, self->next_read_idx won't
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1179 be updated (it should remain at 0 for the entire unpickling process). You
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1180 should use this function's return value to know how many bytes you can
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1181 consume.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1182
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1183 Returns -1 (with an exception set) on failure. On success, return the
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1184 number of chars read. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1185 static Py_ssize_t
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1186 _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1187 {
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1188 Py_ssize_t num_read;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1189
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1190 if (self->next_read_idx + n <= self->input_len) {
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1191 *s = self->input_buffer + self->next_read_idx;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1192 self->next_read_idx += n;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1193 return n;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1194 }
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1195 if (!self->read) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1196 PyErr_Format(PyExc_EOFError, "Ran out of input");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1197 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1198 }
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1199 num_read = _Unpickler_ReadFromFile(self, n);
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1200 if (num_read < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1201 return -1;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1202 if (num_read < n) {
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1203 PyErr_Format(PyExc_EOFError, "Ran out of input");
49705
218a98981183 #4298: pickle.load() can segfault on invalid or truncated input.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49638
diff changeset
1204 return -1;
218a98981183 #4298: pickle.load() can segfault on invalid or truncated input.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49638
diff changeset
1205 }
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1206 *s = self->input_buffer;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1207 self->next_read_idx = n;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1208 return n;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1209 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1210
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1211 static Py_ssize_t
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1212 _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1213 char **result)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1214 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1215 char *input_line = PyMem_Realloc(self->input_line, len + 1);
84577
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1216 if (input_line == NULL) {
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1217 PyErr_NoMemory();
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1218 return -1;
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1219 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1220
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1221 memcpy(input_line, line, len);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1222 input_line[len] = '\0';
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1223 self->input_line = input_line;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1224 *result = self->input_line;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1225 return len;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1226 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1227
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1228 /* Read a line from the input stream/buffer. If we run off the end of the input
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1229 before hitting \n, return the data we found.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1230
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1231 Returns the number of chars read, or -1 on failure. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1232 static Py_ssize_t
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1233 _Unpickler_Readline(UnpicklerObject *self, char **result)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1234 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1235 Py_ssize_t i, num_read;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1236
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1237 for (i = self->next_read_idx; i < self->input_len; i++) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1238 if (self->input_buffer[i] == '\n') {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1239 char *line_start = self->input_buffer + self->next_read_idx;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1240 num_read = i - self->next_read_idx + 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1241 self->next_read_idx = i + 1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1242 return _Unpickler_CopyLine(self, line_start, num_read, result);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1243 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1244 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1245 if (self->read) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1246 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1247 if (num_read < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1248 return -1;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1249 self->next_read_idx = num_read;
71817
c47bc1349e61 Issue #12687: Fix a possible buffering bug when unpickling text mode (protocol 0, mostly) pickles.
Antoine Pitrou <solipsis@pitrou.net>
parents: 71347
diff changeset
1250 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1251 }
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
1252
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1253 /* If we get here, we've run off the end of the input string. Return the
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1254 remaining string and let the caller figure it out. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1255 *result = self->input_buffer + self->next_read_idx;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1256 num_read = i - self->next_read_idx;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1257 self->next_read_idx = i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1258 return num_read;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1259 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1260
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1261 /* Returns -1 (with an exception set) on failure, 0 on success. The memo array
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1262 will be modified in place. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1263 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1264 _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1265 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1266 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1267 PyObject **memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1268
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1269 assert(new_size > self->memo_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1270
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1271 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1272 if (memo == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1273 PyErr_NoMemory();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1274 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1275 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1276 self->memo = memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1277 for (i = self->memo_size; i < new_size; i++)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1278 self->memo[i] = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1279 self->memo_size = new_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1280 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1281 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1282
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1283 /* Returns NULL if idx is out of bounds. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1284 static PyObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1285 _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1286 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1287 if (idx < 0 || idx >= self->memo_size)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1288 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1289
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1290 return self->memo[idx];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1291 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1292
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1293 /* Returns -1 (with an exception set) on failure, 0 on success.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1294 This takes its own reference to `value`. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1295 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1296 _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1297 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1298 PyObject *old_item;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1299
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1300 if (idx >= self->memo_size) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1301 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1302 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1303 assert(idx < self->memo_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1304 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1305 Py_INCREF(value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1306 old_item = self->memo[idx];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1307 self->memo[idx] = value;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1308 if (old_item != NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1309 Py_DECREF(old_item);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1310 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1311 else {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1312 self->memo_len++;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1313 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1314 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1315 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1316
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1317 static PyObject **
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1318 _Unpickler_NewMemo(Py_ssize_t new_size)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1319 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1320 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
84577
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1321 if (memo == NULL) {
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1322 PyErr_NoMemory();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1323 return NULL;
84577
bb5da6e795a1 Issue #18408: _pickle.c: Add missing PyErr_NoMemory() on memory allocation failures
Victor Stinner <victor.stinner@gmail.com>
parents: 84576
diff changeset
1324 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1325 memset(memo, 0, new_size * sizeof(PyObject *));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1326 return memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1327 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1328
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1329 /* Free the unpickler's memo, taking care to decref any items left in it. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1330 static void
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1331 _Unpickler_MemoCleanup(UnpicklerObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1332 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1333 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1334 PyObject **memo = self->memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1335
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1336 if (self->memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1337 return;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1338 self->memo = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1339 i = self->memo_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1340 while (--i >= 0) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1341 Py_XDECREF(memo[i]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1342 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1343 PyMem_FREE(memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1344 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1345
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1346 static UnpicklerObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1347 _Unpickler_New(void)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1348 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1349 UnpicklerObject *self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1350
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1351 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1352 if (self == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1353 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1354
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1355 self->pers_func = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1356 self->input_buffer = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1357 self->input_line = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1358 self->input_len = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1359 self->next_read_idx = 0;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1360 self->prefetched_idx = 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1361 self->read = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1362 self->readline = NULL;
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1363 self->peek = NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1364 self->encoding = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1365 self->errors = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1366 self->marks = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1367 self->num_marks = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1368 self->marks_size = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1369 self->proto = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1370 self->fix_imports = 0;
84565
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1371 memset(&self->buffer, 0, sizeof(Py_buffer));
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1372 self->memo_size = 32;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1373 self->memo_len = 0;
84565
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1374 self->memo = _Unpickler_NewMemo(self->memo_size);
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1375 self->stack = (Pdata *)Pdata_New();
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1376
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1377 if (self->memo == NULL || self->stack == NULL) {
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1378 Py_DECREF(self);
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1379 return NULL;
1a1869baec4c Issue #18408: Fix _Pickler_New() and _Unpickler_New(): initialize all
Victor Stinner <victor.stinner@gmail.com>
parents: 84502
diff changeset
1380 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1381
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1382 return self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1383 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1384
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1385 /* Returns -1 (with an exception set) on failure, 0 on success. This may
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1386 be called once on a freshly created Pickler. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1387 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1388 _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1389 {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
1390 _Py_IDENTIFIER(peek);
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
1391 _Py_IDENTIFIER(read);
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
1392 _Py_IDENTIFIER(readline);
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1393
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1394 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1395 if (self->peek == NULL) {
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1396 if (PyErr_ExceptionMatches(PyExc_AttributeError))
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1397 PyErr_Clear();
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1398 else
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1399 return -1;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1400 }
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1401 self->read = _PyObject_GetAttrId(file, &PyId_read);
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1402 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1403 if (self->readline == NULL || self->read == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1405 PyErr_SetString(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1406 "file must have 'read' and 'readline' attributes");
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1407 Py_CLEAR(self->read);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1408 Py_CLEAR(self->readline);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
1409 Py_CLEAR(self->peek);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1410 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1411 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1412 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1413 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1414
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1415 /* Returns -1 (with an exception set) on failure, 0 on success. This may
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1416 be called once on a freshly created Pickler. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1417 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1418 _Unpickler_SetInputEncoding(UnpicklerObject *self,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1419 const char *encoding,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1420 const char *errors)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1421 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1422 if (encoding == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1423 encoding = "ASCII";
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1424 if (errors == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1425 errors = "strict";
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1426
84502
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
1427 self->encoding = _PyMem_Strdup(encoding);
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
1428 self->errors = _PyMem_Strdup(errors);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1429 if (self->encoding == NULL || self->errors == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1430 PyErr_NoMemory();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1431 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1432 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1433 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1434 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1435
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1436 /* Generate a GET opcode for an object stored in the memo. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1437 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1438 memo_get(PicklerObject *self, PyObject *key)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1439 {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1440 Py_ssize_t *value;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1441 char pdata[30];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1442 Py_ssize_t len;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1443
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1444 value = PyMemoTable_Get(self->memo, key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1445 if (value == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1446 PyErr_SetObject(PyExc_KeyError, key);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1447 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1448 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1449
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1450 if (!self->bin) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1451 pdata[0] = GET;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1452 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1453 "%" PY_FORMAT_SIZE_T "d\n", *value);
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1454 len = strlen(pdata);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1455 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1456 else {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1457 if (*value < 256) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1458 pdata[0] = BINGET;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1459 pdata[1] = (unsigned char)(*value & 0xff);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1460 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1461 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1462 else if (*value <= 0xffffffffL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1463 pdata[0] = LONG_BINGET;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1464 pdata[1] = (unsigned char)(*value & 0xff);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1465 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1466 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1467 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1468 len = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1469 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1470 else { /* unlikely */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1471 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1472 PyErr_SetString(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1473 "memo id too large for LONG_BINGET");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1474 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1475 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1476 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1477
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1478 if (_Pickler_Write(self, pdata, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1479 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1480
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1481 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1482 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1483
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1484 /* Store an object in the memo, assign it a new unique ID based on the number
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1485 of objects currently stored in the memo and generate a PUT opcode. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1486 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1487 memo_put(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1488 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1489 char pdata[30];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1490 Py_ssize_t len;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1491 Py_ssize_t idx;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1492
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1493 const char memoize_op = MEMOIZE;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1494
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1495 if (self->fast)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1496 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1497
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1498 idx = PyMemoTable_Size(self->memo);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1499 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1500 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1501
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1502 if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1503 if (_Pickler_Write(self, &memoize_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1504 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1505 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1506 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1507 else if (!self->bin) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1508 pdata[0] = PUT;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1509 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1510 "%" PY_FORMAT_SIZE_T "d\n", idx);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1511 len = strlen(pdata);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1512 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1513 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1514 if (idx < 256) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1515 pdata[0] = BINPUT;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1516 pdata[1] = (unsigned char)idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1517 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1518 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1519 else if (idx <= 0xffffffffL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1520 pdata[0] = LONG_BINPUT;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1521 pdata[1] = (unsigned char)(idx & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1522 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1523 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1524 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1525 len = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1526 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1527 else { /* unlikely */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1528 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1529 PyErr_SetString(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1530 "memo id too large for LONG_BINPUT");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1531 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1532 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1533 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1534 if (_Pickler_Write(self, pdata, len) < 0)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1535 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1536
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1537 return 0;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1538 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1539
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1540 static PyObject *
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1541 getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1542 PyObject *dotted_path;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1543 Py_ssize_t i;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1544 _Py_static_string(PyId_dot, ".");
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1545 _Py_static_string(PyId_locals, "<locals>");
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1546
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1547 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1548 if (dotted_path == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1549 return NULL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1550 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1551 assert(Py_SIZE(dotted_path) >= 1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1552 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1553 PyErr_Format(PyExc_AttributeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1554 "Can't get qualified attribute %R on %R;"
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1555 "use protocols >= 4 to enable support",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1556 name, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1557 Py_DECREF(dotted_path);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1558 return NULL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1559 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1560 Py_INCREF(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1561 for (i = 0; i < Py_SIZE(dotted_path); i++) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1562 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1563 PyObject *tmp;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1564 PyObject *result = PyUnicode_RichCompare(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1565 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1566 int is_equal = (result == Py_True);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1567 assert(PyBool_Check(result));
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1568 Py_DECREF(result);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1569 if (is_equal) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1570 PyErr_Format(PyExc_AttributeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1571 "Can't get local attribute %R on %R", name, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1572 Py_DECREF(dotted_path);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1573 Py_DECREF(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1574 return NULL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1575 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1576 tmp = PyObject_GetAttr(obj, subpath);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1577 Py_DECREF(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1578 if (tmp == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1579 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1580 PyErr_Clear();
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1581 PyErr_Format(PyExc_AttributeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1582 "Can't get attribute %R on %R", name, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1583 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1584 Py_DECREF(dotted_path);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1585 return NULL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1586 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1587 obj = tmp;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1588 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1589 Py_DECREF(dotted_path);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1590 return obj;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1591 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1592
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1593 static PyObject *
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1594 whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1595 {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1596 PyObject *module_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1597 PyObject *modules_dict;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1598 PyObject *module;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1599 PyObject *obj;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1600 Py_ssize_t i, j;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1601 _Py_IDENTIFIER(__module__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1602 _Py_IDENTIFIER(modules);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1603 _Py_IDENTIFIER(__main__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1604
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1605 module_name = _PyObject_GetAttrId(global, &PyId___module__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1606
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1607 if (module_name == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1608 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1609 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1610 PyErr_Clear();
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1611 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1612 else {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1613 /* In some rare cases (e.g., bound methods of extension types),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1614 __module__ can be None. If it is so, then search sys.modules for
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1615 the module of global. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1616 if (module_name != Py_None)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1617 return module_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1618 Py_CLEAR(module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1619 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1620 assert(module_name == NULL);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1621
86973
6348764bacdd Issue #19512: pickle now uses an identifier to only create the Unicode string
Victor Stinner <victor.stinner@gmail.com>
parents: 86797
diff changeset
1622 modules_dict = _PySys_GetObjectId(&PyId_modules);
84676
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
1623 if (modules_dict == NULL) {
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
1624 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1625 return NULL;
84676
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
1626 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1627
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1628 i = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1629 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1630 PyObject *result = PyUnicode_RichCompare(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1631 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1632 int is_equal = (result == Py_True);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1633 assert(PyBool_Check(result));
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1634 Py_DECREF(result);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1635 if (is_equal)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1636 continue;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1637 if (module == Py_None)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1638 continue;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1639
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1640 obj = getattribute(module, global_name, allow_qualname);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1641 if (obj == NULL) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1642 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1643 return NULL;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1644 PyErr_Clear();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1645 continue;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1646 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1647
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1648 if (obj == global) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1649 Py_DECREF(obj);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1650 Py_INCREF(module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1651 return module_name;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1652 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1653 Py_DECREF(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1654 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1655
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1656 /* If no module is found, use __main__. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1657 module_name = _PyUnicode_FromId(&PyId___main__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1658 Py_INCREF(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1659 return module_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1660 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1661
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1662 /* fast_save_enter() and fast_save_leave() are guards against recursive
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1663 objects when Pickler is used with the "fast mode" (i.e., with object
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1664 memoization disabled). If the nesting of a list or dict object exceed
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1665 FAST_NESTING_LIMIT, these guards will start keeping an internal
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1666 reference to the seen list or dict objects and check whether these objects
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1667 are recursive. These are not strictly necessary, since save() has a
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1668 hard-coded recursion limit, but they give a nicer error message than the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1669 typical RuntimeError. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1670 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1671 fast_save_enter(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1672 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1673 /* if fast_nesting < 0, we're doing an error exit. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1674 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1675 PyObject *key = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1676 if (self->fast_memo == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1677 self->fast_memo = PyDict_New();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1678 if (self->fast_memo == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1679 self->fast_nesting = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1680 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1681 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1682 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1683 key = PyLong_FromVoidPtr(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1684 if (key == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1685 return 0;
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
1686 if (PyDict_GetItemWithError(self->fast_memo, key)) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1687 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1688 PyErr_Format(PyExc_ValueError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1689 "fast mode: can't pickle cyclic objects "
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1690 "including object type %.200s at %p",
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1691 obj->ob_type->tp_name, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1692 self->fast_nesting = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1693 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1694 }
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
1695 if (PyErr_Occurred()) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
1696 return 0;
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
1697 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1698 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1699 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1700 self->fast_nesting = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1701 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1702 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1703 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1704 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1705 return 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1706 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1707
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1708 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1709 fast_save_leave(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1710 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1711 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1712 PyObject *key = PyLong_FromVoidPtr(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1713 if (key == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1714 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1715 if (PyDict_DelItem(self->fast_memo, key) < 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1716 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1717 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1718 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1719 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1720 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1721 return 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1722 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1723
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1724 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1725 save_none(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1726 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1727 const char none_op = NONE;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1728 if (_Pickler_Write(self, &none_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1729 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1730
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1731 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1732 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1733
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1734 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1735 save_bool(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1736 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1737 if (self->proto >= 2) {
87538
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1738 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1739 if (_Pickler_Write(self, &bool_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1740 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1741 }
87538
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1742 else {
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1743 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1744 * so that unpicklers written before bools were introduced unpickle them
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1745 * as ints, but unpicklers after can recognize that bools were intended.
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1746 * Note that protocol 2 added direct ways to pickle bools.
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1747 */
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1748 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1749 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1750 return -1;
f27d6721f446 Simplify save_bool in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87519
diff changeset
1751 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1752 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1753 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1754
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1755 static int
87540
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1756 save_long(PicklerObject *self, PyObject *obj)
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1757 {
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1758 PyObject *repr = NULL;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1759 Py_ssize_t size;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1760 long val;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1761 int status = 0;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1762
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1763 const char long_op = LONG;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1764
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1765 val= PyLong_AsLong(obj);
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1766 if (val == -1 && PyErr_Occurred()) {
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1767 /* out of range for int pickling */
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1768 PyErr_Clear();
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1769 }
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1770 else if (self->bin &&
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1771 (sizeof(long) <= 4 ||
87560
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1772 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1773 /* result fits in a signed 4-byte integer.
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1774
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1775 Note: we can't use -0x80000000L in the above condition because some
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1776 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1777 before applying the unary minus when sizeof(long) <= 4. The
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1778 resulting value stays unsigned which is commonly not what we want,
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1779 so MSVC happily warns us about it. However, that result would have
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1780 been fine because we guard for sizeof(long) <= 4 which turns the
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
1781 condition true in that particular case. */
87540
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1782 char pdata[32];
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1783 Py_ssize_t len = 0;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1784
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1785 pdata[1] = (unsigned char)(val & 0xff);
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1786 pdata[2] = (unsigned char)((val >> 8) & 0xff);
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1787 pdata[3] = (unsigned char)((val >> 16) & 0xff);
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1788 pdata[4] = (unsigned char)((val >> 24) & 0xff);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1789
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1790 if ((pdata[4] == 0) && (pdata[3] == 0)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1791 if (pdata[2] == 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1792 pdata[0] = BININT1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1793 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1794 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1795 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1796 pdata[0] = BININT2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1797 len = 3;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1798 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1799 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1800 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1801 pdata[0] = BININT;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1802 len = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1803 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1804
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1805 if (_Pickler_Write(self, pdata, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1806 return -1;
87540
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1807
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1808 return 0;
14f2776686b3 Merge save_int into save_long in cpickle to remove redundant code.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87538
diff changeset
1809 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1810
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1811 if (self->proto >= 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1812 /* Linear-time pickling. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1813 size_t nbits;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1814 size_t nbytes;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1815 unsigned char *pdata;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1816 char header[5];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1817 int i;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1818 int sign = _PyLong_Sign(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1819
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1820 if (sign == 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1821 header[0] = LONG1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1822 header[1] = 0; /* It's 0 -- an empty bytestring. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1823 if (_Pickler_Write(self, header, 2) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1824 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1825 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1826 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1827 nbits = _PyLong_NumBits(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1828 if (nbits == (size_t)-1 && PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1829 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1830 /* How many bytes do we need? There are nbits >> 3 full
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1831 * bytes of data, and nbits & 7 leftover bits. If there
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1832 * are any leftover bits, then we clearly need another
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1833 * byte. Wnat's not so obvious is that we *probably*
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1834 * need another byte even if there aren't any leftovers:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1835 * the most-significant bit of the most-significant byte
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1836 * acts like a sign bit, and it's usually got a sense
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
1837 * opposite of the one we need. The exception is ints
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
1838 * of the form -(2**(8*j-1)) for j > 0. Such an int is
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1839 * its own 256's-complement, so has the right sign bit
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1840 * even without the extra byte. That's a pain to check
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1841 * for in advance, though, so we always grab an extra
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1842 * byte at the start, and cut it back later if possible.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1843 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1844 nbytes = (nbits >> 3) + 1;
80585
55fe4b57dd9c Issue #12848: The pure Python pickle implementation now treats object lengths as unsigned 32-bit integers, like the C implementation does.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73957
diff changeset
1845 if (nbytes > 0x7fffffffL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1846 PyErr_SetString(PyExc_OverflowError,
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
1847 "int too large to pickle");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1848 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1849 }
48862
678a0f0e61bc Issue #3657: Fix uninitialized memory read when pickling longs.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48696
diff changeset
1850 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1851 if (repr == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1852 goto error;
48862
678a0f0e61bc Issue #3657: Fix uninitialized memory read when pickling longs.
Neal Norwitz <nnorwitz@gmail.com>
parents: 48696
diff changeset
1853 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1854 i = _PyLong_AsByteArray((PyLongObject *)obj,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1855 pdata, nbytes,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1856 1 /* little endian */ , 1 /* signed */ );
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1857 if (i < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1858 goto error;
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
1859 /* If the int is negative, this may be a byte more than
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1860 * needed. This is so iff the MSB is all redundant sign
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1861 * bits.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1862 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1863 if (sign < 0 &&
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
1864 nbytes > 1 &&
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1865 pdata[nbytes - 1] == 0xff &&
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1866 (pdata[nbytes - 2] & 0x80) != 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1867 nbytes--;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1868 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1869
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1870 if (nbytes < 256) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1871 header[0] = LONG1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1872 header[1] = (unsigned char)nbytes;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1873 size = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1874 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1875 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1876 header[0] = LONG4;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
1877 size = (Py_ssize_t) nbytes;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1878 for (i = 1; i < 5; i++) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1879 header[i] = (unsigned char)(size & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1880 size >>= 8;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1881 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1882 size = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1883 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1884 if (_Pickler_Write(self, header, size) < 0 ||
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1885 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1886 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1887 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1888 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1889 char *string;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1890
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
1891 /* proto < 2: write the repr and newline. This is quadratic-time (in
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
1892 the number of digits), in both directions. We add a trailing 'L'
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
1893 to the repr, for compatibility with Python 2.x. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1894
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1895 repr = PyObject_Repr(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1896 if (repr == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1897 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1898
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48462
diff changeset
1899 string = _PyUnicode_AsStringAndSize(repr, &size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1900 if (string == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1901 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1902
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1903 if (_Pickler_Write(self, &long_op, 1) < 0 ||
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1904 _Pickler_Write(self, string, size) < 0 ||
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1905 _Pickler_Write(self, "L\n", 2) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1906 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1907 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1908
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1909 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1910 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1911 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1912 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1913 Py_XDECREF(repr);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1914
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1915 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1916 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1917
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1918 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1919 save_float(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1920 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1921 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1922
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1923 if (self->bin) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1924 char pdata[9];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1925 pdata[0] = BINFLOAT;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1926 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1927 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1928 if (_Pickler_Write(self, pdata, 9) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1929 return -1;
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
1930 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1931 else {
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1932 int result = -1;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1933 char *buf = NULL;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1934 char op = FLOAT;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1935
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1936 if (_Pickler_Write(self, &op, 1) < 0)
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1937 goto done;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1938
53192
973f4d7aa87c Make sure that marshal and pickle continue to output 17
Mark Dickinson <dickinsm@gmail.com>
parents: 53187
diff changeset
1939 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1940 if (!buf) {
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1941 PyErr_NoMemory();
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1942 goto done;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1943 }
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1944
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1945 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1946 goto done;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1947
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
1948 if (_Pickler_Write(self, "\n", 1) < 0)
53187
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1949 goto done;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1950
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1951 result = 0;
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1952 done:
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1953 PyMem_Free(buf);
b653eab13b73 The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents: 52761
diff changeset
1954 return result;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1955 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1956
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1957 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1958 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1959
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1960 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1961 save_bytes(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1962 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1963 if (self->proto < 3) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1964 /* Older pickle protocols do not have an opcode for pickling bytes
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1965 objects. Therefore, we need to fake the copy protocol (i.e.,
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1966 the __reduce__ method) to permit bytes object unpickling.
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1967
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1968 Here we use a hack to be compatible with Python 2. Since in Python
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1969 2 'bytes' is just an alias for 'str' (which has different
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1970 parameters than the actual bytes object), we use codecs.encode
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1971 to create the appropriate 'str' object when unpickled using
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1972 Python 2 *and* the appropriate 'bytes' object when unpickled
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1973 using Python 3. Again this is a hack and we don't need to do this
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1974 with newer protocols. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1975 PyObject *reduce_value = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1976 int status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1977
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1978 if (PyBytes_GET_SIZE(obj) == 0) {
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1979 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1980 }
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1981 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1982 PickleState *st = _Pickle_GetGlobalState();
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1983 PyObject *unicode_str =
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1984 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1985 PyBytes_GET_SIZE(obj),
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1986 "strict");
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1987 _Py_IDENTIFIER(latin1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1988
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1989 if (unicode_str == NULL)
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1990 return -1;
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1991 reduce_value = Py_BuildValue("(O(OO))",
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
1992 st->codecs_encode, unicode_str,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
1993 _PyUnicode_FromId(&PyId_latin1));
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1994 Py_DECREF(unicode_str);
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1995 }
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1996
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
1997 if (reduce_value == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1998 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
1999
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2000 /* save_reduce() will memoize the object automatically. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2001 status = save_reduce(self, reduce_value, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2002 Py_DECREF(reduce_value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2003 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2004 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2005 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2006 Py_ssize_t size;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2007 char header[9];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2008 Py_ssize_t len;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2009
73957
14695b4825dc Issue #13505: Make pickling of bytes object compatible with Python 2.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 72639
diff changeset
2010 size = PyBytes_GET_SIZE(obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2011 if (size < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2012 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2013
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2014 if (size <= 0xff) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2015 header[0] = SHORT_BINBYTES;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2016 header[1] = (unsigned char)size;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2017 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2018 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2019 else if (size <= 0xffffffffL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2020 header[0] = BINBYTES;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2021 header[1] = (unsigned char)(size & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2022 header[2] = (unsigned char)((size >> 8) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2023 header[3] = (unsigned char)((size >> 16) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2024 header[4] = (unsigned char)((size >> 24) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2025 len = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2026 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2027 else if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2028 header[0] = BINBYTES8;
87560
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
2029 _write_size64(header + 1, size);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2030 len = 8;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2031 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2032 else {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2033 PyErr_SetString(PyExc_OverflowError,
82223
c1f846a99c85 Issue #17193: Use binary prefixes (KiB, MiB, GiB) for memory units.
Serhiy Storchaka <storchaka@gmail.com>
parents: 80586
diff changeset
2034 "cannot serialize a bytes object larger than 4 GiB");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2035 return -1; /* string too large */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2036 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2037
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2038 if (_Pickler_Write(self, header, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2039 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2040
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2041 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2042 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2043
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2044 if (memo_put(self, obj) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2045 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2046
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2047 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2048 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2049 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2050
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2051 /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2052 backslash and newline characters to \uXXXX escapes. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2053 static PyObject *
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2054 raw_unicode_escape(PyObject *obj)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2055 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2056 PyObject *repr, *result;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2057 char *p;
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2058 Py_ssize_t i, size, expandsize;
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2059 void *data;
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2060 unsigned int kind;
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2061
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2062 if (PyUnicode_READY(obj))
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2063 return NULL;
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2064
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2065 size = PyUnicode_GET_LENGTH(obj);
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2066 data = PyUnicode_DATA(obj);
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2067 kind = PyUnicode_KIND(obj);
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2068 if (kind == PyUnicode_4BYTE_KIND)
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2069 expandsize = 10;
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2070 else
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2071 expandsize = 6;
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
2072
50544
c9e5c1fa285e Merged revisions 67937-67938 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49849
diff changeset
2073 if (size > PY_SSIZE_T_MAX / expandsize)
c9e5c1fa285e Merged revisions 67937-67938 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49849
diff changeset
2074 return PyErr_NoMemory();
c9e5c1fa285e Merged revisions 67937-67938 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49849
diff changeset
2075 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2076 if (repr == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2077 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2078 if (size == 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2079 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2080
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2081 p = PyByteArray_AS_STRING(repr);
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2082 for (i=0; i < size; i++) {
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2083 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2084 /* Map 32-bit characters to '\Uxxxxxxxx' */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2085 if (ch >= 0x10000) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2086 *p++ = '\\';
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2087 *p++ = 'U';
72918
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2088 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2089 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2090 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2091 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2092 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2093 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2094 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2095 *p++ = Py_hexdigits[ch & 15];
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2096 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2097 /* Map 16-bit characters to '\uxxxx' */
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2098 else if (ch >= 256 || ch == '\\' || ch == '\n') {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2099 *p++ = '\\';
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2100 *p++ = 'u';
72918
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2101 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2102 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2103 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
d76338eacf7c Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72848
diff changeset
2104 *p++ = Py_hexdigits[ch & 15];
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2105 }
50544
c9e5c1fa285e Merged revisions 67937-67938 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49849
diff changeset
2106 /* Copy everything else as-is */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2107 else
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2108 *p++ = (char) ch;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2109 }
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2110 size = p - PyByteArray_AS_STRING(repr);
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2111
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2112 done:
50544
c9e5c1fa285e Merged revisions 67937-67938 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49849
diff changeset
2113 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2114 Py_DECREF(repr);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2115 return result;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2116 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2117
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2118 static int
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2119 write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2120 {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2121 char header[9];
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2122 Py_ssize_t len;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2123
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2124 if (size <= 0xff && self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2125 header[0] = SHORT_BINUNICODE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2126 header[1] = (unsigned char)(size & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2127 len = 2;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2128 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2129 else if (size <= 0xffffffffUL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2130 header[0] = BINUNICODE;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2131 header[1] = (unsigned char)(size & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2132 header[2] = (unsigned char)((size >> 8) & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2133 header[3] = (unsigned char)((size >> 16) & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2134 header[4] = (unsigned char)((size >> 24) & 0xff);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2135 len = 5;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2136 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2137 else if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2138 header[0] = BINUNICODE8;
87560
f8ac01a762c1 Issue #19739: Try to fix compiler warnings on 32-bit Windows.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87540
diff changeset
2139 _write_size64(header + 1, size);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2140 len = 9;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2141 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2142 else {
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2143 PyErr_SetString(PyExc_OverflowError,
83191
0437c19cdad0 gibibytes (Arfrever)
Antoine Pitrou <solipsis@pitrou.net>
parents: 83184
diff changeset
2144 "cannot serialize a string larger than 4GiB");
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2145 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2146 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2147
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2148 if (_Pickler_Write(self, header, len) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2149 return -1;
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2150 if (_Pickler_Write(self, data, size) < 0)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2151 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2152
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2153 return 0;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2154 }
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2155
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2156 static int
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2157 write_unicode_binary(PicklerObject *self, PyObject *obj)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2158 {
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2159 PyObject *encoded = NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2160 Py_ssize_t size;
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2161 char *data;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2162 int r;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2163
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2164 if (PyUnicode_READY(obj))
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2165 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2166
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2167 data = PyUnicode_AsUTF8AndSize(obj, &size);
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2168 if (data != NULL)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2169 return write_utf8(self, data, size);
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2170
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2171 /* Issue #8383: for strings with lone surrogates, fallback on the
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2172 "surrogatepass" error handler. */
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2173 PyErr_Clear();
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2174 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2175 if (encoded == NULL)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2176 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2177
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2178 r = write_utf8(self, PyBytes_AS_STRING(encoded),
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2179 PyBytes_GET_SIZE(encoded));
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2180 Py_DECREF(encoded);
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2181 return r;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2182 }
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2183
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2184 static int
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2185 save_unicode(PicklerObject *self, PyObject *obj)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2186 {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2187 if (self->bin) {
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2188 if (write_unicode_binary(self, obj) < 0)
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2189 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2190 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2191 else {
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2192 PyObject *encoded;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2193 Py_ssize_t size;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2194 const char unicode_op = UNICODE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2195
72539
b9558df8cc58 raw_unicode_escape() uses the new Unicode API
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72538
diff changeset
2196 encoded = raw_unicode_escape(obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2197 if (encoded == NULL)
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2198 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2199
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2200 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2201 Py_DECREF(encoded);
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2202 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2203 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2204
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2205 size = PyBytes_GET_SIZE(encoded);
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2206 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2207 Py_DECREF(encoded);
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2208 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2209 }
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2210 Py_DECREF(encoded);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2211
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2212 if (_Pickler_Write(self, "\n", 1) < 0)
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2213 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2214 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2215 if (memo_put(self, obj) < 0)
83184
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2216 return -1;
09a84091ae96 Issue #15596: Faster pickling of unicode strings.
Antoine Pitrou <solipsis@pitrou.net>
parents: 82223
diff changeset
2217
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2218 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2219 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2220
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2221 /* A helper for save_tuple. Push the len elements in tuple t on the stack. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2222 static int
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2223 store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2224 {
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2225 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2226
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2227 assert(PyTuple_Size(t) == len);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2228
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2229 for (i = 0; i < len; i++) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2230 PyObject *element = PyTuple_GET_ITEM(t, i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2231
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2232 if (element == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2233 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2234 if (save(self, element, 0) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2235 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2236 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2237
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2238 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2239 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2240
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2241 /* Tuples are ubiquitous in the pickle protocols, so many techniques are
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2242 * used across protocols to minimize the space needed to pickle them.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2243 * Tuples are also the only builtin immutable type that can be recursive
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2244 * (a tuple can be reached from itself), and that requires some subtle
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2245 * magic so that it works in all cases. IOW, this is a long routine.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2246 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2247 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2248 save_tuple(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2249 {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2250 Py_ssize_t len, i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2251
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2252 const char mark_op = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2253 const char tuple_op = TUPLE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2254 const char pop_op = POP;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2255 const char pop_mark_op = POP_MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2256 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2257
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2258 if ((len = PyTuple_Size(obj)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2259 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2260
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2261 if (len == 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2262 char pdata[2];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2263
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2264 if (self->proto) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2265 pdata[0] = EMPTY_TUPLE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2266 len = 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2267 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2268 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2269 pdata[0] = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2270 pdata[1] = TUPLE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2271 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2272 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2273 if (_Pickler_Write(self, pdata, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2274 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2275 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2276 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2277
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2278 /* The tuple isn't in the memo now. If it shows up there after
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2279 * saving the tuple elements, the tuple must be recursive, in
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2280 * which case we'll pop everything we put on the stack, and fetch
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2281 * its value from the memo.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2282 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2283 if (len <= 3 && self->proto >= 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2284 /* Use TUPLE{1,2,3} opcodes. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2285 if (store_tuple_elements(self, obj, len) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2286 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2287
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2288 if (PyMemoTable_Get(self->memo, obj)) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2289 /* pop the len elements */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2290 for (i = 0; i < len; i++)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2291 if (_Pickler_Write(self, &pop_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2292 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2293 /* fetch from memo */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2294 if (memo_get(self, obj) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2295 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2296
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2297 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2298 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2299 else { /* Not recursive. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2300 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2301 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2302 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2303 goto memoize;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2304 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2305
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2306 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2307 * Generate MARK e1 e2 ... TUPLE
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2308 */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2309 if (_Pickler_Write(self, &mark_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2310 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2311
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2312 if (store_tuple_elements(self, obj, len) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2313 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2314
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2315 if (PyMemoTable_Get(self->memo, obj)) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2316 /* pop the stack stuff we pushed */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2317 if (self->bin) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2318 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2319 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2320 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2321 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2322 /* Note that we pop one more than len, to remove
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2323 * the MARK too.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2324 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2325 for (i = 0; i <= len; i++)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2326 if (_Pickler_Write(self, &pop_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2327 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2328 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2329 /* fetch from memo */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2330 if (memo_get(self, obj) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2331 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2332
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2333 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2334 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2335 else { /* Not recursive. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2336 if (_Pickler_Write(self, &tuple_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2337 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2338 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2339
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2340 memoize:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2341 if (memo_put(self, obj) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2342 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2343
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2344 return 0;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2345 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2346
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2347 /* iter is an iterator giving items, and we batch up chunks of
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2348 * MARK item item ... item APPENDS
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2349 * opcode sequences. Calling code should have arranged to first create an
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2350 * empty list, or list-like object, for the APPENDS to operate on.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2351 * Returns 0 on success, <0 on error.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2352 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2353 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2354 batch_list(PicklerObject *self, PyObject *iter)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2355 {
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2356 PyObject *obj = NULL;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2357 PyObject *firstitem = NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2358 int i, n;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2359
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2360 const char mark_op = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2361 const char append_op = APPEND;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2362 const char appends_op = APPENDS;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2363
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2364 assert(iter != NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2365
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2366 /* XXX: I think this function could be made faster by avoiding the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2367 iterator interface and fetching objects directly from list using
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2368 PyList_GET_ITEM.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2369 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2370
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2371 if (self->proto == 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2372 /* APPENDS isn't available; do one at a time. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2373 for (;;) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2374 obj = PyIter_Next(iter);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2375 if (obj == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2376 if (PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2377 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2378 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2379 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2380 i = save(self, obj, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2381 Py_DECREF(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2382 if (i < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2383 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2384 if (_Pickler_Write(self, &append_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2385 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2386 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2387 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2388 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2389
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2390 /* proto > 0: write in batches of BATCHSIZE. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2391 do {
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2392 /* Get first item */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2393 firstitem = PyIter_Next(iter);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2394 if (firstitem == NULL) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2395 if (PyErr_Occurred())
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2396 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2397
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2398 /* nothing more to add */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2399 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2400 }
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2401
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2402 /* Try to get a second item */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2403 obj = PyIter_Next(iter);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2404 if (obj == NULL) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2405 if (PyErr_Occurred())
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2406 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2407
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2408 /* Only one item to write */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2409 if (save(self, firstitem, 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2410 goto error;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2411 if (_Pickler_Write(self, &append_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2412 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2413 Py_CLEAR(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2414 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2415 }
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2416
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2417 /* More than one item to write */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2418
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2419 /* Pump out MARK, items, APPENDS. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2420 if (_Pickler_Write(self, &mark_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2421 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2422
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2423 if (save(self, firstitem, 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2424 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2425 Py_CLEAR(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2426 n = 1;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2427
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2428 /* Fetch and save up to BATCHSIZE items */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2429 while (obj) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2430 if (save(self, obj, 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2431 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2432 Py_CLEAR(obj);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2433 n += 1;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2434
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2435 if (n == BATCHSIZE)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2436 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2437
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2438 obj = PyIter_Next(iter);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2439 if (obj == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2440 if (PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2441 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2442 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2443 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2444 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2445
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2446 if (_Pickler_Write(self, &appends_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2447 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2448
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2449 } while (n == BATCHSIZE);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2450 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2451
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2452 error:
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2453 Py_XDECREF(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2454 Py_XDECREF(obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2455 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2456 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2457
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2458 /* This is a variant of batch_list() above, specialized for lists (with no
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2459 * support for list subclasses). Like batch_list(), we batch up chunks of
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2460 * MARK item item ... item APPENDS
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2461 * opcode sequences. Calling code should have arranged to first create an
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2462 * empty list, or list-like object, for the APPENDS to operate on.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2463 * Returns 0 on success, -1 on error.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2464 *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2465 * This version is considerably faster than batch_list(), if less general.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2466 *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2467 * Note that this only works for protocols > 0.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2468 */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2469 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2470 batch_list_exact(PicklerObject *self, PyObject *obj)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2471 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2472 PyObject *item = NULL;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2473 Py_ssize_t this_batch, total;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2474
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2475 const char append_op = APPEND;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2476 const char appends_op = APPENDS;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2477 const char mark_op = MARK;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2478
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2479 assert(obj != NULL);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2480 assert(self->proto > 0);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2481 assert(PyList_CheckExact(obj));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2482
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2483 if (PyList_GET_SIZE(obj) == 1) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2484 item = PyList_GET_ITEM(obj, 0);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2485 if (save(self, item, 0) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2486 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2487 if (_Pickler_Write(self, &append_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2488 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2489 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2490 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2491
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2492 /* Write in batches of BATCHSIZE. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2493 total = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2494 do {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2495 this_batch = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2496 if (_Pickler_Write(self, &mark_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2497 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2498 while (total < PyList_GET_SIZE(obj)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2499 item = PyList_GET_ITEM(obj, total);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2500 if (save(self, item, 0) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2501 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2502 total++;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2503 if (++this_batch == BATCHSIZE)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2504 break;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2505 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2506 if (_Pickler_Write(self, &appends_op, 1) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2507 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2508
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2509 } while (total < PyList_GET_SIZE(obj));
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2510
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2511 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2512 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2513
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2514 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2515 save_list(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2516 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2517 char header[3];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2518 Py_ssize_t len;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2519 int status = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2520
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2521 if (self->fast && !fast_save_enter(self, obj))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2522 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2523
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2524 /* Create an empty list. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2525 if (self->bin) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2526 header[0] = EMPTY_LIST;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2527 len = 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2528 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2529 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2530 header[0] = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2531 header[1] = LIST;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2532 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2533 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2534
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2535 if (_Pickler_Write(self, header, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2536 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2537
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2538 /* Get list length, and bow out early if empty. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2539 if ((len = PyList_Size(obj)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2540 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2541
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2542 if (memo_put(self, obj) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2543 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2544
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2545 if (len != 0) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2546 /* Materialize the list elements. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2547 if (PyList_CheckExact(obj) && self->proto > 0) {
67724
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2548 if (Py_EnterRecursiveCall(" while pickling an object"))
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2549 goto error;
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2550 status = batch_list_exact(self, obj);
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2551 Py_LeaveRecursiveCall();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2552 } else {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2553 PyObject *iter = PyObject_GetIter(obj);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2554 if (iter == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2555 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2556
67724
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2557 if (Py_EnterRecursiveCall(" while pickling an object")) {
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2558 Py_DECREF(iter);
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2559 goto error;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2560 }
67724
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2561 status = batch_list(self, iter);
4c4abfb884fb Issue #10987: Fix the recursion limit handling in the _pickle module.
Antoine Pitrou <solipsis@pitrou.net>
parents: 66106
diff changeset
2562 Py_LeaveRecursiveCall();
67725
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2563 Py_DECREF(iter);
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2564 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2565 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2566 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2567 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2568 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2569 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2570
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2571 if (self->fast && !fast_save_leave(self, obj))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2572 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2573
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2574 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2575 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2576
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2577 /* iter is an iterator giving (key, value) pairs, and we batch up chunks of
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2578 * MARK key value ... key value SETITEMS
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2579 * opcode sequences. Calling code should have arranged to first create an
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2580 * empty dict, or dict-like object, for the SETITEMS to operate on.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2581 * Returns 0 on success, <0 on error.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2582 *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2583 * This is very much like batch_list(). The difference between saving
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2584 * elements directly, and picking apart two-tuples, is so long-winded at
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2585 * the C level, though, that attempts to combine these routines were too
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2586 * ugly to bear.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2587 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2588 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2589 batch_dict(PicklerObject *self, PyObject *iter)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2590 {
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2591 PyObject *obj = NULL;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2592 PyObject *firstitem = NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2593 int i, n;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2594
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2595 const char mark_op = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2596 const char setitem_op = SETITEM;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2597 const char setitems_op = SETITEMS;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2598
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2599 assert(iter != NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2600
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2601 if (self->proto == 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2602 /* SETITEMS isn't available; do one at a time. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2603 for (;;) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2604 obj = PyIter_Next(iter);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2605 if (obj == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2606 if (PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2607 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2608 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2609 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2610 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2611 PyErr_SetString(PyExc_TypeError, "dict items "
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2612 "iterator must return 2-tuples");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2613 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2614 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2615 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2616 if (i >= 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2617 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2618 Py_DECREF(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2619 if (i < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2620 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2621 if (_Pickler_Write(self, &setitem_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2622 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2623 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2624 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2625 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2626
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2627 /* proto > 0: write in batches of BATCHSIZE. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2628 do {
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2629 /* Get first item */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2630 firstitem = PyIter_Next(iter);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2631 if (firstitem == NULL) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2632 if (PyErr_Occurred())
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2633 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2634
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2635 /* nothing more to add */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2636 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2637 }
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2638 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2639 PyErr_SetString(PyExc_TypeError, "dict items "
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2640 "iterator must return 2-tuples");
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2641 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2642 }
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2643
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2644 /* Try to get a second item */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2645 obj = PyIter_Next(iter);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2646 if (obj == NULL) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2647 if (PyErr_Occurred())
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2648 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2649
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2650 /* Only one item to write */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2651 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2652 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2653 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2654 goto error;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2655 if (_Pickler_Write(self, &setitem_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2656 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2657 Py_CLEAR(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2658 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2659 }
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2660
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2661 /* More than one item to write */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2662
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2663 /* Pump out MARK, items, SETITEMS. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2664 if (_Pickler_Write(self, &mark_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2665 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2666
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2667 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2668 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2669 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2670 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2671 Py_CLEAR(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2672 n = 1;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2673
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2674 /* Fetch and save up to BATCHSIZE items */
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2675 while (obj) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2676 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2677 PyErr_SetString(PyExc_TypeError, "dict items "
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2678 "iterator must return 2-tuples");
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2679 goto error;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2680 }
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2681 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2682 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2683 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2684 Py_CLEAR(obj);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2685 n += 1;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2686
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2687 if (n == BATCHSIZE)
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2688 break;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2689
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2690 obj = PyIter_Next(iter);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2691 if (obj == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2692 if (PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2693 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2694 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2695 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2696 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2697
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2698 if (_Pickler_Write(self, &setitems_op, 1) < 0)
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2699 goto error;
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2700
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2701 } while (n == BATCHSIZE);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2702 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2703
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2704 error:
49149
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2705 Py_XDECREF(firstitem);
4cd97f8de69e #3640: Correct a crash in cPickle on 64bit platforms, in the case of deeply nested lists or dicts.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49023
diff changeset
2706 Py_XDECREF(obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2707 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2708 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2709
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2710 /* This is a variant of batch_dict() above that specializes for dicts, with no
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2711 * support for dict subclasses. Like batch_dict(), we batch up chunks of
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2712 * MARK key value ... key value SETITEMS
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2713 * opcode sequences. Calling code should have arranged to first create an
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2714 * empty dict, or dict-like object, for the SETITEMS to operate on.
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2715 * Returns 0 on success, -1 on error.
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2716 *
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2717 * Note that this currently doesn't work for protocol 0.
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2718 */
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2719 static int
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2720 batch_dict_exact(PicklerObject *self, PyObject *obj)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2721 {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2722 PyObject *key = NULL, *value = NULL;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2723 int i;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2724 Py_ssize_t dict_size, ppos = 0;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2725
54331
39d3a75fa821 Make variable declarations for opcodes in batch_dict_exact consistent
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54328
diff changeset
2726 const char mark_op = MARK;
39d3a75fa821 Make variable declarations for opcodes in batch_dict_exact consistent
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54328
diff changeset
2727 const char setitem_op = SETITEM;
39d3a75fa821 Make variable declarations for opcodes in batch_dict_exact consistent
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 54328
diff changeset
2728 const char setitems_op = SETITEMS;
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2729
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2730 assert(obj != NULL);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2731 assert(self->proto > 0);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2732
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2733 dict_size = PyDict_Size(obj);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2734
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2735 /* Special-case len(d) == 1 to save space. */
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2736 if (dict_size == 1) {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2737 PyDict_Next(obj, &ppos, &key, &value);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2738 if (save(self, key, 0) < 0)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2739 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2740 if (save(self, value, 0) < 0)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2741 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2742 if (_Pickler_Write(self, &setitem_op, 1) < 0)
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2743 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2744 return 0;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2745 }
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2746
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2747 /* Write in batches of BATCHSIZE. */
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2748 do {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2749 i = 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2750 if (_Pickler_Write(self, &mark_op, 1) < 0)
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2751 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2752 while (PyDict_Next(obj, &ppos, &key, &value)) {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2753 if (save(self, key, 0) < 0)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2754 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2755 if (save(self, value, 0) < 0)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2756 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2757 if (++i == BATCHSIZE)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2758 break;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2759 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2761 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2762 if (PyDict_Size(obj) != dict_size) {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2763 PyErr_Format(
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2764 PyExc_RuntimeError,
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2765 "dictionary changed size during iteration");
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2766 return -1;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2767 }
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2768
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2769 } while (i == BATCHSIZE);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2770 return 0;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2771 }
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2772
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2773 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2774 save_dict(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2775 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2776 PyObject *items, *iter;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2777 char header[3];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
2778 Py_ssize_t len;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2779 int status = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2780
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2781 if (self->fast && !fast_save_enter(self, obj))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2782 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2783
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2784 /* Create an empty dict. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2785 if (self->bin) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2786 header[0] = EMPTY_DICT;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2787 len = 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2788 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2789 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2790 header[0] = MARK;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2791 header[1] = DICT;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2792 len = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2793 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2794
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2795 if (_Pickler_Write(self, header, len) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2796 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2797
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2798 /* Get dict size, and bow out early if empty. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2799 if ((len = PyDict_Size(obj)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2800 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2801
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2802 if (memo_put(self, obj) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2803 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2804
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2805 if (len != 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2806 /* Save the dict items. */
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2807 if (PyDict_CheckExact(obj) && self->proto > 0) {
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2808 /* We can take certain shortcuts if we know this is a dict and
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2809 not a dict subclass. */
67725
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2810 if (Py_EnterRecursiveCall(" while pickling an object"))
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2811 goto error;
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2812 status = batch_dict_exact(self, obj);
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2813 Py_LeaveRecursiveCall();
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2814 } else {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
2815 _Py_IDENTIFIER(items);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
2816
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
2817 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2818 if (items == NULL)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2819 goto error;
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2820 iter = PyObject_GetIter(items);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2821 Py_DECREF(items);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2822 if (iter == NULL)
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2823 goto error;
67725
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2824 if (Py_EnterRecursiveCall(" while pickling an object")) {
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2825 Py_DECREF(iter);
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2826 goto error;
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2827 }
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2828 status = batch_dict(self, iter);
67725
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
2829 Py_LeaveRecursiveCall();
54328
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2830 Py_DECREF(iter);
5ab2f6ca65aa Merged revisions 72909 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 53707
diff changeset
2831 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2832 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2833
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2834 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2835 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2836 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2837 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2838
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2839 if (self->fast && !fast_save_leave(self, obj))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2840 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2841
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2842 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2843 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2844
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
2845 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2846 save_set(PicklerObject *self, PyObject *obj)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2847 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2848 PyObject *item;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2849 int i;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2850 Py_ssize_t set_size, ppos = 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2851 Py_hash_t hash;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2852
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2853 const char empty_set_op = EMPTY_SET;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2854 const char mark_op = MARK;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2855 const char additems_op = ADDITEMS;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2856
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2857 if (self->proto < 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2858 PyObject *items;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2859 PyObject *reduce_value;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2860 int status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2861
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2862 items = PySequence_List(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2863 if (items == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2864 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2865 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2866 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2867 Py_DECREF(items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2868 if (reduce_value == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2869 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2870 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2871 /* save_reduce() will memoize the object automatically. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2872 status = save_reduce(self, reduce_value, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2873 Py_DECREF(reduce_value);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2874 return status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2875 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2876
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2877 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2878 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2879
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2880 if (memo_put(self, obj) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2881 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2882
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2883 set_size = PySet_GET_SIZE(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2884 if (set_size == 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2885 return 0; /* nothing to do */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2886
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2887 /* Write in batches of BATCHSIZE. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2888 do {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2889 i = 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2890 if (_Pickler_Write(self, &mark_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2891 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2892 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2893 if (save(self, item, 0) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2894 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2895 if (++i == BATCHSIZE)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2896 break;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2897 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2898 if (_Pickler_Write(self, &additems_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2899 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2900 if (PySet_GET_SIZE(obj) != set_size) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2901 PyErr_Format(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2902 PyExc_RuntimeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2903 "set changed size during iteration");
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2904 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2905 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2906 } while (i == BATCHSIZE);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2907
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2908 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2909 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2910
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2911 static int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2912 save_frozenset(PicklerObject *self, PyObject *obj)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2913 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2914 PyObject *iter;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2915
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2916 const char mark_op = MARK;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2917 const char frozenset_op = FROZENSET;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2918
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2919 if (self->fast && !fast_save_enter(self, obj))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2920 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2921
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2922 if (self->proto < 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2923 PyObject *items;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2924 PyObject *reduce_value;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2925 int status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2926
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2927 items = PySequence_List(obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2928 if (items == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2929 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2930 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2931 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2932 items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2933 Py_DECREF(items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2934 if (reduce_value == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2935 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2936 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2937 /* save_reduce() will memoize the object automatically. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2938 status = save_reduce(self, reduce_value, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2939 Py_DECREF(reduce_value);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2940 return status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2941 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2942
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2943 if (_Pickler_Write(self, &mark_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2944 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2945
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2946 iter = PyObject_GetIter(obj);
87444
d719975f4d25 Issue #17810: Add NULL check to save_frozenset
Christian Heimes <christian@cheimes.de>
parents: 87438
diff changeset
2947 if (iter == NULL) {
87445
c54becd69805 Issue #17810: return -1 on error
Christian Heimes <christian@cheimes.de>
parents: 87444
diff changeset
2948 return -1;
87444
d719975f4d25 Issue #17810: Add NULL check to save_frozenset
Christian Heimes <christian@cheimes.de>
parents: 87438
diff changeset
2949 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2950 for (;;) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2951 PyObject *item;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2952
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2953 item = PyIter_Next(iter);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2954 if (item == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2955 if (PyErr_Occurred()) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2956 Py_DECREF(iter);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2957 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2958 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2959 break;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2960 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2961 if (save(self, item, 0) < 0) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2962 Py_DECREF(item);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2963 Py_DECREF(iter);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2964 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2965 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2966 Py_DECREF(item);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2967 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2968 Py_DECREF(iter);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2969
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2970 /* If the object is already in the memo, this means it is
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2971 recursive. In this case, throw away everything we put on the
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2972 stack, and fetch the object back from the memo. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2973 if (PyMemoTable_Get(self->memo, obj)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2974 const char pop_mark_op = POP_MARK;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2975
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2976 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2977 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2978 if (memo_get(self, obj) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2979 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2980 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2981 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2982
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2983 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2984 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2985 if (memo_put(self, obj) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2986 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2987
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2988 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2989 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2990
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2991 static int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2992 fix_imports(PyObject **module_name, PyObject **global_name)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2993 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2994 PyObject *key;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2995 PyObject *item;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
2996 PickleState *st = _Pickle_GetGlobalState();
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2997
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2998 key = PyTuple_Pack(2, *module_name, *global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
2999 if (key == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3000 return -1;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3001 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3002 Py_DECREF(key);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3003 if (item) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3004 PyObject *fixed_module_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3005 PyObject *fixed_global_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3006
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3007 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3008 PyErr_Format(PyExc_RuntimeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3009 "_compat_pickle.REVERSE_NAME_MAPPING values "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3010 "should be 2-tuples, not %.200s",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3011 Py_TYPE(item)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3012 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3013 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3014 fixed_module_name = PyTuple_GET_ITEM(item, 0);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3015 fixed_global_name = PyTuple_GET_ITEM(item, 1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3016 if (!PyUnicode_Check(fixed_module_name) ||
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3017 !PyUnicode_Check(fixed_global_name)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3018 PyErr_Format(PyExc_RuntimeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3019 "_compat_pickle.REVERSE_NAME_MAPPING values "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3020 "should be pairs of str, not (%.200s, %.200s)",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3021 Py_TYPE(fixed_module_name)->tp_name,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3022 Py_TYPE(fixed_global_name)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3023 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3024 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3025
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3026 Py_CLEAR(*module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3027 Py_CLEAR(*global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3028 Py_INCREF(fixed_module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3029 Py_INCREF(fixed_global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3030 *module_name = fixed_module_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3031 *global_name = fixed_global_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3032 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3033 else if (PyErr_Occurred()) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3034 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3035 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3036
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3037 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3038 if (item) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3039 if (!PyUnicode_Check(item)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3040 PyErr_Format(PyExc_RuntimeError,
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3041 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3042 "should be strings, not %.200s",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3043 Py_TYPE(item)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3044 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3045 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3046 Py_CLEAR(*module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3047 Py_INCREF(item);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3048 *module_name = item;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3049 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3050 else if (PyErr_Occurred()) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3051 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3052 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3053
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3054 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3055 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3056
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3057 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3058 save_global(PicklerObject *self, PyObject *obj, PyObject *name)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3059 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3060 PyObject *global_name = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3061 PyObject *module_name = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3062 PyObject *module = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3063 PyObject *cls;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3064 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3065 int status = 0;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3066 _Py_IDENTIFIER(__name__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3067 _Py_IDENTIFIER(__qualname__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3068
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3069 const char global_op = GLOBAL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3070
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3071 if (name) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3072 Py_INCREF(name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3073 global_name = name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3074 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3075 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3076 if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3077 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3078 if (global_name == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3079 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3080 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3081 PyErr_Clear();
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3082 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3083 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3084 if (global_name == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3085 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3086 if (global_name == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3087 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3088 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3089 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3090
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3091 module_name = whichmodule(obj, global_name, self->proto >= 4);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3092 if (module_name == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3093 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3094
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3095 /* XXX: Change to use the import C API directly with level=0 to disallow
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3096 relative imports.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3097
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3098 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3099 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3100 custom import functions (IMHO, this would be a nice security
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3101 feature). The import C API would need to be extended to support the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3102 extra parameters of __import__ to fix that. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3103 module = PyImport_Import(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3104 if (module == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3105 PyErr_Format(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3106 "Can't pickle %R: import of module %R failed",
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3107 obj, module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3108 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3109 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3110 cls = getattribute(module, global_name, self->proto >= 4);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3111 if (cls == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3112 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3113 "Can't pickle %R: attribute lookup %S on %S failed",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3114 obj, global_name, module_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3115 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3116 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3117 if (cls != obj) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3118 Py_DECREF(cls);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3119 PyErr_Format(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3120 "Can't pickle %R: it's not the same object as %S.%S",
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3121 obj, module_name, global_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3122 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3123 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3124 Py_DECREF(cls);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3125
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3126 if (self->proto >= 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3127 /* See whether this is in the extension registry, and if
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3128 * so generate an EXT opcode.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3129 */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3130 PyObject *extension_key;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3131 PyObject *code_obj; /* extension code as Python object */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3132 long code; /* extension code as C value */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3133 char pdata[5];
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
3134 Py_ssize_t n;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3135
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3136 extension_key = PyTuple_Pack(2, module_name, global_name);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3137 if (extension_key == NULL) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3138 goto error;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3139 }
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3140 code_obj = PyDict_GetItemWithError(st->extension_registry,
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3141 extension_key);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3142 Py_DECREF(extension_key);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3143 /* The object is not registered in the extension registry.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3144 This is the most likely code path. */
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3145 if (code_obj == NULL) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3146 if (PyErr_Occurred()) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3147 goto error;
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3148 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3149 goto gen_global;
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3150 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3151
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3152 /* XXX: pickle.py doesn't check neither the type, nor the range
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3153 of the value returned by the extension_registry. It should for
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3154 consistency. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3155
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3156 /* Verify code_obj has the right type and value. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3157 if (!PyLong_Check(code_obj)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3158 PyErr_Format(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3159 "Can't pickle %R: extension code %R isn't an integer",
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3160 obj, code_obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3161 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3162 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3163 code = PyLong_AS_LONG(code_obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3164 if (code <= 0 || code > 0x7fffffffL) {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
3165 if (!PyErr_Occurred())
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3166 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3167 "code %ld is out of range", obj, code);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3168 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3169 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3170
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3171 /* Generate an EXT opcode. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3172 if (code <= 0xff) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3173 pdata[0] = EXT1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3174 pdata[1] = (unsigned char)code;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3175 n = 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3176 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3177 else if (code <= 0xffff) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3178 pdata[0] = EXT2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3179 pdata[1] = (unsigned char)(code & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3180 pdata[2] = (unsigned char)((code >> 8) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3181 n = 3;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3182 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3183 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3184 pdata[0] = EXT4;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3185 pdata[1] = (unsigned char)(code & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3186 pdata[2] = (unsigned char)((code >> 8) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3187 pdata[3] = (unsigned char)((code >> 16) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3188 pdata[4] = (unsigned char)((code >> 24) & 0xff);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3189 n = 5;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3190 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3191
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3192 if (_Pickler_Write(self, pdata, n) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3193 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3194 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3195 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3196 gen_global:
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3197 if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3198 const char stack_global_op = STACK_GLOBAL;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3199
87450
a02adfb3260a Issue #17810: Add two missing error checks to save_global
Christian Heimes <christian@cheimes.de>
parents: 87447
diff changeset
3200 if (save(self, module_name, 0) < 0)
a02adfb3260a Issue #17810: Add two missing error checks to save_global
Christian Heimes <christian@cheimes.de>
parents: 87447
diff changeset
3201 goto error;
a02adfb3260a Issue #17810: Add two missing error checks to save_global
Christian Heimes <christian@cheimes.de>
parents: 87447
diff changeset
3202 if (save(self, global_name, 0) < 0)
a02adfb3260a Issue #17810: Add two missing error checks to save_global
Christian Heimes <christian@cheimes.de>
parents: 87447
diff changeset
3203 goto error;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3204
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3205 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3206 goto error;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3207 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3208 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3209 /* Generate a normal global opcode if we are using a pickle
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3210 protocol < 4, or if the object is not registered in the
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3211 extension registry. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3212 PyObject *encoded;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3213 PyObject *(*unicode_encoder)(PyObject *);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3214
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3215 if (_Pickler_Write(self, &global_op, 1) < 0)
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3216 goto error;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3217
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3218 /* For protocol < 3 and if the user didn't request against doing
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3219 so, we convert module names to the old 2.x module names. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3220 if (self->proto < 3 && self->fix_imports) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3221 if (fix_imports(&module_name, &global_name) < 0) {
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3222 goto error;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3223 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3224 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3225
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3226 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3227 both the module name and the global name using UTF-8. We do so
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3228 only when we are using the pickle protocol newer than version
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3229 3. This is to ensure compatibility with older Unpickler running
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3230 on Python 2.x. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3231 if (self->proto == 3) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3232 unicode_encoder = PyUnicode_AsUTF8String;
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3233 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3234 else {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3235 unicode_encoder = PyUnicode_AsASCIIString;
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3236 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3237 encoded = unicode_encoder(module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3238 if (encoded == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3239 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3240 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3241 "can't pickle module identifier '%S' using "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3242 "pickle protocol %i",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3243 module_name, self->proto);
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3244 goto error;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
3245 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3246 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3247 PyBytes_GET_SIZE(encoded)) < 0) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3248 Py_DECREF(encoded);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3249 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3250 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3251 Py_DECREF(encoded);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3252 if(_Pickler_Write(self, "\n", 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3253 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3254
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3255 /* Save the name of the module. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3256 encoded = unicode_encoder(global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3257 if (encoded == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3258 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3259 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3260 "can't pickle global identifier '%S' using "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3261 "pickle protocol %i",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3262 global_name, self->proto);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3263 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3264 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3265 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3266 PyBytes_GET_SIZE(encoded)) < 0) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3267 Py_DECREF(encoded);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3268 goto error;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3269 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3270 Py_DECREF(encoded);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3271 if (_Pickler_Write(self, "\n", 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3272 goto error;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3273 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3274 /* Memoize the object. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3275 if (memo_put(self, obj) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3276 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3277 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3278
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3279 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3280 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3281 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3282 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3283 Py_XDECREF(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3284 Py_XDECREF(global_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3285 Py_XDECREF(module);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3286
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3287 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3288 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3289
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3290 static int
87665
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3291 save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3292 {
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3293 PyObject *reduce_value;
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3294 int status;
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3295
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3296 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3297 if (reduce_value == NULL) {
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3298 return -1;
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3299 }
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3300 status = save_reduce(self, reduce_value, obj);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3301 Py_DECREF(reduce_value);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3302 return status;
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3303 }
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3304
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3305 static int
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3306 save_type(PicklerObject *self, PyObject *obj)
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3307 {
87670
9fcba15d7685 Issue #6477: Keep PyNotImplemented_Type and PyNone_Type private.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87665
diff changeset
3308 if (obj == (PyObject *)&_PyNone_Type) {
87665
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3309 return save_singleton_type(self, obj, Py_None);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3310 }
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3311 else if (obj == (PyObject *)&PyEllipsis_Type) {
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3312 return save_singleton_type(self, obj, Py_Ellipsis);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3313 }
87670
9fcba15d7685 Issue #6477: Keep PyNotImplemented_Type and PyNone_Type private.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87665
diff changeset
3314 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
87665
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3315 return save_singleton_type(self, obj, Py_NotImplemented);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3316 }
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3317 return save_global(self, obj, NULL);
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3318 }
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3319
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3320 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3321 save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3322 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3323 PyObject *pid = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3324 int status = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3325
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3326 const char persid_op = PERSID;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3327 const char binpersid_op = BINPERSID;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3328
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3329 Py_INCREF(obj);
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
3330 pid = _Pickle_FastCall(func, obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3331 if (pid == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3332 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3333
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3334 if (pid != Py_None) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3335 if (self->bin) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3336 if (save(self, pid, 1) < 0 ||
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3337 _Pickler_Write(self, &binpersid_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3338 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3339 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3340 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3341 PyObject *pid_str = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3342 char *pid_ascii_bytes;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3343 Py_ssize_t size;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3344
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3345 pid_str = PyObject_Str(pid);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3346 if (pid_str == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3347 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3348
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3349 /* XXX: Should it check whether the persistent id only contains
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3350 ASCII characters? And what if the pid contains embedded
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3351 newlines? */
48660
85b67fb5672b Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents: 48462
diff changeset
3352 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3353 Py_DECREF(pid_str);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3354 if (pid_ascii_bytes == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3355 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3356
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3357 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3358 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3359 _Pickler_Write(self, "\n", 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3360 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3361 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3362 status = 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3363 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3364
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3365 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3366 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3367 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3368 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3369 Py_XDECREF(pid);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3370
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3371 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3372 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3373
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3374 static PyObject *
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3375 get_class(PyObject *obj)
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3376 {
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3377 PyObject *cls;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3378 _Py_IDENTIFIER(__class__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3379
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3380 cls = _PyObject_GetAttrId(obj, &PyId___class__);
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3381 if (cls == NULL) {
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3382 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3383 PyErr_Clear();
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3384 cls = (PyObject *) Py_TYPE(obj);
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3385 Py_INCREF(cls);
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3386 }
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3387 }
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3388 return cls;
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3389 }
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3390
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3391 /* We're saving obj, and args is the 2-thru-5 tuple returned by the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3392 * appropriate __reduce__ method for obj.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3393 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3394 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3395 save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3396 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3397 PyObject *callable;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3398 PyObject *argtup;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3399 PyObject *state = NULL;
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3400 PyObject *listitems = Py_None;
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3401 PyObject *dictitems = Py_None;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3402 PickleState *st = _Pickle_GetGlobalState();
49638
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3403 Py_ssize_t size;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3404 int use_newobj = 0, use_newobj_ex = 0;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3405
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3406 const char reduce_op = REDUCE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3407 const char build_op = BUILD;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3408 const char newobj_op = NEWOBJ;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3409 const char newobj_ex_op = NEWOBJ_EX;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3410
49638
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3411 size = PyTuple_Size(args);
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3412 if (size < 2 || size > 5) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3413 PyErr_SetString(st->PicklingError, "tuple returned by "
49638
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3414 "__reduce__ must contain 2 through 5 elements");
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3415 return -1;
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3416 }
f2bb133ef517 Sorry, r67092 is commit miss....
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 49637
diff changeset
3417
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3418 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3419 &callable, &argtup, &state, &listitems, &dictitems))
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3420 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3421
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3422 if (!PyCallable_Check(callable)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3423 PyErr_SetString(st->PicklingError, "first item of the tuple "
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3424 "returned by __reduce__ must be callable");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3425 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3426 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3427 if (!PyTuple_Check(argtup)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3428 PyErr_SetString(st->PicklingError, "second item of the tuple "
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3429 "returned by __reduce__ must be a tuple");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3430 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3431 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3432
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3433 if (state == Py_None)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3434 state = NULL;
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3435
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3436 if (listitems == Py_None)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3437 listitems = NULL;
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3438 else if (!PyIter_Check(listitems)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3439 PyErr_Format(st->PicklingError, "fourth element of the tuple "
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3440 "returned by __reduce__ must be an iterator, not %s",
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3441 Py_TYPE(listitems)->tp_name);
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3442 return -1;
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3443 }
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3444
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3445 if (dictitems == Py_None)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3446 dictitems = NULL;
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3447 else if (!PyIter_Check(dictitems)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3448 PyErr_Format(st->PicklingError, "fifth element of the tuple "
49615
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3449 "returned by __reduce__ must be an iterator, not %s",
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3450 Py_TYPE(dictitems)->tp_name);
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3451 return -1;
a5ca8951bef1 Merged revisions 67049 via svnmerge from
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49546
diff changeset
3452 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3453
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3454 if (self->proto >= 2) {
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3455 PyObject *name;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3456 _Py_IDENTIFIER(__name__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3457
87091
7d0323556c53 Issue #19437: Use an identifier for "__name__" string in pickle to improve
Victor Stinner <victor.stinner@gmail.com>
parents: 86973
diff changeset
3458 name = _PyObject_GetAttrId(callable, &PyId___name__);
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3459 if (name == NULL) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3460 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3461 return -1;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3462 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3463 PyErr_Clear();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3464 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3465 else if (self->proto >= 4) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3466 _Py_IDENTIFIER(__newobj_ex__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3467 use_newobj_ex = PyUnicode_Check(name) &&
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3468 PyUnicode_Compare(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3469 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3470 Py_DECREF(name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3471 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3472 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3473 _Py_IDENTIFIER(__newobj__);
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3474 use_newobj = PyUnicode_Check(name) &&
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3475 PyUnicode_Compare(
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3476 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3477 Py_DECREF(name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3478 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3479 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3480
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3481 if (use_newobj_ex) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3482 PyObject *cls;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3483 PyObject *args;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3484 PyObject *kwargs;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3485
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3486 if (Py_SIZE(argtup) != 3) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3487 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3488 "length of the NEWOBJ_EX argument tuple must be "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3489 "exactly 3, not %zd", Py_SIZE(argtup));
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3490 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3491 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3492
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3493 cls = PyTuple_GET_ITEM(argtup, 0);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3494 if (!PyType_Check(cls)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3495 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3496 "first item from NEWOBJ_EX argument tuple must "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3497 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3498 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3499 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3500 args = PyTuple_GET_ITEM(argtup, 1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3501 if (!PyTuple_Check(args)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3502 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3503 "second item from NEWOBJ_EX argument tuple must "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3504 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3505 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3506 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3507 kwargs = PyTuple_GET_ITEM(argtup, 2);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3508 if (!PyDict_Check(kwargs)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3509 PyErr_Format(st->PicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3510 "third item from NEWOBJ_EX argument tuple must "
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3511 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3512 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3513 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3514
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3515 if (save(self, cls, 0) < 0 ||
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3516 save(self, args, 0) < 0 ||
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3517 save(self, kwargs, 0) < 0 ||
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3518 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3519 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3520 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3521 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3522 else if (use_newobj) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3523 PyObject *cls;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3524 PyObject *newargtup;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3525 PyObject *obj_class;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3526 int p;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3527
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3528 /* Sanity checks. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3529 if (Py_SIZE(argtup) < 1) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3530 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3531 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3532 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3533
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3534 cls = PyTuple_GET_ITEM(argtup, 0);
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3535 if (!PyType_Check(cls)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3536 PyErr_SetString(st->PicklingError, "args[0] from "
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3537 "__newobj__ args is not a type");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3538 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3539 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3540
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3541 if (obj != NULL) {
68383
ff0220c9d213 Issue #9935: Speed up pickling of instances of user-defined classes.
Antoine Pitrou <solipsis@pitrou.net>
parents: 68257
diff changeset
3542 obj_class = get_class(obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3543 p = obj_class != cls; /* true iff a problem */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3544 Py_DECREF(obj_class);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3545 if (p) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3546 PyErr_SetString(st->PicklingError, "args[0] from "
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3547 "__newobj__ args has the wrong class");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3548 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3549 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3550 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3551 /* XXX: These calls save() are prone to infinite recursion. Imagine
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3552 what happen if the value returned by the __reduce__() method of
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3553 some extension type contains another object of the same type. Ouch!
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3554
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3555 Here is a quick example, that I ran into, to illustrate what I
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3556 mean:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3557
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3558 >>> import pickle, copyreg
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3559 >>> copyreg.dispatch_table.pop(complex)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3560 >>> pickle.dumps(1+2j)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3561 Traceback (most recent call last):
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3562 ...
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3563 RuntimeError: maximum recursion depth exceeded
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3564
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3565 Removing the complex class from copyreg.dispatch_table made the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3566 __reduce_ex__() method emit another complex object:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3567
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3568 >>> (1+1j).__reduce_ex__(2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3569 (<function __newobj__ at 0xb7b71c3c>,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3570 (<class 'complex'>, (1+1j)), None, None, None)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3571
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3572 Thus when save() was called on newargstup (the 2nd item) recursion
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3573 ensued. Of course, the bug was in the complex class which had a
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3574 broken __getnewargs__() that emitted another complex object. But,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3575 the point, here, is it is quite easy to end up with a broken reduce
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3576 function. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3577
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3578 /* Save the class and its __new__ arguments. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3579 if (save(self, cls, 0) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3580 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3581
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3582 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3583 if (newargtup == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3584 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3585
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3586 p = save(self, newargtup, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3587 Py_DECREF(newargtup);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3588 if (p < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3589 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3590
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3591 /* Add NEWOBJ opcode. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3592 if (_Pickler_Write(self, &newobj_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3593 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3594 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3595 else { /* Not using NEWOBJ. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3596 if (save(self, callable, 0) < 0 ||
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3597 save(self, argtup, 0) < 0 ||
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3598 _Pickler_Write(self, &reduce_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3599 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3600 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3601
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3602 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3603 the caller do not want to memoize the object. Not particularly useful,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3604 but that is to mimic the behavior save_reduce() in pickle.py when
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3605 obj is None. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3606 if (obj != NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3607 /* If the object is already in the memo, this means it is
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3608 recursive. In this case, throw away everything we put on the
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3609 stack, and fetch the object back from the memo. */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3610 if (PyMemoTable_Get(self->memo, obj)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3611 const char pop_op = POP;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3612
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3613 if (_Pickler_Write(self, &pop_op, 1) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3614 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3615 if (memo_get(self, obj) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3616 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3617
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3618 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3619 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3620 else if (memo_put(self, obj) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3621 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3622 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3623
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3624 if (listitems && batch_list(self, listitems) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3625 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3626
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3627 if (dictitems && batch_dict(self, dictitems) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3628 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3629
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3630 if (state) {
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
3631 if (save(self, state, 0) < 0 ||
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3632 _Pickler_Write(self, &build_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3633 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3634 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3635
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3636 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3637 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3638
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3639 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3640 save(PicklerObject *self, PyObject *obj, int pers_save)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3641 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3642 PyTypeObject *type;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3643 PyObject *reduce_func = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3644 PyObject *reduce_value = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3645 int status = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3646
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
3647 if (_Pickler_OpcodeBoundary(self) < 0)
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
3648 return -1;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
3649
67725
bcf030f73eee Merged revisions 88147 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3650 if (Py_EnterRecursiveCall(" while pickling an object"))
48462
c5710f0debae Fixed _pickle to use Py_EnterRecursiveCall().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47670
diff changeset
3651 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3652
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3653 /* The extra pers_save argument is necessary to avoid calling save_pers()
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3654 on its returned object. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3655 if (!pers_save && self->pers_func) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3656 /* save_pers() returns:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3657 -1 to signal an error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3658 0 if it did nothing successfully;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3659 1 if a persistent id was saved.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3660 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3661 if ((status = save_pers(self, obj, self->pers_func)) != 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3662 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3663 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3664
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3665 type = Py_TYPE(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3666
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3667 /* The old cPickle had an optimization that used switch-case statement
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3668 dispatching on the first letter of the type name. This has was removed
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3669 since benchmarks shown that this optimization was actually slowing
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3670 things down. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3671
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3672 /* Atom types; these aren't memoized, so don't check the memo. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3673
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3674 if (obj == Py_None) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3675 status = save_none(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3676 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3677 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3678 else if (obj == Py_False || obj == Py_True) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3679 status = save_bool(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3680 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3681 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3682 else if (type == &PyLong_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3683 status = save_long(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3684 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3685 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3686 else if (type == &PyFloat_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3687 status = save_float(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3688 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3689 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3690
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3691 /* Check the memo to see if it has the object. If so, generate
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3692 a GET (or BINGET) opcode, instead of pickling the object
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3693 once again. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3694 if (PyMemoTable_Get(self->memo, obj)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3695 if (memo_get(self, obj) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3696 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3697 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3698 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3699
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3700 if (type == &PyBytes_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3701 status = save_bytes(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3702 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3703 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3704 else if (type == &PyUnicode_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3705 status = save_unicode(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3706 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3707 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3708 else if (type == &PyDict_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3709 status = save_dict(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3710 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3711 }
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3712 else if (type == &PySet_Type) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3713 status = save_set(self, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3714 goto done;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3715 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3716 else if (type == &PyFrozenSet_Type) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3717 status = save_frozenset(self, obj);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3718 goto done;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3719 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3720 else if (type == &PyList_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3721 status = save_list(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3722 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3723 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3724 else if (type == &PyTuple_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3725 status = save_tuple(self, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3726 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3727 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3728 else if (type == &PyType_Type) {
87665
16eba94d3cfe Issue #6477: Added support for pickling the types of built-in singletons.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87656
diff changeset
3729 status = save_type(self, obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3730 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3731 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3732 else if (type == &PyFunction_Type) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3733 status = save_global(self, obj, NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3734 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3735 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3736
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3737 /* XXX: This part needs some unit tests. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3738
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3739 /* Get a reduction callable, and call it. This may come from
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3740 * self.dispatch_table, copyreg.dispatch_table, the object's
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3741 * __reduce_ex__ method, or the object's __reduce__ method.
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3742 */
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3743 if (self->dispatch_table == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3744 PickleState *st = _Pickle_GetGlobalState();
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3745 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3746 (PyObject *)type);
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3747 if (reduce_func == NULL) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3748 if (PyErr_Occurred()) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3749 goto error;
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3750 }
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3751 } else {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3752 /* PyDict_GetItemWithError() returns a borrowed reference.
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3753 Increase the reference count to be consistent with
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3754 PyObject_GetItem and _PyObject_GetAttrId used below. */
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3755 Py_INCREF(reduce_func);
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3756 }
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3757 } else {
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3758 reduce_func = PyObject_GetItem(self->dispatch_table,
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
3759 (PyObject *)type);
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3760 if (reduce_func == NULL) {
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3761 if (PyErr_ExceptionMatches(PyExc_KeyError))
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3762 PyErr_Clear();
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3763 else
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3764 goto error;
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3765 }
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3766 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3767 if (reduce_func != NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3768 Py_INCREF(obj);
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
3769 reduce_value = _Pickle_FastCall(reduce_func, obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3770 }
72639
760ac320fa3d Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 72457
diff changeset
3771 else if (PyType_IsSubtype(type, &PyType_Type)) {
760ac320fa3d Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 72457
diff changeset
3772 status = save_global(self, obj, NULL);
760ac320fa3d Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 72457
diff changeset
3773 goto done;
760ac320fa3d Issue #7689: Allow pickling of dynamically created classes when their
Antoine Pitrou <solipsis@pitrou.net>
parents: 72457
diff changeset
3774 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3775 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3776 _Py_IDENTIFIER(__reduce__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3777 _Py_IDENTIFIER(__reduce_ex__);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3778
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3779
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3780 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3781 automatically defined as __reduce__. While this is convenient, this
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3782 make it impossible to know which method was actually called. Of
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3783 course, this is not a big deal. But still, it would be nice to let
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3784 the user know which method was called when something go
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3785 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3786 don't actually have to check for a __reduce__ method. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3787
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3788 /* Check for a __reduce_ex__ method. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3789 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3790 if (reduce_func != NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3791 PyObject *proto;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3792 proto = PyLong_FromLong(self->proto);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3793 if (proto != NULL) {
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
3794 reduce_value = _Pickle_FastCall(reduce_func, proto);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3795 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3796 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3797 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3798 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3799
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3800 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3801 PyErr_Clear();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3802 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3803 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3804 goto error;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3805 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3806 /* Check for a __reduce__ method. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3807 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3808 if (reduce_func != NULL) {
87631
b44dbf07732e Remove explicit empty tuple reuse in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87630
diff changeset
3809 PyObject *empty_tuple = PyTuple_New(0);
b44dbf07732e Remove explicit empty tuple reuse in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87630
diff changeset
3810 reduce_value = PyObject_Call(reduce_func, empty_tuple,
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3811 NULL);
87631
b44dbf07732e Remove explicit empty tuple reuse in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87630
diff changeset
3812 Py_DECREF(empty_tuple);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3813 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3814 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3815 PyErr_Format(st->PicklingError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3816 "can't pickle '%.200s' object: %R",
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3817 type->tp_name, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3818 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3819 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3820 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3821 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3822
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3823 if (reduce_value == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3824 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3825
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3826 if (PyUnicode_Check(reduce_value)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3827 status = save_global(self, obj, reduce_value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3828 goto done;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3829 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3830
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3831 if (!PyTuple_Check(reduce_value)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3832 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3833 PyErr_SetString(st->PicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3834 "__reduce__ must return a string or tuple");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3835 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3836 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3837
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3838 status = save_reduce(self, reduce_value, obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3839
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3840 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3841 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3842 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3843 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3844 done:
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
3845
48462
c5710f0debae Fixed _pickle to use Py_EnterRecursiveCall().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47670
diff changeset
3846 Py_LeaveRecursiveCall();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3847 Py_XDECREF(reduce_func);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3848 Py_XDECREF(reduce_value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3849
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3850 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3851 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3852
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3853 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3854 dump(PicklerObject *self, PyObject *obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3855 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3856 const char stop_op = STOP;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3857
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3858 if (self->proto >= 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3859 char header[2];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3860
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3861 header[0] = PROTO;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3862 assert(self->proto >= 0 && self->proto < 256);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3863 header[1] = (unsigned char)self->proto;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3864 if (_Pickler_Write(self, header, 2) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3865 return -1;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3866 if (self->proto >= 4)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
3867 self->framing = 1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3868 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3869
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3870 if (save(self, obj, 0) < 0 ||
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3871 _Pickler_Write(self, &stop_op, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3872 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3873
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3874 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3875 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3876
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3877 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3878
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3879 _pickle.Pickler.clear_memo
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3880
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3881 self: PicklerObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3882
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3883 Clears the pickler's "memo".
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3884
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3885 The memo is the data structure that remembers which objects the
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3886 pickler has already seen, so that shared or recursive objects are
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3887 pickled by reference and not by value. This method is useful when
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3888 re-using picklers.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3889 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3890
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3891 PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3892 "clear_memo()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3893 "Clears the pickler\'s \"memo\".\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3894 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3895 "The memo is the data structure that remembers which objects the\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3896 "pickler has already seen, so that shared or recursive objects are\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3897 "pickled by reference and not by value. This method is useful when\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3898 "re-using picklers.");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3899
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3900 #define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3901 {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3902
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3903 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3904 _pickle_Pickler_clear_memo(PicklerObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3905 /*[clinic checksum: 9c32be7e7a17ff82a81aae409d0d4f469033a5b2]*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3906 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3907 if (self->memo)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3908 PyMemoTable_Clear(self->memo);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3909
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3910 Py_RETURN_NONE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3911 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3912
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3913 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3914
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3915 _pickle.Pickler.dump
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3916
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3917 self: PicklerObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3918 obj: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3919 /
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3920
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3921 Write a pickled representation of the given object to the open file.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3922 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3923
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3924 PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3925 "dump(obj)\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3926 "Write a pickled representation of the given object to the open file.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3927
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3928 #define _PICKLE_PICKLER_DUMP_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3929 {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3930
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3931 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3932 _pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3933 /*[clinic checksum: b72a69ec98737fabf66dae7c5a3210178bdbd3e6]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3934 {
49546
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3935 /* Check whether the Pickler was initialized correctly (issue3664).
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3936 Developers often forget to call __init__() in their subclasses, which
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3937 would trigger a segfault without this check. */
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3938 if (self->write == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3939 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
3940 PyErr_Format(st->PicklingError,
49546
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3941 "Pickler.__init__() was not called by %s.__init__()",
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3942 Py_TYPE(self)->tp_name);
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3943 return NULL;
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3944 }
bc1f43252c92 #3664: The pickle module could segfault if a Pickler instance is not correctly initialized:
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49149
diff changeset
3945
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3946 if (_Pickler_ClearBuffer(self) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3947 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3948
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3949 if (dump(self, obj) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3950 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3951
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3952 if (_Pickler_FlushToFile(self) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3953 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3954
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3955 Py_RETURN_NONE;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3956 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3957
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3958 static struct PyMethodDef Pickler_methods[] = {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3959 _PICKLE_PICKLER_DUMP_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
3960 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3961 {NULL, NULL} /* sentinel */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3962 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3963
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3964 static void
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3965 Pickler_dealloc(PicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3966 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3967 PyObject_GC_UnTrack(self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3968
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3969 Py_XDECREF(self->output_buffer);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3970 Py_XDECREF(self->write);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3971 Py_XDECREF(self->pers_func);
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3972 Py_XDECREF(self->dispatch_table);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3973 Py_XDECREF(self->fast_memo);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3974
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3975 PyMemoTable_Del(self->memo);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3976
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3977 Py_TYPE(self)->tp_free((PyObject *)self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3978 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3979
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3980 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3981 Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3982 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3983 Py_VISIT(self->write);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3984 Py_VISIT(self->pers_func);
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3985 Py_VISIT(self->dispatch_table);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3986 Py_VISIT(self->fast_memo);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3987 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3988 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3989
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3990 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3991 Pickler_clear(PicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3992 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3993 Py_CLEAR(self->output_buffer);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3994 Py_CLEAR(self->write);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3995 Py_CLEAR(self->pers_func);
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
3996 Py_CLEAR(self->dispatch_table);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3997 Py_CLEAR(self->fast_memo);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
3998
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
3999 if (self->memo != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4000 PyMemoTable *memo = self->memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4001 self->memo = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4002 PyMemoTable_Del(memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4003 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4004 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4005 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4006
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4007
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4008 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4009
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4010 _pickle.Pickler.__init__
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4011
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4012 self: PicklerObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4013 file: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4014 protocol: object = NULL
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4015 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4016
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4017 This takes a binary file for writing a pickle data stream.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4018
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4019 The optional protocol argument tells the pickler to use the
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4020 given protocol; supported protocols are 0, 1, 2, 3 and 4. The
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4021 default protocol is 3; a backward-incompatible protocol designed for
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4022 Python 3.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4023
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4024 Specifying a negative protocol version selects the highest
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4025 protocol version supported. The higher the protocol used, the
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4026 more recent the version of Python needed to read the pickle
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4027 produced.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4028
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4029 The file argument must have a write() method that accepts a single
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4030 bytes argument. It can thus be a file object opened for binary
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4031 writing, a io.BytesIO instance, or any other custom object that
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4032 meets this interface.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4033
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4034 If fix_imports is True and protocol is less than 3, pickle will try to
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4035 map the new Python 3 names to the old module names used in Python 2,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4036 so that the pickle data stream is readable with Python 2.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4037 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4038
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4039 PyDoc_STRVAR(_pickle_Pickler___init____doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4040 "__init__(file, protocol=None, fix_imports=True)\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4041 "This takes a binary file for writing a pickle data stream.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4042 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4043 "The optional protocol argument tells the pickler to use the\n"
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4044 "given protocol; supported protocols are 0, 1, 2, 3 and 4. The\n"
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4045 "default protocol is 3; a backward-incompatible protocol designed for\n"
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4046 "Python 3.\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4047 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4048 "Specifying a negative protocol version selects the highest\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4049 "protocol version supported. The higher the protocol used, the\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4050 "more recent the version of Python needed to read the pickle\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4051 "produced.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4052 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4053 "The file argument must have a write() method that accepts a single\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4054 "bytes argument. It can thus be a file object opened for binary\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4055 "writing, a io.BytesIO instance, or any other custom object that\n"
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4056 "meets this interface.\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4057 "\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4058 "If fix_imports is True and protocol is less than 3, pickle will try to\n"
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4059 "map the new Python 3 names to the old module names used in Python 2,\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4060 "so that the pickle data stream is readable with Python 2.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4061
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4062 #define _PICKLE_PICKLER___INIT___METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4063 {"__init__", (PyCFunction)_pickle_Pickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Pickler___init____doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4064
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4065 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4066 _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4067
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4068 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4069 _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4070 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4071 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4072 static char *_keywords[] = {"file", "protocol", "fix_imports", NULL};
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4073 PyObject *file;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4074 PyObject *protocol = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4075 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4076
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4077 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4078 "O|Op:__init__", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4079 &file, &protocol, &fix_imports))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4080 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4081 return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4082
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4083 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4084 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4085 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4086
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4087 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4088 _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4089 /*[clinic checksum: c99ff417bd703a74affc4b708167e56e135e8969]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4090 {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
4091 _Py_IDENTIFIER(persistent_id);
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4092 _Py_IDENTIFIER(dispatch_table);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4093
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4094 /* In case of multiple __init__() calls, clear previous content. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4095 if (self->write != NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4096 (void)Pickler_clear(self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4097
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4098 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4099 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4100
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4101 if (_Pickler_SetOutputStream(self, file) < 0)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4102 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4103
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4104 /* memo and output_buffer may have already been created in _Pickler_New */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4105 if (self->memo == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4106 self->memo = PyMemoTable_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4107 if (self->memo == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4108 return NULL;
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4109 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4110 self->output_len = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4111 if (self->output_buffer == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4112 self->max_output_len = WRITE_BUF_SIZE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4113 self->output_buffer = PyBytes_FromStringAndSize(NULL,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4114 self->max_output_len);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4115 if (self->output_buffer == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4116 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4117 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4118
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4119 self->fast = 0;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4120 self->fast_nesting = 0;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
4121 self->fast_memo = NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4122 self->pers_func = NULL;
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
4123 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
4124 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
4125 &PyId_persistent_id);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4126 if (self->pers_func == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4127 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4128 }
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4129 self->dispatch_table = NULL;
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4130 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4131 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4132 &PyId_dispatch_table);
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4133 if (self->dispatch_table == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4134 return NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4135 }
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4136
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4137 Py_RETURN_NONE;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4138 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4139
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4140 /* Wrap the Clinic generated signature to slot it in tp_init. */
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4141 static int
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4142 Pickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4143 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4144 PyObject *result = _pickle_Pickler___init__(self, args, kwargs);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4145 if (result == NULL) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4146 return -1;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4147 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4148 Py_DECREF(result);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4149 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4150 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4151
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4152 /* Define a proxy object for the Pickler's internal memo object. This is to
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4153 * avoid breaking code like:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4154 * pickler.memo.clear()
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4155 * and
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4156 * pickler.memo = saved_memo
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4157 * Is this a good idea? Not really, but we don't want to break code that uses
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4158 * it. Note that we don't implement the entire mapping API here. This is
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4159 * intentional, as these should be treated as black-box implementation details.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4160 */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4161
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4162 typedef struct {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4163 PyObject_HEAD
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4164 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4165 } PicklerMemoProxyObject;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4166
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4167 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4168 _pickle.PicklerMemoProxy.clear
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4169
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4170 self: PicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4171
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4172 Remove all items from memo.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4173 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4174
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4175 PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4176 "clear()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4177 "Remove all items from memo.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4178
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4179 #define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4180 {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4181
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4182 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4183 _pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4184 /*[clinic checksum: 507f13938721992e175a3e58b5ad02620045a1cc]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4185 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4186 if (self->pickler->memo)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4187 PyMemoTable_Clear(self->pickler->memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4188 Py_RETURN_NONE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4189 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4190
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4191 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4192 _pickle.PicklerMemoProxy.copy
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4193
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4194 self: PicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4195
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4196 Copy the memo to a new object.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4197 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4198
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4199 PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4200 "copy()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4201 "Copy the memo to a new object.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4202
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4203 #define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4204 {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4205
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4206 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4207 _pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4208 /*[clinic checksum: 73a5117ab354290ebdbe07bd0bf7232d0936a69d]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4209 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4210 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4211 PyMemoTable *memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4212 PyObject *new_memo = PyDict_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4213 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4214 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4215
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4216 memo = self->pickler->memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4217 for (i = 0; i < memo->mt_allocated; ++i) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4218 PyMemoEntry entry = memo->mt_table[i];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4219 if (entry.me_key != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4220 int status;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4221 PyObject *key, *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4222
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4223 key = PyLong_FromVoidPtr(entry.me_key);
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4224 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4225
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4226 if (key == NULL || value == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4227 Py_XDECREF(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4228 Py_XDECREF(value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4229 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4230 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4231 status = PyDict_SetItem(new_memo, key, value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4232 Py_DECREF(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4233 Py_DECREF(value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4234 if (status < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4235 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4236 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4237 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4238 return new_memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4239
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4240 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4241 Py_XDECREF(new_memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4242 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4243 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4244
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4245 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4246 _pickle.PicklerMemoProxy.__reduce__
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4247
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4248 self: PicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4249
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4250 Implement pickle support.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4251 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4252
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4253 PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4254 "__reduce__()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4255 "Implement pickle support.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4256
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4257 #define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4258 {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4259
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4260 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4261 _pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4262 /*[clinic checksum: 40f0bf7a9b161e77130674f0481bda0a0184dcce]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4263 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4264 PyObject *reduce_value, *dict_args;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4265 PyObject *contents = _pickle_PicklerMemoProxy_copy(self);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4266 if (contents == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4267 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4268
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4269 reduce_value = PyTuple_New(2);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4270 if (reduce_value == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4271 Py_DECREF(contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4272 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4273 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4274 dict_args = PyTuple_New(1);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4275 if (dict_args == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4276 Py_DECREF(contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4277 Py_DECREF(reduce_value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4278 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4279 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4280 PyTuple_SET_ITEM(dict_args, 0, contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4281 Py_INCREF((PyObject *)&PyDict_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4282 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4283 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4284 return reduce_value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4285 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4286
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4287 static PyMethodDef picklerproxy_methods[] = {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4288 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4289 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4290 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4291 {NULL, NULL} /* sentinel */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4292 };
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4293
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4294 static void
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4295 PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4296 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4297 PyObject_GC_UnTrack(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4298 Py_XDECREF(self->pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4299 PyObject_GC_Del((PyObject *)self);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4300 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4301
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4302 static int
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4303 PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4304 visitproc visit, void *arg)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4305 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4306 Py_VISIT(self->pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4307 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4308 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4309
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4310 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4311 PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4312 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4313 Py_CLEAR(self->pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4314 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4315 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4316
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4317 static PyTypeObject PicklerMemoProxyType = {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4318 PyVarObject_HEAD_INIT(NULL, 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4319 "_pickle.PicklerMemoProxy", /*tp_name*/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4320 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4321 0,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4322 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4323 0, /* tp_print */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4324 0, /* tp_getattr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4325 0, /* tp_setattr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4326 0, /* tp_compare */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4327 0, /* tp_repr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4328 0, /* tp_as_number */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4329 0, /* tp_as_sequence */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4330 0, /* tp_as_mapping */
65513
5b069892e413 Remove more unneeded casts to hashfunc.
Georg Brandl <georg@python.org>
parents: 65494
diff changeset
4331 PyObject_HashNotImplemented, /* tp_hash */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4332 0, /* tp_call */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4333 0, /* tp_str */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4334 PyObject_GenericGetAttr, /* tp_getattro */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4335 PyObject_GenericSetAttr, /* tp_setattro */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4336 0, /* tp_as_buffer */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4337 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4338 0, /* tp_doc */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4339 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4340 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4341 0, /* tp_richcompare */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4342 0, /* tp_weaklistoffset */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4343 0, /* tp_iter */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4344 0, /* tp_iternext */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4345 picklerproxy_methods, /* tp_methods */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4346 };
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4347
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4348 static PyObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4349 PicklerMemoProxy_New(PicklerObject *pickler)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4350 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4351 PicklerMemoProxyObject *self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4352
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4353 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4354 if (self == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4355 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4356 Py_INCREF(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4357 self->pickler = pickler;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4358 PyObject_GC_Track(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4359 return (PyObject *)self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4360 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4361
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4362 /*****************************************************************************/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4363
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4364 static PyObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4365 Pickler_get_memo(PicklerObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4366 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4367 return PicklerMemoProxy_New(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4368 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4369
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4370 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4371 Pickler_set_memo(PicklerObject *self, PyObject *obj)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4372 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4373 PyMemoTable *new_memo = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4374
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4375 if (obj == NULL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4376 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4377 "attribute deletion is not supported");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4378 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4379 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4380
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4381 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4382 PicklerObject *pickler =
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4383 ((PicklerMemoProxyObject *)obj)->pickler;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4384
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4385 new_memo = PyMemoTable_Copy(pickler->memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4386 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4387 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4388 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4389 else if (PyDict_Check(obj)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4390 Py_ssize_t i = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4391 PyObject *key, *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4392
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4393 new_memo = PyMemoTable_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4394 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4395 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4396
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4397 while (PyDict_Next(obj, &i, &key, &value)) {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4398 Py_ssize_t memo_id;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4399 PyObject *memo_obj;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4400
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4401 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4402 PyErr_SetString(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4403 "'memo' values must be 2-item tuples");
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4404 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4405 }
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4406 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4407 if (memo_id == -1 && PyErr_Occurred())
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4408 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4409 memo_obj = PyTuple_GET_ITEM(value, 1);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4410 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4411 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4412 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4413 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4414 else {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4415 PyErr_Format(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4416 "'memo' attribute must be an PicklerMemoProxy object"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4417 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4418 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4419 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4420
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4421 PyMemoTable_Del(self->memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4422 self->memo = new_memo;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4423
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4424 return 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4425
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4426 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4427 if (new_memo)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4428 PyMemoTable_Del(new_memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4429 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4430 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4431
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4432 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4433 Pickler_get_persid(PicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4434 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4435 if (self->pers_func == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4436 PyErr_SetString(PyExc_AttributeError, "persistent_id");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4437 else
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4438 Py_INCREF(self->pers_func);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4439 return self->pers_func;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4440 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4441
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4442 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4443 Pickler_set_persid(PicklerObject *self, PyObject *value)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4444 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4445 PyObject *tmp;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4446
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4447 if (value == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4448 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4449 "attribute deletion is not supported");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4450 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4451 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4452 if (!PyCallable_Check(value)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4453 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4454 "persistent_id must be a callable taking one argument");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4455 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4456 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4457
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4458 tmp = self->pers_func;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4459 Py_INCREF(value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4460 self->pers_func = value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4461 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4462
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4463 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4464 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4465
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4466 static PyMemberDef Pickler_members[] = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4467 {"bin", T_INT, offsetof(PicklerObject, bin)},
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4468 {"fast", T_INT, offsetof(PicklerObject, fast)},
75391
f7a9a10ae0c0 Issue #14166: Pickler objects now have an optional `dispatch_table` attribute which allows to set custom per-pickler reduction functions.
Antoine Pitrou <solipsis@pitrou.net>
parents: 73958
diff changeset
4469 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4470 {NULL}
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4471 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4472
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4473 static PyGetSetDef Pickler_getsets[] = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4474 {"memo", (getter)Pickler_get_memo,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4475 (setter)Pickler_set_memo},
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4476 {"persistent_id", (getter)Pickler_get_persid,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4477 (setter)Pickler_set_persid},
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4478 {NULL}
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4479 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4480
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4481 static PyTypeObject Pickler_Type = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4482 PyVarObject_HEAD_INIT(NULL, 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4483 "_pickle.Pickler" , /*tp_name*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4484 sizeof(PicklerObject), /*tp_basicsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4485 0, /*tp_itemsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4486 (destructor)Pickler_dealloc, /*tp_dealloc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4487 0, /*tp_print*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4488 0, /*tp_getattr*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4489 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 51243
diff changeset
4490 0, /*tp_reserved*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4491 0, /*tp_repr*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4492 0, /*tp_as_number*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4493 0, /*tp_as_sequence*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4494 0, /*tp_as_mapping*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4495 0, /*tp_hash*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4496 0, /*tp_call*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4497 0, /*tp_str*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4498 0, /*tp_getattro*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4499 0, /*tp_setattro*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4500 0, /*tp_as_buffer*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4501 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4502 _pickle_Pickler___init____doc__, /*tp_doc*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4503 (traverseproc)Pickler_traverse, /*tp_traverse*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4504 (inquiry)Pickler_clear, /*tp_clear*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4505 0, /*tp_richcompare*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4506 0, /*tp_weaklistoffset*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4507 0, /*tp_iter*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4508 0, /*tp_iternext*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4509 Pickler_methods, /*tp_methods*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4510 Pickler_members, /*tp_members*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4511 Pickler_getsets, /*tp_getset*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4512 0, /*tp_base*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4513 0, /*tp_dict*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4514 0, /*tp_descr_get*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4515 0, /*tp_descr_set*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4516 0, /*tp_dictoffset*/
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
4517 Pickler_init, /*tp_init*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4518 PyType_GenericAlloc, /*tp_alloc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4519 PyType_GenericNew, /*tp_new*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4520 PyObject_GC_Del, /*tp_free*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4521 0, /*tp_is_gc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4522 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4523
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
4524 /* Temporary helper for calling self.find_class().
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4525
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4526 XXX: It would be nice to able to avoid Python function call overhead, by
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4527 using directly the C version of find_class(), when find_class() is not
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4528 overridden by a subclass. Although, this could become rather hackish. A
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4529 simpler optimization would be to call the C function when self is not a
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4530 subclass instance. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4531 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4532 find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4533 {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
4534 _Py_IDENTIFIER(find_class);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
4535
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
4536 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
4537 module_name, global_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4538 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4539
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4540 static Py_ssize_t
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4541 marker(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4542 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4543 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4544 if (self->num_marks < 1) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4545 PyErr_SetString(st->UnpicklingError, "could not find MARK");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4546 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4547 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4548
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4549 return self->marks[--self->num_marks];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4550 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4551
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4552 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4553 load_none(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4554 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4555 PDATA_APPEND(self->stack, Py_None, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4556 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4557 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4558
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4559 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4560 bad_readline(void)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4561 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4562 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4563 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4564 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4565 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4566
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4567 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4568 load_int(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4569 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4570 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4571 char *endptr, *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4572 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4573 long x;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4574
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4575 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4576 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4577 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4578 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4579
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4580 errno = 0;
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
4581 /* XXX: Should the base argument of strtol() be explicitly set to 10?
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4582 XXX(avassalotti): Should this uses PyOS_strtol()? */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4583 x = strtol(s, &endptr, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4584
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4585 if (errno || (*endptr != '\n' && *endptr != '\0')) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4586 /* Hm, maybe we've got something long. Let's try reading
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84844
diff changeset
4587 * it as a Python int object. */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4588 errno = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4589 /* XXX: Same thing about the base here. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4590 value = PyLong_FromString(s, NULL, 0);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4591 if (value == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4592 PyErr_SetString(PyExc_ValueError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4593 "could not convert string to int");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4594 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4595 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4596 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4597 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4598 if (len == 3 && (x == 0 || x == 1)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4599 if ((value = PyBool_FromLong(x)) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4600 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4601 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4602 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4603 if ((value = PyLong_FromLong(x)) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4604 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4605 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4606 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4607
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4608 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4609 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4610 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4611
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4612 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4613 load_bool(UnpicklerObject *self, PyObject *boolean)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4614 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4615 assert(boolean == Py_True || boolean == Py_False);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4616 PDATA_APPEND(self->stack, boolean, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4617 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4618 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4619
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4620 /* s contains x bytes of an unsigned little-endian integer. Return its value
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4621 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4622 */
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4623 static Py_ssize_t
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4624 calc_binsize(char *bytes, int nbytes)
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4625 {
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4626 unsigned char *s = (unsigned char *)bytes;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4627 int i;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4628 size_t x = 0;
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4629
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
4630 for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4631 x |= (size_t) s[i] << (8 * i);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4632 }
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4633
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4634 if (x > PY_SSIZE_T_MAX)
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4635 return -1;
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4636 else
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4637 return (Py_ssize_t) x;
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4638 }
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4639
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4640 /* s contains x bytes of a little-endian integer. Return its value as a
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4641 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4642 * int, but when x is 4 it's a signed one. This is an historical source
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4643 * of x-platform bugs.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4644 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4645 static long
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4646 calc_binint(char *bytes, int nbytes)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4647 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4648 unsigned char *s = (unsigned char *)bytes;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4649 int i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4650 long x = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4651
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4652 for (i = 0; i < nbytes; i++) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4653 x |= (long)s[i] << (8 * i);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4654 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4655
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4656 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4657 * is signed, so on a box with longs bigger than 4 bytes we need
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4658 * to extend a BININT's sign bit to the full width.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4659 */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4660 if (SIZEOF_LONG > 4 && nbytes == 4) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4661 x |= -(x & (1L << 31));
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4662 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4663
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4664 return x;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4665 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4666
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4667 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4668 load_binintx(UnpicklerObject *self, char *s, int size)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4669 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4670 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4671 long x;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4672
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4673 x = calc_binint(s, size);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4674
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4675 if ((value = PyLong_FromLong(x)) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4676 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4677
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4678 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4679 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4680 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4681
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4682 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4683 load_binint(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4684 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4685 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4686
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4687 if (_Unpickler_Read(self, &s, 4) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4688 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4689
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4690 return load_binintx(self, s, 4);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4691 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4692
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4693 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4694 load_binint1(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4695 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4696 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4697
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4698 if (_Unpickler_Read(self, &s, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4699 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4700
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4701 return load_binintx(self, s, 1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4702 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4703
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4704 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4705 load_binint2(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4706 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4707 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4708
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4709 if (_Unpickler_Read(self, &s, 2) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4710 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4711
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4712 return load_binintx(self, s, 2);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4713 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4714
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4715 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4716 load_long(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4717 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4718 PyObject *value;
50987
04c95fc3765f Remove unnecessary copying in load_long().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 50946
diff changeset
4719 char *s;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4720 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4721
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4722 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4723 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4724 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4725 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4726
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
4727 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
4728 the 'L' before calling PyLong_FromString. In order to maintain
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
4729 compatibility with Python 3.0.0, we don't actually *require*
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
4730 the 'L' to be present. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4731 if (s[len-2] == 'L')
50987
04c95fc3765f Remove unnecessary copying in load_long().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 50946
diff changeset
4732 s[len-2] = '\0';
50994
52ff0579a41f Factor common branch in load_long().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 50987
diff changeset
4733 /* XXX: Should the base argument explicitly set to 10? */
52ff0579a41f Factor common branch in load_long().
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 50987
diff changeset
4734 value = PyLong_FromString(s, NULL, 0);
50946
a57d10d6bc89 Merged revisions 68814-68815 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 50544
diff changeset
4735 if (value == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4736 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4737
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4738 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4739 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4740 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4741
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4742 /* 'size' bytes contain the # of bytes of little-endian 256's-complement
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4743 * data following.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4744 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4745 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4746 load_counted_long(UnpicklerObject *self, int size)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4747 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4748 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4749 char *nbytes;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4750 char *pdata;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4751
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4752 assert(size == 1 || size == 4);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4753 if (_Unpickler_Read(self, &nbytes, size) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4754 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4755
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4756 size = calc_binint(nbytes, size);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4757 if (size < 0) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4758 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4759 /* Corrupt or hostile pickle -- we never write one like this */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4760 PyErr_SetString(st->UnpicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4761 "LONG pickle has negative byte count");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4762 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4763 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4764
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4765 if (size == 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4766 value = PyLong_FromLong(0L);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4767 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4768 /* Read the raw little-endian bytes and convert. */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4769 if (_Unpickler_Read(self, &pdata, size) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4770 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4771 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4772 1 /* little endian */ , 1 /* signed */ );
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4773 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4774 if (value == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4775 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4776 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4777 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4778 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4779
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4780 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4781 load_float(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4782 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4783 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4784 char *endptr, *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4785 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4786 double d;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4787
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4788 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4789 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4790 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4791 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4792
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4793 errno = 0;
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53694
diff changeset
4794 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53694
diff changeset
4795 if (d == -1.0 && PyErr_Occurred())
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53694
diff changeset
4796 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4797 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4798 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4799 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4800 }
53707
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53694
diff changeset
4801 value = PyFloat_FromDouble(d);
13a82a710f19 Issue #5914: Add new C-API function PyOS_string_to_double, to complement
Mark Dickinson <dickinsm@gmail.com>
parents: 53694
diff changeset
4802 if (value == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4803 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4804
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4805 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4806 return 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4807 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4808
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4809 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4810 load_binfloat(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4811 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4812 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4813 double x;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4814 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4815
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4816 if (_Unpickler_Read(self, &s, 8) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4817 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4818
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4819 x = _PyFloat_Unpack8((unsigned char *)s, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4820 if (x == -1.0 && PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4821 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4822
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4823 if ((value = PyFloat_FromDouble(x)) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4824 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4825
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4826 PDATA_PUSH(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4827 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4828 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4829
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4830 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4831 load_string(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4832 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4833 PyObject *bytes;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4834 PyObject *str = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4835 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4836 char *s, *p;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4837
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4838 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4839 return -1;
83404
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4840 /* Strip the newline */
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4841 len--;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4842 /* Strip outermost quotes */
83404
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4843 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4844 p = s + 1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4845 len -= 2;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4846 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4847 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4848 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4849 PyErr_SetString(st->UnpicklingError,
83404
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4850 "the STRING opcode argument must be quoted");
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4851 return -1;
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4852 }
4a6b8f86b081 Make C and Python implementations of pickle load STRING opcodes the same way.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83401
diff changeset
4853 assert(len >= 0);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4854
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4855 /* Use the PyBytes API to decode the string, since that is what is used
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4856 to encode, and then coerce the result to Unicode. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4857 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4858 if (bytes == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4859 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4860 str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4861 Py_DECREF(bytes);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4862 if (str == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4863 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4864
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4865 PDATA_PUSH(self->stack, str, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4866 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4867 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4868
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4869 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4870 load_counted_binbytes(UnpicklerObject *self, int nbytes)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4871 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4872 PyObject *bytes;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4873 Py_ssize_t size;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4874 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4875
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4876 if (_Unpickler_Read(self, &s, nbytes) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4877 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4878
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4879 size = calc_binsize(s, nbytes);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4880 if (size < 0) {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4881 PyErr_Format(PyExc_OverflowError,
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4882 "BINBYTES exceeds system's maximum size of %zd bytes",
83345
686be49a1c56 Style cleanups for pickle.py and _pickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83341
diff changeset
4883 PY_SSIZE_T_MAX);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4884 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4885 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4886
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4887 if (_Unpickler_Read(self, &s, size) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4888 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4889
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4890 bytes = PyBytes_FromStringAndSize(s, size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4891 if (bytes == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4892 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4893
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4894 PDATA_PUSH(self->stack, bytes, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4895 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4896 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4897
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4898 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4899 load_counted_binstring(UnpicklerObject *self, int nbytes)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4900 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4901 PyObject *str;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4902 Py_ssize_t size;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4903 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4904
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4905 if (_Unpickler_Read(self, &s, nbytes) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4906 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4907
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4908 size = calc_binsize(s, nbytes);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4909 if (size < 0) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4910 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
4911 PyErr_Format(st->UnpicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4912 "BINSTRING exceeds system's maximum size of %zd bytes",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4913 PY_SSIZE_T_MAX);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4914 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4915 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4916
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4917 if (_Unpickler_Read(self, &s, size) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4918 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4919 /* Convert Python 2.x strings to unicode. */
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4920 str = PyUnicode_Decode(s, size, self->encoding, self->errors);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4921 if (str == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4922 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4923
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4924 PDATA_PUSH(self->stack, str, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4925 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4926 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4927
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4928 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4929 load_unicode(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4930 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4931 PyObject *str;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4932 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4933 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4934
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4935 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4936 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4937 if (len < 1)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4938 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4939
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4940 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4941 if (str == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4942 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4943
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4944 PDATA_PUSH(self->stack, str, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4945 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4946 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4947
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4948 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4949 load_counted_binunicode(UnpicklerObject *self, int nbytes)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4950 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4951 PyObject *str;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4952 Py_ssize_t size;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4953 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4954
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4955 if (_Unpickler_Read(self, &s, nbytes) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4956 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4957
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
4958 size = calc_binsize(s, nbytes);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4959 if (size < 0) {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4960 PyErr_Format(PyExc_OverflowError,
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4961 "BINUNICODE exceeds system's maximum size of %zd bytes",
83345
686be49a1c56 Style cleanups for pickle.py and _pickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83341
diff changeset
4962 PY_SSIZE_T_MAX);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4963 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4964 }
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4965
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
4966 if (_Unpickler_Read(self, &s, size) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4967 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4968
60577
7994269ad3a1 Issue #8383: pickle and pickletools use surrogatepass error handler when
Victor Stinner <victor.stinner@haypocalc.com>
parents: 58186
diff changeset
4969 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4970 if (str == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4971 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4972
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4973 PDATA_PUSH(self->stack, str, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4974 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4975 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4976
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4977 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4978 load_tuple(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4979 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4980 PyObject *tuple;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
4981 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4982
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4983 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4984 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4985
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4986 tuple = Pdata_poptuple(self->stack, i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4987 if (tuple == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4988 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4989 PDATA_PUSH(self->stack, tuple, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4990 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4991 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4992
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4993 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4994 load_counted_tuple(UnpicklerObject *self, int len)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4995 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4996 PyObject *tuple;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4997
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4998 tuple = PyTuple_New(len);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
4999 if (tuple == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5000 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5001
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5002 while (--len >= 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5003 PyObject *item;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5004
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5005 PDATA_POP(self->stack, item);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5006 if (item == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5007 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5008 PyTuple_SET_ITEM(tuple, len, item);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5009 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5010 PDATA_PUSH(self->stack, tuple, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5011 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5012 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5013
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5014 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5015 load_empty_list(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5016 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5017 PyObject *list;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5018
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5019 if ((list = PyList_New(0)) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5020 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5021 PDATA_PUSH(self->stack, list, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5022 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5023 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5024
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5025 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5026 load_empty_dict(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5027 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5028 PyObject *dict;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5029
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5030 if ((dict = PyDict_New()) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5031 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5032 PDATA_PUSH(self->stack, dict, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5033 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5034 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5035
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5036 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5037 load_empty_set(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5038 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5039 PyObject *set;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5040
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5041 if ((set = PySet_New(NULL)) == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5042 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5043 PDATA_PUSH(self->stack, set, -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5044 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5045 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5046
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5047 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5048 load_list(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5049 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5050 PyObject *list;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5051 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5052
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5053 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5054 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5055
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5056 list = Pdata_poplist(self->stack, i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5057 if (list == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5058 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5059 PDATA_PUSH(self->stack, list, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5060 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5061 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5062
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5063 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5064 load_dict(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5065 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5066 PyObject *dict, *key, *value;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5067 Py_ssize_t i, j, k;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5068
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5069 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5070 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5071 j = Py_SIZE(self->stack);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5072
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5073 if ((dict = PyDict_New()) == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5074 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5075
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5076 for (k = i + 1; k < j; k += 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5077 key = self->stack->data[k - 1];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5078 value = self->stack->data[k];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5079 if (PyDict_SetItem(dict, key, value) < 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5080 Py_DECREF(dict);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5081 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5082 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5083 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5084 Pdata_clear(self->stack, i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5085 PDATA_PUSH(self->stack, dict, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5086 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5087 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5088
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5089 static int
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5090 load_frozenset(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5091 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5092 PyObject *items;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5093 PyObject *frozenset;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5094 Py_ssize_t i;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5095
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5096 if ((i = marker(self)) < 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5097 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5098
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5099 items = Pdata_poptuple(self->stack, i);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5100 if (items == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5101 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5102
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5103 frozenset = PyFrozenSet_New(items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5104 Py_DECREF(items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5105 if (frozenset == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5106 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5107
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5108 PDATA_PUSH(self->stack, frozenset, -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5109 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5110 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5111
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5112 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5113 instantiate(PyObject *cls, PyObject *args)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5114 {
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5115 PyObject *result = NULL;
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
5116 _Py_IDENTIFIER(__getinitargs__);
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5117 /* Caller must assure args are a tuple. Normally, args come from
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5118 Pdata_poptuple which packs objects from the top of the stack
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5119 into a newly created tuple. */
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5120 assert(PyTuple_Check(args));
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5121 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
5122 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5123 result = PyObject_CallObject(cls, args);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5124 }
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5125 else {
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
5126 _Py_IDENTIFIER(__new__);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
5127
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72640
diff changeset
5128 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5129 }
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5130 return result;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5131 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5132
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5133 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5134 load_obj(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5135 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5136 PyObject *cls, *args, *obj = NULL;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5137 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5138
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5139 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5140 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5141
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5142 args = Pdata_poptuple(self->stack, i + 1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5143 if (args == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5144 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5145
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5146 PDATA_POP(self->stack, cls);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5147 if (cls) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5148 obj = instantiate(cls, args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5149 Py_DECREF(cls);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5150 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5151 Py_DECREF(args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5152 if (obj == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5153 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5154
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5155 PDATA_PUSH(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5156 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5157 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5158
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5159 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5160 load_inst(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5161 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5162 PyObject *cls = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5163 PyObject *args = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5164 PyObject *obj = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5165 PyObject *module_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5166 PyObject *class_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5167 Py_ssize_t len;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5168 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5169 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5170
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5171 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5172 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5173 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5174 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5175 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5176 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5177
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5178 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5179 identifiers are permitted in Python 3.0, since the INST opcode is only
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5180 supported by older protocols on Python 2.x. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5181 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5182 if (module_name == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5183 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5184
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5185 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5186 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5187 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5188 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
5189 if (class_name != NULL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5190 cls = find_class(self, module_name, class_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5191 Py_DECREF(class_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5192 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5193 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5194 Py_DECREF(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5195
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5196 if (cls == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5197 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5198
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5199 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5200 obj = instantiate(cls, args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5201 Py_DECREF(args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5202 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5203 Py_DECREF(cls);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5204
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5205 if (obj == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5206 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5207
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5208 PDATA_PUSH(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5209 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5210 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5211
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5212 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5213 load_newobj(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5214 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5215 PyObject *args = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5216 PyObject *clsraw = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5217 PyTypeObject *cls; /* clsraw cast to its true type */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5218 PyObject *obj;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5219 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5220
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5221 /* Stack is ... cls argtuple, and we want to call
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5222 * cls.__new__(cls, *argtuple).
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5223 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5224 PDATA_POP(self->stack, args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5225 if (args == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5226 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5227 if (!PyTuple_Check(args)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5228 PyErr_SetString(st->UnpicklingError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5229 "NEWOBJ expected an arg " "tuple.");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5230 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5231 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5232
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5233 PDATA_POP(self->stack, clsraw);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5234 cls = (PyTypeObject *)clsraw;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5235 if (cls == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5236 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5237 if (!PyType_Check(cls)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5238 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5239 "isn't a type object");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5240 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5241 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5242 if (cls->tp_new == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5243 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5244 "has NULL tp_new");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5245 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5246 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5247
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5248 /* Call __new__. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5249 obj = cls->tp_new(cls, args, NULL);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5250 if (obj == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5251 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5252
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5253 Py_DECREF(args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5254 Py_DECREF(clsraw);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5255 PDATA_PUSH(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5256 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5257
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5258 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5259 Py_XDECREF(args);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5260 Py_XDECREF(clsraw);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5261 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5262 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5263
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5264 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5265 load_newobj_ex(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5266 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5267 PyObject *cls, *args, *kwargs;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5268 PyObject *obj;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5269 PickleState *st = _Pickle_GetGlobalState();
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5270
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5271 PDATA_POP(self->stack, kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5272 if (kwargs == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5273 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5274 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5275 PDATA_POP(self->stack, args);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5276 if (args == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5277 Py_DECREF(kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5278 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5279 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5280 PDATA_POP(self->stack, cls);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5281 if (cls == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5282 Py_DECREF(kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5283 Py_DECREF(args);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5284 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5285 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5286
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5287 if (!PyType_Check(cls)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5288 Py_DECREF(kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5289 Py_DECREF(args);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5290 Py_DECREF(cls);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5291 PyErr_Format(st->UnpicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5292 "NEWOBJ_EX class argument must be a type, not %.200s",
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5293 Py_TYPE(cls)->tp_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5294 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5295 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5296
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5297 if (((PyTypeObject *)cls)->tp_new == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5298 Py_DECREF(kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5299 Py_DECREF(args);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5300 Py_DECREF(cls);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5301 PyErr_SetString(st->UnpicklingError,
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5302 "NEWOBJ_EX class argument doesn't have __new__");
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5303 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5304 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5305 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5306 Py_DECREF(kwargs);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5307 Py_DECREF(args);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5308 Py_DECREF(cls);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5309 if (obj == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5310 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5311 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5312 PDATA_PUSH(self->stack, obj, -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5313 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5314 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5315
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5316 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5317 load_global(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5318 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5319 PyObject *global = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5320 PyObject *module_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5321 PyObject *global_name;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5322 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5323 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5324
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5325 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5326 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5327 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5328 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5329 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5330 if (!module_name)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5331 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5332
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5333 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5334 if (len < 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5335 Py_DECREF(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5336 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5337 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5338 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5339 if (global_name) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5340 global = find_class(self, module_name, global_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5341 Py_DECREF(global_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5342 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5343 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5344 Py_DECREF(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5345
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5346 if (global == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5347 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5348 PDATA_PUSH(self->stack, global, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5349 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5350 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5351
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5352 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5353 load_stack_global(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5354 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5355 PyObject *global;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5356 PyObject *module_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5357 PyObject *global_name;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5358
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5359 PDATA_POP(self->stack, global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5360 PDATA_POP(self->stack, module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5361 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5362 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5363 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5364 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5365 Py_XDECREF(global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5366 Py_XDECREF(module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5367 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5368 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5369 global = find_class(self, module_name, global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5370 Py_DECREF(global_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5371 Py_DECREF(module_name);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5372 if (global == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5373 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5374 PDATA_PUSH(self->stack, global, -1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5375 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5376 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5377
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5378 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5379 load_persid(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5380 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5381 PyObject *pid;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5382 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5383 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5384
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5385 if (self->pers_func) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5386 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5387 return -1;
87656
b92f9eaedb76 Fixed _pickle.Unpickler to handle empty persistent IDs correctly.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 85418
diff changeset
5388 if (len < 1)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5389 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5390
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5391 pid = PyBytes_FromStringAndSize(s, len - 1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5392 if (pid == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5393 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5394
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5395 /* This does not leak since _Pickle_FastCall() steals the reference
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5396 to pid first. */
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5397 pid = _Pickle_FastCall(self->pers_func, pid);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5398 if (pid == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5399 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5400
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5401 PDATA_PUSH(self->stack, pid, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5402 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5403 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5404 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5405 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5406 PyErr_SetString(st->UnpicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5407 "A load persistent id instruction was encountered,\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5408 "but no persistent_load function was specified.");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5409 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5410 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5411 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5412
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5413 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5414 load_binpersid(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5415 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5416 PyObject *pid;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5417
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5418 if (self->pers_func) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5419 PDATA_POP(self->stack, pid);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5420 if (pid == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5421 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5422
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5423 /* This does not leak since _Pickle_FastCall() steals the
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5424 reference to pid first. */
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5425 pid = _Pickle_FastCall(self->pers_func, pid);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5426 if (pid == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5427 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5428
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5429 PDATA_PUSH(self->stack, pid, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5430 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5431 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5432 else {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5433 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5434 PyErr_SetString(st->UnpicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5435 "A load persistent id instruction was encountered,\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5436 "but no persistent_load function was specified.");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5437 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5438 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5439 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5440
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5441 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5442 load_pop(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5443 {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5444 Py_ssize_t len = Py_SIZE(self->stack);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5445
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5446 /* Note that we split the (pickle.py) stack into two stacks,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5447 * an object stack and a mark stack. We have to be clever and
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5448 * pop the right one. We do this by looking at the top of the
54355
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5449 * mark stack first, and only signalling a stack underflow if
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5450 * the object stack is empty and the mark stack doesn't match
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5451 * our expectations.
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5452 */
54355
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5453 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5454 self->num_marks--;
58186
8903ea6a9e64 Merged revisions 77352-77354 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 54584
diff changeset
5455 } else if (len > 0) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5456 len--;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5457 Py_DECREF(self->stack->data[len]);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5458 Py_SIZE(self->stack) = len;
54355
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5459 } else {
9012ff7d75a2 Merged revisions 72930 via svnmerge from
Collin Winter <collinw@gmail.com>
parents: 54331
diff changeset
5460 return stack_underflow();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5461 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5462 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5463 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5464
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5465 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5466 load_pop_mark(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5467 {
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5468 Py_ssize_t i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5469
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5470 if ((i = marker(self)) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5471 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5472
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5473 Pdata_clear(self->stack, i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5474
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5475 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5476 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5477
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5478 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5479 load_dup(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5480 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5481 PyObject *last;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5482 Py_ssize_t len;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5483
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5484 if ((len = Py_SIZE(self->stack)) <= 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5485 return stack_underflow();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5486 last = self->stack->data[len - 1];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5487 PDATA_APPEND(self->stack, last, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5488 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5489 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5490
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5491 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5492 load_get(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5493 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5494 PyObject *key, *value;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5495 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5496 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5497 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5498
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5499 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5500 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5501 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5502 return bad_readline();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5503
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5504 key = PyLong_FromString(s, NULL, 10);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5505 if (key == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5506 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5507 idx = PyLong_AsSsize_t(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5508 if (idx == -1 && PyErr_Occurred()) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5509 Py_DECREF(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5510 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5511 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5512
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5513 value = _Unpickler_MemoGet(self, idx);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5514 if (value == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5515 if (!PyErr_Occurred())
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5516 PyErr_SetObject(PyExc_KeyError, key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5517 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5518 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5519 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5520 Py_DECREF(key);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5521
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5522 PDATA_APPEND(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5523 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5524 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5525
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5526 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5527 load_binget(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5528 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5529 PyObject *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5530 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5531 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5532
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5533 if (_Unpickler_Read(self, &s, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5534 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5535
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5536 idx = Py_CHARMASK(s[0]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5537
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5538 value = _Unpickler_MemoGet(self, idx);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5539 if (value == NULL) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5540 PyObject *key = PyLong_FromSsize_t(idx);
84844
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5541 if (key != NULL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5542 PyErr_SetObject(PyExc_KeyError, key);
84844
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5543 Py_DECREF(key);
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5544 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5545 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5546 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5547
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5548 PDATA_APPEND(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5549 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5550 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5551
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5552 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5553 load_long_binget(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5554 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5555 PyObject *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5556 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5557 char *s;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5558
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5559 if (_Unpickler_Read(self, &s, 4) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5560 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5561
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5562 idx = calc_binsize(s, 4);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5563
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5564 value = _Unpickler_MemoGet(self, idx);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5565 if (value == NULL) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5566 PyObject *key = PyLong_FromSsize_t(idx);
84844
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5567 if (key != NULL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5568 PyErr_SetObject(PyExc_KeyError, key);
84844
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5569 Py_DECREF(key);
dbdd07657e23 Issue #18559: Fix NULL pointer dereference error in _pickle module
Christian Heimes <christian@cheimes.de>
parents: 84409
diff changeset
5570 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5571 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5572 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5573
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5574 PDATA_APPEND(self->stack, value, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5575 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5576 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5577
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5578 /* Push an object from the extension registry (EXT[124]). nbytes is
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5579 * the number of bytes following the opcode, holding the index (code) value.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5580 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5581 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5582 load_extension(UnpicklerObject *self, int nbytes)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5583 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5584 char *codebytes; /* the nbytes bytes after the opcode */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5585 long code; /* calc_binint returns long */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5586 PyObject *py_code; /* code as a Python int */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5587 PyObject *obj; /* the object to push */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5588 PyObject *pair; /* (module_name, class_name) */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5589 PyObject *module_name, *class_name;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5590 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5591
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5592 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5593 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5594 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5595 code = calc_binint(codebytes, nbytes);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5596 if (code <= 0) { /* note that 0 is forbidden */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5597 /* Corrupt or hostile pickle. */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5598 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5599 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5600 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5601
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5602 /* Look for the code in the cache. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5603 py_code = PyLong_FromLong(code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5604 if (py_code == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5605 return -1;
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5606 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5607 if (obj != NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5608 /* Bingo. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5609 Py_DECREF(py_code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5610 PDATA_APPEND(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5611 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5612 }
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5613 if (PyErr_Occurred()) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5614 Py_DECREF(py_code);
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5615 return -1;
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5616 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5617
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5618 /* Look up the (module_name, class_name) pair. */
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5619 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5620 if (pair == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5621 Py_DECREF(py_code);
87632
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5622 if (!PyErr_Occurred()) {
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5623 PyErr_Format(PyExc_ValueError, "unregistered extension "
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5624 "code %ld", code);
d829111a27e5 Use PyDict_GetItemWithError instead of PyDict_GetItem in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87631
diff changeset
5625 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5626 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5627 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5628 /* Since the extension registry is manipulable via Python code,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5629 * confirm that pair is really a 2-tuple of strings.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5630 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5631 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5632 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5633 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5634 Py_DECREF(py_code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5635 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5636 "isn't a 2-tuple of strings", code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5637 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5638 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5639 /* Load the object. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5640 obj = find_class(self, module_name, class_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5641 if (obj == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5642 Py_DECREF(py_code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5643 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5644 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5645 /* Cache code -> obj. */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5646 code = PyDict_SetItem(st->extension_cache, py_code, obj);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5647 Py_DECREF(py_code);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5648 if (code < 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5649 Py_DECREF(obj);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5650 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5651 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5652 PDATA_PUSH(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5653 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5654 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5655
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5656 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5657 load_put(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5658 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5659 PyObject *key, *value;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5660 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5661 Py_ssize_t len;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5662 char *s;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5663
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5664 if ((len = _Unpickler_Readline(self, &s)) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5665 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5666 if (len < 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5667 return bad_readline();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5668 if (Py_SIZE(self->stack) <= 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5669 return stack_underflow();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5670 value = self->stack->data[Py_SIZE(self->stack) - 1];
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5671
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5672 key = PyLong_FromString(s, NULL, 10);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5673 if (key == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5674 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5675 idx = PyLong_AsSsize_t(key);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5676 Py_DECREF(key);
72121
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5677 if (idx < 0) {
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5678 if (!PyErr_Occurred())
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5679 PyErr_SetString(PyExc_ValueError,
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5680 "negative PUT argument");
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5681 return -1;
72121
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5682 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5683
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5684 return _Unpickler_MemoPut(self, idx, value);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5685 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5686
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5687 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5688 load_binput(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5689 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5690 PyObject *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5691 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5692 char *s;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5693
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5694 if (_Unpickler_Read(self, &s, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5695 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5696
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5697 if (Py_SIZE(self->stack) <= 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5698 return stack_underflow();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5699 value = self->stack->data[Py_SIZE(self->stack) - 1];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5700
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5701 idx = Py_CHARMASK(s[0]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5702
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5703 return _Unpickler_MemoPut(self, idx, value);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5704 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5705
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5706 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5707 load_long_binput(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5708 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5709 PyObject *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5710 Py_ssize_t idx;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5711 char *s;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5712
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5713 if (_Unpickler_Read(self, &s, 4) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5714 return -1;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5715
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5716 if (Py_SIZE(self->stack) <= 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5717 return stack_underflow();
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5718 value = self->stack->data[Py_SIZE(self->stack) - 1];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5719
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5720 idx = calc_binsize(s, 4);
72121
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5721 if (idx < 0) {
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5722 PyErr_SetString(PyExc_ValueError,
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5723 "negative LONG_BINPUT argument");
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5724 return -1;
0d9e4ce1c010 Issue #12847: Fix a crash with negative PUT and LONG_BINPUT arguments in
Antoine Pitrou <solipsis@pitrou.net>
parents: 72119
diff changeset
5725 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5726
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5727 return _Unpickler_MemoPut(self, idx, value);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5728 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5729
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5730 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5731 load_memoize(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5732 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5733 PyObject *value;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5734
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5735 if (Py_SIZE(self->stack) <= 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5736 return stack_underflow();
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5737 value = self->stack->data[Py_SIZE(self->stack) - 1];
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5738
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5739 return _Unpickler_MemoPut(self, self->memo_len, value);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5740 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5741
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5742 static int
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5743 do_append(UnpicklerObject *self, Py_ssize_t x)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5744 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5745 PyObject *value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5746 PyObject *list;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5747 Py_ssize_t len, i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5748
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5749 len = Py_SIZE(self->stack);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5750 if (x > len || x <= 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5751 return stack_underflow();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5752 if (len == x) /* nothing to do */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5753 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5754
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5755 list = self->stack->data[x - 1];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5756
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5757 if (PyList_Check(list)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5758 PyObject *slice;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5759 Py_ssize_t list_len;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5760 int ret;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5761
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5762 slice = Pdata_poplist(self->stack, x);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5763 if (!slice)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5764 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5765 list_len = PyList_GET_SIZE(list);
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5766 ret = PyList_SetSlice(list, list_len, list_len, slice);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5767 Py_DECREF(slice);
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5768 return ret;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5769 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5770 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5771 PyObject *append_func;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
5772 _Py_IDENTIFIER(append);
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
5773
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
5774 append_func = _PyObject_GetAttrId(list, &PyId_append);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5775 if (append_func == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5776 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5777 for (i = x; i < len; i++) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5778 PyObject *result;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5779
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5780 value = self->stack->data[i];
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5781 result = _Pickle_FastCall(append_func, value);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5782 if (result == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5783 Pdata_clear(self->stack, i + 1);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5784 Py_SIZE(self->stack) = x;
83478
54a44184bada Fix reference leak in _pickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83400
diff changeset
5785 Py_DECREF(append_func);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5786 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5787 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5788 Py_DECREF(result);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5789 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5790 Py_SIZE(self->stack) = x;
83478
54a44184bada Fix reference leak in _pickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 83400
diff changeset
5791 Py_DECREF(append_func);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5792 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5793
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5794 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5795 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5796
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5797 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5798 load_append(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5799 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5800 return do_append(self, Py_SIZE(self->stack) - 1);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5801 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5802
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5803 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5804 load_appends(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5805 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5806 return do_append(self, marker(self));
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5807 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5808
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5809 static int
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5810 do_setitems(UnpicklerObject *self, Py_ssize_t x)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5811 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5812 PyObject *value, *key;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5813 PyObject *dict;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
5814 Py_ssize_t len, i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5815 int status = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5816
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5817 len = Py_SIZE(self->stack);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5818 if (x > len || x <= 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5819 return stack_underflow();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5820 if (len == x) /* nothing to do */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5821 return 0;
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
5822 if ((len - x) % 2 != 0) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5823 PickleState *st = _Pickle_GetGlobalState();
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5824 /* Currupt or hostile pickle -- we never write one like this. */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5825 PyErr_SetString(st->UnpicklingError,
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5826 "odd number of items for SETITEMS");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5827 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5828 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5829
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5830 /* Here, dict does not actually need to be a PyDict; it could be anything
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5831 that supports the __setitem__ attribute. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5832 dict = self->stack->data[x - 1];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5833
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5834 for (i = x + 1; i < len; i += 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5835 key = self->stack->data[i - 1];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5836 value = self->stack->data[i];
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5837 if (PyObject_SetItem(dict, key, value) < 0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5838 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5839 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5840 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5841 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5842
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5843 Pdata_clear(self->stack, x);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5844 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5845 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5846
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5847 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5848 load_setitem(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5849 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5850 return do_setitems(self, Py_SIZE(self->stack) - 2);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5851 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5852
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5853 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5854 load_setitems(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5855 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5856 return do_setitems(self, marker(self));
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5857 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5858
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5859 static int
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5860 load_additems(UnpicklerObject *self)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5861 {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5862 PyObject *set;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5863 Py_ssize_t mark, len, i;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5864
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5865 mark = marker(self);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5866 len = Py_SIZE(self->stack);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5867 if (mark > len || mark <= 0)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5868 return stack_underflow();
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5869 if (len == mark) /* nothing to do */
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5870 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5871
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5872 set = self->stack->data[mark - 1];
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5873
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5874 if (PySet_Check(set)) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5875 PyObject *items;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5876 int status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5877
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5878 items = Pdata_poptuple(self->stack, mark);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5879 if (items == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5880 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5881
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5882 status = _PySet_Update(set, items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5883 Py_DECREF(items);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5884 return status;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5885 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5886 else {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5887 PyObject *add_func;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5888 _Py_IDENTIFIER(add);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5889
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5890 add_func = _PyObject_GetAttrId(set, &PyId_add);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5891 if (add_func == NULL)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5892 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5893 for (i = mark; i < len; i++) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5894 PyObject *result;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5895 PyObject *item;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5896
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5897 item = self->stack->data[i];
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5898 result = _Pickle_FastCall(add_func, item);
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5899 if (result == NULL) {
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5900 Pdata_clear(self->stack, i + 1);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5901 Py_SIZE(self->stack) = mark;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5902 return -1;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5903 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5904 Py_DECREF(result);
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5905 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5906 Py_SIZE(self->stack) = mark;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5907 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5908
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5909 return 0;
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5910 }
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5911
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
5912 static int
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5913 load_build(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5914 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5915 PyObject *state, *inst, *slotstate;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5916 PyObject *setstate;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5917 int status = 0;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
5918 _Py_IDENTIFIER(__setstate__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5919
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5920 /* Stack is ... instance, state. We want to leave instance at
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5921 * the stack top, possibly mutated via instance.__setstate__(state).
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5922 */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5923 if (Py_SIZE(self->stack) < 2)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5924 return stack_underflow();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5925
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5926 PDATA_POP(self->stack, state);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5927 if (state == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5928 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5929
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
5930 inst = self->stack->data[Py_SIZE(self->stack) - 1];
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5931
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
5932 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
48696
e961cc014619 Issue 3514: Fixed segfault dues to infinite loop in __getattr__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 48660
diff changeset
5933 if (setstate == NULL) {
e961cc014619 Issue 3514: Fixed segfault dues to infinite loop in __getattr__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 48660
diff changeset
5934 if (PyErr_ExceptionMatches(PyExc_AttributeError))
e961cc014619 Issue 3514: Fixed segfault dues to infinite loop in __getattr__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 48660
diff changeset
5935 PyErr_Clear();
49023
fdd93bfa6c1b Issue #3660 (part of): fix a memory leak in _pickle.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48862
diff changeset
5936 else {
fdd93bfa6c1b Issue #3660 (part of): fix a memory leak in _pickle.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48862
diff changeset
5937 Py_DECREF(state);
48696
e961cc014619 Issue 3514: Fixed segfault dues to infinite loop in __getattr__.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 48660
diff changeset
5938 return -1;
49023
fdd93bfa6c1b Issue #3660 (part of): fix a memory leak in _pickle.
Antoine Pitrou <solipsis@pitrou.net>
parents: 48862
diff changeset
5939 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5940 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5941 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5942 PyObject *result;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5943
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5944 /* The explicit __setstate__ is responsible for everything. */
87612
23459df0753e Combine the FastCall functions in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87564
diff changeset
5945 result = _Pickle_FastCall(setstate, state);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5946 Py_DECREF(setstate);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5947 if (result == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5948 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5949 Py_DECREF(result);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5950 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5951 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5952
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5953 /* A default __setstate__. First see whether state embeds a
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5954 * slot state dict too (a proto 2 addition).
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5955 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5956 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5957 PyObject *tmp = state;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5958
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5959 state = PyTuple_GET_ITEM(tmp, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5960 slotstate = PyTuple_GET_ITEM(tmp, 1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5961 Py_INCREF(state);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5962 Py_INCREF(slotstate);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5963 Py_DECREF(tmp);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5964 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5965 else
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5966 slotstate = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5967
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5968 /* Set inst.__dict__ from the state dict (if any). */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5969 if (state != Py_None) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5970 PyObject *dict;
53694
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5971 PyObject *d_key, *d_value;
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5972 Py_ssize_t i;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72918
diff changeset
5973 _Py_IDENTIFIER(__dict__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5974
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5975 if (!PyDict_Check(state)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5976 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
5977 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5978 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5979 }
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
5980 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5981 if (dict == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5982 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5983
53694
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5984 i = 0;
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5985 while (PyDict_Next(state, &i, &d_key, &d_value)) {
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5986 /* normally the keys for instance attributes are
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5987 interned. we should try to do that here. */
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5988 Py_INCREF(d_key);
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5989 if (PyUnicode_CheckExact(d_key))
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5990 PyUnicode_InternInPlace(&d_key);
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5991 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5992 Py_DECREF(d_key);
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5993 goto error;
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5994 }
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5995 Py_DECREF(d_key);
64d9a9e7f68e Merged revisions 72223 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53192
diff changeset
5996 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5997 Py_DECREF(dict);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5998 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
5999
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6000 /* Also set instance attributes from the slotstate dict (if any). */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6001 if (slotstate != NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6002 PyObject *d_key, *d_value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6003 Py_ssize_t i;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6004
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6005 if (!PyDict_Check(slotstate)) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6006 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6007 PyErr_SetString(st->UnpicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6008 "slot state is not a dictionary");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6009 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6010 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6011 i = 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6012 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6013 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6014 goto error;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6015 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6016 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6017
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6018 if (0) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6019 error:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6020 status = -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6021 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6022
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6023 Py_DECREF(state);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6024 Py_XDECREF(slotstate);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6025 return status;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6026 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6027
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6028 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6029 load_mark(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6030 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6031
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6032 /* Note that we split the (pickle.py) stack into two stacks, an
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6033 * object stack and a mark stack. Here we push a mark onto the
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6034 * mark stack.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6035 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6036
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6037 if ((self->num_marks + 1) >= self->marks_size) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6038 size_t alloc;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
6039 Py_ssize_t *marks;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6040
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6041 /* Use the size_t type to check for overflow. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6042 alloc = ((size_t)self->num_marks << 1) + 20;
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
6043 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
47670
13071add8f48 Fixed compiler warnings on MSVC9.0
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47645
diff changeset
6044 alloc <= ((size_t)self->num_marks + 1)) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6045 PyErr_NoMemory();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6046 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6047 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6048
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6049 if (self->marks == NULL)
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
6050 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6051 else
72119
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
6052 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
babc90f3cbf4 Issue #11564: Avoid crashes when trying to pickle huge objects or containers
Antoine Pitrou <solipsis@pitrou.net>
parents: 71817
diff changeset
6053 alloc * sizeof(Py_ssize_t));
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6054 if (marks == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6055 PyErr_NoMemory();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6056 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6057 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6058 self->marks = marks;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6059 self->marks_size = (Py_ssize_t)alloc;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6060 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6061
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6062 self->marks[self->num_marks++] = Py_SIZE(self->stack);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6063
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6064 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6065 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6066
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6067 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6068 load_reduce(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6069 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6070 PyObject *callable = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6071 PyObject *argtup = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6072 PyObject *obj = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6073
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6074 PDATA_POP(self->stack, argtup);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6075 if (argtup == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6076 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6077 PDATA_POP(self->stack, callable);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6078 if (callable) {
63061
3c78b3cfd462 Issue #5180: Fixed a bug that prevented loading 2.x pickles in 3.x
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 60577
diff changeset
6079 obj = PyObject_CallObject(callable, argtup);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6080 Py_DECREF(callable);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6081 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6082 Py_DECREF(argtup);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6083
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6084 if (obj == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6085 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6086
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6087 PDATA_PUSH(self->stack, obj, -1);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6088 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6089 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6090
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6091 /* Just raises an error if we don't know the protocol specified. PROTO
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6092 * is the first opcode for protocols >= 2.
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6093 */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6094 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6095 load_proto(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6096 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6097 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6098 int i;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6099
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6100 if (_Unpickler_Read(self, &s, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6101 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6102
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6103 i = (unsigned char)s[0];
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6104 if (i <= HIGHEST_PROTOCOL) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6105 self->proto = i;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6106 return 0;
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6107 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6108
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6109 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6110 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6111 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6112
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6113 static int
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6114 load_frame(UnpicklerObject *self)
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6115 {
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6116 char *s;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6117 Py_ssize_t frame_len;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6118
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6119 if (_Unpickler_Read(self, &s, 8) < 0)
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6120 return -1;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6121
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6122 frame_len = calc_binsize(s, 8);
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6123 if (frame_len < 0) {
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6124 PyErr_Format(PyExc_OverflowError,
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6125 "FRAME length exceeds system's maximum of %zd bytes",
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6126 PY_SSIZE_T_MAX);
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6127 return -1;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6128 }
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6129
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6130 if (_Unpickler_Read(self, &s, frame_len) < 0)
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6131 return -1;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6132
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6133 /* Rewind to start of frame */
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6134 self->next_read_idx -= frame_len;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6135 return 0;
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6136 }
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6137
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6138 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6139 load(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6140 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6141 PyObject *value = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6142 char *s;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6143
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6144 self->num_marks = 0;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6145 self->proto = 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6146 if (Py_SIZE(self->stack))
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6147 Pdata_clear(self->stack, 0);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6148
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6149 /* Convenient macros for the dispatch while-switch loop just below. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6150 #define OP(opcode, load_func) \
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6151 case opcode: if (load_func(self) < 0) break; continue;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6152
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6153 #define OP_ARG(opcode, load_func, arg) \
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6154 case opcode: if (load_func(self, (arg)) < 0) break; continue;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6155
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6156 while (1) {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6157 if (_Unpickler_Read(self, &s, 1) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6158 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6159
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6160 switch ((enum opcode)s[0]) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6161 OP(NONE, load_none)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6162 OP(BININT, load_binint)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6163 OP(BININT1, load_binint1)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6164 OP(BININT2, load_binint2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6165 OP(INT, load_int)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6166 OP(LONG, load_long)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6167 OP_ARG(LONG1, load_counted_long, 1)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6168 OP_ARG(LONG4, load_counted_long, 4)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6169 OP(FLOAT, load_float)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6170 OP(BINFLOAT, load_binfloat)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6171 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6172 OP_ARG(BINBYTES, load_counted_binbytes, 4)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6173 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6174 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6175 OP_ARG(BINSTRING, load_counted_binstring, 4)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6176 OP(STRING, load_string)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6177 OP(UNICODE, load_unicode)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6178 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6179 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6180 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6181 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6182 OP_ARG(TUPLE1, load_counted_tuple, 1)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6183 OP_ARG(TUPLE2, load_counted_tuple, 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6184 OP_ARG(TUPLE3, load_counted_tuple, 3)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6185 OP(TUPLE, load_tuple)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6186 OP(EMPTY_LIST, load_empty_list)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6187 OP(LIST, load_list)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6188 OP(EMPTY_DICT, load_empty_dict)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6189 OP(DICT, load_dict)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6190 OP(EMPTY_SET, load_empty_set)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6191 OP(ADDITEMS, load_additems)
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6192 OP(FROZENSET, load_frozenset)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6193 OP(OBJ, load_obj)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6194 OP(INST, load_inst)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6195 OP(NEWOBJ, load_newobj)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6196 OP(NEWOBJ_EX, load_newobj_ex)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6197 OP(GLOBAL, load_global)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6198 OP(STACK_GLOBAL, load_stack_global)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6199 OP(APPEND, load_append)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6200 OP(APPENDS, load_appends)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6201 OP(BUILD, load_build)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6202 OP(DUP, load_dup)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6203 OP(BINGET, load_binget)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6204 OP(LONG_BINGET, load_long_binget)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6205 OP(GET, load_get)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6206 OP(MARK, load_mark)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6207 OP(BINPUT, load_binput)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6208 OP(LONG_BINPUT, load_long_binput)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6209 OP(PUT, load_put)
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6210 OP(MEMOIZE, load_memoize)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6211 OP(POP, load_pop)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6212 OP(POP_MARK, load_pop_mark)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6213 OP(SETITEM, load_setitem)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6214 OP(SETITEMS, load_setitems)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6215 OP(PERSID, load_persid)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6216 OP(BINPERSID, load_binpersid)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6217 OP(REDUCE, load_reduce)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6218 OP(PROTO, load_proto)
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6219 OP(FRAME, load_frame)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6220 OP_ARG(EXT1, load_extension, 1)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6221 OP_ARG(EXT2, load_extension, 2)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6222 OP_ARG(EXT4, load_extension, 4)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6223 OP_ARG(NEWTRUE, load_bool, Py_True)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6224 OP_ARG(NEWFALSE, load_bool, Py_False)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6225
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6226 case STOP:
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6227 break;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6228
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6229 default:
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6230 if (s[0] == '\0') {
72457
db8027df8458 fix compiler compliant about \0 not being an opcode
Benjamin Peterson <benjamin@python.org>
parents: 72121
diff changeset
6231 PyErr_SetNone(PyExc_EOFError);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6232 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6233 else {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6234 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6235 PyErr_Format(st->UnpicklingError,
72457
db8027df8458 fix compiler compliant about \0 not being an opcode
Benjamin Peterson <benjamin@python.org>
parents: 72121
diff changeset
6236 "invalid load key, '%c'.", s[0]);
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6237 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6238 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6239 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6240
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6241 break; /* and we are done! */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6242 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6243
87485
de9bda43d552 Make framing optional in pickle protocol 4.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87456
diff changeset
6244 if (PyErr_Occurred()) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6245 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6246 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6247
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6248 if (_Unpickler_SkipConsumed(self) < 0)
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6249 return NULL;
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6250
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6251 PDATA_POP(self->stack, value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6252 return value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6253 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6254
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6255 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6256
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6257 _pickle.Unpickler.load
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6258
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6259 Load a pickle.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6260
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6261 Read a pickled object representation from the open file object given in
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6262 the constructor, and return the reconstituted object hierarchy specified
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6263 therein.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6264 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6265
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6266 PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6267 "load()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6268 "Load a pickle.\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6269 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6270 "Read a pickled object representation from the open file object given in\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6271 "the constructor, and return the reconstituted object hierarchy specified\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6272 "therein.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6273
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6274 #define _PICKLE_UNPICKLER_LOAD_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6275 {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6276
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6277 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6278 _pickle_Unpickler_load(PyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6279 /*[clinic checksum: 9a30ba4e4d9221d4dcd705e1471ab11b2c9e3ac6]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6280 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6281 UnpicklerObject *unpickler = (UnpicklerObject*)self;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6282
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6283 /* Check whether the Unpickler was initialized correctly. This prevents
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6284 segfaulting if a subclass overridden __init__ with a function that does
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6285 not call Unpickler.__init__(). Here, we simply ensure that self->read
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6286 is not NULL. */
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6287 if (unpickler->read == NULL) {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6288 PickleState *st = _Pickle_GetGlobalState();
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6289 PyErr_Format(st->UnpicklingError,
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6290 "Unpickler.__init__() was not called by %s.__init__()",
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6291 Py_TYPE(unpickler)->tp_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6292 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6293 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6294
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6295 return load(unpickler);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6296 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6297
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6298 /* The name of find_class() is misleading. In newer pickle protocols, this
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6299 function is used for loading any global (i.e., functions), not just
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6300 classes. The name is kept only for backward compatibility. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6301
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6302 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6303
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6304 _pickle.Unpickler.find_class
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6305
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6306 self: UnpicklerObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6307 module_name: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6308 global_name: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6309 /
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6310
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6311 Return an object from a specified module.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6312
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6313 If necessary, the module will be imported. Subclasses may override this
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6314 method (e.g. to restrict unpickling of arbitrary classes and functions).
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6315
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6316 This method is called whenever a class or a function object is
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6317 needed. Both arguments passed are str objects.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6318 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6319
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6320 PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6321 "find_class(module_name, global_name)\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6322 "Return an object from a specified module.\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6323 "\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6324 "If necessary, the module will be imported. Subclasses may override this\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6325 "method (e.g. to restrict unpickling of arbitrary classes and functions).\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6326 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6327 "This method is called whenever a class or a function object is\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6328 "needed. Both arguments passed are str objects.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6329
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6330 #define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6331 {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_VARARGS, _pickle_Unpickler_find_class__doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6332
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6333 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6334 _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6335
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6336 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6337 _pickle_Unpickler_find_class(PyObject *self, PyObject *args)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6338 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6339 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6340 PyObject *module_name;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6341 PyObject *global_name;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6342
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6343 if (!PyArg_ParseTuple(args,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6344 "OO:find_class",
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6345 &module_name, &global_name))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6346 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6347 return_value = _pickle_Unpickler_find_class_impl((UnpicklerObject *)self, module_name, global_name);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6348
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6349 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6350 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6351 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6352
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6353 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6354 _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6355 /*[clinic checksum: b7d05d4dd8adc698e5780c1ac2be0f5062d33915]*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6356 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6357 PyObject *global;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6358 PyObject *modules_dict;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6359 PyObject *module;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6360 _Py_IDENTIFIER(modules);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6361
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6362 /* Try to map the old names used in Python 2.x to the new ones used in
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6363 Python 3.x. We do this only with old pickle protocols and when the
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6364 user has not disabled the feature. */
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6365 if (self->proto < 3 && self->fix_imports) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6366 PyObject *key;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6367 PyObject *item;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6368 PickleState *st = _Pickle_GetGlobalState();
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6369
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6370 /* Check if the global (i.e., a function or a class) was renamed
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6371 or moved to another module. */
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6372 key = PyTuple_Pack(2, module_name, global_name);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6373 if (key == NULL)
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6374 return NULL;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6375 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6376 Py_DECREF(key);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6377 if (item) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6378 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6379 PyErr_Format(PyExc_RuntimeError,
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6380 "_compat_pickle.NAME_MAPPING values should be "
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6381 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6382 return NULL;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6383 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6384 module_name = PyTuple_GET_ITEM(item, 0);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6385 global_name = PyTuple_GET_ITEM(item, 1);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6386 if (!PyUnicode_Check(module_name) ||
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6387 !PyUnicode_Check(global_name)) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6388 PyErr_Format(PyExc_RuntimeError,
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6389 "_compat_pickle.NAME_MAPPING values should be "
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6390 "pairs of str, not (%.200s, %.200s)",
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6391 Py_TYPE(module_name)->tp_name,
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6392 Py_TYPE(global_name)->tp_name);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6393 return NULL;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6394 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6395 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6396 else if (PyErr_Occurred()) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6397 return NULL;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6398 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6399
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6400 /* Check if the module was renamed. */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6401 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6402 if (item) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6403 if (!PyUnicode_Check(item)) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6404 PyErr_Format(PyExc_RuntimeError,
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6405 "_compat_pickle.IMPORT_MAPPING values should be "
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6406 "strings, not %.200s", Py_TYPE(item)->tp_name);
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6407 return NULL;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6408 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6409 module_name = item;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6410 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6411 else if (PyErr_Occurred()) {
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6412 return NULL;
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6413 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6414 }
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6415
86973
6348764bacdd Issue #19512: pickle now uses an identifier to only create the Unicode string
Victor Stinner <victor.stinner@gmail.com>
parents: 86797
diff changeset
6416 modules_dict = _PySys_GetObjectId(&PyId_modules);
84676
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
6417 if (modules_dict == NULL) {
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
6418 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6419 return NULL;
84676
6bd01a59762a Issue #18408: handle PySys_GetObject() failure, raise a RuntimeError
Victor Stinner <victor.stinner@gmail.com>
parents: 84577
diff changeset
6420 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6421
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6422 module = PyDict_GetItemWithError(modules_dict, module_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6423 if (module == NULL) {
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6424 if (PyErr_Occurred())
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6425 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6426 module = PyImport_Import(module_name);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6427 if (module == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6428 return NULL;
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6429 global = getattribute(module, global_name, self->proto >= 4);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6430 Py_DECREF(module);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6431 }
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
6432 else {
87438
992ef855b3ed Issue #17810: Implement PEP 3154, pickle protocol 4.
Antoine Pitrou <solipsis@pitrou.net>
parents: 87091
diff changeset
6433 global = getattribute(module, global_name, self->proto >= 4);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6434 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6435 return global;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6436 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6437
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6438 static struct PyMethodDef Unpickler_methods[] = {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6439 _PICKLE_UNPICKLER_LOAD_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6440 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6441 {NULL, NULL} /* sentinel */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6442 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6443
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6444 static void
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6445 Unpickler_dealloc(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6446 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6447 PyObject_GC_UnTrack((PyObject *)self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6448 Py_XDECREF(self->readline);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6449 Py_XDECREF(self->read);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6450 Py_XDECREF(self->peek);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6451 Py_XDECREF(self->stack);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6452 Py_XDECREF(self->pers_func);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6453 if (self->buffer.buf != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6454 PyBuffer_Release(&self->buffer);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6455 self->buffer.buf = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6456 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6457
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6458 _Unpickler_MemoCleanup(self);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6459 PyMem_Free(self->marks);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6460 PyMem_Free(self->input_line);
84502
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
6461 PyMem_Free(self->encoding);
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
6462 PyMem_Free(self->errors);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6463
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6464 Py_TYPE(self)->tp_free((PyObject *)self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6465 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6466
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6467 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6468 Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6469 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6470 Py_VISIT(self->readline);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6471 Py_VISIT(self->read);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6472 Py_VISIT(self->peek);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6473 Py_VISIT(self->stack);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6474 Py_VISIT(self->pers_func);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6475 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6476 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6477
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6478 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6479 Unpickler_clear(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6480 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6481 Py_CLEAR(self->readline);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6482 Py_CLEAR(self->read);
65224
4613e1449000 Issue #3873: Speed up unpickling from file objects which have a peek()
Antoine Pitrou <solipsis@pitrou.net>
parents: 64588
diff changeset
6483 Py_CLEAR(self->peek);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6484 Py_CLEAR(self->stack);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6485 Py_CLEAR(self->pers_func);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6486 if (self->buffer.buf != NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6487 PyBuffer_Release(&self->buffer);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6488 self->buffer.buf = NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6489 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6490
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6491 _Unpickler_MemoCleanup(self);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6492 PyMem_Free(self->marks);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6493 self->marks = NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6494 PyMem_Free(self->input_line);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6495 self->input_line = NULL;
84502
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
6496 PyMem_Free(self->encoding);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6497 self->encoding = NULL;
84502
65f2c92ed079 Issue #18203: Add _PyMem_RawStrdup() and _PyMem_Strdup()
Victor Stinner <victor.stinner@gmail.com>
parents: 84410
diff changeset
6498 PyMem_Free(self->errors);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6499 self->errors = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6500
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6501 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6502 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6503
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6504 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6505
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6506 _pickle.Unpickler.__init__
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6507
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6508 self: UnpicklerObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6509 file: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6510 *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6511 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6512 encoding: str = 'ASCII'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6513 errors: str = 'strict'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6514
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6515 This takes a binary file for reading a pickle data stream.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6516
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6517 The protocol version of the pickle is detected automatically, so no
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6518 proto argument is needed.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6520 The file-like object must have two methods, a read() method
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6521 that takes an integer argument, and a readline() method that
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6522 requires no arguments. Both methods should return bytes.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6523 Thus file-like object can be a binary file object opened for
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6524 reading, a BytesIO object, or any other custom object that
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6525 meets this interface.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6526
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6527 Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6528 which are used to control compatiblity support for pickle stream
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6529 generated by Python 2.x. If *fix_imports* is True, pickle will try to
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6530 map the old Python 2.x names to the new names used in Python 3.x. The
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6531 *encoding* and *errors* tell pickle how to decode 8-bit string
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6532 instances pickled by Python 2.x; these default to 'ASCII' and
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6533 'strict', respectively.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6534
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6535 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6536
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6537 PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6538 "__init__(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6539 "This takes a binary file for reading a pickle data stream.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6540 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6541 "The protocol version of the pickle is detected automatically, so no\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6542 "proto argument is needed.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6543 "\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6544 "The file-like object must have two methods, a read() method\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6545 "that takes an integer argument, and a readline() method that\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6546 "requires no arguments. Both methods should return bytes.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6547 "Thus file-like object can be a binary file object opened for\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6548 "reading, a BytesIO object, or any other custom object that\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6549 "meets this interface.\n"
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6550 "\n"
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6551 "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6552 "which are used to control compatiblity support for pickle stream\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6553 "generated by Python 2.x. If *fix_imports* is True, pickle will try to\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6554 "map the old Python 2.x names to the new names used in Python 3.x. The\n"
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6555 "*encoding* and *errors* tell pickle how to decode 8-bit string\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6556 "instances pickled by Python 2.x; these default to \'ASCII\' and\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6557 "\'strict\', respectively.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6558
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6559 #define _PICKLE_UNPICKLER___INIT___METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6560 {"__init__", (PyCFunction)_pickle_Unpickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Unpickler___init____doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6561
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6562 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6563 _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6564
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6565 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6566 _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6567 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6568 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6569 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6570 PyObject *file;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6571 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6572 const char *encoding = "ASCII";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6573 const char *errors = "strict";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6574
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6575 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6576 "O|$pss:__init__", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6577 &file, &fix_imports, &encoding, &errors))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6578 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6579 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6580
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6581 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6582 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6583 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6584
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6585 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6586 _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6587 /*[clinic checksum: bed0d8bbe1c647960ccc6f997b33bf33935fa56f]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6588 {
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
6589 _Py_IDENTIFIER(persistent_load);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6590
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6591 /* In case of multiple __init__() calls, clear previous content. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6592 if (self->read != NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6593 (void)Unpickler_clear(self);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6594
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6595 if (_Unpickler_SetInputStream(self, file) < 0)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6596 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6597
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6598 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6599 return NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6600
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6601 self->fix_imports = fix_imports;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6602 if (self->fix_imports == -1)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6603 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6604
72926
db4aa878cea2 Port SetAttrString/HasAttrString to SetAttrId/GetAttrId.
Martin v. Löwis <martin@v.loewis.de>
parents: 72925
diff changeset
6605 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
72848
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
6606 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
81380082d216 Use identifier API for PyObject_GetAttrString.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
6607 &PyId_persistent_load);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6608 if (self->pers_func == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6609 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6610 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6611 else {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6612 self->pers_func = NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6613 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6614
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6615 self->stack = (Pdata *)Pdata_New();
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6616 if (self->stack == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6617 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6618
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6619 self->memo_size = 32;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6620 self->memo = _Unpickler_NewMemo(self->memo_size);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6621 if (self->memo == NULL)
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6622 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6623
54584
0805ea211122 Issue #6137: The pickle module now translates module names when loading
Antoine Pitrou <solipsis@pitrou.net>
parents: 54355
diff changeset
6624 self->proto = 0;
52761
cbfbff233ef5 Update a comment about why the __module__ can sometime be NULL.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 51270
diff changeset
6625
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6626 Py_RETURN_NONE;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6627 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6628
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6629 /* Wrap the Clinic generated signature to slot it in tp_init. */
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6630 static int
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6631 Unpickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6632 {
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6633 PyObject *result = _pickle_Unpickler___init__(self, args, kwargs);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6634 if (result == NULL) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6635 return -1;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6636 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
6637 Py_DECREF(result);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6638 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6639 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6640
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6641 /* Define a proxy object for the Unpickler's internal memo object. This is to
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6642 * avoid breaking code like:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6643 * unpickler.memo.clear()
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6644 * and
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6645 * unpickler.memo = saved_memo
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6646 * Is this a good idea? Not really, but we don't want to break code that uses
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6647 * it. Note that we don't implement the entire mapping API here. This is
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6648 * intentional, as these should be treated as black-box implementation details.
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6649 *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6650 * We do, however, have to implement pickling/unpickling support because of
72538
a4a9ba2c31ef Strip trailing spaces of _pickle.c
Victor Stinner <victor.stinner@haypocalc.com>
parents: 72475
diff changeset
6651 * real-world code like cvs2svn.
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6652 */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6653
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6654 typedef struct {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6655 PyObject_HEAD
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6656 UnpicklerObject *unpickler;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6657 } UnpicklerMemoProxyObject;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6658
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6659 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6660 _pickle.UnpicklerMemoProxy.clear
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6661
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6662 self: UnpicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6663
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6664 Remove all items from memo.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6665 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6666
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6667 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6668 "clear()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6669 "Remove all items from memo.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6670
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6671 #define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6672 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6673
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6674 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6675 _pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6676 /*[clinic checksum: 46fecf4e33c0c873124f845edf6cc3a2e9864bd5]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6677 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6678 _Unpickler_MemoCleanup(self->unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6679 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6680 if (self->unpickler->memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6681 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6682 Py_RETURN_NONE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6683 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6684
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6685 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6686 _pickle.UnpicklerMemoProxy.copy
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6687
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6688 self: UnpicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6689
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6690 Copy the memo to a new object.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6691 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6692
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6693 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6694 "copy()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6695 "Copy the memo to a new object.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6696
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6697 #define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6698 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6699
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6700 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6701 _pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6702 /*[clinic checksum: f8856c4e8a33540886dfbb245f286af3008fa0ad]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6703 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6704 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6705 PyObject *new_memo = PyDict_New();
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6706 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6707 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6708
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6709 for (i = 0; i < self->unpickler->memo_size; i++) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6710 int status;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6711 PyObject *key, *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6712
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6713 value = self->unpickler->memo[i];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6714 if (value == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6715 continue;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6716
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6717 key = PyLong_FromSsize_t(i);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6718 if (key == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6719 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6720 status = PyDict_SetItem(new_memo, key, value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6721 Py_DECREF(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6722 if (status < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6723 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6724 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6725 return new_memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6726
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6727 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6728 Py_DECREF(new_memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6729 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6730 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6731
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6732 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6733 _pickle.UnpicklerMemoProxy.__reduce__
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6734
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6735 self: UnpicklerMemoProxyObject
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6736
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6737 Implement pickling support.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6738 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6739
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6740 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6741 "__reduce__()\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6742 "Implement pickling support.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6743
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6744 #define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6745 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6746
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6747 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6748 _pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6749 /*[clinic checksum: ab5516a77659144e1191c7dd70a0c6c7455660bc]*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6750 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6751 PyObject *reduce_value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6752 PyObject *constructor_args;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6753 PyObject *contents = _pickle_UnpicklerMemoProxy_copy(self);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6754 if (contents == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6755 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6756
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6757 reduce_value = PyTuple_New(2);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6758 if (reduce_value == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6759 Py_DECREF(contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6760 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6761 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6762 constructor_args = PyTuple_New(1);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6763 if (constructor_args == NULL) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6764 Py_DECREF(contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6765 Py_DECREF(reduce_value);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6766 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6767 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6768 PyTuple_SET_ITEM(constructor_args, 0, contents);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6769 Py_INCREF((PyObject *)&PyDict_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6770 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6771 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6772 return reduce_value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6773 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6774
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6775 static PyMethodDef unpicklerproxy_methods[] = {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6776 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6777 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
6778 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6779 {NULL, NULL} /* sentinel */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6780 };
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6781
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6782 static void
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6783 UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6784 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6785 PyObject_GC_UnTrack(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6786 Py_XDECREF(self->unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6787 PyObject_GC_Del((PyObject *)self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6788 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6789
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6790 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6791 UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6792 visitproc visit, void *arg)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6793 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6794 Py_VISIT(self->unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6795 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6796 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6797
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6798 static int
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6799 UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6800 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6801 Py_CLEAR(self->unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6802 return 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6803 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6804
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6805 static PyTypeObject UnpicklerMemoProxyType = {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6806 PyVarObject_HEAD_INIT(NULL, 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6807 "_pickle.UnpicklerMemoProxy", /*tp_name*/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6808 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6809 0,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6810 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6811 0, /* tp_print */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6812 0, /* tp_getattr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6813 0, /* tp_setattr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6814 0, /* tp_compare */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6815 0, /* tp_repr */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6816 0, /* tp_as_number */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6817 0, /* tp_as_sequence */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6818 0, /* tp_as_mapping */
65513
5b069892e413 Remove more unneeded casts to hashfunc.
Georg Brandl <georg@python.org>
parents: 65494
diff changeset
6819 PyObject_HashNotImplemented, /* tp_hash */
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6820 0, /* tp_call */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6821 0, /* tp_str */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6822 PyObject_GenericGetAttr, /* tp_getattro */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6823 PyObject_GenericSetAttr, /* tp_setattro */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6824 0, /* tp_as_buffer */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6825 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6826 0, /* tp_doc */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6827 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6828 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6829 0, /* tp_richcompare */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6830 0, /* tp_weaklistoffset */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6831 0, /* tp_iter */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6832 0, /* tp_iternext */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6833 unpicklerproxy_methods, /* tp_methods */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6834 };
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6835
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6836 static PyObject *
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6837 UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6838 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6839 UnpicklerMemoProxyObject *self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6840
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6841 self = PyObject_GC_New(UnpicklerMemoProxyObject,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6842 &UnpicklerMemoProxyType);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6843 if (self == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6844 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6845 Py_INCREF(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6846 self->unpickler = unpickler;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6847 PyObject_GC_Track(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6848 return (PyObject *)self;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6849 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6850
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6851 /*****************************************************************************/
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6852
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6853
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6854 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6855 Unpickler_get_memo(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6856 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6857 return UnpicklerMemoProxy_New(self);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6858 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6859
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6860 static int
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6861 Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6862 {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6863 PyObject **new_memo;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6864 Py_ssize_t new_memo_size = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6865 Py_ssize_t i;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6866
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6867 if (obj == NULL) {
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6868 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6869 "attribute deletion is not supported");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6870 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6871 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6872
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6873 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6874 UnpicklerObject *unpickler =
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6875 ((UnpicklerMemoProxyObject *)obj)->unpickler;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6876
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6877 new_memo_size = unpickler->memo_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6878 new_memo = _Unpickler_NewMemo(new_memo_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6879 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6880 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6881
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6882 for (i = 0; i < new_memo_size; i++) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6883 Py_XINCREF(unpickler->memo[i]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6884 new_memo[i] = unpickler->memo[i];
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6885 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6886 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6887 else if (PyDict_Check(obj)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6888 Py_ssize_t i = 0;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6889 PyObject *key, *value;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6890
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6891 new_memo_size = PyDict_Size(obj);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6892 new_memo = _Unpickler_NewMemo(new_memo_size);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6893 if (new_memo == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6894 return -1;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6895
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6896 while (PyDict_Next(obj, &i, &key, &value)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6897 Py_ssize_t idx;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6898 if (!PyLong_Check(key)) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6899 PyErr_SetString(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6900 "memo key must be integers");
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6901 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6902 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6903 idx = PyLong_AsSsize_t(key);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6904 if (idx == -1 && PyErr_Occurred())
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6905 goto error;
84407
17b7af660f82 Issue #18339: Negative ints keys in unpickler.memo dict no longer cause a
Christian Heimes <christian@cheimes.de>
parents: 83478
diff changeset
6906 if (idx < 0) {
17b7af660f82 Issue #18339: Negative ints keys in unpickler.memo dict no longer cause a
Christian Heimes <christian@cheimes.de>
parents: 83478
diff changeset
6907 PyErr_SetString(PyExc_ValueError,
84409
61b6cd7b9819 Singular form just like the other error message.
Christian Heimes <christian@cheimes.de>
parents: 84407
diff changeset
6908 "memo key must be positive integers.");
84407
17b7af660f82 Issue #18339: Negative ints keys in unpickler.memo dict no longer cause a
Christian Heimes <christian@cheimes.de>
parents: 83478
diff changeset
6909 goto error;
17b7af660f82 Issue #18339: Negative ints keys in unpickler.memo dict no longer cause a
Christian Heimes <christian@cheimes.de>
parents: 83478
diff changeset
6910 }
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6911 if (_Unpickler_MemoPut(self, idx, value) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6912 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6913 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6914 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6915 else {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6916 PyErr_Format(PyExc_TypeError,
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6917 "'memo' attribute must be an UnpicklerMemoProxy object"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6918 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6919 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6920 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6921
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6922 _Unpickler_MemoCleanup(self);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6923 self->memo_size = new_memo_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6924 self->memo = new_memo;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6925
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6926 return 0;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6927
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6928 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6929 if (new_memo_size) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6930 i = new_memo_size;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6931 while (--i >= 0) {
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6932 Py_XDECREF(new_memo[i]);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6933 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6934 PyMem_FREE(new_memo);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6935 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6936 return -1;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6937 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6938
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6939 static PyObject *
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6940 Unpickler_get_persload(UnpicklerObject *self)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6941 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6942 if (self->pers_func == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6943 PyErr_SetString(PyExc_AttributeError, "persistent_load");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6944 else
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6945 Py_INCREF(self->pers_func);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6946 return self->pers_func;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6947 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6948
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6949 static int
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6950 Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6951 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6952 PyObject *tmp;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6953
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6954 if (value == NULL) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6955 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6956 "attribute deletion is not supported");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6957 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6958 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6959 if (!PyCallable_Check(value)) {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6960 PyErr_SetString(PyExc_TypeError,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6961 "persistent_load must be a callable taking "
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6962 "one argument");
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6963 return -1;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6964 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6965
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6966 tmp = self->pers_func;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6967 Py_INCREF(value);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6968 self->pers_func = value;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6969 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6970
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6971 return 0;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6972 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6973
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6974 static PyGetSetDef Unpickler_getsets[] = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6975 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6976 {"persistent_load", (getter)Unpickler_get_persload,
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6977 (setter)Unpickler_set_persload},
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6978 {NULL}
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6979 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6980
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6981 static PyTypeObject Unpickler_Type = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6982 PyVarObject_HEAD_INIT(NULL, 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6983 "_pickle.Unpickler", /*tp_name*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6984 sizeof(UnpicklerObject), /*tp_basicsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6985 0, /*tp_itemsize*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6986 (destructor)Unpickler_dealloc, /*tp_dealloc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6987 0, /*tp_print*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6988 0, /*tp_getattr*/
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
6989 0, /*tp_setattr*/
51270
debd14c489b9 Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents: 51243
diff changeset
6990 0, /*tp_reserved*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6991 0, /*tp_repr*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6992 0, /*tp_as_number*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6993 0, /*tp_as_sequence*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6994 0, /*tp_as_mapping*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6995 0, /*tp_hash*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6996 0, /*tp_call*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6997 0, /*tp_str*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6998 0, /*tp_getattro*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
6999 0, /*tp_setattro*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7000 0, /*tp_as_buffer*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7001 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7002 _pickle_Unpickler___init____doc__, /*tp_doc*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7003 (traverseproc)Unpickler_traverse, /*tp_traverse*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7004 (inquiry)Unpickler_clear, /*tp_clear*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7005 0, /*tp_richcompare*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7006 0, /*tp_weaklistoffset*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7007 0, /*tp_iter*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7008 0, /*tp_iternext*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7009 Unpickler_methods, /*tp_methods*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7010 0, /*tp_members*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7011 Unpickler_getsets, /*tp_getset*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7012 0, /*tp_base*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7013 0, /*tp_dict*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7014 0, /*tp_descr_get*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7015 0, /*tp_descr_set*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7016 0, /*tp_dictoffset*/
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7017 Unpickler_init, /*tp_init*/
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7018 PyType_GenericAlloc, /*tp_alloc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7019 PyType_GenericNew, /*tp_new*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7020 PyObject_GC_Del, /*tp_free*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7021 0, /*tp_is_gc*/
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7022 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7023
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7024 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7025
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7026 _pickle.dump
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7027
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7028 obj: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7029 file: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7030 protocol: object = NULL
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7031 *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7032 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7033
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7034 Write a pickled representation of obj to the open file object file.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7035
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7036 This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7037 efficient.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7038
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7039 The optional protocol argument tells the pickler to use the given protocol
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7040 supported protocols are 0, 1, 2, 3. The default protocol is 3; a
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7041 backward-incompatible protocol designed for Python 3.0.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7042
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7043 Specifying a negative protocol version selects the highest protocol version
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7044 supported. The higher the protocol used, the more recent the version of
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7045 Python needed to read the pickle produced.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7046
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7047 The file argument must have a write() method that accepts a single bytes
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7048 argument. It can thus be a file object opened for binary writing, a
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7049 io.BytesIO instance, or any other custom object that meets this interface.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7050
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7051 If fix_imports is True and protocol is less than 3, pickle will try to
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7052 map the new Python 3.x names to the old module names used in Python 2.x,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7053 so that the pickle data stream is readable with Python 2.x.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7054 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7055
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7056 PyDoc_STRVAR(_pickle_dump__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7057 "dump(obj, file, protocol=None, *, fix_imports=True)\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7058 "Write a pickled representation of obj to the open file object file.\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7059 "\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7060 "This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7061 "efficient.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7062 "\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7063 "The optional protocol argument tells the pickler to use the given protocol\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7064 "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7065 "backward-incompatible protocol designed for Python 3.0.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7066 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7067 "Specifying a negative protocol version selects the highest protocol version\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7068 "supported. The higher the protocol used, the more recent the version of\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7069 "Python needed to read the pickle produced.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7070 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7071 "The file argument must have a write() method that accepts a single bytes\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7072 "argument. It can thus be a file object opened for binary writing, a\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7073 "io.BytesIO instance, or any other custom object that meets this interface.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7074 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7075 "If fix_imports is True and protocol is less than 3, pickle will try to\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7076 "map the new Python 3.x names to the old module names used in Python 2.x,\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7077 "so that the pickle data stream is readable with Python 2.x.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7078
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7079 #define _PICKLE_DUMP_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7080 {"dump", (PyCFunction)_pickle_dump, METH_VARARGS|METH_KEYWORDS, _pickle_dump__doc__},
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7081
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7082 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7083 _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7084
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7085 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7086 _pickle_dump(PyModuleDef *module, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7087 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7088 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7089 static char *_keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7090 PyObject *obj;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7091 PyObject *file;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7092 PyObject *protocol = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7093 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7094
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7095 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7096 "OO|O$p:dump", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7097 &obj, &file, &protocol, &fix_imports))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7098 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7099 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7100
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7101 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7102 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7103 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7104
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7105 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7106 _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7107 /*[clinic checksum: e442721b16052d921b5e3fbd146d0a62e94a459e]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7108 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7109 PicklerObject *pickler = _Pickler_New();
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7110
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7111 if (pickler == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7112 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7113
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7114 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7115 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7116
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7117 if (_Pickler_SetOutputStream(pickler, file) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7118 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7119
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7120 if (dump(pickler, obj) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7121 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7122
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7123 if (_Pickler_FlushToFile(pickler) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7124 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7125
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7126 Py_DECREF(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7127 Py_RETURN_NONE;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7128
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7129 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7130 Py_XDECREF(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7131 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7132 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7133
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7134 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7135
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7136 _pickle.dumps
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7137
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7138 obj: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7139 protocol: object = NULL
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7140 *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7141 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7142
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7143 Return the pickled representation of the object as a bytes object.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7144
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7145 The optional protocol argument tells the pickler to use the given protocol;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7146 supported protocols are 0, 1, 2, 3. The default protocol is 3; a
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7147 backward-incompatible protocol designed for Python 3.0.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7148
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7149 Specifying a negative protocol version selects the highest protocol version
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7150 supported. The higher the protocol used, the more recent the version of
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7151 Python needed to read the pickle produced.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7152
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7153 If fix_imports is True and *protocol* is less than 3, pickle will try to
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7154 map the new Python 3.x names to the old module names used in Python 2.x,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7155 so that the pickle data stream is readable with Python 2.x.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7156 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7157
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7158 PyDoc_STRVAR(_pickle_dumps__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7159 "dumps(obj, protocol=None, *, fix_imports=True)\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7160 "Return the pickled representation of the object as a bytes object.\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7161 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7162 "The optional protocol argument tells the pickler to use the given protocol;\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7163 "supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7164 "backward-incompatible protocol designed for Python 3.0.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7165 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7166 "Specifying a negative protocol version selects the highest protocol version\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7167 "supported. The higher the protocol used, the more recent the version of\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7168 "Python needed to read the pickle produced.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7169 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7170 "If fix_imports is True and *protocol* is less than 3, pickle will try to\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7171 "map the new Python 3.x names to the old module names used in Python 2.x,\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7172 "so that the pickle data stream is readable with Python 2.x.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7173
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7174 #define _PICKLE_DUMPS_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7175 {"dumps", (PyCFunction)_pickle_dumps, METH_VARARGS|METH_KEYWORDS, _pickle_dumps__doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7176
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7177 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7178 _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7179
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7180 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7181 _pickle_dumps(PyModuleDef *module, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7182 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7183 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7184 static char *_keywords[] = {"obj", "protocol", "fix_imports", NULL};
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7185 PyObject *obj;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7186 PyObject *protocol = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7187 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7188
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7189 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7190 "O|O$p:dumps", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7191 &obj, &protocol, &fix_imports))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7192 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7193 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7194
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7195 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7196 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7197 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7198
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7199 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7200 _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7201 /*[clinic checksum: df6262c4c487f537f47aec8a1709318204c1e174]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7202 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7203 PyObject *result;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7204 PicklerObject *pickler = _Pickler_New();
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7205
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7206 if (pickler == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7207 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7208
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7209 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7210 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7211
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7212 if (dump(pickler, obj) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7213 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7214
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7215 result = _Pickler_GetString(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7216 Py_DECREF(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7217 return result;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7218
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7219 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7220 Py_XDECREF(pickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7221 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7222 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7223
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7224 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7225
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7226 _pickle.load
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7227
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7228 file: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7229 *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7230 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7231 encoding: str = 'ASCII'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7232 errors: str = 'strict'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7233
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7234 Return a reconstituted object from the pickle data stored in a file.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7235
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7236 This is equivalent to ``Unpickler(file).load()``, but may be more efficient.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7237
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7238 The protocol version of the pickle is detected automatically, so no protocol
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7239 argument is needed. Bytes past the pickled object's representation are
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7240 ignored.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7241
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7242 The argument file must have two methods, a read() method that takes an
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7243 integer argument, and a readline() method that requires no arguments. Both
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7244 methods should return bytes. Thus *file* can be a binary file object opened
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7245 for reading, a BytesIO object, or any other custom object that meets this
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7246 interface.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7247
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7248 Optional keyword arguments are fix_imports, encoding and errors,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7249 which are used to control compatiblity support for pickle stream generated
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7250 by Python 2.x. If fix_imports is True, pickle will try to map the old
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7251 Python 2.x names to the new names used in Python 3.x. The encoding and
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7252 errors tell pickle how to decode 8-bit string instances pickled by Python
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7253 2.x; these default to 'ASCII' and 'strict', respectively.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7254 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7255
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7256 PyDoc_STRVAR(_pickle_load__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7257 "load(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7258 "Return a reconstituted object from the pickle data stored in a file.\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7259 "\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7260 "This is equivalent to ``Unpickler(file).load()``, but may be more efficient.\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7261 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7262 "The protocol version of the pickle is detected automatically, so no protocol\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7263 "argument is needed. Bytes past the pickled object\'s representation are\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7264 "ignored.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7265 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7266 "The argument file must have two methods, a read() method that takes an\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7267 "integer argument, and a readline() method that requires no arguments. Both\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7268 "methods should return bytes. Thus *file* can be a binary file object opened\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7269 "for reading, a BytesIO object, or any other custom object that meets this\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7270 "interface.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7271 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7272 "Optional keyword arguments are fix_imports, encoding and errors,\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7273 "which are used to control compatiblity support for pickle stream generated\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7274 "by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7275 "Python 2.x names to the new names used in Python 3.x. The encoding and\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7276 "errors tell pickle how to decode 8-bit string instances pickled by Python\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7277 "2.x; these default to \'ASCII\' and \'strict\', respectively.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7278
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7279 #define _PICKLE_LOAD_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7280 {"load", (PyCFunction)_pickle_load, METH_VARARGS|METH_KEYWORDS, _pickle_load__doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7281
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7282 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7283 _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7284
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7285 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7286 _pickle_load(PyModuleDef *module, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7287 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7288 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7289 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7290 PyObject *file;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7291 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7292 const char *encoding = "ASCII";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7293 const char *errors = "strict";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7294
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7295 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7296 "O|$pss:load", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7297 &file, &fix_imports, &encoding, &errors))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7298 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7299 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7300
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7301 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7302 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7303 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7304
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7305 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7306 _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7307 /*[clinic checksum: e10796f6765b22ce48dca6940f11b3933853ca35]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7308 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7309 PyObject *result;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7310 UnpicklerObject *unpickler = _Unpickler_New();
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7311
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7312 if (unpickler == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7313 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7314
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7315 if (_Unpickler_SetInputStream(unpickler, file) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7316 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7317
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7318 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7319 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7320
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7321 unpickler->fix_imports = fix_imports;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7322
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7323 result = load(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7324 Py_DECREF(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7325 return result;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7326
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7327 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7328 Py_XDECREF(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7329 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7330 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7331
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7332 /*[clinic]
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7333
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7334 _pickle.loads
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7335
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7336 data: object
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7337 *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7338 fix_imports: bool = True
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7339 encoding: str = 'ASCII'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7340 errors: str = 'strict'
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7341
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7342 Return a reconstituted object from the given pickle data.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7343
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7344 The protocol version of the pickle is detected automatically, so no protocol
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7345 argument is needed. Bytes past the pickled object's representation are
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7346 ignored.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7347
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7348 Optional keyword arguments are fix_imports, encoding and errors, which
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7349 are used to control compatiblity support for pickle stream generated
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7350 by Python 2.x. If fix_imports is True, pickle will try to map the old
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7351 Python 2.x names to the new names used in Python 3.x. The encoding and
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7352 errors tell pickle how to decode 8-bit string instances pickled by Python
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7353 2.x; these default to 'ASCII' and 'strict', respectively.
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7354 [clinic]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7355
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7356 PyDoc_STRVAR(_pickle_loads__doc__,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7357 "loads(data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7358 "Return a reconstituted object from the given pickle data.\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7359 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7360 "The protocol version of the pickle is detected automatically, so no protocol\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7361 "argument is needed. Bytes past the pickled object\'s representation are\n"
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7362 "ignored.\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7363 "\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7364 "Optional keyword arguments are fix_imports, encoding and errors, which\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7365 "are used to control compatiblity support for pickle stream generated\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7366 "by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7367 "Python 2.x names to the new names used in Python 3.x. The encoding and\n"
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7368 "errors tell pickle how to decode 8-bit string instances pickled by Python\n"
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7369 "2.x; these default to \'ASCII\' and \'strict\', respectively.");
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7370
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7371 #define _PICKLE_LOADS_METHODDEF \
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7372 {"loads", (PyCFunction)_pickle_loads, METH_VARARGS|METH_KEYWORDS, _pickle_loads__doc__},
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7373
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7374 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7375 _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors);
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7376
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7377 static PyObject *
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7378 _pickle_loads(PyModuleDef *module, PyObject *args, PyObject *kwargs)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7379 {
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7380 PyObject *return_value = NULL;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7381 static char *_keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7382 PyObject *data;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7383 int fix_imports = 1;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7384 const char *encoding = "ASCII";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7385 const char *errors = "strict";
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7386
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7387 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7388 "O|$pss:loads", _keywords,
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7389 &data, &fix_imports, &encoding, &errors))
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7390 goto exit;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7391 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7392
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7393 exit:
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7394 return return_value;
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7395 }
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7396
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7397 static PyObject *
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7398 _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7399 /*[clinic checksum: 29ee725efcbf51a3533c19cb8261a8e267b7080a]*/
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7400 {
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7401 PyObject *result;
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7402 UnpicklerObject *unpickler = _Unpickler_New();
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7403
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7404 if (unpickler == NULL)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7405 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7406
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7407 if (_Unpickler_SetStringInput(unpickler, data) < 0)
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7408 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7409
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7410 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7411 goto error;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7412
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7413 unpickler->fix_imports = fix_imports;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7414
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7415 result = load(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7416 Py_DECREF(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7417 return result;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7418
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7419 error:
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7420 Py_XDECREF(unpickler);
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7421 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7422 }
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7423
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7424 static struct PyMethodDef pickle_methods[] = {
87519
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7425 _PICKLE_DUMP_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7426 _PICKLE_DUMPS_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7427 _PICKLE_LOAD_METHODDEF
ead3f5a907bd Use Clinic to process arguments in cpickle.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87496
diff changeset
7428 _PICKLE_LOADS_METHODDEF
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7429 {NULL, NULL} /* sentinel */
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7430 };
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7431
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7432 static int
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7433 pickle_clear(PyObject *m)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7434 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7435 _Pickle_ClearState(_Pickle_GetState(m));
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7436 return 0;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7437 }
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7438
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7439 static int
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7440 pickle_traverse(PyObject *m, visitproc visit, void *arg)
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7441 {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7442 PickleState *st = _Pickle_GetState(m);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7443 Py_VISIT(st->PickleError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7444 Py_VISIT(st->PicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7445 Py_VISIT(st->UnpicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7446 Py_VISIT(st->dispatch_table);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7447 Py_VISIT(st->extension_registry);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7448 Py_VISIT(st->extension_cache);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7449 Py_VISIT(st->inverted_registry);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7450 Py_VISIT(st->name_mapping_2to3);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7451 Py_VISIT(st->import_mapping_2to3);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7452 Py_VISIT(st->name_mapping_3to2);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7453 Py_VISIT(st->import_mapping_3to2);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7454 Py_VISIT(st->codecs_encode);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7455 return 0;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7456 }
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7457
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7458 static struct PyModuleDef _picklemodule = {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7459 PyModuleDef_HEAD_INIT,
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7460 "_pickle", /* m_name */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7461 pickle_module_doc, /* m_doc */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7462 sizeof(PickleState), /* m_size */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7463 pickle_methods, /* m_methods */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7464 NULL, /* m_reload */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7465 pickle_traverse, /* m_traverse */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7466 pickle_clear, /* m_clear */
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7467 NULL /* m_free */
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7468 };
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7469
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7470 PyMODINIT_FUNC
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7471 PyInit__pickle(void)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7472 {
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7473 PyObject *m;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7474 PickleState *st;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7475
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7476 m = PyState_FindModule(&_picklemodule);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7477 if (m) {
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7478 Py_INCREF(m);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7479 return m;
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7480 }
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7481
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7482 if (PyType_Ready(&Unpickler_Type) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7483 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7484 if (PyType_Ready(&Pickler_Type) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7485 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7486 if (PyType_Ready(&Pdata_Type) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7487 return NULL;
64588
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7488 if (PyType_Ready(&PicklerMemoProxyType) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7489 return NULL;
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7490 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
027f81579b4a Issue #9410: Various optimizations to the pickle module, leading to
Antoine Pitrou <solipsis@pitrou.net>
parents: 64440
diff changeset
7491 return NULL;
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7492
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7493 /* Create the module and add the functions. */
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7494 m = PyModule_Create(&_picklemodule);
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7495 if (m == NULL)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7496 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7497
71347
1ae0b7b8de0b Issue #11321: Fix a crash with multiple imports of the _pickle module when
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
7498 Py_INCREF(&Pickler_Type);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7499 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7500 return NULL;
71347
1ae0b7b8de0b Issue #11321: Fix a crash with multiple imports of the _pickle module when
Antoine Pitrou <solipsis@pitrou.net>
parents: 68586
diff changeset
7501 Py_INCREF(&Unpickler_Type);
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7502 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7503 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7504
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7505 st = _Pickle_GetState(m);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7506
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7507 /* Initialize the exceptions. */
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7508 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7509 if (st->PickleError == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7510 return NULL;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7511 st->PicklingError = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7512 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7513 if (st->PicklingError == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7514 return NULL;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7515 st->UnpicklingError = \
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7516 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7517 if (st->UnpicklingError == NULL)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7518 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7519
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7520 Py_INCREF(st->PickleError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7521 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7522 return NULL;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7523 Py_INCREF(st->PicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7524 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7525 return NULL;
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7526 Py_INCREF(st->UnpicklingError);
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7527 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7528 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7529
87620
64c6d52793be Encapsulate cpickle global state in a dedicated object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 87612
diff changeset
7530 if (_Pickle_InitState(st) < 0)
47645
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7531 return NULL;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7532
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7533 return m;
0ae50aa7d97c Restore _pickle module accelerator module.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
diff changeset
7534 }