blob: c6cb8bc7c0e47f679a9bf4a4f191eb51829022ea [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +02008output preset file
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08009module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080010class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
11class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
12class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
13class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080014[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080015/*[clinic end generated code: output=da39a3ee5e6b4b0d input=11c45248a41dd3fc]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080016
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000017/* Bump this when new opcodes are added to the pickle protocol. */
18enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010019 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000020 DEFAULT_PROTOCOL = 3
21};
22
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Pickle opcodes. These must be kept updated with pickle.py.
24 Extensive docs are in pickletools.py. */
25enum opcode {
26 MARK = '(',
27 STOP = '.',
28 POP = '0',
29 POP_MARK = '1',
30 DUP = '2',
31 FLOAT = 'F',
32 INT = 'I',
33 BININT = 'J',
34 BININT1 = 'K',
35 LONG = 'L',
36 BININT2 = 'M',
37 NONE = 'N',
38 PERSID = 'P',
39 BINPERSID = 'Q',
40 REDUCE = 'R',
41 STRING = 'S',
42 BINSTRING = 'T',
43 SHORT_BINSTRING = 'U',
44 UNICODE = 'V',
45 BINUNICODE = 'X',
46 APPEND = 'a',
47 BUILD = 'b',
48 GLOBAL = 'c',
49 DICT = 'd',
50 EMPTY_DICT = '}',
51 APPENDS = 'e',
52 GET = 'g',
53 BINGET = 'h',
54 INST = 'i',
55 LONG_BINGET = 'j',
56 LIST = 'l',
57 EMPTY_LIST = ']',
58 OBJ = 'o',
59 PUT = 'p',
60 BINPUT = 'q',
61 LONG_BINPUT = 'r',
62 SETITEM = 's',
63 TUPLE = 't',
64 EMPTY_TUPLE = ')',
65 SETITEMS = 'u',
66 BINFLOAT = 'G',
67
68 /* Protocol 2. */
69 PROTO = '\x80',
70 NEWOBJ = '\x81',
71 EXT1 = '\x82',
72 EXT2 = '\x83',
73 EXT4 = '\x84',
74 TUPLE1 = '\x85',
75 TUPLE2 = '\x86',
76 TUPLE3 = '\x87',
77 NEWTRUE = '\x88',
78 NEWFALSE = '\x89',
79 LONG1 = '\x8a',
80 LONG4 = '\x8b',
81
82 /* Protocol 3 (Python 3.x) */
83 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010084 SHORT_BINBYTES = 'C',
85
86 /* Protocol 4 */
87 SHORT_BINUNICODE = '\x8c',
88 BINUNICODE8 = '\x8d',
89 BINBYTES8 = '\x8e',
90 EMPTY_SET = '\x8f',
91 ADDITEMS = '\x90',
92 FROZENSET = '\x91',
93 NEWOBJ_EX = '\x92',
94 STACK_GLOBAL = '\x93',
95 MEMOIZE = '\x94',
96 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000097};
98
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000099enum {
100 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
101 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
102 break if this gets out of synch with pickle.py, but it's unclear that would
103 help anything either. */
104 BATCHSIZE = 1000,
105
106 /* Nesting limit until Pickler, when running in "fast mode", starts
107 checking for self-referential data-structures. */
108 FAST_NESTING_LIMIT = 50,
109
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000110 /* Initial size of the write buffer of Pickler. */
111 WRITE_BUF_SIZE = 4096,
112
Antoine Pitrou04248a82010-10-12 20:51:21 +0000113 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100114 PREFETCH = 8192 * 16,
115
116 FRAME_SIZE_TARGET = 64 * 1024,
117
118 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000119};
120
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800121/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000122
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800123/* State of the pickle module, per PEP 3121. */
124typedef struct {
125 /* Exception classes for pickle. */
126 PyObject *PickleError;
127 PyObject *PicklingError;
128 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800129
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800130 /* copyreg.dispatch_table, {type_object: pickling_function} */
131 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000132
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800133 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000134
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800135 /* copyreg._extension_registry, {(module_name, function_name): code} */
136 PyObject *extension_registry;
137 /* copyreg._extension_cache, {code: object} */
138 PyObject *extension_cache;
139 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
140 PyObject *inverted_registry;
141
142 /* Import mappings for compatibility with Python 2.x */
143
144 /* _compat_pickle.NAME_MAPPING,
145 {(oldmodule, oldname): (newmodule, newname)} */
146 PyObject *name_mapping_2to3;
147 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
148 PyObject *import_mapping_2to3;
149 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
150 PyObject *name_mapping_3to2;
151 PyObject *import_mapping_3to2;
152
153 /* codecs.encode, used for saving bytes in older protocols */
154 PyObject *codecs_encode;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800155} PickleState;
156
157/* Forward declaration of the _pickle module definition. */
158static struct PyModuleDef _picklemodule;
159
160/* Given a module object, get its per-module state. */
161static PickleState *
162_Pickle_GetState(PyObject *module)
163{
164 return (PickleState *)PyModule_GetState(module);
165}
166
167/* Find the module instance imported in the currently running sub-interpreter
168 and get its state. */
169static PickleState *
170_Pickle_GetGlobalState(void)
171{
172 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
173}
174
175/* Clear the given pickle module state. */
176static void
177_Pickle_ClearState(PickleState *st)
178{
179 Py_CLEAR(st->PickleError);
180 Py_CLEAR(st->PicklingError);
181 Py_CLEAR(st->UnpicklingError);
182 Py_CLEAR(st->dispatch_table);
183 Py_CLEAR(st->extension_registry);
184 Py_CLEAR(st->extension_cache);
185 Py_CLEAR(st->inverted_registry);
186 Py_CLEAR(st->name_mapping_2to3);
187 Py_CLEAR(st->import_mapping_2to3);
188 Py_CLEAR(st->name_mapping_3to2);
189 Py_CLEAR(st->import_mapping_3to2);
190 Py_CLEAR(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800191}
192
193/* Initialize the given pickle module state. */
194static int
195_Pickle_InitState(PickleState *st)
196{
197 PyObject *copyreg = NULL;
198 PyObject *compat_pickle = NULL;
199 PyObject *codecs = NULL;
200
201 copyreg = PyImport_ImportModule("copyreg");
202 if (!copyreg)
203 goto error;
204 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
205 if (!st->dispatch_table)
206 goto error;
207 if (!PyDict_CheckExact(st->dispatch_table)) {
208 PyErr_Format(PyExc_RuntimeError,
209 "copyreg.dispatch_table should be a dict, not %.200s",
210 Py_TYPE(st->dispatch_table)->tp_name);
211 goto error;
212 }
213 st->extension_registry = \
214 PyObject_GetAttrString(copyreg, "_extension_registry");
215 if (!st->extension_registry)
216 goto error;
217 if (!PyDict_CheckExact(st->extension_registry)) {
218 PyErr_Format(PyExc_RuntimeError,
219 "copyreg._extension_registry should be a dict, "
220 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
221 goto error;
222 }
223 st->inverted_registry = \
224 PyObject_GetAttrString(copyreg, "_inverted_registry");
225 if (!st->inverted_registry)
226 goto error;
227 if (!PyDict_CheckExact(st->inverted_registry)) {
228 PyErr_Format(PyExc_RuntimeError,
229 "copyreg._inverted_registry should be a dict, "
230 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
231 goto error;
232 }
233 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
234 if (!st->extension_cache)
235 goto error;
236 if (!PyDict_CheckExact(st->extension_cache)) {
237 PyErr_Format(PyExc_RuntimeError,
238 "copyreg._extension_cache should be a dict, "
239 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
240 goto error;
241 }
242 Py_CLEAR(copyreg);
243
244 /* Load the 2.x -> 3.x stdlib module mapping tables */
245 compat_pickle = PyImport_ImportModule("_compat_pickle");
246 if (!compat_pickle)
247 goto error;
248 st->name_mapping_2to3 = \
249 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
250 if (!st->name_mapping_2to3)
251 goto error;
252 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
255 Py_TYPE(st->name_mapping_2to3)->tp_name);
256 goto error;
257 }
258 st->import_mapping_2to3 = \
259 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
260 if (!st->import_mapping_2to3)
261 goto error;
262 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
263 PyErr_Format(PyExc_RuntimeError,
264 "_compat_pickle.IMPORT_MAPPING should be a dict, "
265 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
266 goto error;
267 }
268 /* ... and the 3.x -> 2.x mapping tables */
269 st->name_mapping_3to2 = \
270 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
271 if (!st->name_mapping_3to2)
272 goto error;
273 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
274 PyErr_Format(PyExc_RuntimeError,
275 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
276 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
277 goto error;
278 }
279 st->import_mapping_3to2 = \
280 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
281 if (!st->import_mapping_3to2)
282 goto error;
283 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
284 PyErr_Format(PyExc_RuntimeError,
285 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
286 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
287 goto error;
288 }
289 Py_CLEAR(compat_pickle);
290
291 codecs = PyImport_ImportModule("codecs");
292 if (codecs == NULL)
293 goto error;
294 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
295 if (st->codecs_encode == NULL) {
296 goto error;
297 }
298 if (!PyCallable_Check(st->codecs_encode)) {
299 PyErr_Format(PyExc_RuntimeError,
300 "codecs.encode should be a callable, not %.200s",
301 Py_TYPE(st->codecs_encode)->tp_name);
302 goto error;
303 }
304 Py_CLEAR(codecs);
305
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800306 return 0;
307
308 error:
309 Py_CLEAR(copyreg);
310 Py_CLEAR(compat_pickle);
311 Py_CLEAR(codecs);
312 _Pickle_ClearState(st);
313 return -1;
314}
315
316/* Helper for calling a function with a single argument quickly.
317
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800318 This function steals the reference of the given argument. */
319static PyObject *
320_Pickle_FastCall(PyObject *func, PyObject *obj)
321{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800322 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800323 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800324
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800325 /* Note: this function used to reuse the argument tuple. This used to give
326 a slight performance boost with older pickle implementations where many
327 unbuffered reads occurred (thus needing many function calls).
328
329 However, this optimization was removed because it was too complicated
330 to get right. It abused the C API for tuples to mutate them which led
331 to subtle reference counting and concurrency bugs. Furthermore, the
332 introduction of protocol 4 and the prefetching optimization via peek()
333 significantly reduced the number of function calls we do. Thus, the
334 benefits became marginal at best. */
335
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800336 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800337 Py_DECREF(obj);
338 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800339 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800340 PyTuple_SET_ITEM(arg_tuple, 0, obj);
341 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800342 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 return result;
344}
345
346/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000347
348static int
349stack_underflow(void)
350{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 PickleState *st = _Pickle_GetGlobalState();
352 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000353 return -1;
354}
355
356/* Internal data type used as the unpickling stack. */
357typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000358 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000359 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000360 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000361} Pdata;
362
363static void
364Pdata_dealloc(Pdata *self)
365{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200366 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000367 while (--i >= 0) {
368 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000369 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000370 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000371 PyObject_Del(self);
372}
373
374static PyTypeObject Pdata_Type = {
375 PyVarObject_HEAD_INIT(NULL, 0)
376 "_pickle.Pdata", /*tp_name*/
377 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200378 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000379 (destructor)Pdata_dealloc, /*tp_dealloc*/
380};
381
382static PyObject *
383Pdata_New(void)
384{
385 Pdata *self;
386
387 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
388 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000389 Py_SIZE(self) = 0;
390 self->allocated = 8;
391 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000392 if (self->data)
393 return (PyObject *)self;
394 Py_DECREF(self);
395 return PyErr_NoMemory();
396}
397
398
399/* Retain only the initial clearto items. If clearto >= the current
400 * number of items, this is a (non-erroneous) NOP.
401 */
402static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200403Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000404{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200405 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000406
407 if (clearto < 0)
408 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000409 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000410 return 0;
411
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000412 while (--i >= clearto) {
413 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000414 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000415 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000416 return 0;
417}
418
419static int
420Pdata_grow(Pdata *self)
421{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000422 PyObject **data = self->data;
423 Py_ssize_t allocated = self->allocated;
424 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 new_allocated = (allocated >> 3) + 6;
427 /* check for integer overflow */
428 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000429 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000430 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500431 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000433 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000434
435 self->data = data;
436 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000437 return 0;
438
439 nomemory:
440 PyErr_NoMemory();
441 return -1;
442}
443
444/* D is a Pdata*. Pop the topmost element and store it into V, which
445 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
446 * is raised and V is set to NULL.
447 */
448static PyObject *
449Pdata_pop(Pdata *self)
450{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800451 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000452 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800453 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000454 return NULL;
455 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000456 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457}
458#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
459
460static int
461Pdata_push(Pdata *self, PyObject *obj)
462{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000463 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000464 return -1;
465 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000466 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000467 return 0;
468}
469
470/* Push an object on stack, transferring its ownership to the stack. */
471#define PDATA_PUSH(D, O, ER) do { \
472 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
473
474/* Push an object on stack, adding a new reference to the object. */
475#define PDATA_APPEND(D, O, ER) do { \
476 Py_INCREF((O)); \
477 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
478
479static PyObject *
480Pdata_poptuple(Pdata *self, Py_ssize_t start)
481{
482 PyObject *tuple;
483 Py_ssize_t len, i, j;
484
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 tuple = PyTuple_New(len);
487 if (tuple == NULL)
488 return NULL;
489 for (i = start, j = 0; j < len; i++, j++)
490 PyTuple_SET_ITEM(tuple, j, self->data[i]);
491
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000493 return tuple;
494}
495
496static PyObject *
497Pdata_poplist(Pdata *self, Py_ssize_t start)
498{
499 PyObject *list;
500 Py_ssize_t len, i, j;
501
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 list = PyList_New(len);
504 if (list == NULL)
505 return NULL;
506 for (i = start, j = 0; j < len; i++, j++)
507 PyList_SET_ITEM(list, j, self->data[i]);
508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 return list;
511}
512
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000513typedef struct {
514 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200515 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516} PyMemoEntry;
517
518typedef struct {
519 Py_ssize_t mt_mask;
520 Py_ssize_t mt_used;
521 Py_ssize_t mt_allocated;
522 PyMemoEntry *mt_table;
523} PyMemoTable;
524
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000525typedef struct PicklerObject {
526 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000527 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000528 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100531 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532
533 PyObject *write; /* write() method of the output stream. */
534 PyObject *output_buffer; /* Write into a local bytearray buffer before
535 flushing to the stream. */
536 Py_ssize_t output_len; /* Length of output_buffer. */
537 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538 int proto; /* Pickle protocol number, >= 0 */
539 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100540 int framing; /* True when framing is enabled, proto >= 4 */
541 Py_ssize_t frame_start; /* Position in output_buffer where the
542 where the current frame begins. -1 if there
543 is no frame currently open. */
544
545 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000546 int fast; /* Enable fast mode if set to a true value.
547 The fast mode disable the usage of memo,
548 therefore speeding the pickling process by
549 not generating superfluous PUT opcodes. It
550 should not be used if with self-referential
551 objects. */
552 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000553 int fix_imports; /* Indicate whether Pickler should fix
554 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000555 PyObject *fast_memo;
556} PicklerObject;
557
558typedef struct UnpicklerObject {
559 PyObject_HEAD
560 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000561
562 /* The unpickler memo is just an array of PyObject *s. Using a dict
563 is unnecessary, since the keys are contiguous ints. */
564 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100565 Py_ssize_t memo_size; /* Capacity of the memo array */
566 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000567
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000568 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000569
570 Py_buffer buffer;
571 char *input_buffer;
572 char *input_line;
573 Py_ssize_t input_len;
574 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000575 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000577 PyObject *read; /* read() method of the input stream. */
578 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000579 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000580
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000581 char *encoding; /* Name of the encoding to be used for
582 decoding strings pickled using Python
583 2.x. The default value is "ASCII" */
584 char *errors; /* Name of errors handling scheme to used when
585 decoding strings. The default value is
586 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500587 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000588 objects. */
589 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
590 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000591 int proto; /* Protocol of the pickle loaded. */
592 int fix_imports; /* Indicate whether Unpickler should fix
593 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594} UnpicklerObject;
595
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200596typedef struct {
597 PyObject_HEAD
598 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
599} PicklerMemoProxyObject;
600
601typedef struct {
602 PyObject_HEAD
603 UnpicklerObject *unpickler;
604} UnpicklerMemoProxyObject;
605
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000606/* Forward declarations */
607static int save(PicklerObject *, PyObject *, int);
608static int save_reduce(PicklerObject *, PyObject *, PyObject *);
609static PyTypeObject Pickler_Type;
610static PyTypeObject Unpickler_Type;
611
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200612#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000613
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300615 A custom hashtable mapping void* to Python ints. This is used by the pickler
616 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000617 a bunch of unnecessary object creation. This makes a huge performance
618 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000620#define MT_MINSIZE 8
621#define PERTURB_SHIFT 5
622
623
624static PyMemoTable *
625PyMemoTable_New(void)
626{
627 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
628 if (memo == NULL) {
629 PyErr_NoMemory();
630 return NULL;
631 }
632
633 memo->mt_used = 0;
634 memo->mt_allocated = MT_MINSIZE;
635 memo->mt_mask = MT_MINSIZE - 1;
636 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
637 if (memo->mt_table == NULL) {
638 PyMem_FREE(memo);
639 PyErr_NoMemory();
640 return NULL;
641 }
642 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
643
644 return memo;
645}
646
647static PyMemoTable *
648PyMemoTable_Copy(PyMemoTable *self)
649{
650 Py_ssize_t i;
651 PyMemoTable *new = PyMemoTable_New();
652 if (new == NULL)
653 return NULL;
654
655 new->mt_used = self->mt_used;
656 new->mt_allocated = self->mt_allocated;
657 new->mt_mask = self->mt_mask;
658 /* The table we get from _New() is probably smaller than we wanted.
659 Free it and allocate one that's the right size. */
660 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500661 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000662 if (new->mt_table == NULL) {
663 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200664 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000665 return NULL;
666 }
667 for (i = 0; i < self->mt_allocated; i++) {
668 Py_XINCREF(self->mt_table[i].me_key);
669 }
670 memcpy(new->mt_table, self->mt_table,
671 sizeof(PyMemoEntry) * self->mt_allocated);
672
673 return new;
674}
675
676static Py_ssize_t
677PyMemoTable_Size(PyMemoTable *self)
678{
679 return self->mt_used;
680}
681
682static int
683PyMemoTable_Clear(PyMemoTable *self)
684{
685 Py_ssize_t i = self->mt_allocated;
686
687 while (--i >= 0) {
688 Py_XDECREF(self->mt_table[i].me_key);
689 }
690 self->mt_used = 0;
691 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
692 return 0;
693}
694
695static void
696PyMemoTable_Del(PyMemoTable *self)
697{
698 if (self == NULL)
699 return;
700 PyMemoTable_Clear(self);
701
702 PyMem_FREE(self->mt_table);
703 PyMem_FREE(self);
704}
705
706/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
707 can be considerably simpler than dictobject.c's lookdict(). */
708static PyMemoEntry *
709_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
710{
711 size_t i;
712 size_t perturb;
713 size_t mask = (size_t)self->mt_mask;
714 PyMemoEntry *table = self->mt_table;
715 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000716 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000717
718 i = hash & mask;
719 entry = &table[i];
720 if (entry->me_key == NULL || entry->me_key == key)
721 return entry;
722
723 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
724 i = (i << 2) + i + perturb + 1;
725 entry = &table[i & mask];
726 if (entry->me_key == NULL || entry->me_key == key)
727 return entry;
728 }
729 assert(0); /* Never reached */
730 return NULL;
731}
732
733/* Returns -1 on failure, 0 on success. */
734static int
735_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
736{
737 PyMemoEntry *oldtable = NULL;
738 PyMemoEntry *oldentry, *newentry;
739 Py_ssize_t new_size = MT_MINSIZE;
740 Py_ssize_t to_process;
741
742 assert(min_size > 0);
743
744 /* Find the smallest valid table size >= min_size. */
745 while (new_size < min_size && new_size > 0)
746 new_size <<= 1;
747 if (new_size <= 0) {
748 PyErr_NoMemory();
749 return -1;
750 }
751 /* new_size needs to be a power of two. */
752 assert((new_size & (new_size - 1)) == 0);
753
754 /* Allocate new table. */
755 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500756 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000757 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200758 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000759 PyErr_NoMemory();
760 return -1;
761 }
762 self->mt_allocated = new_size;
763 self->mt_mask = new_size - 1;
764 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
765
766 /* Copy entries from the old table. */
767 to_process = self->mt_used;
768 for (oldentry = oldtable; to_process > 0; oldentry++) {
769 if (oldentry->me_key != NULL) {
770 to_process--;
771 /* newentry is a pointer to a chunk of the new
772 mt_table, so we're setting the key:value pair
773 in-place. */
774 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
775 newentry->me_key = oldentry->me_key;
776 newentry->me_value = oldentry->me_value;
777 }
778 }
779
780 /* Deallocate the old table. */
781 PyMem_FREE(oldtable);
782 return 0;
783}
784
785/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200786static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000787PyMemoTable_Get(PyMemoTable *self, PyObject *key)
788{
789 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
790 if (entry->me_key == NULL)
791 return NULL;
792 return &entry->me_value;
793}
794
795/* Returns -1 on failure, 0 on success. */
796static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200797PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000798{
799 PyMemoEntry *entry;
800
801 assert(key != NULL);
802
803 entry = _PyMemoTable_Lookup(self, key);
804 if (entry->me_key != NULL) {
805 entry->me_value = value;
806 return 0;
807 }
808 Py_INCREF(key);
809 entry->me_key = key;
810 entry->me_value = value;
811 self->mt_used++;
812
813 /* If we added a key, we can safely resize. Otherwise just return!
814 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
815 *
816 * Quadrupling the size improves average table sparseness
817 * (reducing collisions) at the cost of some memory. It also halves
818 * the number of expensive resize operations in a growing memo table.
819 *
820 * Very large memo tables (over 50K items) use doubling instead.
821 * This may help applications with severe memory constraints.
822 */
823 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
824 return 0;
825 return _PyMemoTable_ResizeTable(self,
826 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
827}
828
829#undef MT_MINSIZE
830#undef PERTURB_SHIFT
831
832/*************************************************************************/
833
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000834
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000835static int
836_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000837{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000838 Py_CLEAR(self->output_buffer);
839 self->output_buffer =
840 PyBytes_FromStringAndSize(NULL, self->max_output_len);
841 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000842 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100844 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 return 0;
846}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000847
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100848static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100849_write_size64(char *out, size_t value)
850{
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800851 int i;
852
853 assert(sizeof(size_t) <= 8);
854
855 for (i = 0; i < sizeof(size_t); i++) {
856 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
857 }
858 for (i = sizeof(size_t); i < 8; i++) {
859 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800860 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100861}
862
863static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100864_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
865{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100866 qdata[0] = FRAME;
867 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100868}
869
870static int
871_Pickler_CommitFrame(PicklerObject *self)
872{
873 size_t frame_len;
874 char *qdata;
875
876 if (!self->framing || self->frame_start == -1)
877 return 0;
878 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
879 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
880 _Pickler_WriteFrameHeader(self, qdata, frame_len);
881 self->frame_start = -1;
882 return 0;
883}
884
885static int
886_Pickler_OpcodeBoundary(PicklerObject *self)
887{
888 Py_ssize_t frame_len;
889
890 if (!self->framing || self->frame_start == -1)
891 return 0;
892 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
893 if (frame_len >= FRAME_SIZE_TARGET)
894 return _Pickler_CommitFrame(self);
895 else
896 return 0;
897}
898
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000899static PyObject *
900_Pickler_GetString(PicklerObject *self)
901{
902 PyObject *output_buffer = self->output_buffer;
903
904 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100905
906 if (_Pickler_CommitFrame(self))
907 return NULL;
908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000909 self->output_buffer = NULL;
910 /* Resize down to exact size */
911 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
912 return NULL;
913 return output_buffer;
914}
915
916static int
917_Pickler_FlushToFile(PicklerObject *self)
918{
919 PyObject *output, *result;
920
921 assert(self->write != NULL);
922
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100923 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000924 output = _Pickler_GetString(self);
925 if (output == NULL)
926 return -1;
927
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800928 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 Py_XDECREF(result);
930 return (result == NULL) ? -1 : 0;
931}
932
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200933static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100934_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000935{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000939
940 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100941 need_new_frame = (self->framing && self->frame_start == -1);
942
943 if (need_new_frame)
944 n = data_len + FRAME_HEADER_SIZE;
945 else
946 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000947
948 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949 if (required > self->max_output_len) {
950 /* Make place in buffer for the pickle chunk */
951 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
952 PyErr_NoMemory();
953 return -1;
954 }
955 self->max_output_len = (self->output_len + n) / 2 * 3;
956 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
957 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000958 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 if (need_new_frame) {
961 /* Setup new frame */
962 Py_ssize_t frame_start = self->output_len;
963 self->frame_start = frame_start;
964 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
965 /* Write an invalid value, for debugging */
966 buffer[frame_start + i] = 0xFE;
967 }
968 self->output_len += FRAME_HEADER_SIZE;
969 }
970 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000971 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000973 buffer[self->output_len + i] = s[i];
974 }
975 }
976 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100977 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000978 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100979 self->output_len += data_len;
980 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000981}
982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983static PicklerObject *
984_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000985{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
989 if (self == NULL)
990 return NULL;
991
992 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100993 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000994 self->write = NULL;
995 self->proto = 0;
996 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100997 self->framing = 0;
998 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 self->fast = 0;
1000 self->fast_nesting = 0;
1001 self->fix_imports = 0;
1002 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001003 self->max_output_len = WRITE_BUF_SIZE;
1004 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001005
1006 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1008 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001009
1010 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001011 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 return NULL;
1013 }
1014 return self;
1015}
1016
1017static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001018_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001020 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001022 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001024 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001026 proto = PyLong_AsLong(protocol);
1027 if (proto < 0) {
1028 if (proto == -1 && PyErr_Occurred())
1029 return -1;
1030 proto = HIGHEST_PROTOCOL;
1031 }
1032 else if (proto > HIGHEST_PROTOCOL) {
1033 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1034 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001035 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001036 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001038 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 self->bin = proto > 0;
1040 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 return 0;
1042}
1043
1044/* Returns -1 (with an exception set) on failure, 0 on success. This may
1045 be called once on a freshly created Pickler. */
1046static int
1047_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1048{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001049 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001051 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 if (self->write == NULL) {
1053 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1054 PyErr_SetString(PyExc_TypeError,
1055 "file must have a 'write' attribute");
1056 return -1;
1057 }
1058
1059 return 0;
1060}
1061
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062/* Returns the size of the input on success, -1 on failure. This takes its
1063 own reference to `input`. */
1064static Py_ssize_t
1065_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1066{
1067 if (self->buffer.buf != NULL)
1068 PyBuffer_Release(&self->buffer);
1069 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1070 return -1;
1071 self->input_buffer = self->buffer.buf;
1072 self->input_len = self->buffer.len;
1073 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001074 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 return self->input_len;
1076}
1077
Antoine Pitrou04248a82010-10-12 20:51:21 +00001078static int
1079_Unpickler_SkipConsumed(UnpicklerObject *self)
1080{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001081 Py_ssize_t consumed;
1082 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001083
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001084 consumed = self->next_read_idx - self->prefetched_idx;
1085 if (consumed <= 0)
1086 return 0;
1087
1088 assert(self->peek); /* otherwise we did something wrong */
1089 /* This makes an useless copy... */
1090 r = PyObject_CallFunction(self->read, "n", consumed);
1091 if (r == NULL)
1092 return -1;
1093 Py_DECREF(r);
1094
1095 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096 return 0;
1097}
1098
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001099static const Py_ssize_t READ_WHOLE_LINE = -1;
1100
1101/* If reading from a file, we need to only pull the bytes we need, since there
1102 may be multiple pickle objects arranged contiguously in the same input
1103 buffer.
1104
1105 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1106 bytes from the input stream/buffer.
1107
1108 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1109 failure; on success, returns the number of bytes read from the file.
1110
1111 On success, self->input_len will be 0; this is intentional so that when
1112 unpickling from a file, the "we've run out of data" code paths will trigger,
1113 causing the Unpickler to go back to the file for more data. Use the returned
1114 size to tell you how much data you can process. */
1115static Py_ssize_t
1116_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1117{
1118 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001119 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001120
1121 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001122
Antoine Pitrou04248a82010-10-12 20:51:21 +00001123 if (_Unpickler_SkipConsumed(self) < 0)
1124 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001126 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001127 PyObject *empty_tuple = PyTuple_New(0);
1128 data = PyObject_Call(self->readline, empty_tuple, NULL);
1129 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001130 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001131 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001132 PyObject *len;
1133 /* Prefetch some data without advancing the file pointer, if possible */
1134 if (self->peek && n < PREFETCH) {
1135 len = PyLong_FromSsize_t(PREFETCH);
1136 if (len == NULL)
1137 return -1;
1138 data = _Pickle_FastCall(self->peek, len);
1139 if (data == NULL) {
1140 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1141 return -1;
1142 /* peek() is probably not supported by the given file object */
1143 PyErr_Clear();
1144 Py_CLEAR(self->peek);
1145 }
1146 else {
1147 read_size = _Unpickler_SetStringInput(self, data);
1148 Py_DECREF(data);
1149 self->prefetched_idx = 0;
1150 if (n <= read_size)
1151 return n;
1152 }
1153 }
1154 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 if (len == NULL)
1156 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001157 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001159 if (data == NULL)
1160 return -1;
1161
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001162 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 Py_DECREF(data);
1164 return read_size;
1165}
1166
1167/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1168
1169 This should be used for all data reads, rather than accessing the unpickler's
1170 input buffer directly. This method deals correctly with reading from input
1171 streams, which the input buffer doesn't deal with.
1172
1173 Note that when reading from a file-like object, self->next_read_idx won't
1174 be updated (it should remain at 0 for the entire unpickling process). You
1175 should use this function's return value to know how many bytes you can
1176 consume.
1177
1178 Returns -1 (with an exception set) on failure. On success, return the
1179 number of chars read. */
1180static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001181_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001182{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001183 Py_ssize_t num_read;
1184
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001185 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001186 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1187 PickleState *st = _Pickle_GetGlobalState();
1188 PyErr_SetString(st->UnpicklingError,
1189 "read would overflow (invalid bytecode)");
1190 return -1;
1191 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001192 if (self->next_read_idx + n <= self->input_len) {
1193 *s = self->input_buffer + self->next_read_idx;
1194 self->next_read_idx += n;
1195 return n;
1196 }
1197 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001199 return -1;
1200 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001201 num_read = _Unpickler_ReadFromFile(self, n);
1202 if (num_read < 0)
1203 return -1;
1204 if (num_read < n) {
1205 PyErr_Format(PyExc_EOFError, "Ran out of input");
1206 return -1;
1207 }
1208 *s = self->input_buffer;
1209 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001210 return n;
1211}
1212
1213static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001214_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1215 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001216{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001217 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001218 if (input_line == NULL) {
1219 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001220 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001221 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223 memcpy(input_line, line, len);
1224 input_line[len] = '\0';
1225 self->input_line = input_line;
1226 *result = self->input_line;
1227 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001228}
1229
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001230/* Read a line from the input stream/buffer. If we run off the end of the input
1231 before hitting \n, return the data we found.
1232
1233 Returns the number of chars read, or -1 on failure. */
1234static Py_ssize_t
1235_Unpickler_Readline(UnpicklerObject *self, char **result)
1236{
1237 Py_ssize_t i, num_read;
1238
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001239 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240 if (self->input_buffer[i] == '\n') {
1241 char *line_start = self->input_buffer + self->next_read_idx;
1242 num_read = i - self->next_read_idx + 1;
1243 self->next_read_idx = i + 1;
1244 return _Unpickler_CopyLine(self, line_start, num_read, result);
1245 }
1246 }
1247 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1249 if (num_read < 0)
1250 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001251 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001252 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001253 }
Victor Stinner121aab42011-09-29 23:40:53 +02001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 /* If we get here, we've run off the end of the input string. Return the
1256 remaining string and let the caller figure it out. */
1257 *result = self->input_buffer + self->next_read_idx;
1258 num_read = i - self->next_read_idx;
1259 self->next_read_idx = i;
1260 return num_read;
1261}
1262
1263/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1264 will be modified in place. */
1265static int
1266_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1267{
1268 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001269
1270 assert(new_size > self->memo_size);
1271
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001272 PyMem_RESIZE(self->memo, PyObject *, new_size);
1273 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001274 PyErr_NoMemory();
1275 return -1;
1276 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001277 for (i = self->memo_size; i < new_size; i++)
1278 self->memo[i] = NULL;
1279 self->memo_size = new_size;
1280 return 0;
1281}
1282
1283/* Returns NULL if idx is out of bounds. */
1284static PyObject *
1285_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1286{
1287 if (idx < 0 || idx >= self->memo_size)
1288 return NULL;
1289
1290 return self->memo[idx];
1291}
1292
1293/* Returns -1 (with an exception set) on failure, 0 on success.
1294 This takes its own reference to `value`. */
1295static int
1296_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1297{
1298 PyObject *old_item;
1299
1300 if (idx >= self->memo_size) {
1301 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1302 return -1;
1303 assert(idx < self->memo_size);
1304 }
1305 Py_INCREF(value);
1306 old_item = self->memo[idx];
1307 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001308 if (old_item != NULL) {
1309 Py_DECREF(old_item);
1310 }
1311 else {
1312 self->memo_len++;
1313 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001314 return 0;
1315}
1316
1317static PyObject **
1318_Unpickler_NewMemo(Py_ssize_t new_size)
1319{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001320 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001321 if (memo == NULL) {
1322 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001323 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001324 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001325 memset(memo, 0, new_size * sizeof(PyObject *));
1326 return memo;
1327}
1328
1329/* Free the unpickler's memo, taking care to decref any items left in it. */
1330static void
1331_Unpickler_MemoCleanup(UnpicklerObject *self)
1332{
1333 Py_ssize_t i;
1334 PyObject **memo = self->memo;
1335
1336 if (self->memo == NULL)
1337 return;
1338 self->memo = NULL;
1339 i = self->memo_size;
1340 while (--i >= 0) {
1341 Py_XDECREF(memo[i]);
1342 }
1343 PyMem_FREE(memo);
1344}
1345
1346static UnpicklerObject *
1347_Unpickler_New(void)
1348{
1349 UnpicklerObject *self;
1350
1351 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1352 if (self == NULL)
1353 return NULL;
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 self->pers_func = NULL;
1356 self->input_buffer = NULL;
1357 self->input_line = NULL;
1358 self->input_len = 0;
1359 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001360 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001361 self->read = NULL;
1362 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001363 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001364 self->encoding = NULL;
1365 self->errors = NULL;
1366 self->marks = NULL;
1367 self->num_marks = 0;
1368 self->marks_size = 0;
1369 self->proto = 0;
1370 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001371 memset(&self->buffer, 0, sizeof(Py_buffer));
1372 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001373 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001374 self->memo = _Unpickler_NewMemo(self->memo_size);
1375 self->stack = (Pdata *)Pdata_New();
1376
1377 if (self->memo == NULL || self->stack == NULL) {
1378 Py_DECREF(self);
1379 return NULL;
1380 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381
1382 return self;
1383}
1384
1385/* Returns -1 (with an exception set) on failure, 0 on success. This may
1386 be called once on a freshly created Pickler. */
1387static int
1388_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1389{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001390 _Py_IDENTIFIER(peek);
1391 _Py_IDENTIFIER(read);
1392 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001393
1394 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001395 if (self->peek == NULL) {
1396 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1397 PyErr_Clear();
1398 else
1399 return -1;
1400 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001401 self->read = _PyObject_GetAttrId(file, &PyId_read);
1402 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001403 if (self->readline == NULL || self->read == NULL) {
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1405 PyErr_SetString(PyExc_TypeError,
1406 "file must have 'read' and 'readline' attributes");
1407 Py_CLEAR(self->read);
1408 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001409 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001410 return -1;
1411 }
1412 return 0;
1413}
1414
1415/* Returns -1 (with an exception set) on failure, 0 on success. This may
1416 be called once on a freshly created Pickler. */
1417static int
1418_Unpickler_SetInputEncoding(UnpicklerObject *self,
1419 const char *encoding,
1420 const char *errors)
1421{
1422 if (encoding == NULL)
1423 encoding = "ASCII";
1424 if (errors == NULL)
1425 errors = "strict";
1426
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001427 self->encoding = _PyMem_Strdup(encoding);
1428 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001429 if (self->encoding == NULL || self->errors == NULL) {
1430 PyErr_NoMemory();
1431 return -1;
1432 }
1433 return 0;
1434}
1435
1436/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001437static int
1438memo_get(PicklerObject *self, PyObject *key)
1439{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001440 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001442 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001444 value = PyMemoTable_Get(self->memo, key);
1445 if (value == NULL) {
1446 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447 return -1;
1448 }
1449
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001450 if (!self->bin) {
1451 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001452 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1453 "%" PY_FORMAT_SIZE_T "d\n", *value);
1454 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 }
1456 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001459 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001460 len = 2;
1461 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001462 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001464 pdata[1] = (unsigned char)(*value & 0xff);
1465 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1466 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1467 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 len = 5;
1469 }
1470 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001471 PickleState *st = _Pickle_GetGlobalState();
1472 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 "memo id too large for LONG_BINGET");
1474 return -1;
1475 }
1476 }
1477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 return -1;
1480
1481 return 0;
1482}
1483
1484/* Store an object in the memo, assign it a new unique ID based on the number
1485 of objects currently stored in the memo and generate a PUT opcode. */
1486static int
1487memo_put(PicklerObject *self, PyObject *obj)
1488{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001490 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001491 Py_ssize_t idx;
1492
1493 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001494
1495 if (self->fast)
1496 return 0;
1497
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001498 idx = PyMemoTable_Size(self->memo);
1499 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1500 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001502 if (self->proto >= 4) {
1503 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1504 return -1;
1505 return 0;
1506 }
1507 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001509 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001510 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511 len = strlen(pdata);
1512 }
1513 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001514 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001515 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001516 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 len = 2;
1518 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001520 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001521 pdata[1] = (unsigned char)(idx & 0xff);
1522 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1523 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1524 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 len = 5;
1526 }
1527 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001528 PickleState *st = _Pickle_GetGlobalState();
1529 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530 "memo id too large for LONG_BINPUT");
1531 return -1;
1532 }
1533 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001534 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001537 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538}
1539
1540static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1542 PyObject *dotted_path;
1543 Py_ssize_t i;
1544 _Py_static_string(PyId_dot, ".");
1545 _Py_static_string(PyId_locals, "<locals>");
1546
1547 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1548 if (dotted_path == NULL) {
1549 return NULL;
1550 }
1551 assert(Py_SIZE(dotted_path) >= 1);
1552 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1553 PyErr_Format(PyExc_AttributeError,
1554 "Can't get qualified attribute %R on %R;"
1555 "use protocols >= 4 to enable support",
1556 name, obj);
1557 Py_DECREF(dotted_path);
1558 return NULL;
1559 }
1560 Py_INCREF(obj);
1561 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1562 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1563 PyObject *tmp;
1564 PyObject *result = PyUnicode_RichCompare(
1565 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1566 int is_equal = (result == Py_True);
1567 assert(PyBool_Check(result));
1568 Py_DECREF(result);
1569 if (is_equal) {
1570 PyErr_Format(PyExc_AttributeError,
1571 "Can't get local attribute %R on %R", name, obj);
1572 Py_DECREF(dotted_path);
1573 Py_DECREF(obj);
1574 return NULL;
1575 }
1576 tmp = PyObject_GetAttr(obj, subpath);
1577 Py_DECREF(obj);
1578 if (tmp == NULL) {
1579 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1580 PyErr_Clear();
1581 PyErr_Format(PyExc_AttributeError,
1582 "Can't get attribute %R on %R", name, obj);
1583 }
1584 Py_DECREF(dotted_path);
1585 return NULL;
1586 }
1587 obj = tmp;
1588 }
1589 Py_DECREF(dotted_path);
1590 return obj;
1591}
1592
1593static PyObject *
1594whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001595{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001596 PyObject *module_name;
1597 PyObject *modules_dict;
1598 PyObject *module;
1599 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 Py_ssize_t i, j;
1601 _Py_IDENTIFIER(__module__);
1602 _Py_IDENTIFIER(modules);
1603 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001605 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1606
1607 if (module_name == NULL) {
1608 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001609 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 }
1612 else {
1613 /* In some rare cases (e.g., bound methods of extension types),
1614 __module__ can be None. If it is so, then search sys.modules for
1615 the module of global. */
1616 if (module_name != Py_None)
1617 return module_name;
1618 Py_CLEAR(module_name);
1619 }
1620 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621
Victor Stinnerbb520202013-11-06 22:40:41 +01001622 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001623 if (modules_dict == NULL) {
1624 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627
1628 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 PyObject *result = PyUnicode_RichCompare(
1631 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1632 int is_equal = (result == Py_True);
1633 assert(PyBool_Check(result));
1634 Py_DECREF(result);
1635 if (is_equal)
1636 continue;
1637 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 continue;
1639
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001640 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001642 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645 continue;
1646 }
1647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 Py_INCREF(module_name);
1651 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001652 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001654 }
1655
1656 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001658 Py_INCREF(module_name);
1659 return module_name;
1660}
1661
1662/* fast_save_enter() and fast_save_leave() are guards against recursive
1663 objects when Pickler is used with the "fast mode" (i.e., with object
1664 memoization disabled). If the nesting of a list or dict object exceed
1665 FAST_NESTING_LIMIT, these guards will start keeping an internal
1666 reference to the seen list or dict objects and check whether these objects
1667 are recursive. These are not strictly necessary, since save() has a
1668 hard-coded recursion limit, but they give a nicer error message than the
1669 typical RuntimeError. */
1670static int
1671fast_save_enter(PicklerObject *self, PyObject *obj)
1672{
1673 /* if fast_nesting < 0, we're doing an error exit. */
1674 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1675 PyObject *key = NULL;
1676 if (self->fast_memo == NULL) {
1677 self->fast_memo = PyDict_New();
1678 if (self->fast_memo == NULL) {
1679 self->fast_nesting = -1;
1680 return 0;
1681 }
1682 }
1683 key = PyLong_FromVoidPtr(obj);
1684 if (key == NULL)
1685 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001686 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001687 Py_DECREF(key);
1688 PyErr_Format(PyExc_ValueError,
1689 "fast mode: can't pickle cyclic objects "
1690 "including object type %.200s at %p",
1691 obj->ob_type->tp_name, obj);
1692 self->fast_nesting = -1;
1693 return 0;
1694 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001695 if (PyErr_Occurred()) {
1696 return 0;
1697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001698 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1699 Py_DECREF(key);
1700 self->fast_nesting = -1;
1701 return 0;
1702 }
1703 Py_DECREF(key);
1704 }
1705 return 1;
1706}
1707
1708static int
1709fast_save_leave(PicklerObject *self, PyObject *obj)
1710{
1711 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1712 PyObject *key = PyLong_FromVoidPtr(obj);
1713 if (key == NULL)
1714 return 0;
1715 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1716 Py_DECREF(key);
1717 return 0;
1718 }
1719 Py_DECREF(key);
1720 }
1721 return 1;
1722}
1723
1724static int
1725save_none(PicklerObject *self, PyObject *obj)
1726{
1727 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001728 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 return -1;
1730
1731 return 0;
1732}
1733
1734static int
1735save_bool(PicklerObject *self, PyObject *obj)
1736{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001738 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001739 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001740 return -1;
1741 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001742 else {
1743 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1744 * so that unpicklers written before bools were introduced unpickle them
1745 * as ints, but unpicklers after can recognize that bools were intended.
1746 * Note that protocol 2 added direct ways to pickle bools.
1747 */
1748 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1749 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1750 return -1;
1751 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 return 0;
1753}
1754
1755static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001756save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001758 PyObject *repr = NULL;
1759 Py_ssize_t size;
1760 long val;
1761 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001763 const char long_op = LONG;
1764
1765 val= PyLong_AsLong(obj);
1766 if (val == -1 && PyErr_Occurred()) {
1767 /* out of range for int pickling */
1768 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001769 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001770 else if (self->bin &&
1771 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001772 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001773 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001774
1775 Note: we can't use -0x80000000L in the above condition because some
1776 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1777 before applying the unary minus when sizeof(long) <= 4. The
1778 resulting value stays unsigned which is commonly not what we want,
1779 so MSVC happily warns us about it. However, that result would have
1780 been fine because we guard for sizeof(long) <= 4 which turns the
1781 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001782 char pdata[32];
1783 Py_ssize_t len = 0;
1784
1785 pdata[1] = (unsigned char)(val & 0xff);
1786 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1787 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1788 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789
1790 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1791 if (pdata[2] == 0) {
1792 pdata[0] = BININT1;
1793 len = 2;
1794 }
1795 else {
1796 pdata[0] = BININT2;
1797 len = 3;
1798 }
1799 }
1800 else {
1801 pdata[0] = BININT;
1802 len = 5;
1803 }
1804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001805 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001807
1808 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001809 }
1810
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001811 if (self->proto >= 2) {
1812 /* Linear-time pickling. */
1813 size_t nbits;
1814 size_t nbytes;
1815 unsigned char *pdata;
1816 char header[5];
1817 int i;
1818 int sign = _PyLong_Sign(obj);
1819
1820 if (sign == 0) {
1821 header[0] = LONG1;
1822 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001823 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824 goto error;
1825 return 0;
1826 }
1827 nbits = _PyLong_NumBits(obj);
1828 if (nbits == (size_t)-1 && PyErr_Occurred())
1829 goto error;
1830 /* How many bytes do we need? There are nbits >> 3 full
1831 * bytes of data, and nbits & 7 leftover bits. If there
1832 * are any leftover bits, then we clearly need another
1833 * byte. Wnat's not so obvious is that we *probably*
1834 * need another byte even if there aren't any leftovers:
1835 * the most-significant bit of the most-significant byte
1836 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001837 * opposite of the one we need. The exception is ints
1838 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001839 * its own 256's-complement, so has the right sign bit
1840 * even without the extra byte. That's a pain to check
1841 * for in advance, though, so we always grab an extra
1842 * byte at the start, and cut it back later if possible.
1843 */
1844 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001845 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001847 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 goto error;
1849 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001850 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 if (repr == NULL)
1852 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001853 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854 i = _PyLong_AsByteArray((PyLongObject *)obj,
1855 pdata, nbytes,
1856 1 /* little endian */ , 1 /* signed */ );
1857 if (i < 0)
1858 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001859 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001860 * needed. This is so iff the MSB is all redundant sign
1861 * bits.
1862 */
1863 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001864 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 pdata[nbytes - 1] == 0xff &&
1866 (pdata[nbytes - 2] & 0x80) != 0) {
1867 nbytes--;
1868 }
1869
1870 if (nbytes < 256) {
1871 header[0] = LONG1;
1872 header[1] = (unsigned char)nbytes;
1873 size = 2;
1874 }
1875 else {
1876 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001877 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001878 for (i = 1; i < 5; i++) {
1879 header[i] = (unsigned char)(size & 0xff);
1880 size >>= 8;
1881 }
1882 size = 5;
1883 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001884 if (_Pickler_Write(self, header, size) < 0 ||
1885 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 goto error;
1887 }
1888 else {
1889 char *string;
1890
Mark Dickinson8dd05142009-01-20 20:43:58 +00001891 /* proto < 2: write the repr and newline. This is quadratic-time (in
1892 the number of digits), in both directions. We add a trailing 'L'
1893 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001894
1895 repr = PyObject_Repr(obj);
1896 if (repr == NULL)
1897 goto error;
1898
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001899 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 if (string == NULL)
1901 goto error;
1902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001903 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1904 _Pickler_Write(self, string, size) < 0 ||
1905 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 goto error;
1907 }
1908
1909 if (0) {
1910 error:
1911 status = -1;
1912 }
1913 Py_XDECREF(repr);
1914
1915 return status;
1916}
1917
1918static int
1919save_float(PicklerObject *self, PyObject *obj)
1920{
1921 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1922
1923 if (self->bin) {
1924 char pdata[9];
1925 pdata[0] = BINFLOAT;
1926 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001928 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001929 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001930 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001932 int result = -1;
1933 char *buf = NULL;
1934 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001936 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001937 goto done;
1938
Mark Dickinson3e09f432009-04-17 08:41:23 +00001939 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001940 if (!buf) {
1941 PyErr_NoMemory();
1942 goto done;
1943 }
1944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001945 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001946 goto done;
1947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001948 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001949 goto done;
1950
1951 result = 0;
1952done:
1953 PyMem_Free(buf);
1954 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955 }
1956
1957 return 0;
1958}
1959
1960static int
1961save_bytes(PicklerObject *self, PyObject *obj)
1962{
1963 if (self->proto < 3) {
1964 /* Older pickle protocols do not have an opcode for pickling bytes
1965 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001966 the __reduce__ method) to permit bytes object unpickling.
1967
1968 Here we use a hack to be compatible with Python 2. Since in Python
1969 2 'bytes' is just an alias for 'str' (which has different
1970 parameters than the actual bytes object), we use codecs.encode
1971 to create the appropriate 'str' object when unpickled using
1972 Python 2 *and* the appropriate 'bytes' object when unpickled
1973 using Python 3. Again this is a hack and we don't need to do this
1974 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 int status;
1977
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001978 if (PyBytes_GET_SIZE(obj) == 0) {
1979 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1980 }
1981 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001982 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001983 PyObject *unicode_str =
1984 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1985 PyBytes_GET_SIZE(obj),
1986 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001987 _Py_IDENTIFIER(latin1);
1988
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001989 if (unicode_str == NULL)
1990 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001991 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001992 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001993 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001994 Py_DECREF(unicode_str);
1995 }
1996
1997 if (reduce_value == NULL)
1998 return -1;
1999
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000 /* save_reduce() will memoize the object automatically. */
2001 status = save_reduce(self, reduce_value, obj);
2002 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002003 return status;
2004 }
2005 else {
2006 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002007 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002008 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002009
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002010 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 if (size < 0)
2012 return -1;
2013
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002014 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 header[0] = SHORT_BINBYTES;
2016 header[1] = (unsigned char)size;
2017 len = 2;
2018 }
2019 else if (size <= 0xffffffffL) {
2020 header[0] = BINBYTES;
2021 header[1] = (unsigned char)(size & 0xff);
2022 header[2] = (unsigned char)((size >> 8) & 0xff);
2023 header[3] = (unsigned char)((size >> 16) & 0xff);
2024 header[4] = (unsigned char)((size >> 24) & 0xff);
2025 len = 5;
2026 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002027 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002029 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002030 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002031 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002033 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002034 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 return -1; /* string too large */
2036 }
2037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002038 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002039 return -1;
2040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042 return -1;
2043
2044 if (memo_put(self, obj) < 0)
2045 return -1;
2046
2047 return 0;
2048 }
2049}
2050
2051/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2052 backslash and newline characters to \uXXXX escapes. */
2053static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002054raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055{
2056 PyObject *repr, *result;
2057 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002058 Py_ssize_t i, size, expandsize;
2059 void *data;
2060 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002062 if (PyUnicode_READY(obj))
2063 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002065 size = PyUnicode_GET_LENGTH(obj);
2066 data = PyUnicode_DATA(obj);
2067 kind = PyUnicode_KIND(obj);
2068 if (kind == PyUnicode_4BYTE_KIND)
2069 expandsize = 10;
2070 else
2071 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002072
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002073 if (size > PY_SSIZE_T_MAX / expandsize)
2074 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002075 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 if (repr == NULL)
2077 return NULL;
2078 if (size == 0)
2079 goto done;
2080
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002081 p = PyByteArray_AS_STRING(repr);
2082 for (i=0; i < size; i++) {
2083 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 /* Map 32-bit characters to '\Uxxxxxxxx' */
2085 if (ch >= 0x10000) {
2086 *p++ = '\\';
2087 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002088 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2089 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2090 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2091 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2092 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2093 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2094 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2095 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002098 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 *p++ = '\\';
2100 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002101 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2102 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2103 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2104 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002106 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 else
2108 *p++ = (char) ch;
2109 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002110 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002113 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 Py_DECREF(repr);
2115 return result;
2116}
2117
2118static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002119write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2120{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002121 char header[9];
2122 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002123
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002124 if (size <= 0xff && self->proto >= 4) {
2125 header[0] = SHORT_BINUNICODE;
2126 header[1] = (unsigned char)(size & 0xff);
2127 len = 2;
2128 }
2129 else if (size <= 0xffffffffUL) {
2130 header[0] = BINUNICODE;
2131 header[1] = (unsigned char)(size & 0xff);
2132 header[2] = (unsigned char)((size >> 8) & 0xff);
2133 header[3] = (unsigned char)((size >> 16) & 0xff);
2134 header[4] = (unsigned char)((size >> 24) & 0xff);
2135 len = 5;
2136 }
2137 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002138 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002139 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002140 len = 9;
2141 }
2142 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002143 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002144 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002145 return -1;
2146 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002147
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002148 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002149 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002150 if (_Pickler_Write(self, data, size) < 0)
2151 return -1;
2152
2153 return 0;
2154}
2155
2156static int
2157write_unicode_binary(PicklerObject *self, PyObject *obj)
2158{
2159 PyObject *encoded = NULL;
2160 Py_ssize_t size;
2161 char *data;
2162 int r;
2163
2164 if (PyUnicode_READY(obj))
2165 return -1;
2166
2167 data = PyUnicode_AsUTF8AndSize(obj, &size);
2168 if (data != NULL)
2169 return write_utf8(self, data, size);
2170
2171 /* Issue #8383: for strings with lone surrogates, fallback on the
2172 "surrogatepass" error handler. */
2173 PyErr_Clear();
2174 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2175 if (encoded == NULL)
2176 return -1;
2177
2178 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2179 PyBytes_GET_SIZE(encoded));
2180 Py_DECREF(encoded);
2181 return r;
2182}
2183
2184static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185save_unicode(PicklerObject *self, PyObject *obj)
2186{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002188 if (write_unicode_binary(self, obj) < 0)
2189 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002190 }
2191 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192 PyObject *encoded;
2193 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002194 const char unicode_op = UNICODE;
2195
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002196 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002197 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2201 Py_DECREF(encoded);
2202 return -1;
2203 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204
2205 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2207 Py_DECREF(encoded);
2208 return -1;
2209 }
2210 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002212 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002213 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002214 }
2215 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002216 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002218 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219}
2220
2221/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2222static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002223store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002225 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002226
2227 assert(PyTuple_Size(t) == len);
2228
2229 for (i = 0; i < len; i++) {
2230 PyObject *element = PyTuple_GET_ITEM(t, i);
2231
2232 if (element == NULL)
2233 return -1;
2234 if (save(self, element, 0) < 0)
2235 return -1;
2236 }
2237
2238 return 0;
2239}
2240
2241/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2242 * used across protocols to minimize the space needed to pickle them.
2243 * Tuples are also the only builtin immutable type that can be recursive
2244 * (a tuple can be reached from itself), and that requires some subtle
2245 * magic so that it works in all cases. IOW, this is a long routine.
2246 */
2247static int
2248save_tuple(PicklerObject *self, PyObject *obj)
2249{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002250 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251
2252 const char mark_op = MARK;
2253 const char tuple_op = TUPLE;
2254 const char pop_op = POP;
2255 const char pop_mark_op = POP_MARK;
2256 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2257
2258 if ((len = PyTuple_Size(obj)) < 0)
2259 return -1;
2260
2261 if (len == 0) {
2262 char pdata[2];
2263
2264 if (self->proto) {
2265 pdata[0] = EMPTY_TUPLE;
2266 len = 1;
2267 }
2268 else {
2269 pdata[0] = MARK;
2270 pdata[1] = TUPLE;
2271 len = 2;
2272 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002273 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 return -1;
2275 return 0;
2276 }
2277
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002278 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 * saving the tuple elements, the tuple must be recursive, in
2280 * which case we'll pop everything we put on the stack, and fetch
2281 * its value from the memo.
2282 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283 if (len <= 3 && self->proto >= 2) {
2284 /* Use TUPLE{1,2,3} opcodes. */
2285 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002286 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002288 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289 /* pop the len elements */
2290 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002291 if (_Pickler_Write(self, &pop_op, 1) < 0)
2292 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002294 if (memo_get(self, obj) < 0)
2295 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 return 0;
2298 }
2299 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002300 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2301 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302 }
2303 goto memoize;
2304 }
2305
2306 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2307 * Generate MARK e1 e2 ... TUPLE
2308 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002309 if (_Pickler_Write(self, &mark_op, 1) < 0)
2310 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002311
2312 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316 /* pop the stack stuff we pushed */
2317 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002318 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2319 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320 }
2321 else {
2322 /* Note that we pop one more than len, to remove
2323 * the MARK too.
2324 */
2325 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002326 if (_Pickler_Write(self, &pop_op, 1) < 0)
2327 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 }
2329 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (memo_get(self, obj) < 0)
2331 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002333 return 0;
2334 }
2335 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338 }
2339
2340 memoize:
2341 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345}
2346
2347/* iter is an iterator giving items, and we batch up chunks of
2348 * MARK item item ... item APPENDS
2349 * opcode sequences. Calling code should have arranged to first create an
2350 * empty list, or list-like object, for the APPENDS to operate on.
2351 * Returns 0 on success, <0 on error.
2352 */
2353static int
2354batch_list(PicklerObject *self, PyObject *iter)
2355{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002356 PyObject *obj = NULL;
2357 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 int i, n;
2359
2360 const char mark_op = MARK;
2361 const char append_op = APPEND;
2362 const char appends_op = APPENDS;
2363
2364 assert(iter != NULL);
2365
2366 /* XXX: I think this function could be made faster by avoiding the
2367 iterator interface and fetching objects directly from list using
2368 PyList_GET_ITEM.
2369 */
2370
2371 if (self->proto == 0) {
2372 /* APPENDS isn't available; do one at a time. */
2373 for (;;) {
2374 obj = PyIter_Next(iter);
2375 if (obj == NULL) {
2376 if (PyErr_Occurred())
2377 return -1;
2378 break;
2379 }
2380 i = save(self, obj, 0);
2381 Py_DECREF(obj);
2382 if (i < 0)
2383 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385 return -1;
2386 }
2387 return 0;
2388 }
2389
2390 /* proto > 0: write in batches of BATCHSIZE. */
2391 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002392 /* Get first item */
2393 firstitem = PyIter_Next(iter);
2394 if (firstitem == NULL) {
2395 if (PyErr_Occurred())
2396 goto error;
2397
2398 /* nothing more to add */
2399 break;
2400 }
2401
2402 /* Try to get a second item */
2403 obj = PyIter_Next(iter);
2404 if (obj == NULL) {
2405 if (PyErr_Occurred())
2406 goto error;
2407
2408 /* Only one item to write */
2409 if (save(self, firstitem, 0) < 0)
2410 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002411 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002412 goto error;
2413 Py_CLEAR(firstitem);
2414 break;
2415 }
2416
2417 /* More than one item to write */
2418
2419 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002420 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002421 goto error;
2422
2423 if (save(self, firstitem, 0) < 0)
2424 goto error;
2425 Py_CLEAR(firstitem);
2426 n = 1;
2427
2428 /* Fetch and save up to BATCHSIZE items */
2429 while (obj) {
2430 if (save(self, obj, 0) < 0)
2431 goto error;
2432 Py_CLEAR(obj);
2433 n += 1;
2434
2435 if (n == BATCHSIZE)
2436 break;
2437
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 obj = PyIter_Next(iter);
2439 if (obj == NULL) {
2440 if (PyErr_Occurred())
2441 goto error;
2442 break;
2443 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444 }
2445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002446 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002447 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449 } while (n == BATCHSIZE);
2450 return 0;
2451
2452 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002453 Py_XDECREF(firstitem);
2454 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 return -1;
2456}
2457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002458/* This is a variant of batch_list() above, specialized for lists (with no
2459 * support for list subclasses). Like batch_list(), we batch up chunks of
2460 * MARK item item ... item APPENDS
2461 * opcode sequences. Calling code should have arranged to first create an
2462 * empty list, or list-like object, for the APPENDS to operate on.
2463 * Returns 0 on success, -1 on error.
2464 *
2465 * This version is considerably faster than batch_list(), if less general.
2466 *
2467 * Note that this only works for protocols > 0.
2468 */
2469static int
2470batch_list_exact(PicklerObject *self, PyObject *obj)
2471{
2472 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002473 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002474
2475 const char append_op = APPEND;
2476 const char appends_op = APPENDS;
2477 const char mark_op = MARK;
2478
2479 assert(obj != NULL);
2480 assert(self->proto > 0);
2481 assert(PyList_CheckExact(obj));
2482
2483 if (PyList_GET_SIZE(obj) == 1) {
2484 item = PyList_GET_ITEM(obj, 0);
2485 if (save(self, item, 0) < 0)
2486 return -1;
2487 if (_Pickler_Write(self, &append_op, 1) < 0)
2488 return -1;
2489 return 0;
2490 }
2491
2492 /* Write in batches of BATCHSIZE. */
2493 total = 0;
2494 do {
2495 this_batch = 0;
2496 if (_Pickler_Write(self, &mark_op, 1) < 0)
2497 return -1;
2498 while (total < PyList_GET_SIZE(obj)) {
2499 item = PyList_GET_ITEM(obj, total);
2500 if (save(self, item, 0) < 0)
2501 return -1;
2502 total++;
2503 if (++this_batch == BATCHSIZE)
2504 break;
2505 }
2506 if (_Pickler_Write(self, &appends_op, 1) < 0)
2507 return -1;
2508
2509 } while (total < PyList_GET_SIZE(obj));
2510
2511 return 0;
2512}
2513
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002514static int
2515save_list(PicklerObject *self, PyObject *obj)
2516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002518 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 int status = 0;
2520
2521 if (self->fast && !fast_save_enter(self, obj))
2522 goto error;
2523
2524 /* Create an empty list. */
2525 if (self->bin) {
2526 header[0] = EMPTY_LIST;
2527 len = 1;
2528 }
2529 else {
2530 header[0] = MARK;
2531 header[1] = LIST;
2532 len = 2;
2533 }
2534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 goto error;
2537
2538 /* Get list length, and bow out early if empty. */
2539 if ((len = PyList_Size(obj)) < 0)
2540 goto error;
2541
2542 if (memo_put(self, obj) < 0)
2543 goto error;
2544
2545 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002546 /* Materialize the list elements. */
2547 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002548 if (Py_EnterRecursiveCall(" while pickling an object"))
2549 goto error;
2550 status = batch_list_exact(self, obj);
2551 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 } else {
2553 PyObject *iter = PyObject_GetIter(obj);
2554 if (iter == NULL)
2555 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002557 if (Py_EnterRecursiveCall(" while pickling an object")) {
2558 Py_DECREF(iter);
2559 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002560 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002561 status = batch_list(self, iter);
2562 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002563 Py_DECREF(iter);
2564 }
2565 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002566 if (0) {
2567 error:
2568 status = -1;
2569 }
2570
2571 if (self->fast && !fast_save_leave(self, obj))
2572 status = -1;
2573
2574 return status;
2575}
2576
2577/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2578 * MARK key value ... key value SETITEMS
2579 * opcode sequences. Calling code should have arranged to first create an
2580 * empty dict, or dict-like object, for the SETITEMS to operate on.
2581 * Returns 0 on success, <0 on error.
2582 *
2583 * This is very much like batch_list(). The difference between saving
2584 * elements directly, and picking apart two-tuples, is so long-winded at
2585 * the C level, though, that attempts to combine these routines were too
2586 * ugly to bear.
2587 */
2588static int
2589batch_dict(PicklerObject *self, PyObject *iter)
2590{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002591 PyObject *obj = NULL;
2592 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002593 int i, n;
2594
2595 const char mark_op = MARK;
2596 const char setitem_op = SETITEM;
2597 const char setitems_op = SETITEMS;
2598
2599 assert(iter != NULL);
2600
2601 if (self->proto == 0) {
2602 /* SETITEMS isn't available; do one at a time. */
2603 for (;;) {
2604 obj = PyIter_Next(iter);
2605 if (obj == NULL) {
2606 if (PyErr_Occurred())
2607 return -1;
2608 break;
2609 }
2610 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2611 PyErr_SetString(PyExc_TypeError, "dict items "
2612 "iterator must return 2-tuples");
2613 return -1;
2614 }
2615 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2616 if (i >= 0)
2617 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2618 Py_DECREF(obj);
2619 if (i < 0)
2620 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002622 return -1;
2623 }
2624 return 0;
2625 }
2626
2627 /* proto > 0: write in batches of BATCHSIZE. */
2628 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002629 /* Get first item */
2630 firstitem = PyIter_Next(iter);
2631 if (firstitem == NULL) {
2632 if (PyErr_Occurred())
2633 goto error;
2634
2635 /* nothing more to add */
2636 break;
2637 }
2638 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2639 PyErr_SetString(PyExc_TypeError, "dict items "
2640 "iterator must return 2-tuples");
2641 goto error;
2642 }
2643
2644 /* Try to get a second item */
2645 obj = PyIter_Next(iter);
2646 if (obj == NULL) {
2647 if (PyErr_Occurred())
2648 goto error;
2649
2650 /* Only one item to write */
2651 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2652 goto error;
2653 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2654 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002655 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002656 goto error;
2657 Py_CLEAR(firstitem);
2658 break;
2659 }
2660
2661 /* More than one item to write */
2662
2663 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002664 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002665 goto error;
2666
2667 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2668 goto error;
2669 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2670 goto error;
2671 Py_CLEAR(firstitem);
2672 n = 1;
2673
2674 /* Fetch and save up to BATCHSIZE items */
2675 while (obj) {
2676 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2677 PyErr_SetString(PyExc_TypeError, "dict items "
2678 "iterator must return 2-tuples");
2679 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002680 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002681 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2682 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2683 goto error;
2684 Py_CLEAR(obj);
2685 n += 1;
2686
2687 if (n == BATCHSIZE)
2688 break;
2689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002690 obj = PyIter_Next(iter);
2691 if (obj == NULL) {
2692 if (PyErr_Occurred())
2693 goto error;
2694 break;
2695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 }
2697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002698 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002699 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002701 } while (n == BATCHSIZE);
2702 return 0;
2703
2704 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002705 Py_XDECREF(firstitem);
2706 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002707 return -1;
2708}
2709
Collin Winter5c9b02d2009-05-25 05:43:30 +00002710/* This is a variant of batch_dict() above that specializes for dicts, with no
2711 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2712 * MARK key value ... key value SETITEMS
2713 * opcode sequences. Calling code should have arranged to first create an
2714 * empty dict, or dict-like object, for the SETITEMS to operate on.
2715 * Returns 0 on success, -1 on error.
2716 *
2717 * Note that this currently doesn't work for protocol 0.
2718 */
2719static int
2720batch_dict_exact(PicklerObject *self, PyObject *obj)
2721{
2722 PyObject *key = NULL, *value = NULL;
2723 int i;
2724 Py_ssize_t dict_size, ppos = 0;
2725
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002726 const char mark_op = MARK;
2727 const char setitem_op = SETITEM;
2728 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002729
2730 assert(obj != NULL);
2731 assert(self->proto > 0);
2732
2733 dict_size = PyDict_Size(obj);
2734
2735 /* Special-case len(d) == 1 to save space. */
2736 if (dict_size == 1) {
2737 PyDict_Next(obj, &ppos, &key, &value);
2738 if (save(self, key, 0) < 0)
2739 return -1;
2740 if (save(self, value, 0) < 0)
2741 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002743 return -1;
2744 return 0;
2745 }
2746
2747 /* Write in batches of BATCHSIZE. */
2748 do {
2749 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002750 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002751 return -1;
2752 while (PyDict_Next(obj, &ppos, &key, &value)) {
2753 if (save(self, key, 0) < 0)
2754 return -1;
2755 if (save(self, value, 0) < 0)
2756 return -1;
2757 if (++i == BATCHSIZE)
2758 break;
2759 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002761 return -1;
2762 if (PyDict_Size(obj) != dict_size) {
2763 PyErr_Format(
2764 PyExc_RuntimeError,
2765 "dictionary changed size during iteration");
2766 return -1;
2767 }
2768
2769 } while (i == BATCHSIZE);
2770 return 0;
2771}
2772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002773static int
2774save_dict(PicklerObject *self, PyObject *obj)
2775{
2776 PyObject *items, *iter;
2777 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002778 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 int status = 0;
2780
2781 if (self->fast && !fast_save_enter(self, obj))
2782 goto error;
2783
2784 /* Create an empty dict. */
2785 if (self->bin) {
2786 header[0] = EMPTY_DICT;
2787 len = 1;
2788 }
2789 else {
2790 header[0] = MARK;
2791 header[1] = DICT;
2792 len = 2;
2793 }
2794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002795 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002796 goto error;
2797
2798 /* Get dict size, and bow out early if empty. */
2799 if ((len = PyDict_Size(obj)) < 0)
2800 goto error;
2801
2802 if (memo_put(self, obj) < 0)
2803 goto error;
2804
2805 if (len != 0) {
2806 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002807 if (PyDict_CheckExact(obj) && self->proto > 0) {
2808 /* We can take certain shortcuts if we know this is a dict and
2809 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002810 if (Py_EnterRecursiveCall(" while pickling an object"))
2811 goto error;
2812 status = batch_dict_exact(self, obj);
2813 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002814 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002815 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002816
2817 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002818 if (items == NULL)
2819 goto error;
2820 iter = PyObject_GetIter(items);
2821 Py_DECREF(items);
2822 if (iter == NULL)
2823 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002824 if (Py_EnterRecursiveCall(" while pickling an object")) {
2825 Py_DECREF(iter);
2826 goto error;
2827 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002828 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002829 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002830 Py_DECREF(iter);
2831 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002832 }
2833
2834 if (0) {
2835 error:
2836 status = -1;
2837 }
2838
2839 if (self->fast && !fast_save_leave(self, obj))
2840 status = -1;
2841
2842 return status;
2843}
2844
2845static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002846save_set(PicklerObject *self, PyObject *obj)
2847{
2848 PyObject *item;
2849 int i;
2850 Py_ssize_t set_size, ppos = 0;
2851 Py_hash_t hash;
2852
2853 const char empty_set_op = EMPTY_SET;
2854 const char mark_op = MARK;
2855 const char additems_op = ADDITEMS;
2856
2857 if (self->proto < 4) {
2858 PyObject *items;
2859 PyObject *reduce_value;
2860 int status;
2861
2862 items = PySequence_List(obj);
2863 if (items == NULL) {
2864 return -1;
2865 }
2866 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2867 Py_DECREF(items);
2868 if (reduce_value == NULL) {
2869 return -1;
2870 }
2871 /* save_reduce() will memoize the object automatically. */
2872 status = save_reduce(self, reduce_value, obj);
2873 Py_DECREF(reduce_value);
2874 return status;
2875 }
2876
2877 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2878 return -1;
2879
2880 if (memo_put(self, obj) < 0)
2881 return -1;
2882
2883 set_size = PySet_GET_SIZE(obj);
2884 if (set_size == 0)
2885 return 0; /* nothing to do */
2886
2887 /* Write in batches of BATCHSIZE. */
2888 do {
2889 i = 0;
2890 if (_Pickler_Write(self, &mark_op, 1) < 0)
2891 return -1;
2892 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2893 if (save(self, item, 0) < 0)
2894 return -1;
2895 if (++i == BATCHSIZE)
2896 break;
2897 }
2898 if (_Pickler_Write(self, &additems_op, 1) < 0)
2899 return -1;
2900 if (PySet_GET_SIZE(obj) != set_size) {
2901 PyErr_Format(
2902 PyExc_RuntimeError,
2903 "set changed size during iteration");
2904 return -1;
2905 }
2906 } while (i == BATCHSIZE);
2907
2908 return 0;
2909}
2910
2911static int
2912save_frozenset(PicklerObject *self, PyObject *obj)
2913{
2914 PyObject *iter;
2915
2916 const char mark_op = MARK;
2917 const char frozenset_op = FROZENSET;
2918
2919 if (self->fast && !fast_save_enter(self, obj))
2920 return -1;
2921
2922 if (self->proto < 4) {
2923 PyObject *items;
2924 PyObject *reduce_value;
2925 int status;
2926
2927 items = PySequence_List(obj);
2928 if (items == NULL) {
2929 return -1;
2930 }
2931 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2932 items);
2933 Py_DECREF(items);
2934 if (reduce_value == NULL) {
2935 return -1;
2936 }
2937 /* save_reduce() will memoize the object automatically. */
2938 status = save_reduce(self, reduce_value, obj);
2939 Py_DECREF(reduce_value);
2940 return status;
2941 }
2942
2943 if (_Pickler_Write(self, &mark_op, 1) < 0)
2944 return -1;
2945
2946 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002947 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002948 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002949 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002950 for (;;) {
2951 PyObject *item;
2952
2953 item = PyIter_Next(iter);
2954 if (item == NULL) {
2955 if (PyErr_Occurred()) {
2956 Py_DECREF(iter);
2957 return -1;
2958 }
2959 break;
2960 }
2961 if (save(self, item, 0) < 0) {
2962 Py_DECREF(item);
2963 Py_DECREF(iter);
2964 return -1;
2965 }
2966 Py_DECREF(item);
2967 }
2968 Py_DECREF(iter);
2969
2970 /* If the object is already in the memo, this means it is
2971 recursive. In this case, throw away everything we put on the
2972 stack, and fetch the object back from the memo. */
2973 if (PyMemoTable_Get(self->memo, obj)) {
2974 const char pop_mark_op = POP_MARK;
2975
2976 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2977 return -1;
2978 if (memo_get(self, obj) < 0)
2979 return -1;
2980 return 0;
2981 }
2982
2983 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2984 return -1;
2985 if (memo_put(self, obj) < 0)
2986 return -1;
2987
2988 return 0;
2989}
2990
2991static int
2992fix_imports(PyObject **module_name, PyObject **global_name)
2993{
2994 PyObject *key;
2995 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002996 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002997
2998 key = PyTuple_Pack(2, *module_name, *global_name);
2999 if (key == NULL)
3000 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003001 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003002 Py_DECREF(key);
3003 if (item) {
3004 PyObject *fixed_module_name;
3005 PyObject *fixed_global_name;
3006
3007 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3008 PyErr_Format(PyExc_RuntimeError,
3009 "_compat_pickle.REVERSE_NAME_MAPPING values "
3010 "should be 2-tuples, not %.200s",
3011 Py_TYPE(item)->tp_name);
3012 return -1;
3013 }
3014 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3015 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3016 if (!PyUnicode_Check(fixed_module_name) ||
3017 !PyUnicode_Check(fixed_global_name)) {
3018 PyErr_Format(PyExc_RuntimeError,
3019 "_compat_pickle.REVERSE_NAME_MAPPING values "
3020 "should be pairs of str, not (%.200s, %.200s)",
3021 Py_TYPE(fixed_module_name)->tp_name,
3022 Py_TYPE(fixed_global_name)->tp_name);
3023 return -1;
3024 }
3025
3026 Py_CLEAR(*module_name);
3027 Py_CLEAR(*global_name);
3028 Py_INCREF(fixed_module_name);
3029 Py_INCREF(fixed_global_name);
3030 *module_name = fixed_module_name;
3031 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003032 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003033 }
3034 else if (PyErr_Occurred()) {
3035 return -1;
3036 }
3037
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003038 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003039 if (item) {
3040 if (!PyUnicode_Check(item)) {
3041 PyErr_Format(PyExc_RuntimeError,
3042 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3043 "should be strings, not %.200s",
3044 Py_TYPE(item)->tp_name);
3045 return -1;
3046 }
3047 Py_CLEAR(*module_name);
3048 Py_INCREF(item);
3049 *module_name = item;
3050 }
3051 else if (PyErr_Occurred()) {
3052 return -1;
3053 }
3054
3055 return 0;
3056}
3057
3058static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003059save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3060{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003061 PyObject *global_name = NULL;
3062 PyObject *module_name = NULL;
3063 PyObject *module = NULL;
3064 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003065 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003066 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003067 _Py_IDENTIFIER(__name__);
3068 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003069
3070 const char global_op = GLOBAL;
3071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003072 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003073 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003074 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003075 }
3076 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003077 if (self->proto >= 4) {
3078 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3079 if (global_name == NULL) {
3080 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3081 goto error;
3082 PyErr_Clear();
3083 }
3084 }
3085 if (global_name == NULL) {
3086 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3087 if (global_name == NULL)
3088 goto error;
3089 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003090 }
3091
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003092 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003093 if (module_name == NULL)
3094 goto error;
3095
3096 /* XXX: Change to use the import C API directly with level=0 to disallow
3097 relative imports.
3098
3099 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3100 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3101 custom import functions (IMHO, this would be a nice security
3102 feature). The import C API would need to be extended to support the
3103 extra parameters of __import__ to fix that. */
3104 module = PyImport_Import(module_name);
3105 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003106 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003107 "Can't pickle %R: import of module %R failed",
3108 obj, module_name);
3109 goto error;
3110 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003111 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003112 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003113 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003114 "Can't pickle %R: attribute lookup %S on %S failed",
3115 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003116 goto error;
3117 }
3118 if (cls != obj) {
3119 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003120 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003121 "Can't pickle %R: it's not the same object as %S.%S",
3122 obj, module_name, global_name);
3123 goto error;
3124 }
3125 Py_DECREF(cls);
3126
3127 if (self->proto >= 2) {
3128 /* See whether this is in the extension registry, and if
3129 * so generate an EXT opcode.
3130 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003131 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003132 PyObject *code_obj; /* extension code as Python object */
3133 long code; /* extension code as C value */
3134 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003135 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003137 extension_key = PyTuple_Pack(2, module_name, global_name);
3138 if (extension_key == NULL) {
3139 goto error;
3140 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003141 code_obj = PyDict_GetItemWithError(st->extension_registry,
3142 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003143 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003144 /* The object is not registered in the extension registry.
3145 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003146 if (code_obj == NULL) {
3147 if (PyErr_Occurred()) {
3148 goto error;
3149 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003150 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003151 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003152
3153 /* XXX: pickle.py doesn't check neither the type, nor the range
3154 of the value returned by the extension_registry. It should for
3155 consistency. */
3156
3157 /* Verify code_obj has the right type and value. */
3158 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003159 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003160 "Can't pickle %R: extension code %R isn't an integer",
3161 obj, code_obj);
3162 goto error;
3163 }
3164 code = PyLong_AS_LONG(code_obj);
3165 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003166 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003167 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3168 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169 goto error;
3170 }
3171
3172 /* Generate an EXT opcode. */
3173 if (code <= 0xff) {
3174 pdata[0] = EXT1;
3175 pdata[1] = (unsigned char)code;
3176 n = 2;
3177 }
3178 else if (code <= 0xffff) {
3179 pdata[0] = EXT2;
3180 pdata[1] = (unsigned char)(code & 0xff);
3181 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3182 n = 3;
3183 }
3184 else {
3185 pdata[0] = EXT4;
3186 pdata[1] = (unsigned char)(code & 0xff);
3187 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3188 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3189 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3190 n = 5;
3191 }
3192
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003193 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194 goto error;
3195 }
3196 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003198 if (self->proto >= 4) {
3199 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003200
Christian Heimese8b1ba12013-11-23 21:13:39 +01003201 if (save(self, module_name, 0) < 0)
3202 goto error;
3203 if (save(self, global_name, 0) < 0)
3204 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003205
3206 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3207 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 }
3209 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003210 /* Generate a normal global opcode if we are using a pickle
3211 protocol < 4, or if the object is not registered in the
3212 extension registry. */
3213 PyObject *encoded;
3214 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003216 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003217 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003218
3219 /* For protocol < 3 and if the user didn't request against doing
3220 so, we convert module names to the old 2.x module names. */
3221 if (self->proto < 3 && self->fix_imports) {
3222 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003223 goto error;
3224 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003225 }
3226
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003227 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3228 both the module name and the global name using UTF-8. We do so
3229 only when we are using the pickle protocol newer than version
3230 3. This is to ensure compatibility with older Unpickler running
3231 on Python 2.x. */
3232 if (self->proto == 3) {
3233 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003234 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235 else {
3236 unicode_encoder = PyUnicode_AsASCIIString;
3237 }
3238 encoded = unicode_encoder(module_name);
3239 if (encoded == NULL) {
3240 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003241 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003242 "can't pickle module identifier '%S' using "
3243 "pickle protocol %i",
3244 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003245 goto error;
3246 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003247 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3248 PyBytes_GET_SIZE(encoded)) < 0) {
3249 Py_DECREF(encoded);
3250 goto error;
3251 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003252 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003253 if(_Pickler_Write(self, "\n", 1) < 0)
3254 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003255
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003256 /* Save the name of the module. */
3257 encoded = unicode_encoder(global_name);
3258 if (encoded == NULL) {
3259 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003260 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003261 "can't pickle global identifier '%S' using "
3262 "pickle protocol %i",
3263 global_name, self->proto);
3264 goto error;
3265 }
3266 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3267 PyBytes_GET_SIZE(encoded)) < 0) {
3268 Py_DECREF(encoded);
3269 goto error;
3270 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003272 if (_Pickler_Write(self, "\n", 1) < 0)
3273 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003275 /* Memoize the object. */
3276 if (memo_put(self, obj) < 0)
3277 goto error;
3278 }
3279
3280 if (0) {
3281 error:
3282 status = -1;
3283 }
3284 Py_XDECREF(module_name);
3285 Py_XDECREF(global_name);
3286 Py_XDECREF(module);
3287
3288 return status;
3289}
3290
3291static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003292save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3293{
3294 PyObject *reduce_value;
3295 int status;
3296
3297 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3298 if (reduce_value == NULL) {
3299 return -1;
3300 }
3301 status = save_reduce(self, reduce_value, obj);
3302 Py_DECREF(reduce_value);
3303 return status;
3304}
3305
3306static int
3307save_type(PicklerObject *self, PyObject *obj)
3308{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003309 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003310 return save_singleton_type(self, obj, Py_None);
3311 }
3312 else if (obj == (PyObject *)&PyEllipsis_Type) {
3313 return save_singleton_type(self, obj, Py_Ellipsis);
3314 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003315 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003316 return save_singleton_type(self, obj, Py_NotImplemented);
3317 }
3318 return save_global(self, obj, NULL);
3319}
3320
3321static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003322save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3323{
3324 PyObject *pid = NULL;
3325 int status = 0;
3326
3327 const char persid_op = PERSID;
3328 const char binpersid_op = BINPERSID;
3329
3330 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003331 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 if (pid == NULL)
3333 return -1;
3334
3335 if (pid != Py_None) {
3336 if (self->bin) {
3337 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003338 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003339 goto error;
3340 }
3341 else {
3342 PyObject *pid_str = NULL;
3343 char *pid_ascii_bytes;
3344 Py_ssize_t size;
3345
3346 pid_str = PyObject_Str(pid);
3347 if (pid_str == NULL)
3348 goto error;
3349
3350 /* XXX: Should it check whether the persistent id only contains
3351 ASCII characters? And what if the pid contains embedded
3352 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003353 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 Py_DECREF(pid_str);
3355 if (pid_ascii_bytes == NULL)
3356 goto error;
3357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003358 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3359 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3360 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 goto error;
3362 }
3363 status = 1;
3364 }
3365
3366 if (0) {
3367 error:
3368 status = -1;
3369 }
3370 Py_XDECREF(pid);
3371
3372 return status;
3373}
3374
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003375static PyObject *
3376get_class(PyObject *obj)
3377{
3378 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003379 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003380
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003381 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003382 if (cls == NULL) {
3383 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3384 PyErr_Clear();
3385 cls = (PyObject *) Py_TYPE(obj);
3386 Py_INCREF(cls);
3387 }
3388 }
3389 return cls;
3390}
3391
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3393 * appropriate __reduce__ method for obj.
3394 */
3395static int
3396save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3397{
3398 PyObject *callable;
3399 PyObject *argtup;
3400 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003401 PyObject *listitems = Py_None;
3402 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003403 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003404 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003405 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003406
3407 const char reduce_op = REDUCE;
3408 const char build_op = BUILD;
3409 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003410 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003411
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003412 size = PyTuple_Size(args);
3413 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003414 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003415 "__reduce__ must contain 2 through 5 elements");
3416 return -1;
3417 }
3418
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3420 &callable, &argtup, &state, &listitems, &dictitems))
3421 return -1;
3422
3423 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003424 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003425 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003426 return -1;
3427 }
3428 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003429 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003430 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431 return -1;
3432 }
3433
3434 if (state == Py_None)
3435 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003436
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 if (listitems == Py_None)
3438 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003439 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003440 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003441 "returned by __reduce__ must be an iterator, not %s",
3442 Py_TYPE(listitems)->tp_name);
3443 return -1;
3444 }
3445
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003446 if (dictitems == Py_None)
3447 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003448 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003449 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003450 "returned by __reduce__ must be an iterator, not %s",
3451 Py_TYPE(dictitems)->tp_name);
3452 return -1;
3453 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003454
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003455 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003456 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003457 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458
Victor Stinner804e05e2013-11-14 01:26:17 +01003459 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003460 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003461 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003462 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003463 }
3464 PyErr_Clear();
3465 }
3466 else if (self->proto >= 4) {
3467 _Py_IDENTIFIER(__newobj_ex__);
3468 use_newobj_ex = PyUnicode_Check(name) &&
3469 PyUnicode_Compare(
3470 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3471 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003472 }
3473 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003475 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 PyUnicode_Compare(
3477 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003478 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479 }
3480 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003481
3482 if (use_newobj_ex) {
3483 PyObject *cls;
3484 PyObject *args;
3485 PyObject *kwargs;
3486
3487 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003488 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 "length of the NEWOBJ_EX argument tuple must be "
3490 "exactly 3, not %zd", Py_SIZE(argtup));
3491 return -1;
3492 }
3493
3494 cls = PyTuple_GET_ITEM(argtup, 0);
3495 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003496 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 "first item from NEWOBJ_EX argument tuple must "
3498 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3499 return -1;
3500 }
3501 args = PyTuple_GET_ITEM(argtup, 1);
3502 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003503 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 "second item from NEWOBJ_EX argument tuple must "
3505 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3506 return -1;
3507 }
3508 kwargs = PyTuple_GET_ITEM(argtup, 2);
3509 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003510 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003511 "third item from NEWOBJ_EX argument tuple must "
3512 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3513 return -1;
3514 }
3515
3516 if (save(self, cls, 0) < 0 ||
3517 save(self, args, 0) < 0 ||
3518 save(self, kwargs, 0) < 0 ||
3519 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3520 return -1;
3521 }
3522 }
3523 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 PyObject *cls;
3525 PyObject *newargtup;
3526 PyObject *obj_class;
3527 int p;
3528
3529 /* Sanity checks. */
3530 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003531 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 return -1;
3533 }
3534
3535 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003536 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003537 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003538 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539 return -1;
3540 }
3541
3542 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003543 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 p = obj_class != cls; /* true iff a problem */
3545 Py_DECREF(obj_class);
3546 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003547 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548 "__newobj__ args has the wrong class");
3549 return -1;
3550 }
3551 }
3552 /* XXX: These calls save() are prone to infinite recursion. Imagine
3553 what happen if the value returned by the __reduce__() method of
3554 some extension type contains another object of the same type. Ouch!
3555
3556 Here is a quick example, that I ran into, to illustrate what I
3557 mean:
3558
3559 >>> import pickle, copyreg
3560 >>> copyreg.dispatch_table.pop(complex)
3561 >>> pickle.dumps(1+2j)
3562 Traceback (most recent call last):
3563 ...
3564 RuntimeError: maximum recursion depth exceeded
3565
3566 Removing the complex class from copyreg.dispatch_table made the
3567 __reduce_ex__() method emit another complex object:
3568
3569 >>> (1+1j).__reduce_ex__(2)
3570 (<function __newobj__ at 0xb7b71c3c>,
3571 (<class 'complex'>, (1+1j)), None, None, None)
3572
3573 Thus when save() was called on newargstup (the 2nd item) recursion
3574 ensued. Of course, the bug was in the complex class which had a
3575 broken __getnewargs__() that emitted another complex object. But,
3576 the point, here, is it is quite easy to end up with a broken reduce
3577 function. */
3578
3579 /* Save the class and its __new__ arguments. */
3580 if (save(self, cls, 0) < 0)
3581 return -1;
3582
3583 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3584 if (newargtup == NULL)
3585 return -1;
3586
3587 p = save(self, newargtup, 0);
3588 Py_DECREF(newargtup);
3589 if (p < 0)
3590 return -1;
3591
3592 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003593 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003594 return -1;
3595 }
3596 else { /* Not using NEWOBJ. */
3597 if (save(self, callable, 0) < 0 ||
3598 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003599 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600 return -1;
3601 }
3602
3603 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3604 the caller do not want to memoize the object. Not particularly useful,
3605 but that is to mimic the behavior save_reduce() in pickle.py when
3606 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003607 if (obj != NULL) {
3608 /* If the object is already in the memo, this means it is
3609 recursive. In this case, throw away everything we put on the
3610 stack, and fetch the object back from the memo. */
3611 if (PyMemoTable_Get(self->memo, obj)) {
3612 const char pop_op = POP;
3613
3614 if (_Pickler_Write(self, &pop_op, 1) < 0)
3615 return -1;
3616 if (memo_get(self, obj) < 0)
3617 return -1;
3618
3619 return 0;
3620 }
3621 else if (memo_put(self, obj) < 0)
3622 return -1;
3623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003624
3625 if (listitems && batch_list(self, listitems) < 0)
3626 return -1;
3627
3628 if (dictitems && batch_dict(self, dictitems) < 0)
3629 return -1;
3630
3631 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003632 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003633 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003634 return -1;
3635 }
3636
3637 return 0;
3638}
3639
3640static int
3641save(PicklerObject *self, PyObject *obj, int pers_save)
3642{
3643 PyTypeObject *type;
3644 PyObject *reduce_func = NULL;
3645 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646 int status = 0;
3647
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003648 if (_Pickler_OpcodeBoundary(self) < 0)
3649 return -1;
3650
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003651 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003652 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003653
3654 /* The extra pers_save argument is necessary to avoid calling save_pers()
3655 on its returned object. */
3656 if (!pers_save && self->pers_func) {
3657 /* save_pers() returns:
3658 -1 to signal an error;
3659 0 if it did nothing successfully;
3660 1 if a persistent id was saved.
3661 */
3662 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3663 goto done;
3664 }
3665
3666 type = Py_TYPE(obj);
3667
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003668 /* The old cPickle had an optimization that used switch-case statement
3669 dispatching on the first letter of the type name. This has was removed
3670 since benchmarks shown that this optimization was actually slowing
3671 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672
3673 /* Atom types; these aren't memoized, so don't check the memo. */
3674
3675 if (obj == Py_None) {
3676 status = save_none(self, obj);
3677 goto done;
3678 }
3679 else if (obj == Py_False || obj == Py_True) {
3680 status = save_bool(self, obj);
3681 goto done;
3682 }
3683 else if (type == &PyLong_Type) {
3684 status = save_long(self, obj);
3685 goto done;
3686 }
3687 else if (type == &PyFloat_Type) {
3688 status = save_float(self, obj);
3689 goto done;
3690 }
3691
3692 /* Check the memo to see if it has the object. If so, generate
3693 a GET (or BINGET) opcode, instead of pickling the object
3694 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003695 if (PyMemoTable_Get(self->memo, obj)) {
3696 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003697 goto error;
3698 goto done;
3699 }
3700
3701 if (type == &PyBytes_Type) {
3702 status = save_bytes(self, obj);
3703 goto done;
3704 }
3705 else if (type == &PyUnicode_Type) {
3706 status = save_unicode(self, obj);
3707 goto done;
3708 }
3709 else if (type == &PyDict_Type) {
3710 status = save_dict(self, obj);
3711 goto done;
3712 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003713 else if (type == &PySet_Type) {
3714 status = save_set(self, obj);
3715 goto done;
3716 }
3717 else if (type == &PyFrozenSet_Type) {
3718 status = save_frozenset(self, obj);
3719 goto done;
3720 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003721 else if (type == &PyList_Type) {
3722 status = save_list(self, obj);
3723 goto done;
3724 }
3725 else if (type == &PyTuple_Type) {
3726 status = save_tuple(self, obj);
3727 goto done;
3728 }
3729 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003730 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003731 goto done;
3732 }
3733 else if (type == &PyFunction_Type) {
3734 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003735 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003736 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003737
3738 /* XXX: This part needs some unit tests. */
3739
3740 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003741 * self.dispatch_table, copyreg.dispatch_table, the object's
3742 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003743 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003744 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003745 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003746 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3747 (PyObject *)type);
3748 if (reduce_func == NULL) {
3749 if (PyErr_Occurred()) {
3750 goto error;
3751 }
3752 } else {
3753 /* PyDict_GetItemWithError() returns a borrowed reference.
3754 Increase the reference count to be consistent with
3755 PyObject_GetItem and _PyObject_GetAttrId used below. */
3756 Py_INCREF(reduce_func);
3757 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003758 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003759 reduce_func = PyObject_GetItem(self->dispatch_table,
3760 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003761 if (reduce_func == NULL) {
3762 if (PyErr_ExceptionMatches(PyExc_KeyError))
3763 PyErr_Clear();
3764 else
3765 goto error;
3766 }
3767 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003769 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003770 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003771 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003772 else if (PyType_IsSubtype(type, &PyType_Type)) {
3773 status = save_global(self, obj, NULL);
3774 goto done;
3775 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003776 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003777 _Py_IDENTIFIER(__reduce__);
3778 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003779
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003780
3781 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3782 automatically defined as __reduce__. While this is convenient, this
3783 make it impossible to know which method was actually called. Of
3784 course, this is not a big deal. But still, it would be nice to let
3785 the user know which method was called when something go
3786 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3787 don't actually have to check for a __reduce__ method. */
3788
3789 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003790 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003791 if (reduce_func != NULL) {
3792 PyObject *proto;
3793 proto = PyLong_FromLong(self->proto);
3794 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003795 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796 }
3797 }
3798 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003799 PickleState *st = _Pickle_GetGlobalState();
3800
3801 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003803 }
3804 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003805 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003807 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003808 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003809 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003810 PyObject *empty_tuple = PyTuple_New(0);
3811 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003812 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003813 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003814 }
3815 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003816 PyErr_Format(st->PicklingError,
3817 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003818 type->tp_name, obj);
3819 goto error;
3820 }
3821 }
3822 }
3823
3824 if (reduce_value == NULL)
3825 goto error;
3826
3827 if (PyUnicode_Check(reduce_value)) {
3828 status = save_global(self, obj, reduce_value);
3829 goto done;
3830 }
3831
3832 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003833 PickleState *st = _Pickle_GetGlobalState();
3834 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 "__reduce__ must return a string or tuple");
3836 goto error;
3837 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838
3839 status = save_reduce(self, reduce_value, obj);
3840
3841 if (0) {
3842 error:
3843 status = -1;
3844 }
3845 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003846
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003847 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003848 Py_XDECREF(reduce_func);
3849 Py_XDECREF(reduce_value);
3850
3851 return status;
3852}
3853
3854static int
3855dump(PicklerObject *self, PyObject *obj)
3856{
3857 const char stop_op = STOP;
3858
3859 if (self->proto >= 2) {
3860 char header[2];
3861
3862 header[0] = PROTO;
3863 assert(self->proto >= 0 && self->proto < 256);
3864 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003865 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003867 if (self->proto >= 4)
3868 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 }
3870
3871 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003872 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003873 return -1;
3874
3875 return 0;
3876}
3877
Larry Hastings61272b72014-01-07 12:41:53 -08003878/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003879
3880_pickle.Pickler.clear_memo
3881
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003882Clears the pickler's "memo".
3883
3884The memo is the data structure that remembers which objects the
3885pickler has already seen, so that shared or recursive objects are
3886pickled by reference and not by value. This method is useful when
3887re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003888[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003889
Larry Hastings3cceb382014-01-04 11:09:09 -08003890static PyObject *
3891_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003892/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893{
3894 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003895 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896
3897 Py_RETURN_NONE;
3898}
3899
Larry Hastings61272b72014-01-07 12:41:53 -08003900/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003901
3902_pickle.Pickler.dump
3903
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003904 obj: object
3905 /
3906
3907Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003908[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003909
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003911_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003912/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003914 /* Check whether the Pickler was initialized correctly (issue3664).
3915 Developers often forget to call __init__() in their subclasses, which
3916 would trigger a segfault without this check. */
3917 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003918 PickleState *st = _Pickle_GetGlobalState();
3919 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003920 "Pickler.__init__() was not called by %s.__init__()",
3921 Py_TYPE(self)->tp_name);
3922 return NULL;
3923 }
3924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003925 if (_Pickler_ClearBuffer(self) < 0)
3926 return NULL;
3927
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928 if (dump(self, obj) < 0)
3929 return NULL;
3930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003931 if (_Pickler_FlushToFile(self) < 0)
3932 return NULL;
3933
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 Py_RETURN_NONE;
3935}
3936
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003937/*[clinic input]
3938
3939_pickle.Pickler.__sizeof__ -> Py_ssize_t
3940
3941Returns size in memory, in bytes.
3942[clinic start generated code]*/
3943
3944static Py_ssize_t
3945_pickle_Pickler___sizeof___impl(PicklerObject *self)
3946/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
3947{
3948 Py_ssize_t res, s;
3949
3950 res = sizeof(PicklerObject);
3951 if (self->memo != NULL) {
3952 res += sizeof(PyMemoTable);
3953 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
3954 }
3955 if (self->output_buffer != NULL) {
3956 s = _PySys_GetSizeOf(self->output_buffer);
3957 if (s == -1)
3958 return -1;
3959 res += s;
3960 }
3961 return res;
3962}
3963
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003965 _PICKLE_PICKLER_DUMP_METHODDEF
3966 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003967 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968 {NULL, NULL} /* sentinel */
3969};
3970
3971static void
3972Pickler_dealloc(PicklerObject *self)
3973{
3974 PyObject_GC_UnTrack(self);
3975
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003976 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003979 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003980 Py_XDECREF(self->fast_memo);
3981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003982 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983
3984 Py_TYPE(self)->tp_free((PyObject *)self);
3985}
3986
3987static int
3988Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3989{
3990 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003992 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 Py_VISIT(self->fast_memo);
3994 return 0;
3995}
3996
3997static int
3998Pickler_clear(PicklerObject *self)
3999{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004000 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004001 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004002 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004003 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 Py_CLEAR(self->fast_memo);
4005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004006 if (self->memo != NULL) {
4007 PyMemoTable *memo = self->memo;
4008 self->memo = NULL;
4009 PyMemoTable_Del(memo);
4010 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004011 return 0;
4012}
4013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004014
Larry Hastings61272b72014-01-07 12:41:53 -08004015/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004016
4017_pickle.Pickler.__init__
4018
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004019 file: object
4020 protocol: object = NULL
4021 fix_imports: bool = True
4022
4023This takes a binary file for writing a pickle data stream.
4024
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004025The optional *protocol* argument tells the pickler to use the given
4026protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4027protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004028
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004029Specifying a negative protocol version selects the highest protocol
4030version supported. The higher the protocol used, the more recent the
4031version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004032
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004033The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004034bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004035writing, a io.BytesIO instance, or any other custom object that meets
4036this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004038If *fix_imports* is True and protocol is less than 3, pickle will try
4039to map the new Python 3 names to the old module names used in Python
40402, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004041[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004042
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004043static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08004045/*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004046{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004047 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004048 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004049
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004050 /* In case of multiple __init__() calls, clear previous content. */
4051 if (self->write != NULL)
4052 (void)Pickler_clear(self);
4053
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004054 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004055 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004056
4057 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004058 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004059
4060 /* memo and output_buffer may have already been created in _Pickler_New */
4061 if (self->memo == NULL) {
4062 self->memo = PyMemoTable_New();
4063 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004064 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004065 }
4066 self->output_len = 0;
4067 if (self->output_buffer == NULL) {
4068 self->max_output_len = WRITE_BUF_SIZE;
4069 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4070 self->max_output_len);
4071 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004072 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004073 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004075 self->fast = 0;
4076 self->fast_nesting = 0;
4077 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004079 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4080 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4081 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004082 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004083 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004084 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004085 self->dispatch_table = NULL;
4086 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4087 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4088 &PyId_dispatch_table);
4089 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004090 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004091 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 return 0;
4094}
4095
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004097/* Define a proxy object for the Pickler's internal memo object. This is to
4098 * avoid breaking code like:
4099 * pickler.memo.clear()
4100 * and
4101 * pickler.memo = saved_memo
4102 * Is this a good idea? Not really, but we don't want to break code that uses
4103 * it. Note that we don't implement the entire mapping API here. This is
4104 * intentional, as these should be treated as black-box implementation details.
4105 */
4106
Larry Hastings61272b72014-01-07 12:41:53 -08004107/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004108_pickle.PicklerMemoProxy.clear
4109
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004110Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004111[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004112
Larry Hastings3cceb382014-01-04 11:09:09 -08004113static PyObject *
4114_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004115/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004116{
4117 if (self->pickler->memo)
4118 PyMemoTable_Clear(self->pickler->memo);
4119 Py_RETURN_NONE;
4120}
4121
Larry Hastings61272b72014-01-07 12:41:53 -08004122/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004123_pickle.PicklerMemoProxy.copy
4124
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004125Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004126[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004127
Larry Hastings3cceb382014-01-04 11:09:09 -08004128static PyObject *
4129_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004130/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131{
4132 Py_ssize_t i;
4133 PyMemoTable *memo;
4134 PyObject *new_memo = PyDict_New();
4135 if (new_memo == NULL)
4136 return NULL;
4137
4138 memo = self->pickler->memo;
4139 for (i = 0; i < memo->mt_allocated; ++i) {
4140 PyMemoEntry entry = memo->mt_table[i];
4141 if (entry.me_key != NULL) {
4142 int status;
4143 PyObject *key, *value;
4144
4145 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004146 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004147
4148 if (key == NULL || value == NULL) {
4149 Py_XDECREF(key);
4150 Py_XDECREF(value);
4151 goto error;
4152 }
4153 status = PyDict_SetItem(new_memo, key, value);
4154 Py_DECREF(key);
4155 Py_DECREF(value);
4156 if (status < 0)
4157 goto error;
4158 }
4159 }
4160 return new_memo;
4161
4162 error:
4163 Py_XDECREF(new_memo);
4164 return NULL;
4165}
4166
Larry Hastings61272b72014-01-07 12:41:53 -08004167/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168_pickle.PicklerMemoProxy.__reduce__
4169
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004171[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172
Larry Hastings3cceb382014-01-04 11:09:09 -08004173static PyObject *
4174_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004175/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004176{
4177 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004178 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179 if (contents == NULL)
4180 return NULL;
4181
4182 reduce_value = PyTuple_New(2);
4183 if (reduce_value == NULL) {
4184 Py_DECREF(contents);
4185 return NULL;
4186 }
4187 dict_args = PyTuple_New(1);
4188 if (dict_args == NULL) {
4189 Py_DECREF(contents);
4190 Py_DECREF(reduce_value);
4191 return NULL;
4192 }
4193 PyTuple_SET_ITEM(dict_args, 0, contents);
4194 Py_INCREF((PyObject *)&PyDict_Type);
4195 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4196 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4197 return reduce_value;
4198}
4199
4200static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004201 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4202 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4203 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004204 {NULL, NULL} /* sentinel */
4205};
4206
4207static void
4208PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4209{
4210 PyObject_GC_UnTrack(self);
4211 Py_XDECREF(self->pickler);
4212 PyObject_GC_Del((PyObject *)self);
4213}
4214
4215static int
4216PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4217 visitproc visit, void *arg)
4218{
4219 Py_VISIT(self->pickler);
4220 return 0;
4221}
4222
4223static int
4224PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4225{
4226 Py_CLEAR(self->pickler);
4227 return 0;
4228}
4229
4230static PyTypeObject PicklerMemoProxyType = {
4231 PyVarObject_HEAD_INIT(NULL, 0)
4232 "_pickle.PicklerMemoProxy", /*tp_name*/
4233 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4234 0,
4235 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4236 0, /* tp_print */
4237 0, /* tp_getattr */
4238 0, /* tp_setattr */
4239 0, /* tp_compare */
4240 0, /* tp_repr */
4241 0, /* tp_as_number */
4242 0, /* tp_as_sequence */
4243 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004244 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004245 0, /* tp_call */
4246 0, /* tp_str */
4247 PyObject_GenericGetAttr, /* tp_getattro */
4248 PyObject_GenericSetAttr, /* tp_setattro */
4249 0, /* tp_as_buffer */
4250 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4251 0, /* tp_doc */
4252 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4253 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4254 0, /* tp_richcompare */
4255 0, /* tp_weaklistoffset */
4256 0, /* tp_iter */
4257 0, /* tp_iternext */
4258 picklerproxy_methods, /* tp_methods */
4259};
4260
4261static PyObject *
4262PicklerMemoProxy_New(PicklerObject *pickler)
4263{
4264 PicklerMemoProxyObject *self;
4265
4266 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4267 if (self == NULL)
4268 return NULL;
4269 Py_INCREF(pickler);
4270 self->pickler = pickler;
4271 PyObject_GC_Track(self);
4272 return (PyObject *)self;
4273}
4274
4275/*****************************************************************************/
4276
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004277static PyObject *
4278Pickler_get_memo(PicklerObject *self)
4279{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004280 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004281}
4282
4283static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004284Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004289 PyErr_SetString(PyExc_TypeError,
4290 "attribute deletion is not supported");
4291 return -1;
4292 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004293
4294 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4295 PicklerObject *pickler =
4296 ((PicklerMemoProxyObject *)obj)->pickler;
4297
4298 new_memo = PyMemoTable_Copy(pickler->memo);
4299 if (new_memo == NULL)
4300 return -1;
4301 }
4302 else if (PyDict_Check(obj)) {
4303 Py_ssize_t i = 0;
4304 PyObject *key, *value;
4305
4306 new_memo = PyMemoTable_New();
4307 if (new_memo == NULL)
4308 return -1;
4309
4310 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004311 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004312 PyObject *memo_obj;
4313
4314 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4315 PyErr_SetString(PyExc_TypeError,
4316 "'memo' values must be 2-item tuples");
4317 goto error;
4318 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004319 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004320 if (memo_id == -1 && PyErr_Occurred())
4321 goto error;
4322 memo_obj = PyTuple_GET_ITEM(value, 1);
4323 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4324 goto error;
4325 }
4326 }
4327 else {
4328 PyErr_Format(PyExc_TypeError,
4329 "'memo' attribute must be an PicklerMemoProxy object"
4330 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004331 return -1;
4332 }
4333
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004334 PyMemoTable_Del(self->memo);
4335 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004336
4337 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004338
4339 error:
4340 if (new_memo)
4341 PyMemoTable_Del(new_memo);
4342 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343}
4344
4345static PyObject *
4346Pickler_get_persid(PicklerObject *self)
4347{
4348 if (self->pers_func == NULL)
4349 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4350 else
4351 Py_INCREF(self->pers_func);
4352 return self->pers_func;
4353}
4354
4355static int
4356Pickler_set_persid(PicklerObject *self, PyObject *value)
4357{
4358 PyObject *tmp;
4359
4360 if (value == NULL) {
4361 PyErr_SetString(PyExc_TypeError,
4362 "attribute deletion is not supported");
4363 return -1;
4364 }
4365 if (!PyCallable_Check(value)) {
4366 PyErr_SetString(PyExc_TypeError,
4367 "persistent_id must be a callable taking one argument");
4368 return -1;
4369 }
4370
4371 tmp = self->pers_func;
4372 Py_INCREF(value);
4373 self->pers_func = value;
4374 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4375
4376 return 0;
4377}
4378
4379static PyMemberDef Pickler_members[] = {
4380 {"bin", T_INT, offsetof(PicklerObject, bin)},
4381 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004382 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004383 {NULL}
4384};
4385
4386static PyGetSetDef Pickler_getsets[] = {
4387 {"memo", (getter)Pickler_get_memo,
4388 (setter)Pickler_set_memo},
4389 {"persistent_id", (getter)Pickler_get_persid,
4390 (setter)Pickler_set_persid},
4391 {NULL}
4392};
4393
4394static PyTypeObject Pickler_Type = {
4395 PyVarObject_HEAD_INIT(NULL, 0)
4396 "_pickle.Pickler" , /*tp_name*/
4397 sizeof(PicklerObject), /*tp_basicsize*/
4398 0, /*tp_itemsize*/
4399 (destructor)Pickler_dealloc, /*tp_dealloc*/
4400 0, /*tp_print*/
4401 0, /*tp_getattr*/
4402 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004403 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004404 0, /*tp_repr*/
4405 0, /*tp_as_number*/
4406 0, /*tp_as_sequence*/
4407 0, /*tp_as_mapping*/
4408 0, /*tp_hash*/
4409 0, /*tp_call*/
4410 0, /*tp_str*/
4411 0, /*tp_getattro*/
4412 0, /*tp_setattro*/
4413 0, /*tp_as_buffer*/
4414 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004415 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416 (traverseproc)Pickler_traverse, /*tp_traverse*/
4417 (inquiry)Pickler_clear, /*tp_clear*/
4418 0, /*tp_richcompare*/
4419 0, /*tp_weaklistoffset*/
4420 0, /*tp_iter*/
4421 0, /*tp_iternext*/
4422 Pickler_methods, /*tp_methods*/
4423 Pickler_members, /*tp_members*/
4424 Pickler_getsets, /*tp_getset*/
4425 0, /*tp_base*/
4426 0, /*tp_dict*/
4427 0, /*tp_descr_get*/
4428 0, /*tp_descr_set*/
4429 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004430 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431 PyType_GenericAlloc, /*tp_alloc*/
4432 PyType_GenericNew, /*tp_new*/
4433 PyObject_GC_Del, /*tp_free*/
4434 0, /*tp_is_gc*/
4435};
4436
Victor Stinner121aab42011-09-29 23:40:53 +02004437/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004438
4439 XXX: It would be nice to able to avoid Python function call overhead, by
4440 using directly the C version of find_class(), when find_class() is not
4441 overridden by a subclass. Although, this could become rather hackish. A
4442 simpler optimization would be to call the C function when self is not a
4443 subclass instance. */
4444static PyObject *
4445find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4446{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004447 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004448
4449 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4450 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451}
4452
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004453static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004454marker(UnpicklerObject *self)
4455{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004456 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004458 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004459 return -1;
4460 }
4461
4462 return self->marks[--self->num_marks];
4463}
4464
4465static int
4466load_none(UnpicklerObject *self)
4467{
4468 PDATA_APPEND(self->stack, Py_None, -1);
4469 return 0;
4470}
4471
4472static int
4473bad_readline(void)
4474{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004475 PickleState *st = _Pickle_GetGlobalState();
4476 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004477 return -1;
4478}
4479
4480static int
4481load_int(UnpicklerObject *self)
4482{
4483 PyObject *value;
4484 char *endptr, *s;
4485 Py_ssize_t len;
4486 long x;
4487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004488 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004489 return -1;
4490 if (len < 2)
4491 return bad_readline();
4492
4493 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004494 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004495 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004496 x = strtol(s, &endptr, 0);
4497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004498 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004499 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004500 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004501 errno = 0;
4502 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004503 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004504 if (value == NULL) {
4505 PyErr_SetString(PyExc_ValueError,
4506 "could not convert string to int");
4507 return -1;
4508 }
4509 }
4510 else {
4511 if (len == 3 && (x == 0 || x == 1)) {
4512 if ((value = PyBool_FromLong(x)) == NULL)
4513 return -1;
4514 }
4515 else {
4516 if ((value = PyLong_FromLong(x)) == NULL)
4517 return -1;
4518 }
4519 }
4520
4521 PDATA_PUSH(self->stack, value, -1);
4522 return 0;
4523}
4524
4525static int
4526load_bool(UnpicklerObject *self, PyObject *boolean)
4527{
4528 assert(boolean == Py_True || boolean == Py_False);
4529 PDATA_APPEND(self->stack, boolean, -1);
4530 return 0;
4531}
4532
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004533/* s contains x bytes of an unsigned little-endian integer. Return its value
4534 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4535 */
4536static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004537calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004538{
4539 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004540 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004541 size_t x = 0;
4542
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004543 for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004544 x |= (size_t) s[i] << (8 * i);
4545 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004546
4547 if (x > PY_SSIZE_T_MAX)
4548 return -1;
4549 else
4550 return (Py_ssize_t) x;
4551}
4552
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004553/* s contains x bytes of a little-endian integer. Return its value as a
4554 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4555 * int, but when x is 4 it's a signed one. This is an historical source
4556 * of x-platform bugs.
4557 */
4558static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004559calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004560{
4561 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004562 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563 long x = 0;
4564
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004565 for (i = 0; i < nbytes; i++) {
4566 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567 }
4568
4569 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4570 * is signed, so on a box with longs bigger than 4 bytes we need
4571 * to extend a BININT's sign bit to the full width.
4572 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004573 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004574 x |= -(x & (1L << 31));
4575 }
4576
4577 return x;
4578}
4579
4580static int
4581load_binintx(UnpicklerObject *self, char *s, int size)
4582{
4583 PyObject *value;
4584 long x;
4585
4586 x = calc_binint(s, size);
4587
4588 if ((value = PyLong_FromLong(x)) == NULL)
4589 return -1;
4590
4591 PDATA_PUSH(self->stack, value, -1);
4592 return 0;
4593}
4594
4595static int
4596load_binint(UnpicklerObject *self)
4597{
4598 char *s;
4599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004600 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004601 return -1;
4602
4603 return load_binintx(self, s, 4);
4604}
4605
4606static int
4607load_binint1(UnpicklerObject *self)
4608{
4609 char *s;
4610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004611 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004612 return -1;
4613
4614 return load_binintx(self, s, 1);
4615}
4616
4617static int
4618load_binint2(UnpicklerObject *self)
4619{
4620 char *s;
4621
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004622 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623 return -1;
4624
4625 return load_binintx(self, s, 2);
4626}
4627
4628static int
4629load_long(UnpicklerObject *self)
4630{
4631 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004632 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633 Py_ssize_t len;
4634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004635 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 return -1;
4637 if (len < 2)
4638 return bad_readline();
4639
Mark Dickinson8dd05142009-01-20 20:43:58 +00004640 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4641 the 'L' before calling PyLong_FromString. In order to maintain
4642 compatibility with Python 3.0.0, we don't actually *require*
4643 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004644 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004645 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004646 /* XXX: Should the base argument explicitly set to 10? */
4647 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004648 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649 return -1;
4650
4651 PDATA_PUSH(self->stack, value, -1);
4652 return 0;
4653}
4654
4655/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4656 * data following.
4657 */
4658static int
4659load_counted_long(UnpicklerObject *self, int size)
4660{
4661 PyObject *value;
4662 char *nbytes;
4663 char *pdata;
4664
4665 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004666 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 return -1;
4668
4669 size = calc_binint(nbytes, size);
4670 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004671 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004672 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004673 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004674 "LONG pickle has negative byte count");
4675 return -1;
4676 }
4677
4678 if (size == 0)
4679 value = PyLong_FromLong(0L);
4680 else {
4681 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004682 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004683 return -1;
4684 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4685 1 /* little endian */ , 1 /* signed */ );
4686 }
4687 if (value == NULL)
4688 return -1;
4689 PDATA_PUSH(self->stack, value, -1);
4690 return 0;
4691}
4692
4693static int
4694load_float(UnpicklerObject *self)
4695{
4696 PyObject *value;
4697 char *endptr, *s;
4698 Py_ssize_t len;
4699 double d;
4700
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004701 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004702 return -1;
4703 if (len < 2)
4704 return bad_readline();
4705
4706 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004707 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4708 if (d == -1.0 && PyErr_Occurred())
4709 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004710 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4712 return -1;
4713 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004714 value = PyFloat_FromDouble(d);
4715 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004716 return -1;
4717
4718 PDATA_PUSH(self->stack, value, -1);
4719 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004720}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721
4722static int
4723load_binfloat(UnpicklerObject *self)
4724{
4725 PyObject *value;
4726 double x;
4727 char *s;
4728
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004729 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004730 return -1;
4731
4732 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4733 if (x == -1.0 && PyErr_Occurred())
4734 return -1;
4735
4736 if ((value = PyFloat_FromDouble(x)) == NULL)
4737 return -1;
4738
4739 PDATA_PUSH(self->stack, value, -1);
4740 return 0;
4741}
4742
4743static int
4744load_string(UnpicklerObject *self)
4745{
4746 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004747 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748 Py_ssize_t len;
4749 char *s, *p;
4750
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004751 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004752 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004753 /* Strip the newline */
4754 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004755 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004756 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004757 p = s + 1;
4758 len -= 2;
4759 }
4760 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004761 PickleState *st = _Pickle_GetGlobalState();
4762 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004763 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 return -1;
4765 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004766 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767
4768 /* Use the PyBytes API to decode the string, since that is what is used
4769 to encode, and then coerce the result to Unicode. */
4770 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004771 if (bytes == NULL)
4772 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004773
4774 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4775 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4776 if (strcmp(self->encoding, "bytes") == 0) {
4777 obj = bytes;
4778 }
4779 else {
4780 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4781 Py_DECREF(bytes);
4782 if (obj == NULL) {
4783 return -1;
4784 }
4785 }
4786
4787 PDATA_PUSH(self->stack, obj, -1);
4788 return 0;
4789}
4790
4791static int
4792load_counted_binstring(UnpicklerObject *self, int nbytes)
4793{
4794 PyObject *obj;
4795 Py_ssize_t size;
4796 char *s;
4797
4798 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004799 return -1;
4800
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004801 size = calc_binsize(s, nbytes);
4802 if (size < 0) {
4803 PickleState *st = _Pickle_GetGlobalState();
4804 PyErr_Format(st->UnpicklingError,
4805 "BINSTRING exceeds system's maximum size of %zd bytes",
4806 PY_SSIZE_T_MAX);
4807 return -1;
4808 }
4809
4810 if (_Unpickler_Read(self, &s, size) < 0)
4811 return -1;
4812
4813 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4814 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4815 if (strcmp(self->encoding, "bytes") == 0) {
4816 obj = PyBytes_FromStringAndSize(s, size);
4817 }
4818 else {
4819 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4820 }
4821 if (obj == NULL) {
4822 return -1;
4823 }
4824
4825 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004826 return 0;
4827}
4828
4829static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004830load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004831{
4832 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004833 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834 char *s;
4835
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004836 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004837 return -1;
4838
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004839 size = calc_binsize(s, nbytes);
4840 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004841 PyErr_Format(PyExc_OverflowError,
4842 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004843 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 return -1;
4845 }
4846
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004847 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004849
4850 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004851 if (bytes == NULL)
4852 return -1;
4853
4854 PDATA_PUSH(self->stack, bytes, -1);
4855 return 0;
4856}
4857
4858static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859load_unicode(UnpicklerObject *self)
4860{
4861 PyObject *str;
4862 Py_ssize_t len;
4863 char *s;
4864
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004865 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 return -1;
4867 if (len < 1)
4868 return bad_readline();
4869
4870 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4871 if (str == NULL)
4872 return -1;
4873
4874 PDATA_PUSH(self->stack, str, -1);
4875 return 0;
4876}
4877
4878static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004879load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004880{
4881 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004882 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004883 char *s;
4884
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004885 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004886 return -1;
4887
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004888 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004889 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004890 PyErr_Format(PyExc_OverflowError,
4891 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004892 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893 return -1;
4894 }
4895
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004896 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 return -1;
4898
Victor Stinner485fb562010-04-13 11:07:24 +00004899 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004900 if (str == NULL)
4901 return -1;
4902
4903 PDATA_PUSH(self->stack, str, -1);
4904 return 0;
4905}
4906
4907static int
4908load_tuple(UnpicklerObject *self)
4909{
4910 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004911 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004912
4913 if ((i = marker(self)) < 0)
4914 return -1;
4915
4916 tuple = Pdata_poptuple(self->stack, i);
4917 if (tuple == NULL)
4918 return -1;
4919 PDATA_PUSH(self->stack, tuple, -1);
4920 return 0;
4921}
4922
4923static int
4924load_counted_tuple(UnpicklerObject *self, int len)
4925{
4926 PyObject *tuple;
4927
4928 tuple = PyTuple_New(len);
4929 if (tuple == NULL)
4930 return -1;
4931
4932 while (--len >= 0) {
4933 PyObject *item;
4934
4935 PDATA_POP(self->stack, item);
4936 if (item == NULL)
4937 return -1;
4938 PyTuple_SET_ITEM(tuple, len, item);
4939 }
4940 PDATA_PUSH(self->stack, tuple, -1);
4941 return 0;
4942}
4943
4944static int
4945load_empty_list(UnpicklerObject *self)
4946{
4947 PyObject *list;
4948
4949 if ((list = PyList_New(0)) == NULL)
4950 return -1;
4951 PDATA_PUSH(self->stack, list, -1);
4952 return 0;
4953}
4954
4955static int
4956load_empty_dict(UnpicklerObject *self)
4957{
4958 PyObject *dict;
4959
4960 if ((dict = PyDict_New()) == NULL)
4961 return -1;
4962 PDATA_PUSH(self->stack, dict, -1);
4963 return 0;
4964}
4965
4966static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004967load_empty_set(UnpicklerObject *self)
4968{
4969 PyObject *set;
4970
4971 if ((set = PySet_New(NULL)) == NULL)
4972 return -1;
4973 PDATA_PUSH(self->stack, set, -1);
4974 return 0;
4975}
4976
4977static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978load_list(UnpicklerObject *self)
4979{
4980 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004981 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004982
4983 if ((i = marker(self)) < 0)
4984 return -1;
4985
4986 list = Pdata_poplist(self->stack, i);
4987 if (list == NULL)
4988 return -1;
4989 PDATA_PUSH(self->stack, list, -1);
4990 return 0;
4991}
4992
4993static int
4994load_dict(UnpicklerObject *self)
4995{
4996 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004997 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004998
4999 if ((i = marker(self)) < 0)
5000 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005001 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005002
5003 if ((dict = PyDict_New()) == NULL)
5004 return -1;
5005
5006 for (k = i + 1; k < j; k += 2) {
5007 key = self->stack->data[k - 1];
5008 value = self->stack->data[k];
5009 if (PyDict_SetItem(dict, key, value) < 0) {
5010 Py_DECREF(dict);
5011 return -1;
5012 }
5013 }
5014 Pdata_clear(self->stack, i);
5015 PDATA_PUSH(self->stack, dict, -1);
5016 return 0;
5017}
5018
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005019static int
5020load_frozenset(UnpicklerObject *self)
5021{
5022 PyObject *items;
5023 PyObject *frozenset;
5024 Py_ssize_t i;
5025
5026 if ((i = marker(self)) < 0)
5027 return -1;
5028
5029 items = Pdata_poptuple(self->stack, i);
5030 if (items == NULL)
5031 return -1;
5032
5033 frozenset = PyFrozenSet_New(items);
5034 Py_DECREF(items);
5035 if (frozenset == NULL)
5036 return -1;
5037
5038 PDATA_PUSH(self->stack, frozenset, -1);
5039 return 0;
5040}
5041
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042static PyObject *
5043instantiate(PyObject *cls, PyObject *args)
5044{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005045 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005046 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005047 /* Caller must assure args are a tuple. Normally, args come from
5048 Pdata_poptuple which packs objects from the top of the stack
5049 into a newly created tuple. */
5050 assert(PyTuple_Check(args));
5051 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005052 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005053 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005055 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005056 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005057
5058 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005059 }
5060 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061}
5062
5063static int
5064load_obj(UnpicklerObject *self)
5065{
5066 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005067 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005068
5069 if ((i = marker(self)) < 0)
5070 return -1;
5071
5072 args = Pdata_poptuple(self->stack, i + 1);
5073 if (args == NULL)
5074 return -1;
5075
5076 PDATA_POP(self->stack, cls);
5077 if (cls) {
5078 obj = instantiate(cls, args);
5079 Py_DECREF(cls);
5080 }
5081 Py_DECREF(args);
5082 if (obj == NULL)
5083 return -1;
5084
5085 PDATA_PUSH(self->stack, obj, -1);
5086 return 0;
5087}
5088
5089static int
5090load_inst(UnpicklerObject *self)
5091{
5092 PyObject *cls = NULL;
5093 PyObject *args = NULL;
5094 PyObject *obj = NULL;
5095 PyObject *module_name;
5096 PyObject *class_name;
5097 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005098 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005099 char *s;
5100
5101 if ((i = marker(self)) < 0)
5102 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005103 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005104 return -1;
5105 if (len < 2)
5106 return bad_readline();
5107
5108 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5109 identifiers are permitted in Python 3.0, since the INST opcode is only
5110 supported by older protocols on Python 2.x. */
5111 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5112 if (module_name == NULL)
5113 return -1;
5114
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005115 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116 if (len < 2)
5117 return bad_readline();
5118 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005119 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120 cls = find_class(self, module_name, class_name);
5121 Py_DECREF(class_name);
5122 }
5123 }
5124 Py_DECREF(module_name);
5125
5126 if (cls == NULL)
5127 return -1;
5128
5129 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5130 obj = instantiate(cls, args);
5131 Py_DECREF(args);
5132 }
5133 Py_DECREF(cls);
5134
5135 if (obj == NULL)
5136 return -1;
5137
5138 PDATA_PUSH(self->stack, obj, -1);
5139 return 0;
5140}
5141
5142static int
5143load_newobj(UnpicklerObject *self)
5144{
5145 PyObject *args = NULL;
5146 PyObject *clsraw = NULL;
5147 PyTypeObject *cls; /* clsraw cast to its true type */
5148 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005149 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005150
5151 /* Stack is ... cls argtuple, and we want to call
5152 * cls.__new__(cls, *argtuple).
5153 */
5154 PDATA_POP(self->stack, args);
5155 if (args == NULL)
5156 goto error;
5157 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005158 PyErr_SetString(st->UnpicklingError,
5159 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005160 goto error;
5161 }
5162
5163 PDATA_POP(self->stack, clsraw);
5164 cls = (PyTypeObject *)clsraw;
5165 if (cls == NULL)
5166 goto error;
5167 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005168 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005169 "isn't a type object");
5170 goto error;
5171 }
5172 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005173 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005174 "has NULL tp_new");
5175 goto error;
5176 }
5177
5178 /* Call __new__. */
5179 obj = cls->tp_new(cls, args, NULL);
5180 if (obj == NULL)
5181 goto error;
5182
5183 Py_DECREF(args);
5184 Py_DECREF(clsraw);
5185 PDATA_PUSH(self->stack, obj, -1);
5186 return 0;
5187
5188 error:
5189 Py_XDECREF(args);
5190 Py_XDECREF(clsraw);
5191 return -1;
5192}
5193
5194static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005195load_newobj_ex(UnpicklerObject *self)
5196{
5197 PyObject *cls, *args, *kwargs;
5198 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005199 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005200
5201 PDATA_POP(self->stack, kwargs);
5202 if (kwargs == NULL) {
5203 return -1;
5204 }
5205 PDATA_POP(self->stack, args);
5206 if (args == NULL) {
5207 Py_DECREF(kwargs);
5208 return -1;
5209 }
5210 PDATA_POP(self->stack, cls);
5211 if (cls == NULL) {
5212 Py_DECREF(kwargs);
5213 Py_DECREF(args);
5214 return -1;
5215 }
Larry Hastings61272b72014-01-07 12:41:53 -08005216
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005217 if (!PyType_Check(cls)) {
5218 Py_DECREF(kwargs);
5219 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005220 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005221 "NEWOBJ_EX class argument must be a type, not %.200s",
5222 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005223 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005224 return -1;
5225 }
5226
5227 if (((PyTypeObject *)cls)->tp_new == NULL) {
5228 Py_DECREF(kwargs);
5229 Py_DECREF(args);
5230 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005231 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005232 "NEWOBJ_EX class argument doesn't have __new__");
5233 return -1;
5234 }
5235 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5236 Py_DECREF(kwargs);
5237 Py_DECREF(args);
5238 Py_DECREF(cls);
5239 if (obj == NULL) {
5240 return -1;
5241 }
5242 PDATA_PUSH(self->stack, obj, -1);
5243 return 0;
5244}
5245
5246static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005247load_global(UnpicklerObject *self)
5248{
5249 PyObject *global = NULL;
5250 PyObject *module_name;
5251 PyObject *global_name;
5252 Py_ssize_t len;
5253 char *s;
5254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005255 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005256 return -1;
5257 if (len < 2)
5258 return bad_readline();
5259 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5260 if (!module_name)
5261 return -1;
5262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005263 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005264 if (len < 2) {
5265 Py_DECREF(module_name);
5266 return bad_readline();
5267 }
5268 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5269 if (global_name) {
5270 global = find_class(self, module_name, global_name);
5271 Py_DECREF(global_name);
5272 }
5273 }
5274 Py_DECREF(module_name);
5275
5276 if (global == NULL)
5277 return -1;
5278 PDATA_PUSH(self->stack, global, -1);
5279 return 0;
5280}
5281
5282static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005283load_stack_global(UnpicklerObject *self)
5284{
5285 PyObject *global;
5286 PyObject *module_name;
5287 PyObject *global_name;
5288
5289 PDATA_POP(self->stack, global_name);
5290 PDATA_POP(self->stack, module_name);
5291 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5292 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005293 PickleState *st = _Pickle_GetGlobalState();
5294 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005295 Py_XDECREF(global_name);
5296 Py_XDECREF(module_name);
5297 return -1;
5298 }
5299 global = find_class(self, module_name, global_name);
5300 Py_DECREF(global_name);
5301 Py_DECREF(module_name);
5302 if (global == NULL)
5303 return -1;
5304 PDATA_PUSH(self->stack, global, -1);
5305 return 0;
5306}
5307
5308static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005309load_persid(UnpicklerObject *self)
5310{
5311 PyObject *pid;
5312 Py_ssize_t len;
5313 char *s;
5314
5315 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005316 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005317 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005318 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005319 return bad_readline();
5320
5321 pid = PyBytes_FromStringAndSize(s, len - 1);
5322 if (pid == NULL)
5323 return -1;
5324
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005325 /* This does not leak since _Pickle_FastCall() steals the reference
5326 to pid first. */
5327 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005328 if (pid == NULL)
5329 return -1;
5330
5331 PDATA_PUSH(self->stack, pid, -1);
5332 return 0;
5333 }
5334 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005335 PickleState *st = _Pickle_GetGlobalState();
5336 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005337 "A load persistent id instruction was encountered,\n"
5338 "but no persistent_load function was specified.");
5339 return -1;
5340 }
5341}
5342
5343static int
5344load_binpersid(UnpicklerObject *self)
5345{
5346 PyObject *pid;
5347
5348 if (self->pers_func) {
5349 PDATA_POP(self->stack, pid);
5350 if (pid == NULL)
5351 return -1;
5352
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005353 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005354 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005355 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005356 if (pid == NULL)
5357 return -1;
5358
5359 PDATA_PUSH(self->stack, pid, -1);
5360 return 0;
5361 }
5362 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005363 PickleState *st = _Pickle_GetGlobalState();
5364 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005365 "A load persistent id instruction was encountered,\n"
5366 "but no persistent_load function was specified.");
5367 return -1;
5368 }
5369}
5370
5371static int
5372load_pop(UnpicklerObject *self)
5373{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005374 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005375
5376 /* Note that we split the (pickle.py) stack into two stacks,
5377 * an object stack and a mark stack. We have to be clever and
5378 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005379 * mark stack first, and only signalling a stack underflow if
5380 * the object stack is empty and the mark stack doesn't match
5381 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005382 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005383 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005385 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005386 len--;
5387 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005388 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005389 } else {
5390 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005391 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005392 return 0;
5393}
5394
5395static int
5396load_pop_mark(UnpicklerObject *self)
5397{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005398 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399
5400 if ((i = marker(self)) < 0)
5401 return -1;
5402
5403 Pdata_clear(self->stack, i);
5404
5405 return 0;
5406}
5407
5408static int
5409load_dup(UnpicklerObject *self)
5410{
5411 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005412 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005414 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005415 return stack_underflow();
5416 last = self->stack->data[len - 1];
5417 PDATA_APPEND(self->stack, last, -1);
5418 return 0;
5419}
5420
5421static int
5422load_get(UnpicklerObject *self)
5423{
5424 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005425 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005426 Py_ssize_t len;
5427 char *s;
5428
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005429 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005430 return -1;
5431 if (len < 2)
5432 return bad_readline();
5433
5434 key = PyLong_FromString(s, NULL, 10);
5435 if (key == NULL)
5436 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005437 idx = PyLong_AsSsize_t(key);
5438 if (idx == -1 && PyErr_Occurred()) {
5439 Py_DECREF(key);
5440 return -1;
5441 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005442
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005443 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444 if (value == NULL) {
5445 if (!PyErr_Occurred())
5446 PyErr_SetObject(PyExc_KeyError, key);
5447 Py_DECREF(key);
5448 return -1;
5449 }
5450 Py_DECREF(key);
5451
5452 PDATA_APPEND(self->stack, value, -1);
5453 return 0;
5454}
5455
5456static int
5457load_binget(UnpicklerObject *self)
5458{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005459 PyObject *value;
5460 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461 char *s;
5462
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005463 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005464 return -1;
5465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005466 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005468 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005470 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005471 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005472 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005473 Py_DECREF(key);
5474 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475 return -1;
5476 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477
5478 PDATA_APPEND(self->stack, value, -1);
5479 return 0;
5480}
5481
5482static int
5483load_long_binget(UnpicklerObject *self)
5484{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005485 PyObject *value;
5486 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005487 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005489 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490 return -1;
5491
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005492 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005494 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005496 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005497 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005498 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005499 Py_DECREF(key);
5500 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005501 return -1;
5502 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005503
5504 PDATA_APPEND(self->stack, value, -1);
5505 return 0;
5506}
5507
5508/* Push an object from the extension registry (EXT[124]). nbytes is
5509 * the number of bytes following the opcode, holding the index (code) value.
5510 */
5511static int
5512load_extension(UnpicklerObject *self, int nbytes)
5513{
5514 char *codebytes; /* the nbytes bytes after the opcode */
5515 long code; /* calc_binint returns long */
5516 PyObject *py_code; /* code as a Python int */
5517 PyObject *obj; /* the object to push */
5518 PyObject *pair; /* (module_name, class_name) */
5519 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005520 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005521
5522 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005523 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005524 return -1;
5525 code = calc_binint(codebytes, nbytes);
5526 if (code <= 0) { /* note that 0 is forbidden */
5527 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005528 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005529 return -1;
5530 }
5531
5532 /* Look for the code in the cache. */
5533 py_code = PyLong_FromLong(code);
5534 if (py_code == NULL)
5535 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005536 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005537 if (obj != NULL) {
5538 /* Bingo. */
5539 Py_DECREF(py_code);
5540 PDATA_APPEND(self->stack, obj, -1);
5541 return 0;
5542 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005543 if (PyErr_Occurred()) {
5544 Py_DECREF(py_code);
5545 return -1;
5546 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547
5548 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005549 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550 if (pair == NULL) {
5551 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005552 if (!PyErr_Occurred()) {
5553 PyErr_Format(PyExc_ValueError, "unregistered extension "
5554 "code %ld", code);
5555 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 return -1;
5557 }
5558 /* Since the extension registry is manipulable via Python code,
5559 * confirm that pair is really a 2-tuple of strings.
5560 */
5561 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5562 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5563 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5564 Py_DECREF(py_code);
5565 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5566 "isn't a 2-tuple of strings", code);
5567 return -1;
5568 }
5569 /* Load the object. */
5570 obj = find_class(self, module_name, class_name);
5571 if (obj == NULL) {
5572 Py_DECREF(py_code);
5573 return -1;
5574 }
5575 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005576 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005577 Py_DECREF(py_code);
5578 if (code < 0) {
5579 Py_DECREF(obj);
5580 return -1;
5581 }
5582 PDATA_PUSH(self->stack, obj, -1);
5583 return 0;
5584}
5585
5586static int
5587load_put(UnpicklerObject *self)
5588{
5589 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005590 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 Py_ssize_t len;
5592 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005593
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005594 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005595 return -1;
5596 if (len < 2)
5597 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601
5602 key = PyLong_FromString(s, NULL, 10);
5603 if (key == NULL)
5604 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005605 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005607 if (idx < 0) {
5608 if (!PyErr_Occurred())
5609 PyErr_SetString(PyExc_ValueError,
5610 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005612 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005613
5614 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615}
5616
5617static int
5618load_binput(UnpicklerObject *self)
5619{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005620 PyObject *value;
5621 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005624 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626
5627 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005633 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634}
5635
5636static int
5637load_long_binput(UnpicklerObject *self)
5638{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005639 PyObject *value;
5640 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005643 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005645
5646 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005648 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005650 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005651 if (idx < 0) {
5652 PyErr_SetString(PyExc_ValueError,
5653 "negative LONG_BINPUT argument");
5654 return -1;
5655 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005657 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658}
5659
5660static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005661load_memoize(UnpicklerObject *self)
5662{
5663 PyObject *value;
5664
5665 if (Py_SIZE(self->stack) <= 0)
5666 return stack_underflow();
5667 value = self->stack->data[Py_SIZE(self->stack) - 1];
5668
5669 return _Unpickler_MemoPut(self, self->memo_len, value);
5670}
5671
5672static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005673do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674{
5675 PyObject *value;
5676 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005677 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005679 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680 if (x > len || x <= 0)
5681 return stack_underflow();
5682 if (len == x) /* nothing to do */
5683 return 0;
5684
5685 list = self->stack->data[x - 1];
5686
5687 if (PyList_Check(list)) {
5688 PyObject *slice;
5689 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005690 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691
5692 slice = Pdata_poplist(self->stack, x);
5693 if (!slice)
5694 return -1;
5695 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005696 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005698 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699 }
5700 else {
5701 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005702 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005703
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005704 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005705 if (append_func == NULL)
5706 return -1;
5707 for (i = x; i < len; i++) {
5708 PyObject *result;
5709
5710 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005711 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005712 if (result == NULL) {
5713 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005714 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005715 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716 return -1;
5717 }
5718 Py_DECREF(result);
5719 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005720 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005721 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722 }
5723
5724 return 0;
5725}
5726
5727static int
5728load_append(UnpicklerObject *self)
5729{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731}
5732
5733static int
5734load_appends(UnpicklerObject *self)
5735{
5736 return do_append(self, marker(self));
5737}
5738
5739static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005740do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741{
5742 PyObject *value, *key;
5743 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005744 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745 int status = 0;
5746
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005747 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005748 if (x > len || x <= 0)
5749 return stack_underflow();
5750 if (len == x) /* nothing to do */
5751 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005752 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005753 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005755 PyErr_SetString(st->UnpicklingError,
5756 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757 return -1;
5758 }
5759
5760 /* Here, dict does not actually need to be a PyDict; it could be anything
5761 that supports the __setitem__ attribute. */
5762 dict = self->stack->data[x - 1];
5763
5764 for (i = x + 1; i < len; i += 2) {
5765 key = self->stack->data[i - 1];
5766 value = self->stack->data[i];
5767 if (PyObject_SetItem(dict, key, value) < 0) {
5768 status = -1;
5769 break;
5770 }
5771 }
5772
5773 Pdata_clear(self->stack, x);
5774 return status;
5775}
5776
5777static int
5778load_setitem(UnpicklerObject *self)
5779{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781}
5782
5783static int
5784load_setitems(UnpicklerObject *self)
5785{
5786 return do_setitems(self, marker(self));
5787}
5788
5789static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005790load_additems(UnpicklerObject *self)
5791{
5792 PyObject *set;
5793 Py_ssize_t mark, len, i;
5794
5795 mark = marker(self);
5796 len = Py_SIZE(self->stack);
5797 if (mark > len || mark <= 0)
5798 return stack_underflow();
5799 if (len == mark) /* nothing to do */
5800 return 0;
5801
5802 set = self->stack->data[mark - 1];
5803
5804 if (PySet_Check(set)) {
5805 PyObject *items;
5806 int status;
5807
5808 items = Pdata_poptuple(self->stack, mark);
5809 if (items == NULL)
5810 return -1;
5811
5812 status = _PySet_Update(set, items);
5813 Py_DECREF(items);
5814 return status;
5815 }
5816 else {
5817 PyObject *add_func;
5818 _Py_IDENTIFIER(add);
5819
5820 add_func = _PyObject_GetAttrId(set, &PyId_add);
5821 if (add_func == NULL)
5822 return -1;
5823 for (i = mark; i < len; i++) {
5824 PyObject *result;
5825 PyObject *item;
5826
5827 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005828 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005829 if (result == NULL) {
5830 Pdata_clear(self->stack, i + 1);
5831 Py_SIZE(self->stack) = mark;
5832 return -1;
5833 }
5834 Py_DECREF(result);
5835 }
5836 Py_SIZE(self->stack) = mark;
5837 }
5838
5839 return 0;
5840}
5841
5842static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843load_build(UnpicklerObject *self)
5844{
5845 PyObject *state, *inst, *slotstate;
5846 PyObject *setstate;
5847 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005848 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005849
5850 /* Stack is ... instance, state. We want to leave instance at
5851 * the stack top, possibly mutated via instance.__setstate__(state).
5852 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005853 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 return stack_underflow();
5855
5856 PDATA_POP(self->stack, state);
5857 if (state == NULL)
5858 return -1;
5859
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005860 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005861
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005862 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005863 if (setstate == NULL) {
5864 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5865 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005866 else {
5867 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005868 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005869 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870 }
5871 else {
5872 PyObject *result;
5873
5874 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005875 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005876 Py_DECREF(setstate);
5877 if (result == NULL)
5878 return -1;
5879 Py_DECREF(result);
5880 return 0;
5881 }
5882
5883 /* A default __setstate__. First see whether state embeds a
5884 * slot state dict too (a proto 2 addition).
5885 */
5886 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5887 PyObject *tmp = state;
5888
5889 state = PyTuple_GET_ITEM(tmp, 0);
5890 slotstate = PyTuple_GET_ITEM(tmp, 1);
5891 Py_INCREF(state);
5892 Py_INCREF(slotstate);
5893 Py_DECREF(tmp);
5894 }
5895 else
5896 slotstate = NULL;
5897
5898 /* Set inst.__dict__ from the state dict (if any). */
5899 if (state != Py_None) {
5900 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005901 PyObject *d_key, *d_value;
5902 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005903 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005904
5905 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005906 PickleState *st = _Pickle_GetGlobalState();
5907 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908 goto error;
5909 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005910 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911 if (dict == NULL)
5912 goto error;
5913
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005914 i = 0;
5915 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5916 /* normally the keys for instance attributes are
5917 interned. we should try to do that here. */
5918 Py_INCREF(d_key);
5919 if (PyUnicode_CheckExact(d_key))
5920 PyUnicode_InternInPlace(&d_key);
5921 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5922 Py_DECREF(d_key);
5923 goto error;
5924 }
5925 Py_DECREF(d_key);
5926 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927 Py_DECREF(dict);
5928 }
5929
5930 /* Also set instance attributes from the slotstate dict (if any). */
5931 if (slotstate != NULL) {
5932 PyObject *d_key, *d_value;
5933 Py_ssize_t i;
5934
5935 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005936 PickleState *st = _Pickle_GetGlobalState();
5937 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938 "slot state is not a dictionary");
5939 goto error;
5940 }
5941 i = 0;
5942 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5943 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5944 goto error;
5945 }
5946 }
5947
5948 if (0) {
5949 error:
5950 status = -1;
5951 }
5952
5953 Py_DECREF(state);
5954 Py_XDECREF(slotstate);
5955 return status;
5956}
5957
5958static int
5959load_mark(UnpicklerObject *self)
5960{
5961
5962 /* Note that we split the (pickle.py) stack into two stacks, an
5963 * object stack and a mark stack. Here we push a mark onto the
5964 * mark stack.
5965 */
5966
5967 if ((self->num_marks + 1) >= self->marks_size) {
5968 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005969
5970 /* Use the size_t type to check for overflow. */
5971 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005972 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005973 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005974 PyErr_NoMemory();
5975 return -1;
5976 }
5977
5978 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005979 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005981 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
5982 if (self->marks == NULL) {
5983 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984 PyErr_NoMemory();
5985 return -1;
5986 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005987 self->marks_size = (Py_ssize_t)alloc;
5988 }
5989
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005990 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991
5992 return 0;
5993}
5994
5995static int
5996load_reduce(UnpicklerObject *self)
5997{
5998 PyObject *callable = NULL;
5999 PyObject *argtup = NULL;
6000 PyObject *obj = NULL;
6001
6002 PDATA_POP(self->stack, argtup);
6003 if (argtup == NULL)
6004 return -1;
6005 PDATA_POP(self->stack, callable);
6006 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006007 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006008 Py_DECREF(callable);
6009 }
6010 Py_DECREF(argtup);
6011
6012 if (obj == NULL)
6013 return -1;
6014
6015 PDATA_PUSH(self->stack, obj, -1);
6016 return 0;
6017}
6018
6019/* Just raises an error if we don't know the protocol specified. PROTO
6020 * is the first opcode for protocols >= 2.
6021 */
6022static int
6023load_proto(UnpicklerObject *self)
6024{
6025 char *s;
6026 int i;
6027
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006028 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006029 return -1;
6030
6031 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006032 if (i <= HIGHEST_PROTOCOL) {
6033 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006034 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006035 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006036
6037 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6038 return -1;
6039}
6040
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006041static int
6042load_frame(UnpicklerObject *self)
6043{
6044 char *s;
6045 Py_ssize_t frame_len;
6046
6047 if (_Unpickler_Read(self, &s, 8) < 0)
6048 return -1;
6049
6050 frame_len = calc_binsize(s, 8);
6051 if (frame_len < 0) {
6052 PyErr_Format(PyExc_OverflowError,
6053 "FRAME length exceeds system's maximum of %zd bytes",
6054 PY_SSIZE_T_MAX);
6055 return -1;
6056 }
6057
6058 if (_Unpickler_Read(self, &s, frame_len) < 0)
6059 return -1;
6060
6061 /* Rewind to start of frame */
6062 self->next_read_idx -= frame_len;
6063 return 0;
6064}
6065
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006066static PyObject *
6067load(UnpicklerObject *self)
6068{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006070 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006071
6072 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006073 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006074 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 Pdata_clear(self->stack, 0);
6076
6077 /* Convenient macros for the dispatch while-switch loop just below. */
6078#define OP(opcode, load_func) \
6079 case opcode: if (load_func(self) < 0) break; continue;
6080
6081#define OP_ARG(opcode, load_func, arg) \
6082 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6083
6084 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006085 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006086 break;
6087
6088 switch ((enum opcode)s[0]) {
6089 OP(NONE, load_none)
6090 OP(BININT, load_binint)
6091 OP(BININT1, load_binint1)
6092 OP(BININT2, load_binint2)
6093 OP(INT, load_int)
6094 OP(LONG, load_long)
6095 OP_ARG(LONG1, load_counted_long, 1)
6096 OP_ARG(LONG4, load_counted_long, 4)
6097 OP(FLOAT, load_float)
6098 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006099 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6100 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6101 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6102 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6103 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006104 OP(STRING, load_string)
6105 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006106 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6107 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6108 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006109 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6110 OP_ARG(TUPLE1, load_counted_tuple, 1)
6111 OP_ARG(TUPLE2, load_counted_tuple, 2)
6112 OP_ARG(TUPLE3, load_counted_tuple, 3)
6113 OP(TUPLE, load_tuple)
6114 OP(EMPTY_LIST, load_empty_list)
6115 OP(LIST, load_list)
6116 OP(EMPTY_DICT, load_empty_dict)
6117 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006118 OP(EMPTY_SET, load_empty_set)
6119 OP(ADDITEMS, load_additems)
6120 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121 OP(OBJ, load_obj)
6122 OP(INST, load_inst)
6123 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006124 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006126 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006127 OP(APPEND, load_append)
6128 OP(APPENDS, load_appends)
6129 OP(BUILD, load_build)
6130 OP(DUP, load_dup)
6131 OP(BINGET, load_binget)
6132 OP(LONG_BINGET, load_long_binget)
6133 OP(GET, load_get)
6134 OP(MARK, load_mark)
6135 OP(BINPUT, load_binput)
6136 OP(LONG_BINPUT, load_long_binput)
6137 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006138 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006139 OP(POP, load_pop)
6140 OP(POP_MARK, load_pop_mark)
6141 OP(SETITEM, load_setitem)
6142 OP(SETITEMS, load_setitems)
6143 OP(PERSID, load_persid)
6144 OP(BINPERSID, load_binpersid)
6145 OP(REDUCE, load_reduce)
6146 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006147 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006148 OP_ARG(EXT1, load_extension, 1)
6149 OP_ARG(EXT2, load_extension, 2)
6150 OP_ARG(EXT4, load_extension, 4)
6151 OP_ARG(NEWTRUE, load_bool, Py_True)
6152 OP_ARG(NEWFALSE, load_bool, Py_False)
6153
6154 case STOP:
6155 break;
6156
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006157 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006158 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006159 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006160 }
6161 else {
6162 PickleState *st = _Pickle_GetGlobalState();
6163 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006164 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006165 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006166 return NULL;
6167 }
6168
6169 break; /* and we are done! */
6170 }
6171
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006172 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006173 return NULL;
6174 }
6175
Victor Stinner2ae57e32013-10-31 13:39:23 +01006176 if (_Unpickler_SkipConsumed(self) < 0)
6177 return NULL;
6178
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006179 PDATA_POP(self->stack, value);
6180 return value;
6181}
6182
Larry Hastings61272b72014-01-07 12:41:53 -08006183/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006184
6185_pickle.Unpickler.load
6186
6187Load a pickle.
6188
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006189Read a pickled object representation from the open file object given
6190in the constructor, and return the reconstituted object hierarchy
6191specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006192[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006193
Larry Hastings3cceb382014-01-04 11:09:09 -08006194static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006195_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006196/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006197{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006198 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006199
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200 /* Check whether the Unpickler was initialized correctly. This prevents
6201 segfaulting if a subclass overridden __init__ with a function that does
6202 not call Unpickler.__init__(). Here, we simply ensure that self->read
6203 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006204 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006205 PickleState *st = _Pickle_GetGlobalState();
6206 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006208 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006209 return NULL;
6210 }
6211
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006212 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213}
6214
6215/* The name of find_class() is misleading. In newer pickle protocols, this
6216 function is used for loading any global (i.e., functions), not just
6217 classes. The name is kept only for backward compatibility. */
6218
Larry Hastings61272b72014-01-07 12:41:53 -08006219/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006220
6221_pickle.Unpickler.find_class
6222
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006223 module_name: object
6224 global_name: object
6225 /
6226
6227Return an object from a specified module.
6228
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006229If necessary, the module will be imported. Subclasses may override
6230this method (e.g. to restrict unpickling of arbitrary classes and
6231functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006232
6233This method is called whenever a class or a function object is
6234needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006235[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006236
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006237static PyObject *
6238_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006239/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006240{
6241 PyObject *global;
6242 PyObject *modules_dict;
6243 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006244 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006245
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006246 /* Try to map the old names used in Python 2.x to the new ones used in
6247 Python 3.x. We do this only with old pickle protocols and when the
6248 user has not disabled the feature. */
6249 if (self->proto < 3 && self->fix_imports) {
6250 PyObject *key;
6251 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006252 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006253
6254 /* Check if the global (i.e., a function or a class) was renamed
6255 or moved to another module. */
6256 key = PyTuple_Pack(2, module_name, global_name);
6257 if (key == NULL)
6258 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006259 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006260 Py_DECREF(key);
6261 if (item) {
6262 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6263 PyErr_Format(PyExc_RuntimeError,
6264 "_compat_pickle.NAME_MAPPING values should be "
6265 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6266 return NULL;
6267 }
6268 module_name = PyTuple_GET_ITEM(item, 0);
6269 global_name = PyTuple_GET_ITEM(item, 1);
6270 if (!PyUnicode_Check(module_name) ||
6271 !PyUnicode_Check(global_name)) {
6272 PyErr_Format(PyExc_RuntimeError,
6273 "_compat_pickle.NAME_MAPPING values should be "
6274 "pairs of str, not (%.200s, %.200s)",
6275 Py_TYPE(module_name)->tp_name,
6276 Py_TYPE(global_name)->tp_name);
6277 return NULL;
6278 }
6279 }
6280 else if (PyErr_Occurred()) {
6281 return NULL;
6282 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006283 else {
6284 /* Check if the module was renamed. */
6285 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6286 if (item) {
6287 if (!PyUnicode_Check(item)) {
6288 PyErr_Format(PyExc_RuntimeError,
6289 "_compat_pickle.IMPORT_MAPPING values should be "
6290 "strings, not %.200s", Py_TYPE(item)->tp_name);
6291 return NULL;
6292 }
6293 module_name = item;
6294 }
6295 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006296 return NULL;
6297 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006298 }
6299 }
6300
Victor Stinnerbb520202013-11-06 22:40:41 +01006301 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006302 if (modules_dict == NULL) {
6303 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006305 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006307 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006308 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006309 if (PyErr_Occurred())
6310 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006311 module = PyImport_Import(module_name);
6312 if (module == NULL)
6313 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006314 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006315 Py_DECREF(module);
6316 }
Victor Stinner121aab42011-09-29 23:40:53 +02006317 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006318 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006319 }
6320 return global;
6321}
6322
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006323/*[clinic input]
6324
6325_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6326
6327Returns size in memory, in bytes.
6328[clinic start generated code]*/
6329
6330static Py_ssize_t
6331_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6332/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6333{
6334 Py_ssize_t res;
6335
6336 res = sizeof(UnpicklerObject);
6337 if (self->memo != NULL)
6338 res += self->memo_size * sizeof(PyObject *);
6339 if (self->marks != NULL)
6340 res += self->marks_size * sizeof(Py_ssize_t);
6341 if (self->input_line != NULL)
6342 res += strlen(self->input_line) + 1;
6343 if (self->encoding != NULL)
6344 res += strlen(self->encoding) + 1;
6345 if (self->errors != NULL)
6346 res += strlen(self->errors) + 1;
6347 return res;
6348}
6349
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006351 _PICKLE_UNPICKLER_LOAD_METHODDEF
6352 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006353 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006354 {NULL, NULL} /* sentinel */
6355};
6356
6357static void
6358Unpickler_dealloc(UnpicklerObject *self)
6359{
6360 PyObject_GC_UnTrack((PyObject *)self);
6361 Py_XDECREF(self->readline);
6362 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006363 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364 Py_XDECREF(self->stack);
6365 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006366 if (self->buffer.buf != NULL) {
6367 PyBuffer_Release(&self->buffer);
6368 self->buffer.buf = NULL;
6369 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006370
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006371 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006372 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006373 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006374 PyMem_Free(self->encoding);
6375 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006376
6377 Py_TYPE(self)->tp_free((PyObject *)self);
6378}
6379
6380static int
6381Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6382{
6383 Py_VISIT(self->readline);
6384 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006385 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006386 Py_VISIT(self->stack);
6387 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006388 return 0;
6389}
6390
6391static int
6392Unpickler_clear(UnpicklerObject *self)
6393{
6394 Py_CLEAR(self->readline);
6395 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006396 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397 Py_CLEAR(self->stack);
6398 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006399 if (self->buffer.buf != NULL) {
6400 PyBuffer_Release(&self->buffer);
6401 self->buffer.buf = NULL;
6402 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006404 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006405 PyMem_Free(self->marks);
6406 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006407 PyMem_Free(self->input_line);
6408 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006409 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006410 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006411 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006412 self->errors = NULL;
6413
6414 return 0;
6415}
6416
Larry Hastings61272b72014-01-07 12:41:53 -08006417/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006418
6419_pickle.Unpickler.__init__
6420
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006421 file: object
6422 *
6423 fix_imports: bool = True
6424 encoding: str = 'ASCII'
6425 errors: str = 'strict'
6426
6427This takes a binary file for reading a pickle data stream.
6428
6429The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006430protocol argument is needed. Bytes past the pickled object's
6431representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006432
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006433The argument *file* must have two methods, a read() method that takes
6434an integer argument, and a readline() method that requires no
6435arguments. Both methods should return bytes. Thus *file* can be a
6436binary file object opened for reading, a io.BytesIO object, or any
6437other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006438
6439Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6440which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006441generated by Python 2. If *fix_imports* is True, pickle will try to
6442map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006443*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006444instances pickled by Python 2; these default to 'ASCII' and 'strict',
6445respectively. The *encoding* can be 'bytes' to read these 8-bit
6446string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006447[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006448
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006449static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006450_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006451/*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006452{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006453 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006454
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 /* In case of multiple __init__() calls, clear previous content. */
6456 if (self->read != NULL)
6457 (void)Unpickler_clear(self);
6458
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006459 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006460 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006461
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006462 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006463 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006464
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006465 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006466 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006467 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006469 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006470 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6471 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006472 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006473 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 }
6475 else {
6476 self->pers_func = NULL;
6477 }
6478
6479 self->stack = (Pdata *)Pdata_New();
6480 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006481 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006483 self->memo_size = 32;
6484 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006486 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006487
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006488 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006489
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006490 return 0;
6491}
6492
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006494/* Define a proxy object for the Unpickler's internal memo object. This is to
6495 * avoid breaking code like:
6496 * unpickler.memo.clear()
6497 * and
6498 * unpickler.memo = saved_memo
6499 * Is this a good idea? Not really, but we don't want to break code that uses
6500 * it. Note that we don't implement the entire mapping API here. This is
6501 * intentional, as these should be treated as black-box implementation details.
6502 *
6503 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006504 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006505 */
6506
Larry Hastings61272b72014-01-07 12:41:53 -08006507/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006508_pickle.UnpicklerMemoProxy.clear
6509
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006510Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006511[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006512
Larry Hastings3cceb382014-01-04 11:09:09 -08006513static PyObject *
6514_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006515/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006516{
6517 _Unpickler_MemoCleanup(self->unpickler);
6518 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6519 if (self->unpickler->memo == NULL)
6520 return NULL;
6521 Py_RETURN_NONE;
6522}
6523
Larry Hastings61272b72014-01-07 12:41:53 -08006524/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006525_pickle.UnpicklerMemoProxy.copy
6526
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006527Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006528[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006529
Larry Hastings3cceb382014-01-04 11:09:09 -08006530static PyObject *
6531_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006532/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006533{
6534 Py_ssize_t i;
6535 PyObject *new_memo = PyDict_New();
6536 if (new_memo == NULL)
6537 return NULL;
6538
6539 for (i = 0; i < self->unpickler->memo_size; i++) {
6540 int status;
6541 PyObject *key, *value;
6542
6543 value = self->unpickler->memo[i];
6544 if (value == NULL)
6545 continue;
6546
6547 key = PyLong_FromSsize_t(i);
6548 if (key == NULL)
6549 goto error;
6550 status = PyDict_SetItem(new_memo, key, value);
6551 Py_DECREF(key);
6552 if (status < 0)
6553 goto error;
6554 }
6555 return new_memo;
6556
6557error:
6558 Py_DECREF(new_memo);
6559 return NULL;
6560}
6561
Larry Hastings61272b72014-01-07 12:41:53 -08006562/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006563_pickle.UnpicklerMemoProxy.__reduce__
6564
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006565Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006566[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006567
Larry Hastings3cceb382014-01-04 11:09:09 -08006568static PyObject *
6569_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006570/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006571{
6572 PyObject *reduce_value;
6573 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006574 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006575 if (contents == NULL)
6576 return NULL;
6577
6578 reduce_value = PyTuple_New(2);
6579 if (reduce_value == NULL) {
6580 Py_DECREF(contents);
6581 return NULL;
6582 }
6583 constructor_args = PyTuple_New(1);
6584 if (constructor_args == NULL) {
6585 Py_DECREF(contents);
6586 Py_DECREF(reduce_value);
6587 return NULL;
6588 }
6589 PyTuple_SET_ITEM(constructor_args, 0, contents);
6590 Py_INCREF((PyObject *)&PyDict_Type);
6591 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6592 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6593 return reduce_value;
6594}
6595
6596static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6598 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6599 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006600 {NULL, NULL} /* sentinel */
6601};
6602
6603static void
6604UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6605{
6606 PyObject_GC_UnTrack(self);
6607 Py_XDECREF(self->unpickler);
6608 PyObject_GC_Del((PyObject *)self);
6609}
6610
6611static int
6612UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6613 visitproc visit, void *arg)
6614{
6615 Py_VISIT(self->unpickler);
6616 return 0;
6617}
6618
6619static int
6620UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6621{
6622 Py_CLEAR(self->unpickler);
6623 return 0;
6624}
6625
6626static PyTypeObject UnpicklerMemoProxyType = {
6627 PyVarObject_HEAD_INIT(NULL, 0)
6628 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6629 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6630 0,
6631 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6632 0, /* tp_print */
6633 0, /* tp_getattr */
6634 0, /* tp_setattr */
6635 0, /* tp_compare */
6636 0, /* tp_repr */
6637 0, /* tp_as_number */
6638 0, /* tp_as_sequence */
6639 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006640 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006641 0, /* tp_call */
6642 0, /* tp_str */
6643 PyObject_GenericGetAttr, /* tp_getattro */
6644 PyObject_GenericSetAttr, /* tp_setattro */
6645 0, /* tp_as_buffer */
6646 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6647 0, /* tp_doc */
6648 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6649 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6650 0, /* tp_richcompare */
6651 0, /* tp_weaklistoffset */
6652 0, /* tp_iter */
6653 0, /* tp_iternext */
6654 unpicklerproxy_methods, /* tp_methods */
6655};
6656
6657static PyObject *
6658UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6659{
6660 UnpicklerMemoProxyObject *self;
6661
6662 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6663 &UnpicklerMemoProxyType);
6664 if (self == NULL)
6665 return NULL;
6666 Py_INCREF(unpickler);
6667 self->unpickler = unpickler;
6668 PyObject_GC_Track(self);
6669 return (PyObject *)self;
6670}
6671
6672/*****************************************************************************/
6673
6674
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006675static PyObject *
6676Unpickler_get_memo(UnpicklerObject *self)
6677{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006678 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006679}
6680
6681static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006682Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006683{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006684 PyObject **new_memo;
6685 Py_ssize_t new_memo_size = 0;
6686 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006687
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006688 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689 PyErr_SetString(PyExc_TypeError,
6690 "attribute deletion is not supported");
6691 return -1;
6692 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006693
6694 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6695 UnpicklerObject *unpickler =
6696 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6697
6698 new_memo_size = unpickler->memo_size;
6699 new_memo = _Unpickler_NewMemo(new_memo_size);
6700 if (new_memo == NULL)
6701 return -1;
6702
6703 for (i = 0; i < new_memo_size; i++) {
6704 Py_XINCREF(unpickler->memo[i]);
6705 new_memo[i] = unpickler->memo[i];
6706 }
6707 }
6708 else if (PyDict_Check(obj)) {
6709 Py_ssize_t i = 0;
6710 PyObject *key, *value;
6711
6712 new_memo_size = PyDict_Size(obj);
6713 new_memo = _Unpickler_NewMemo(new_memo_size);
6714 if (new_memo == NULL)
6715 return -1;
6716
6717 while (PyDict_Next(obj, &i, &key, &value)) {
6718 Py_ssize_t idx;
6719 if (!PyLong_Check(key)) {
6720 PyErr_SetString(PyExc_TypeError,
6721 "memo key must be integers");
6722 goto error;
6723 }
6724 idx = PyLong_AsSsize_t(key);
6725 if (idx == -1 && PyErr_Occurred())
6726 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006727 if (idx < 0) {
6728 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006729 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006730 goto error;
6731 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 if (_Unpickler_MemoPut(self, idx, value) < 0)
6733 goto error;
6734 }
6735 }
6736 else {
6737 PyErr_Format(PyExc_TypeError,
6738 "'memo' attribute must be an UnpicklerMemoProxy object"
6739 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006740 return -1;
6741 }
6742
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006743 _Unpickler_MemoCleanup(self);
6744 self->memo_size = new_memo_size;
6745 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006746
6747 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006748
6749 error:
6750 if (new_memo_size) {
6751 i = new_memo_size;
6752 while (--i >= 0) {
6753 Py_XDECREF(new_memo[i]);
6754 }
6755 PyMem_FREE(new_memo);
6756 }
6757 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006758}
6759
6760static PyObject *
6761Unpickler_get_persload(UnpicklerObject *self)
6762{
6763 if (self->pers_func == NULL)
6764 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6765 else
6766 Py_INCREF(self->pers_func);
6767 return self->pers_func;
6768}
6769
6770static int
6771Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6772{
6773 PyObject *tmp;
6774
6775 if (value == NULL) {
6776 PyErr_SetString(PyExc_TypeError,
6777 "attribute deletion is not supported");
6778 return -1;
6779 }
6780 if (!PyCallable_Check(value)) {
6781 PyErr_SetString(PyExc_TypeError,
6782 "persistent_load must be a callable taking "
6783 "one argument");
6784 return -1;
6785 }
6786
6787 tmp = self->pers_func;
6788 Py_INCREF(value);
6789 self->pers_func = value;
6790 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6791
6792 return 0;
6793}
6794
6795static PyGetSetDef Unpickler_getsets[] = {
6796 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6797 {"persistent_load", (getter)Unpickler_get_persload,
6798 (setter)Unpickler_set_persload},
6799 {NULL}
6800};
6801
6802static PyTypeObject Unpickler_Type = {
6803 PyVarObject_HEAD_INIT(NULL, 0)
6804 "_pickle.Unpickler", /*tp_name*/
6805 sizeof(UnpicklerObject), /*tp_basicsize*/
6806 0, /*tp_itemsize*/
6807 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6808 0, /*tp_print*/
6809 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006810 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006811 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812 0, /*tp_repr*/
6813 0, /*tp_as_number*/
6814 0, /*tp_as_sequence*/
6815 0, /*tp_as_mapping*/
6816 0, /*tp_hash*/
6817 0, /*tp_call*/
6818 0, /*tp_str*/
6819 0, /*tp_getattro*/
6820 0, /*tp_setattro*/
6821 0, /*tp_as_buffer*/
6822 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006823 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006824 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6825 (inquiry)Unpickler_clear, /*tp_clear*/
6826 0, /*tp_richcompare*/
6827 0, /*tp_weaklistoffset*/
6828 0, /*tp_iter*/
6829 0, /*tp_iternext*/
6830 Unpickler_methods, /*tp_methods*/
6831 0, /*tp_members*/
6832 Unpickler_getsets, /*tp_getset*/
6833 0, /*tp_base*/
6834 0, /*tp_dict*/
6835 0, /*tp_descr_get*/
6836 0, /*tp_descr_set*/
6837 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006838 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006839 PyType_GenericAlloc, /*tp_alloc*/
6840 PyType_GenericNew, /*tp_new*/
6841 PyObject_GC_Del, /*tp_free*/
6842 0, /*tp_is_gc*/
6843};
6844
Larry Hastings61272b72014-01-07 12:41:53 -08006845/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006846
6847_pickle.dump
6848
6849 obj: object
6850 file: object
6851 protocol: object = NULL
6852 *
6853 fix_imports: bool = True
6854
6855Write a pickled representation of obj to the open file object file.
6856
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006857This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6858be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006859
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006860The optional *protocol* argument tells the pickler to use the given
6861protocol supported protocols are 0, 1, 2, 3 and 4. The default
6862protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006863
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006864Specifying a negative protocol version selects the highest protocol
6865version supported. The higher the protocol used, the more recent the
6866version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006867
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006868The *file* argument must have a write() method that accepts a single
6869bytes argument. It can thus be a file object opened for binary
6870writing, a io.BytesIO instance, or any other custom object that meets
6871this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006872
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006873If *fix_imports* is True and protocol is less than 3, pickle will try
6874to map the new Python 3 names to the old module names used in Python
68752, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006876[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006877
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006878static PyObject *
6879_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006880/*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006881{
6882 PicklerObject *pickler = _Pickler_New();
6883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006884 if (pickler == NULL)
6885 return NULL;
6886
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006887 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006888 goto error;
6889
6890 if (_Pickler_SetOutputStream(pickler, file) < 0)
6891 goto error;
6892
6893 if (dump(pickler, obj) < 0)
6894 goto error;
6895
6896 if (_Pickler_FlushToFile(pickler) < 0)
6897 goto error;
6898
6899 Py_DECREF(pickler);
6900 Py_RETURN_NONE;
6901
6902 error:
6903 Py_XDECREF(pickler);
6904 return NULL;
6905}
6906
Larry Hastings61272b72014-01-07 12:41:53 -08006907/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006908
6909_pickle.dumps
6910
6911 obj: object
6912 protocol: object = NULL
6913 *
6914 fix_imports: bool = True
6915
6916Return the pickled representation of the object as a bytes object.
6917
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006918The optional *protocol* argument tells the pickler to use the given
6919protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6920protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006921
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006922Specifying a negative protocol version selects the highest protocol
6923version supported. The higher the protocol used, the more recent the
6924version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006925
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006926If *fix_imports* is True and *protocol* is less than 3, pickle will
6927try to map the new Python 3 names to the old module names used in
6928Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006929[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006930
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006931static PyObject *
6932_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006933/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006934{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006935 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006936 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006937
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006938 if (pickler == NULL)
6939 return NULL;
6940
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006941 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006942 goto error;
6943
6944 if (dump(pickler, obj) < 0)
6945 goto error;
6946
6947 result = _Pickler_GetString(pickler);
6948 Py_DECREF(pickler);
6949 return result;
6950
6951 error:
6952 Py_XDECREF(pickler);
6953 return NULL;
6954}
6955
Larry Hastings61272b72014-01-07 12:41:53 -08006956/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006957
6958_pickle.load
6959
6960 file: object
6961 *
6962 fix_imports: bool = True
6963 encoding: str = 'ASCII'
6964 errors: str = 'strict'
6965
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006966Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006967
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006968This is equivalent to ``Unpickler(file).load()``, but may be more
6969efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006970
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006971The protocol version of the pickle is detected automatically, so no
6972protocol argument is needed. Bytes past the pickled object's
6973representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006974
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006975The argument *file* must have two methods, a read() method that takes
6976an integer argument, and a readline() method that requires no
6977arguments. Both methods should return bytes. Thus *file* can be a
6978binary file object opened for reading, a io.BytesIO object, or any
6979other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006980
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006981Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6982which are used to control compatiblity support for pickle stream
6983generated by Python 2. If *fix_imports* is True, pickle will try to
6984map the old Python 2 names to the new names used in Python 3. The
6985*encoding* and *errors* tell pickle how to decode 8-bit string
6986instances pickled by Python 2; these default to 'ASCII' and 'strict',
6987respectively. The *encoding* can be 'bytes' to read these 8-bit
6988string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006989[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006990
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006991static PyObject *
6992_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006993/*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006994{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006995 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006996 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006997
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006998 if (unpickler == NULL)
6999 return NULL;
7000
7001 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7002 goto error;
7003
7004 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7005 goto error;
7006
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007007 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007008
7009 result = load(unpickler);
7010 Py_DECREF(unpickler);
7011 return result;
7012
7013 error:
7014 Py_XDECREF(unpickler);
7015 return NULL;
7016}
7017
Larry Hastings61272b72014-01-07 12:41:53 -08007018/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007019
7020_pickle.loads
7021
7022 data: object
7023 *
7024 fix_imports: bool = True
7025 encoding: str = 'ASCII'
7026 errors: str = 'strict'
7027
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007028Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007029
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007030The protocol version of the pickle is detected automatically, so no
7031protocol argument is needed. Bytes past the pickled object's
7032representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007033
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007034Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7035which are used to control compatiblity support for pickle stream
7036generated by Python 2. If *fix_imports* is True, pickle will try to
7037map the old Python 2 names to the new names used in Python 3. The
7038*encoding* and *errors* tell pickle how to decode 8-bit string
7039instances pickled by Python 2; these default to 'ASCII' and 'strict',
7040respectively. The *encoding* can be 'bytes' to read these 8-bit
7041string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007042[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007044static PyObject *
7045_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007046/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007048 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007049 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007050
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007051 if (unpickler == NULL)
7052 return NULL;
7053
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007054 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007055 goto error;
7056
7057 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7058 goto error;
7059
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007060 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007061
7062 result = load(unpickler);
7063 Py_DECREF(unpickler);
7064 return result;
7065
7066 error:
7067 Py_XDECREF(unpickler);
7068 return NULL;
7069}
7070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007071static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007072 _PICKLE_DUMP_METHODDEF
7073 _PICKLE_DUMPS_METHODDEF
7074 _PICKLE_LOAD_METHODDEF
7075 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007076 {NULL, NULL} /* sentinel */
7077};
7078
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007079static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007080pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007081{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007082 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007083 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007084}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007085
Stefan Krahf483b0f2013-12-14 13:43:10 +01007086static void
7087pickle_free(PyObject *m)
7088{
7089 _Pickle_ClearState(_Pickle_GetState(m));
7090}
7091
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007092static int
7093pickle_traverse(PyObject *m, visitproc visit, void *arg)
7094{
7095 PickleState *st = _Pickle_GetState(m);
7096 Py_VISIT(st->PickleError);
7097 Py_VISIT(st->PicklingError);
7098 Py_VISIT(st->UnpicklingError);
7099 Py_VISIT(st->dispatch_table);
7100 Py_VISIT(st->extension_registry);
7101 Py_VISIT(st->extension_cache);
7102 Py_VISIT(st->inverted_registry);
7103 Py_VISIT(st->name_mapping_2to3);
7104 Py_VISIT(st->import_mapping_2to3);
7105 Py_VISIT(st->name_mapping_3to2);
7106 Py_VISIT(st->import_mapping_3to2);
7107 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007108 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007109}
7110
7111static struct PyModuleDef _picklemodule = {
7112 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007113 "_pickle", /* m_name */
7114 pickle_module_doc, /* m_doc */
7115 sizeof(PickleState), /* m_size */
7116 pickle_methods, /* m_methods */
7117 NULL, /* m_reload */
7118 pickle_traverse, /* m_traverse */
7119 pickle_clear, /* m_clear */
7120 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007121};
7122
7123PyMODINIT_FUNC
7124PyInit__pickle(void)
7125{
7126 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007127 PickleState *st;
7128
7129 m = PyState_FindModule(&_picklemodule);
7130 if (m) {
7131 Py_INCREF(m);
7132 return m;
7133 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007134
7135 if (PyType_Ready(&Unpickler_Type) < 0)
7136 return NULL;
7137 if (PyType_Ready(&Pickler_Type) < 0)
7138 return NULL;
7139 if (PyType_Ready(&Pdata_Type) < 0)
7140 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007141 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7142 return NULL;
7143 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7144 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007145
7146 /* Create the module and add the functions. */
7147 m = PyModule_Create(&_picklemodule);
7148 if (m == NULL)
7149 return NULL;
7150
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007151 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007152 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7153 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007154 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007155 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7156 return NULL;
7157
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007158 st = _Pickle_GetState(m);
7159
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007160 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007161 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7162 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007163 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007164 st->PicklingError = \
7165 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7166 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007167 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007168 st->UnpicklingError = \
7169 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7170 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007171 return NULL;
7172
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007173 Py_INCREF(st->PickleError);
7174 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007175 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007176 Py_INCREF(st->PicklingError);
7177 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007178 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007179 Py_INCREF(st->UnpicklingError);
7180 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007181 return NULL;
7182
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007183 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007184 return NULL;
7185
7186 return m;
7187}