blob: 9f16b4d081019a9078cbf641f20aab75ea043529 [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
Antoine Pitrou04248a82010-10-12 20:51:21 +00001185 if (self->next_read_idx + n <= self->input_len) {
1186 *s = self->input_buffer + self->next_read_idx;
1187 self->next_read_idx += n;
1188 return n;
1189 }
1190 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001192 return -1;
1193 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001194 num_read = _Unpickler_ReadFromFile(self, n);
1195 if (num_read < 0)
1196 return -1;
1197 if (num_read < n) {
1198 PyErr_Format(PyExc_EOFError, "Ran out of input");
1199 return -1;
1200 }
1201 *s = self->input_buffer;
1202 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001203 return n;
1204}
1205
1206static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001207_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1208 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001209{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001210 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001211 if (input_line == NULL) {
1212 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001213 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001214 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001215
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001216 memcpy(input_line, line, len);
1217 input_line[len] = '\0';
1218 self->input_line = input_line;
1219 *result = self->input_line;
1220 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001221}
1222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223/* Read a line from the input stream/buffer. If we run off the end of the input
1224 before hitting \n, return the data we found.
1225
1226 Returns the number of chars read, or -1 on failure. */
1227static Py_ssize_t
1228_Unpickler_Readline(UnpicklerObject *self, char **result)
1229{
1230 Py_ssize_t i, num_read;
1231
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001232 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001233 if (self->input_buffer[i] == '\n') {
1234 char *line_start = self->input_buffer + self->next_read_idx;
1235 num_read = i - self->next_read_idx + 1;
1236 self->next_read_idx = i + 1;
1237 return _Unpickler_CopyLine(self, line_start, num_read, result);
1238 }
1239 }
1240 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001241 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1242 if (num_read < 0)
1243 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001244 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001245 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246 }
Victor Stinner121aab42011-09-29 23:40:53 +02001247
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248 /* If we get here, we've run off the end of the input string. Return the
1249 remaining string and let the caller figure it out. */
1250 *result = self->input_buffer + self->next_read_idx;
1251 num_read = i - self->next_read_idx;
1252 self->next_read_idx = i;
1253 return num_read;
1254}
1255
1256/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1257 will be modified in place. */
1258static int
1259_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1260{
1261 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001262
1263 assert(new_size > self->memo_size);
1264
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001265 PyMem_RESIZE(self->memo, PyObject *, new_size);
1266 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001267 PyErr_NoMemory();
1268 return -1;
1269 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001270 for (i = self->memo_size; i < new_size; i++)
1271 self->memo[i] = NULL;
1272 self->memo_size = new_size;
1273 return 0;
1274}
1275
1276/* Returns NULL if idx is out of bounds. */
1277static PyObject *
1278_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1279{
1280 if (idx < 0 || idx >= self->memo_size)
1281 return NULL;
1282
1283 return self->memo[idx];
1284}
1285
1286/* Returns -1 (with an exception set) on failure, 0 on success.
1287 This takes its own reference to `value`. */
1288static int
1289_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1290{
1291 PyObject *old_item;
1292
1293 if (idx >= self->memo_size) {
1294 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1295 return -1;
1296 assert(idx < self->memo_size);
1297 }
1298 Py_INCREF(value);
1299 old_item = self->memo[idx];
1300 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001301 if (old_item != NULL) {
1302 Py_DECREF(old_item);
1303 }
1304 else {
1305 self->memo_len++;
1306 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001307 return 0;
1308}
1309
1310static PyObject **
1311_Unpickler_NewMemo(Py_ssize_t new_size)
1312{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001313 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001314 if (memo == NULL) {
1315 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001316 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001317 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001318 memset(memo, 0, new_size * sizeof(PyObject *));
1319 return memo;
1320}
1321
1322/* Free the unpickler's memo, taking care to decref any items left in it. */
1323static void
1324_Unpickler_MemoCleanup(UnpicklerObject *self)
1325{
1326 Py_ssize_t i;
1327 PyObject **memo = self->memo;
1328
1329 if (self->memo == NULL)
1330 return;
1331 self->memo = NULL;
1332 i = self->memo_size;
1333 while (--i >= 0) {
1334 Py_XDECREF(memo[i]);
1335 }
1336 PyMem_FREE(memo);
1337}
1338
1339static UnpicklerObject *
1340_Unpickler_New(void)
1341{
1342 UnpicklerObject *self;
1343
1344 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1345 if (self == NULL)
1346 return NULL;
1347
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348 self->pers_func = NULL;
1349 self->input_buffer = NULL;
1350 self->input_line = NULL;
1351 self->input_len = 0;
1352 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001353 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001354 self->read = NULL;
1355 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001356 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001357 self->encoding = NULL;
1358 self->errors = NULL;
1359 self->marks = NULL;
1360 self->num_marks = 0;
1361 self->marks_size = 0;
1362 self->proto = 0;
1363 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001364 memset(&self->buffer, 0, sizeof(Py_buffer));
1365 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001366 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001367 self->memo = _Unpickler_NewMemo(self->memo_size);
1368 self->stack = (Pdata *)Pdata_New();
1369
1370 if (self->memo == NULL || self->stack == NULL) {
1371 Py_DECREF(self);
1372 return NULL;
1373 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001374
1375 return self;
1376}
1377
1378/* Returns -1 (with an exception set) on failure, 0 on success. This may
1379 be called once on a freshly created Pickler. */
1380static int
1381_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1382{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001383 _Py_IDENTIFIER(peek);
1384 _Py_IDENTIFIER(read);
1385 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001386
1387 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001388 if (self->peek == NULL) {
1389 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1390 PyErr_Clear();
1391 else
1392 return -1;
1393 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001394 self->read = _PyObject_GetAttrId(file, &PyId_read);
1395 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001396 if (self->readline == NULL || self->read == NULL) {
1397 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1398 PyErr_SetString(PyExc_TypeError,
1399 "file must have 'read' and 'readline' attributes");
1400 Py_CLEAR(self->read);
1401 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001402 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001403 return -1;
1404 }
1405 return 0;
1406}
1407
1408/* Returns -1 (with an exception set) on failure, 0 on success. This may
1409 be called once on a freshly created Pickler. */
1410static int
1411_Unpickler_SetInputEncoding(UnpicklerObject *self,
1412 const char *encoding,
1413 const char *errors)
1414{
1415 if (encoding == NULL)
1416 encoding = "ASCII";
1417 if (errors == NULL)
1418 errors = "strict";
1419
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001420 self->encoding = _PyMem_Strdup(encoding);
1421 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001422 if (self->encoding == NULL || self->errors == NULL) {
1423 PyErr_NoMemory();
1424 return -1;
1425 }
1426 return 0;
1427}
1428
1429/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001430static int
1431memo_get(PicklerObject *self, PyObject *key)
1432{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001433 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001434 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001435 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 value = PyMemoTable_Get(self->memo, key);
1438 if (value == NULL) {
1439 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001440 return -1;
1441 }
1442
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001443 if (!self->bin) {
1444 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001445 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1446 "%" PY_FORMAT_SIZE_T "d\n", *value);
1447 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001448 }
1449 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001450 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001451 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001452 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001453 len = 2;
1454 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001455 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001456 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 pdata[1] = (unsigned char)(*value & 0xff);
1458 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1459 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1460 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001461 len = 5;
1462 }
1463 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001464 PickleState *st = _Pickle_GetGlobalState();
1465 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001466 "memo id too large for LONG_BINGET");
1467 return -1;
1468 }
1469 }
1470
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001471 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001472 return -1;
1473
1474 return 0;
1475}
1476
1477/* Store an object in the memo, assign it a new unique ID based on the number
1478 of objects currently stored in the memo and generate a PUT opcode. */
1479static int
1480memo_put(PicklerObject *self, PyObject *obj)
1481{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001482 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001483 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001484 Py_ssize_t idx;
1485
1486 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487
1488 if (self->fast)
1489 return 0;
1490
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001491 idx = PyMemoTable_Size(self->memo);
1492 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1493 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001494
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001495 if (self->proto >= 4) {
1496 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1497 return -1;
1498 return 0;
1499 }
1500 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001502 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001503 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001504 len = strlen(pdata);
1505 }
1506 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001507 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001509 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001510 len = 2;
1511 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001512 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001513 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001514 pdata[1] = (unsigned char)(idx & 0xff);
1515 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1516 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1517 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001518 len = 5;
1519 }
1520 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001521 PickleState *st = _Pickle_GetGlobalState();
1522 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523 "memo id too large for LONG_BINPUT");
1524 return -1;
1525 }
1526 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001527 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001528 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001530 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001531}
1532
1533static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001534getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1535 PyObject *dotted_path;
1536 Py_ssize_t i;
1537 _Py_static_string(PyId_dot, ".");
1538 _Py_static_string(PyId_locals, "<locals>");
1539
1540 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1541 if (dotted_path == NULL) {
1542 return NULL;
1543 }
1544 assert(Py_SIZE(dotted_path) >= 1);
1545 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1546 PyErr_Format(PyExc_AttributeError,
1547 "Can't get qualified attribute %R on %R;"
1548 "use protocols >= 4 to enable support",
1549 name, obj);
1550 Py_DECREF(dotted_path);
1551 return NULL;
1552 }
1553 Py_INCREF(obj);
1554 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1555 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1556 PyObject *tmp;
1557 PyObject *result = PyUnicode_RichCompare(
1558 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1559 int is_equal = (result == Py_True);
1560 assert(PyBool_Check(result));
1561 Py_DECREF(result);
1562 if (is_equal) {
1563 PyErr_Format(PyExc_AttributeError,
1564 "Can't get local attribute %R on %R", name, obj);
1565 Py_DECREF(dotted_path);
1566 Py_DECREF(obj);
1567 return NULL;
1568 }
1569 tmp = PyObject_GetAttr(obj, subpath);
1570 Py_DECREF(obj);
1571 if (tmp == NULL) {
1572 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1573 PyErr_Clear();
1574 PyErr_Format(PyExc_AttributeError,
1575 "Can't get attribute %R on %R", name, obj);
1576 }
1577 Py_DECREF(dotted_path);
1578 return NULL;
1579 }
1580 obj = tmp;
1581 }
1582 Py_DECREF(dotted_path);
1583 return obj;
1584}
1585
1586static PyObject *
1587whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001588{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001589 PyObject *module_name;
1590 PyObject *modules_dict;
1591 PyObject *module;
1592 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001593 Py_ssize_t i, j;
1594 _Py_IDENTIFIER(__module__);
1595 _Py_IDENTIFIER(modules);
1596 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001597
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001598 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1599
1600 if (module_name == NULL) {
1601 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001602 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001603 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 }
1605 else {
1606 /* In some rare cases (e.g., bound methods of extension types),
1607 __module__ can be None. If it is so, then search sys.modules for
1608 the module of global. */
1609 if (module_name != Py_None)
1610 return module_name;
1611 Py_CLEAR(module_name);
1612 }
1613 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001614
Victor Stinnerbb520202013-11-06 22:40:41 +01001615 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001616 if (modules_dict == NULL) {
1617 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001618 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001619 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001620
1621 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001622 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001623 PyObject *result = PyUnicode_RichCompare(
1624 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1625 int is_equal = (result == Py_True);
1626 assert(PyBool_Check(result));
1627 Py_DECREF(result);
1628 if (is_equal)
1629 continue;
1630 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001631 continue;
1632
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001633 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001634 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001635 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001636 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001637 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 continue;
1639 }
1640
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001641 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001642 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001643 Py_INCREF(module_name);
1644 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647 }
1648
1649 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651 Py_INCREF(module_name);
1652 return module_name;
1653}
1654
1655/* fast_save_enter() and fast_save_leave() are guards against recursive
1656 objects when Pickler is used with the "fast mode" (i.e., with object
1657 memoization disabled). If the nesting of a list or dict object exceed
1658 FAST_NESTING_LIMIT, these guards will start keeping an internal
1659 reference to the seen list or dict objects and check whether these objects
1660 are recursive. These are not strictly necessary, since save() has a
1661 hard-coded recursion limit, but they give a nicer error message than the
1662 typical RuntimeError. */
1663static int
1664fast_save_enter(PicklerObject *self, PyObject *obj)
1665{
1666 /* if fast_nesting < 0, we're doing an error exit. */
1667 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1668 PyObject *key = NULL;
1669 if (self->fast_memo == NULL) {
1670 self->fast_memo = PyDict_New();
1671 if (self->fast_memo == NULL) {
1672 self->fast_nesting = -1;
1673 return 0;
1674 }
1675 }
1676 key = PyLong_FromVoidPtr(obj);
1677 if (key == NULL)
1678 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001679 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001680 Py_DECREF(key);
1681 PyErr_Format(PyExc_ValueError,
1682 "fast mode: can't pickle cyclic objects "
1683 "including object type %.200s at %p",
1684 obj->ob_type->tp_name, obj);
1685 self->fast_nesting = -1;
1686 return 0;
1687 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001688 if (PyErr_Occurred()) {
1689 return 0;
1690 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001691 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1692 Py_DECREF(key);
1693 self->fast_nesting = -1;
1694 return 0;
1695 }
1696 Py_DECREF(key);
1697 }
1698 return 1;
1699}
1700
1701static int
1702fast_save_leave(PicklerObject *self, PyObject *obj)
1703{
1704 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1705 PyObject *key = PyLong_FromVoidPtr(obj);
1706 if (key == NULL)
1707 return 0;
1708 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1709 Py_DECREF(key);
1710 return 0;
1711 }
1712 Py_DECREF(key);
1713 }
1714 return 1;
1715}
1716
1717static int
1718save_none(PicklerObject *self, PyObject *obj)
1719{
1720 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001721 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001722 return -1;
1723
1724 return 0;
1725}
1726
1727static int
1728save_bool(PicklerObject *self, PyObject *obj)
1729{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001730 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001731 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001732 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001733 return -1;
1734 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001735 else {
1736 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1737 * so that unpicklers written before bools were introduced unpickle them
1738 * as ints, but unpicklers after can recognize that bools were intended.
1739 * Note that protocol 2 added direct ways to pickle bools.
1740 */
1741 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1742 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1743 return -1;
1744 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001745 return 0;
1746}
1747
1748static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001749save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001750{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001751 PyObject *repr = NULL;
1752 Py_ssize_t size;
1753 long val;
1754 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001756 const char long_op = LONG;
1757
1758 val= PyLong_AsLong(obj);
1759 if (val == -1 && PyErr_Occurred()) {
1760 /* out of range for int pickling */
1761 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001763 else if (self->bin &&
1764 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001765 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001766 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001767
1768 Note: we can't use -0x80000000L in the above condition because some
1769 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1770 before applying the unary minus when sizeof(long) <= 4. The
1771 resulting value stays unsigned which is commonly not what we want,
1772 so MSVC happily warns us about it. However, that result would have
1773 been fine because we guard for sizeof(long) <= 4 which turns the
1774 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001775 char pdata[32];
1776 Py_ssize_t len = 0;
1777
1778 pdata[1] = (unsigned char)(val & 0xff);
1779 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1780 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1781 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001782
1783 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1784 if (pdata[2] == 0) {
1785 pdata[0] = BININT1;
1786 len = 2;
1787 }
1788 else {
1789 pdata[0] = BININT2;
1790 len = 3;
1791 }
1792 }
1793 else {
1794 pdata[0] = BININT;
1795 len = 5;
1796 }
1797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001798 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001799 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001800
1801 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001802 }
1803
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001804 if (self->proto >= 2) {
1805 /* Linear-time pickling. */
1806 size_t nbits;
1807 size_t nbytes;
1808 unsigned char *pdata;
1809 char header[5];
1810 int i;
1811 int sign = _PyLong_Sign(obj);
1812
1813 if (sign == 0) {
1814 header[0] = LONG1;
1815 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001816 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817 goto error;
1818 return 0;
1819 }
1820 nbits = _PyLong_NumBits(obj);
1821 if (nbits == (size_t)-1 && PyErr_Occurred())
1822 goto error;
1823 /* How many bytes do we need? There are nbits >> 3 full
1824 * bytes of data, and nbits & 7 leftover bits. If there
1825 * are any leftover bits, then we clearly need another
1826 * byte. Wnat's not so obvious is that we *probably*
1827 * need another byte even if there aren't any leftovers:
1828 * the most-significant bit of the most-significant byte
1829 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001830 * opposite of the one we need. The exception is ints
1831 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001832 * its own 256's-complement, so has the right sign bit
1833 * even without the extra byte. That's a pain to check
1834 * for in advance, though, so we always grab an extra
1835 * byte at the start, and cut it back later if possible.
1836 */
1837 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001838 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001839 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001840 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001841 goto error;
1842 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001843 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001844 if (repr == NULL)
1845 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001846 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 i = _PyLong_AsByteArray((PyLongObject *)obj,
1848 pdata, nbytes,
1849 1 /* little endian */ , 1 /* signed */ );
1850 if (i < 0)
1851 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001852 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001853 * needed. This is so iff the MSB is all redundant sign
1854 * bits.
1855 */
1856 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001857 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001858 pdata[nbytes - 1] == 0xff &&
1859 (pdata[nbytes - 2] & 0x80) != 0) {
1860 nbytes--;
1861 }
1862
1863 if (nbytes < 256) {
1864 header[0] = LONG1;
1865 header[1] = (unsigned char)nbytes;
1866 size = 2;
1867 }
1868 else {
1869 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001870 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001871 for (i = 1; i < 5; i++) {
1872 header[i] = (unsigned char)(size & 0xff);
1873 size >>= 8;
1874 }
1875 size = 5;
1876 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001877 if (_Pickler_Write(self, header, size) < 0 ||
1878 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001879 goto error;
1880 }
1881 else {
1882 char *string;
1883
Mark Dickinson8dd05142009-01-20 20:43:58 +00001884 /* proto < 2: write the repr and newline. This is quadratic-time (in
1885 the number of digits), in both directions. We add a trailing 'L'
1886 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001887
1888 repr = PyObject_Repr(obj);
1889 if (repr == NULL)
1890 goto error;
1891
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001892 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001893 if (string == NULL)
1894 goto error;
1895
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001896 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1897 _Pickler_Write(self, string, size) < 0 ||
1898 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001899 goto error;
1900 }
1901
1902 if (0) {
1903 error:
1904 status = -1;
1905 }
1906 Py_XDECREF(repr);
1907
1908 return status;
1909}
1910
1911static int
1912save_float(PicklerObject *self, PyObject *obj)
1913{
1914 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1915
1916 if (self->bin) {
1917 char pdata[9];
1918 pdata[0] = BINFLOAT;
1919 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1920 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001921 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001923 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001924 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001925 int result = -1;
1926 char *buf = NULL;
1927 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001928
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001929 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001930 goto done;
1931
Mark Dickinson3e09f432009-04-17 08:41:23 +00001932 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001933 if (!buf) {
1934 PyErr_NoMemory();
1935 goto done;
1936 }
1937
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001938 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001939 goto done;
1940
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001941 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001942 goto done;
1943
1944 result = 0;
1945done:
1946 PyMem_Free(buf);
1947 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001948 }
1949
1950 return 0;
1951}
1952
1953static int
1954save_bytes(PicklerObject *self, PyObject *obj)
1955{
1956 if (self->proto < 3) {
1957 /* Older pickle protocols do not have an opcode for pickling bytes
1958 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001959 the __reduce__ method) to permit bytes object unpickling.
1960
1961 Here we use a hack to be compatible with Python 2. Since in Python
1962 2 'bytes' is just an alias for 'str' (which has different
1963 parameters than the actual bytes object), we use codecs.encode
1964 to create the appropriate 'str' object when unpickled using
1965 Python 2 *and* the appropriate 'bytes' object when unpickled
1966 using Python 3. Again this is a hack and we don't need to do this
1967 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001968 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 int status;
1970
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001971 if (PyBytes_GET_SIZE(obj) == 0) {
1972 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1973 }
1974 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001975 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001976 PyObject *unicode_str =
1977 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1978 PyBytes_GET_SIZE(obj),
1979 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001980 _Py_IDENTIFIER(latin1);
1981
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001982 if (unicode_str == NULL)
1983 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001984 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001985 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001986 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001987 Py_DECREF(unicode_str);
1988 }
1989
1990 if (reduce_value == NULL)
1991 return -1;
1992
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001993 /* save_reduce() will memoize the object automatically. */
1994 status = save_reduce(self, reduce_value, obj);
1995 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 return status;
1997 }
1998 else {
1999 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002000 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002001 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002002
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002003 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002004 if (size < 0)
2005 return -1;
2006
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002007 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008 header[0] = SHORT_BINBYTES;
2009 header[1] = (unsigned char)size;
2010 len = 2;
2011 }
2012 else if (size <= 0xffffffffL) {
2013 header[0] = BINBYTES;
2014 header[1] = (unsigned char)(size & 0xff);
2015 header[2] = (unsigned char)((size >> 8) & 0xff);
2016 header[3] = (unsigned char)((size >> 16) & 0xff);
2017 header[4] = (unsigned char)((size >> 24) & 0xff);
2018 len = 5;
2019 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002020 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002021 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002022 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002023 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002024 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002025 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002026 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002027 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002028 return -1; /* string too large */
2029 }
2030
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002031 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 return -1;
2033
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002034 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 return -1;
2036
2037 if (memo_put(self, obj) < 0)
2038 return -1;
2039
2040 return 0;
2041 }
2042}
2043
2044/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2045 backslash and newline characters to \uXXXX escapes. */
2046static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002047raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002048{
2049 PyObject *repr, *result;
2050 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002051 Py_ssize_t i, size, expandsize;
2052 void *data;
2053 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002054
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002055 if (PyUnicode_READY(obj))
2056 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002058 size = PyUnicode_GET_LENGTH(obj);
2059 data = PyUnicode_DATA(obj);
2060 kind = PyUnicode_KIND(obj);
2061 if (kind == PyUnicode_4BYTE_KIND)
2062 expandsize = 10;
2063 else
2064 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002065
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002066 if (size > PY_SSIZE_T_MAX / expandsize)
2067 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002068 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069 if (repr == NULL)
2070 return NULL;
2071 if (size == 0)
2072 goto done;
2073
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002074 p = PyByteArray_AS_STRING(repr);
2075 for (i=0; i < size; i++) {
2076 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 /* Map 32-bit characters to '\Uxxxxxxxx' */
2078 if (ch >= 0x10000) {
2079 *p++ = '\\';
2080 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002081 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2082 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2083 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2084 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2085 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2086 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2087 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2088 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002089 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002090 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002091 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092 *p++ = '\\';
2093 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002094 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2095 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2096 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2097 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002099 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 else
2101 *p++ = (char) ch;
2102 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002103 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002104
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002105done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002106 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 Py_DECREF(repr);
2108 return result;
2109}
2110
2111static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002112write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2113{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002114 char header[9];
2115 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002116
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002117 if (size <= 0xff && self->proto >= 4) {
2118 header[0] = SHORT_BINUNICODE;
2119 header[1] = (unsigned char)(size & 0xff);
2120 len = 2;
2121 }
2122 else if (size <= 0xffffffffUL) {
2123 header[0] = BINUNICODE;
2124 header[1] = (unsigned char)(size & 0xff);
2125 header[2] = (unsigned char)((size >> 8) & 0xff);
2126 header[3] = (unsigned char)((size >> 16) & 0xff);
2127 header[4] = (unsigned char)((size >> 24) & 0xff);
2128 len = 5;
2129 }
2130 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002131 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002132 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002133 len = 9;
2134 }
2135 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002136 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002137 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002138 return -1;
2139 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002140
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002141 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002142 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002143 if (_Pickler_Write(self, data, size) < 0)
2144 return -1;
2145
2146 return 0;
2147}
2148
2149static int
2150write_unicode_binary(PicklerObject *self, PyObject *obj)
2151{
2152 PyObject *encoded = NULL;
2153 Py_ssize_t size;
2154 char *data;
2155 int r;
2156
2157 if (PyUnicode_READY(obj))
2158 return -1;
2159
2160 data = PyUnicode_AsUTF8AndSize(obj, &size);
2161 if (data != NULL)
2162 return write_utf8(self, data, size);
2163
2164 /* Issue #8383: for strings with lone surrogates, fallback on the
2165 "surrogatepass" error handler. */
2166 PyErr_Clear();
2167 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2168 if (encoded == NULL)
2169 return -1;
2170
2171 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2172 PyBytes_GET_SIZE(encoded));
2173 Py_DECREF(encoded);
2174 return r;
2175}
2176
2177static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002178save_unicode(PicklerObject *self, PyObject *obj)
2179{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002180 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002181 if (write_unicode_binary(self, obj) < 0)
2182 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002183 }
2184 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002185 PyObject *encoded;
2186 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187 const char unicode_op = UNICODE;
2188
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002189 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002190 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002191 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002192
Antoine Pitrou299978d2013-04-07 17:38:11 +02002193 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2194 Py_DECREF(encoded);
2195 return -1;
2196 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002197
2198 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002199 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2200 Py_DECREF(encoded);
2201 return -1;
2202 }
2203 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002205 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002207 }
2208 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002209 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002210
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002212}
2213
2214/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2215static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002216store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002218 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219
2220 assert(PyTuple_Size(t) == len);
2221
2222 for (i = 0; i < len; i++) {
2223 PyObject *element = PyTuple_GET_ITEM(t, i);
2224
2225 if (element == NULL)
2226 return -1;
2227 if (save(self, element, 0) < 0)
2228 return -1;
2229 }
2230
2231 return 0;
2232}
2233
2234/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2235 * used across protocols to minimize the space needed to pickle them.
2236 * Tuples are also the only builtin immutable type that can be recursive
2237 * (a tuple can be reached from itself), and that requires some subtle
2238 * magic so that it works in all cases. IOW, this is a long routine.
2239 */
2240static int
2241save_tuple(PicklerObject *self, PyObject *obj)
2242{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002243 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002244
2245 const char mark_op = MARK;
2246 const char tuple_op = TUPLE;
2247 const char pop_op = POP;
2248 const char pop_mark_op = POP_MARK;
2249 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2250
2251 if ((len = PyTuple_Size(obj)) < 0)
2252 return -1;
2253
2254 if (len == 0) {
2255 char pdata[2];
2256
2257 if (self->proto) {
2258 pdata[0] = EMPTY_TUPLE;
2259 len = 1;
2260 }
2261 else {
2262 pdata[0] = MARK;
2263 pdata[1] = TUPLE;
2264 len = 2;
2265 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002266 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002267 return -1;
2268 return 0;
2269 }
2270
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002271 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 * saving the tuple elements, the tuple must be recursive, in
2273 * which case we'll pop everything we put on the stack, and fetch
2274 * its value from the memo.
2275 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 if (len <= 3 && self->proto >= 2) {
2277 /* Use TUPLE{1,2,3} opcodes. */
2278 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002279 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002281 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282 /* pop the len elements */
2283 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002284 if (_Pickler_Write(self, &pop_op, 1) < 0)
2285 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002287 if (memo_get(self, obj) < 0)
2288 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002290 return 0;
2291 }
2292 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002293 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2294 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 }
2296 goto memoize;
2297 }
2298
2299 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2300 * Generate MARK e1 e2 ... TUPLE
2301 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002302 if (_Pickler_Write(self, &mark_op, 1) < 0)
2303 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
2305 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002306 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002307
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002308 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002309 /* pop the stack stuff we pushed */
2310 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002311 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2312 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002313 }
2314 else {
2315 /* Note that we pop one more than len, to remove
2316 * the MARK too.
2317 */
2318 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002319 if (_Pickler_Write(self, &pop_op, 1) < 0)
2320 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 }
2322 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002323 if (memo_get(self, obj) < 0)
2324 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326 return 0;
2327 }
2328 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002329 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2330 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 }
2332
2333 memoize:
2334 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002335 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002337 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338}
2339
2340/* iter is an iterator giving items, and we batch up chunks of
2341 * MARK item item ... item APPENDS
2342 * opcode sequences. Calling code should have arranged to first create an
2343 * empty list, or list-like object, for the APPENDS to operate on.
2344 * Returns 0 on success, <0 on error.
2345 */
2346static int
2347batch_list(PicklerObject *self, PyObject *iter)
2348{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002349 PyObject *obj = NULL;
2350 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 int i, n;
2352
2353 const char mark_op = MARK;
2354 const char append_op = APPEND;
2355 const char appends_op = APPENDS;
2356
2357 assert(iter != NULL);
2358
2359 /* XXX: I think this function could be made faster by avoiding the
2360 iterator interface and fetching objects directly from list using
2361 PyList_GET_ITEM.
2362 */
2363
2364 if (self->proto == 0) {
2365 /* APPENDS isn't available; do one at a time. */
2366 for (;;) {
2367 obj = PyIter_Next(iter);
2368 if (obj == NULL) {
2369 if (PyErr_Occurred())
2370 return -1;
2371 break;
2372 }
2373 i = save(self, obj, 0);
2374 Py_DECREF(obj);
2375 if (i < 0)
2376 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002377 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002378 return -1;
2379 }
2380 return 0;
2381 }
2382
2383 /* proto > 0: write in batches of BATCHSIZE. */
2384 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002385 /* Get first item */
2386 firstitem = PyIter_Next(iter);
2387 if (firstitem == NULL) {
2388 if (PyErr_Occurred())
2389 goto error;
2390
2391 /* nothing more to add */
2392 break;
2393 }
2394
2395 /* Try to get a second item */
2396 obj = PyIter_Next(iter);
2397 if (obj == NULL) {
2398 if (PyErr_Occurred())
2399 goto error;
2400
2401 /* Only one item to write */
2402 if (save(self, firstitem, 0) < 0)
2403 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002404 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002405 goto error;
2406 Py_CLEAR(firstitem);
2407 break;
2408 }
2409
2410 /* More than one item to write */
2411
2412 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002413 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002414 goto error;
2415
2416 if (save(self, firstitem, 0) < 0)
2417 goto error;
2418 Py_CLEAR(firstitem);
2419 n = 1;
2420
2421 /* Fetch and save up to BATCHSIZE items */
2422 while (obj) {
2423 if (save(self, obj, 0) < 0)
2424 goto error;
2425 Py_CLEAR(obj);
2426 n += 1;
2427
2428 if (n == BATCHSIZE)
2429 break;
2430
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002431 obj = PyIter_Next(iter);
2432 if (obj == NULL) {
2433 if (PyErr_Occurred())
2434 goto error;
2435 break;
2436 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002437 }
2438
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002439 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002440 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002441
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442 } while (n == BATCHSIZE);
2443 return 0;
2444
2445 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002446 Py_XDECREF(firstitem);
2447 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448 return -1;
2449}
2450
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002451/* This is a variant of batch_list() above, specialized for lists (with no
2452 * support for list subclasses). Like batch_list(), we batch up chunks of
2453 * MARK item item ... item APPENDS
2454 * opcode sequences. Calling code should have arranged to first create an
2455 * empty list, or list-like object, for the APPENDS to operate on.
2456 * Returns 0 on success, -1 on error.
2457 *
2458 * This version is considerably faster than batch_list(), if less general.
2459 *
2460 * Note that this only works for protocols > 0.
2461 */
2462static int
2463batch_list_exact(PicklerObject *self, PyObject *obj)
2464{
2465 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002466 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002467
2468 const char append_op = APPEND;
2469 const char appends_op = APPENDS;
2470 const char mark_op = MARK;
2471
2472 assert(obj != NULL);
2473 assert(self->proto > 0);
2474 assert(PyList_CheckExact(obj));
2475
2476 if (PyList_GET_SIZE(obj) == 1) {
2477 item = PyList_GET_ITEM(obj, 0);
2478 if (save(self, item, 0) < 0)
2479 return -1;
2480 if (_Pickler_Write(self, &append_op, 1) < 0)
2481 return -1;
2482 return 0;
2483 }
2484
2485 /* Write in batches of BATCHSIZE. */
2486 total = 0;
2487 do {
2488 this_batch = 0;
2489 if (_Pickler_Write(self, &mark_op, 1) < 0)
2490 return -1;
2491 while (total < PyList_GET_SIZE(obj)) {
2492 item = PyList_GET_ITEM(obj, total);
2493 if (save(self, item, 0) < 0)
2494 return -1;
2495 total++;
2496 if (++this_batch == BATCHSIZE)
2497 break;
2498 }
2499 if (_Pickler_Write(self, &appends_op, 1) < 0)
2500 return -1;
2501
2502 } while (total < PyList_GET_SIZE(obj));
2503
2504 return 0;
2505}
2506
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002507static int
2508save_list(PicklerObject *self, PyObject *obj)
2509{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002511 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002512 int status = 0;
2513
2514 if (self->fast && !fast_save_enter(self, obj))
2515 goto error;
2516
2517 /* Create an empty list. */
2518 if (self->bin) {
2519 header[0] = EMPTY_LIST;
2520 len = 1;
2521 }
2522 else {
2523 header[0] = MARK;
2524 header[1] = LIST;
2525 len = 2;
2526 }
2527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002528 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002529 goto error;
2530
2531 /* Get list length, and bow out early if empty. */
2532 if ((len = PyList_Size(obj)) < 0)
2533 goto error;
2534
2535 if (memo_put(self, obj) < 0)
2536 goto error;
2537
2538 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002539 /* Materialize the list elements. */
2540 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002541 if (Py_EnterRecursiveCall(" while pickling an object"))
2542 goto error;
2543 status = batch_list_exact(self, obj);
2544 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002545 } else {
2546 PyObject *iter = PyObject_GetIter(obj);
2547 if (iter == NULL)
2548 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002549
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002550 if (Py_EnterRecursiveCall(" while pickling an object")) {
2551 Py_DECREF(iter);
2552 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002553 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002554 status = batch_list(self, iter);
2555 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002556 Py_DECREF(iter);
2557 }
2558 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 if (0) {
2560 error:
2561 status = -1;
2562 }
2563
2564 if (self->fast && !fast_save_leave(self, obj))
2565 status = -1;
2566
2567 return status;
2568}
2569
2570/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2571 * MARK key value ... key value SETITEMS
2572 * opcode sequences. Calling code should have arranged to first create an
2573 * empty dict, or dict-like object, for the SETITEMS to operate on.
2574 * Returns 0 on success, <0 on error.
2575 *
2576 * This is very much like batch_list(). The difference between saving
2577 * elements directly, and picking apart two-tuples, is so long-winded at
2578 * the C level, though, that attempts to combine these routines were too
2579 * ugly to bear.
2580 */
2581static int
2582batch_dict(PicklerObject *self, PyObject *iter)
2583{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002584 PyObject *obj = NULL;
2585 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002586 int i, n;
2587
2588 const char mark_op = MARK;
2589 const char setitem_op = SETITEM;
2590 const char setitems_op = SETITEMS;
2591
2592 assert(iter != NULL);
2593
2594 if (self->proto == 0) {
2595 /* SETITEMS isn't available; do one at a time. */
2596 for (;;) {
2597 obj = PyIter_Next(iter);
2598 if (obj == NULL) {
2599 if (PyErr_Occurred())
2600 return -1;
2601 break;
2602 }
2603 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2604 PyErr_SetString(PyExc_TypeError, "dict items "
2605 "iterator must return 2-tuples");
2606 return -1;
2607 }
2608 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2609 if (i >= 0)
2610 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2611 Py_DECREF(obj);
2612 if (i < 0)
2613 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002614 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002615 return -1;
2616 }
2617 return 0;
2618 }
2619
2620 /* proto > 0: write in batches of BATCHSIZE. */
2621 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002622 /* Get first item */
2623 firstitem = PyIter_Next(iter);
2624 if (firstitem == NULL) {
2625 if (PyErr_Occurred())
2626 goto error;
2627
2628 /* nothing more to add */
2629 break;
2630 }
2631 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2632 PyErr_SetString(PyExc_TypeError, "dict items "
2633 "iterator must return 2-tuples");
2634 goto error;
2635 }
2636
2637 /* Try to get a second item */
2638 obj = PyIter_Next(iter);
2639 if (obj == NULL) {
2640 if (PyErr_Occurred())
2641 goto error;
2642
2643 /* Only one item to write */
2644 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2645 goto error;
2646 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2647 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002648 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002649 goto error;
2650 Py_CLEAR(firstitem);
2651 break;
2652 }
2653
2654 /* More than one item to write */
2655
2656 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002657 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002658 goto error;
2659
2660 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2661 goto error;
2662 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2663 goto error;
2664 Py_CLEAR(firstitem);
2665 n = 1;
2666
2667 /* Fetch and save up to BATCHSIZE items */
2668 while (obj) {
2669 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2670 PyErr_SetString(PyExc_TypeError, "dict items "
2671 "iterator must return 2-tuples");
2672 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002673 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002674 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2675 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2676 goto error;
2677 Py_CLEAR(obj);
2678 n += 1;
2679
2680 if (n == BATCHSIZE)
2681 break;
2682
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002683 obj = PyIter_Next(iter);
2684 if (obj == NULL) {
2685 if (PyErr_Occurred())
2686 goto error;
2687 break;
2688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002689 }
2690
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002691 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002692 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002693
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 } while (n == BATCHSIZE);
2695 return 0;
2696
2697 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002698 Py_XDECREF(firstitem);
2699 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700 return -1;
2701}
2702
Collin Winter5c9b02d2009-05-25 05:43:30 +00002703/* This is a variant of batch_dict() above that specializes for dicts, with no
2704 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2705 * MARK key value ... key value SETITEMS
2706 * opcode sequences. Calling code should have arranged to first create an
2707 * empty dict, or dict-like object, for the SETITEMS to operate on.
2708 * Returns 0 on success, -1 on error.
2709 *
2710 * Note that this currently doesn't work for protocol 0.
2711 */
2712static int
2713batch_dict_exact(PicklerObject *self, PyObject *obj)
2714{
2715 PyObject *key = NULL, *value = NULL;
2716 int i;
2717 Py_ssize_t dict_size, ppos = 0;
2718
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002719 const char mark_op = MARK;
2720 const char setitem_op = SETITEM;
2721 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002722
2723 assert(obj != NULL);
2724 assert(self->proto > 0);
2725
2726 dict_size = PyDict_Size(obj);
2727
2728 /* Special-case len(d) == 1 to save space. */
2729 if (dict_size == 1) {
2730 PyDict_Next(obj, &ppos, &key, &value);
2731 if (save(self, key, 0) < 0)
2732 return -1;
2733 if (save(self, value, 0) < 0)
2734 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002735 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002736 return -1;
2737 return 0;
2738 }
2739
2740 /* Write in batches of BATCHSIZE. */
2741 do {
2742 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002743 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002744 return -1;
2745 while (PyDict_Next(obj, &ppos, &key, &value)) {
2746 if (save(self, key, 0) < 0)
2747 return -1;
2748 if (save(self, value, 0) < 0)
2749 return -1;
2750 if (++i == BATCHSIZE)
2751 break;
2752 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002753 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002754 return -1;
2755 if (PyDict_Size(obj) != dict_size) {
2756 PyErr_Format(
2757 PyExc_RuntimeError,
2758 "dictionary changed size during iteration");
2759 return -1;
2760 }
2761
2762 } while (i == BATCHSIZE);
2763 return 0;
2764}
2765
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002766static int
2767save_dict(PicklerObject *self, PyObject *obj)
2768{
2769 PyObject *items, *iter;
2770 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002771 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002772 int status = 0;
2773
2774 if (self->fast && !fast_save_enter(self, obj))
2775 goto error;
2776
2777 /* Create an empty dict. */
2778 if (self->bin) {
2779 header[0] = EMPTY_DICT;
2780 len = 1;
2781 }
2782 else {
2783 header[0] = MARK;
2784 header[1] = DICT;
2785 len = 2;
2786 }
2787
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002788 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002789 goto error;
2790
2791 /* Get dict size, and bow out early if empty. */
2792 if ((len = PyDict_Size(obj)) < 0)
2793 goto error;
2794
2795 if (memo_put(self, obj) < 0)
2796 goto error;
2797
2798 if (len != 0) {
2799 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002800 if (PyDict_CheckExact(obj) && self->proto > 0) {
2801 /* We can take certain shortcuts if we know this is a dict and
2802 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002803 if (Py_EnterRecursiveCall(" while pickling an object"))
2804 goto error;
2805 status = batch_dict_exact(self, obj);
2806 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002807 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002808 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002809
2810 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002811 if (items == NULL)
2812 goto error;
2813 iter = PyObject_GetIter(items);
2814 Py_DECREF(items);
2815 if (iter == NULL)
2816 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002817 if (Py_EnterRecursiveCall(" while pickling an object")) {
2818 Py_DECREF(iter);
2819 goto error;
2820 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002821 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002822 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002823 Py_DECREF(iter);
2824 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002825 }
2826
2827 if (0) {
2828 error:
2829 status = -1;
2830 }
2831
2832 if (self->fast && !fast_save_leave(self, obj))
2833 status = -1;
2834
2835 return status;
2836}
2837
2838static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002839save_set(PicklerObject *self, PyObject *obj)
2840{
2841 PyObject *item;
2842 int i;
2843 Py_ssize_t set_size, ppos = 0;
2844 Py_hash_t hash;
2845
2846 const char empty_set_op = EMPTY_SET;
2847 const char mark_op = MARK;
2848 const char additems_op = ADDITEMS;
2849
2850 if (self->proto < 4) {
2851 PyObject *items;
2852 PyObject *reduce_value;
2853 int status;
2854
2855 items = PySequence_List(obj);
2856 if (items == NULL) {
2857 return -1;
2858 }
2859 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2860 Py_DECREF(items);
2861 if (reduce_value == NULL) {
2862 return -1;
2863 }
2864 /* save_reduce() will memoize the object automatically. */
2865 status = save_reduce(self, reduce_value, obj);
2866 Py_DECREF(reduce_value);
2867 return status;
2868 }
2869
2870 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2871 return -1;
2872
2873 if (memo_put(self, obj) < 0)
2874 return -1;
2875
2876 set_size = PySet_GET_SIZE(obj);
2877 if (set_size == 0)
2878 return 0; /* nothing to do */
2879
2880 /* Write in batches of BATCHSIZE. */
2881 do {
2882 i = 0;
2883 if (_Pickler_Write(self, &mark_op, 1) < 0)
2884 return -1;
2885 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2886 if (save(self, item, 0) < 0)
2887 return -1;
2888 if (++i == BATCHSIZE)
2889 break;
2890 }
2891 if (_Pickler_Write(self, &additems_op, 1) < 0)
2892 return -1;
2893 if (PySet_GET_SIZE(obj) != set_size) {
2894 PyErr_Format(
2895 PyExc_RuntimeError,
2896 "set changed size during iteration");
2897 return -1;
2898 }
2899 } while (i == BATCHSIZE);
2900
2901 return 0;
2902}
2903
2904static int
2905save_frozenset(PicklerObject *self, PyObject *obj)
2906{
2907 PyObject *iter;
2908
2909 const char mark_op = MARK;
2910 const char frozenset_op = FROZENSET;
2911
2912 if (self->fast && !fast_save_enter(self, obj))
2913 return -1;
2914
2915 if (self->proto < 4) {
2916 PyObject *items;
2917 PyObject *reduce_value;
2918 int status;
2919
2920 items = PySequence_List(obj);
2921 if (items == NULL) {
2922 return -1;
2923 }
2924 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2925 items);
2926 Py_DECREF(items);
2927 if (reduce_value == NULL) {
2928 return -1;
2929 }
2930 /* save_reduce() will memoize the object automatically. */
2931 status = save_reduce(self, reduce_value, obj);
2932 Py_DECREF(reduce_value);
2933 return status;
2934 }
2935
2936 if (_Pickler_Write(self, &mark_op, 1) < 0)
2937 return -1;
2938
2939 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002940 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002941 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002942 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002943 for (;;) {
2944 PyObject *item;
2945
2946 item = PyIter_Next(iter);
2947 if (item == NULL) {
2948 if (PyErr_Occurred()) {
2949 Py_DECREF(iter);
2950 return -1;
2951 }
2952 break;
2953 }
2954 if (save(self, item, 0) < 0) {
2955 Py_DECREF(item);
2956 Py_DECREF(iter);
2957 return -1;
2958 }
2959 Py_DECREF(item);
2960 }
2961 Py_DECREF(iter);
2962
2963 /* If the object is already in the memo, this means it is
2964 recursive. In this case, throw away everything we put on the
2965 stack, and fetch the object back from the memo. */
2966 if (PyMemoTable_Get(self->memo, obj)) {
2967 const char pop_mark_op = POP_MARK;
2968
2969 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2970 return -1;
2971 if (memo_get(self, obj) < 0)
2972 return -1;
2973 return 0;
2974 }
2975
2976 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2977 return -1;
2978 if (memo_put(self, obj) < 0)
2979 return -1;
2980
2981 return 0;
2982}
2983
2984static int
2985fix_imports(PyObject **module_name, PyObject **global_name)
2986{
2987 PyObject *key;
2988 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002989 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002990
2991 key = PyTuple_Pack(2, *module_name, *global_name);
2992 if (key == NULL)
2993 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002994 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002995 Py_DECREF(key);
2996 if (item) {
2997 PyObject *fixed_module_name;
2998 PyObject *fixed_global_name;
2999
3000 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3001 PyErr_Format(PyExc_RuntimeError,
3002 "_compat_pickle.REVERSE_NAME_MAPPING values "
3003 "should be 2-tuples, not %.200s",
3004 Py_TYPE(item)->tp_name);
3005 return -1;
3006 }
3007 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3008 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3009 if (!PyUnicode_Check(fixed_module_name) ||
3010 !PyUnicode_Check(fixed_global_name)) {
3011 PyErr_Format(PyExc_RuntimeError,
3012 "_compat_pickle.REVERSE_NAME_MAPPING values "
3013 "should be pairs of str, not (%.200s, %.200s)",
3014 Py_TYPE(fixed_module_name)->tp_name,
3015 Py_TYPE(fixed_global_name)->tp_name);
3016 return -1;
3017 }
3018
3019 Py_CLEAR(*module_name);
3020 Py_CLEAR(*global_name);
3021 Py_INCREF(fixed_module_name);
3022 Py_INCREF(fixed_global_name);
3023 *module_name = fixed_module_name;
3024 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003025 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003026 }
3027 else if (PyErr_Occurred()) {
3028 return -1;
3029 }
3030
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003031 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003032 if (item) {
3033 if (!PyUnicode_Check(item)) {
3034 PyErr_Format(PyExc_RuntimeError,
3035 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3036 "should be strings, not %.200s",
3037 Py_TYPE(item)->tp_name);
3038 return -1;
3039 }
3040 Py_CLEAR(*module_name);
3041 Py_INCREF(item);
3042 *module_name = item;
3043 }
3044 else if (PyErr_Occurred()) {
3045 return -1;
3046 }
3047
3048 return 0;
3049}
3050
3051static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003052save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3053{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003054 PyObject *global_name = NULL;
3055 PyObject *module_name = NULL;
3056 PyObject *module = NULL;
3057 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003058 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003059 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003060 _Py_IDENTIFIER(__name__);
3061 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003062
3063 const char global_op = GLOBAL;
3064
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003065 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003066 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003067 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003068 }
3069 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003070 if (self->proto >= 4) {
3071 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3072 if (global_name == NULL) {
3073 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3074 goto error;
3075 PyErr_Clear();
3076 }
3077 }
3078 if (global_name == NULL) {
3079 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3080 if (global_name == NULL)
3081 goto error;
3082 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003083 }
3084
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003085 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003086 if (module_name == NULL)
3087 goto error;
3088
3089 /* XXX: Change to use the import C API directly with level=0 to disallow
3090 relative imports.
3091
3092 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3093 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3094 custom import functions (IMHO, this would be a nice security
3095 feature). The import C API would need to be extended to support the
3096 extra parameters of __import__ to fix that. */
3097 module = PyImport_Import(module_name);
3098 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003099 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003100 "Can't pickle %R: import of module %R failed",
3101 obj, module_name);
3102 goto error;
3103 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003104 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003105 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003106 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003107 "Can't pickle %R: attribute lookup %S on %S failed",
3108 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003109 goto error;
3110 }
3111 if (cls != obj) {
3112 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003113 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003114 "Can't pickle %R: it's not the same object as %S.%S",
3115 obj, module_name, global_name);
3116 goto error;
3117 }
3118 Py_DECREF(cls);
3119
3120 if (self->proto >= 2) {
3121 /* See whether this is in the extension registry, and if
3122 * so generate an EXT opcode.
3123 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003124 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003125 PyObject *code_obj; /* extension code as Python object */
3126 long code; /* extension code as C value */
3127 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003128 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003130 extension_key = PyTuple_Pack(2, module_name, global_name);
3131 if (extension_key == NULL) {
3132 goto error;
3133 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003134 code_obj = PyDict_GetItemWithError(st->extension_registry,
3135 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003136 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003137 /* The object is not registered in the extension registry.
3138 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003139 if (code_obj == NULL) {
3140 if (PyErr_Occurred()) {
3141 goto error;
3142 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003143 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003144 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003145
3146 /* XXX: pickle.py doesn't check neither the type, nor the range
3147 of the value returned by the extension_registry. It should for
3148 consistency. */
3149
3150 /* Verify code_obj has the right type and value. */
3151 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003152 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003153 "Can't pickle %R: extension code %R isn't an integer",
3154 obj, code_obj);
3155 goto error;
3156 }
3157 code = PyLong_AS_LONG(code_obj);
3158 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003159 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003160 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3161 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003162 goto error;
3163 }
3164
3165 /* Generate an EXT opcode. */
3166 if (code <= 0xff) {
3167 pdata[0] = EXT1;
3168 pdata[1] = (unsigned char)code;
3169 n = 2;
3170 }
3171 else if (code <= 0xffff) {
3172 pdata[0] = EXT2;
3173 pdata[1] = (unsigned char)(code & 0xff);
3174 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3175 n = 3;
3176 }
3177 else {
3178 pdata[0] = EXT4;
3179 pdata[1] = (unsigned char)(code & 0xff);
3180 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3181 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3182 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3183 n = 5;
3184 }
3185
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003186 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003187 goto error;
3188 }
3189 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003190 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003191 if (self->proto >= 4) {
3192 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193
Christian Heimese8b1ba12013-11-23 21:13:39 +01003194 if (save(self, module_name, 0) < 0)
3195 goto error;
3196 if (save(self, global_name, 0) < 0)
3197 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003198
3199 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3200 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201 }
3202 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003203 /* Generate a normal global opcode if we are using a pickle
3204 protocol < 4, or if the object is not registered in the
3205 extension registry. */
3206 PyObject *encoded;
3207 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003209 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003210 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003211
3212 /* For protocol < 3 and if the user didn't request against doing
3213 so, we convert module names to the old 2.x module names. */
3214 if (self->proto < 3 && self->fix_imports) {
3215 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003216 goto error;
3217 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003218 }
3219
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003220 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3221 both the module name and the global name using UTF-8. We do so
3222 only when we are using the pickle protocol newer than version
3223 3. This is to ensure compatibility with older Unpickler running
3224 on Python 2.x. */
3225 if (self->proto == 3) {
3226 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003227 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003228 else {
3229 unicode_encoder = PyUnicode_AsASCIIString;
3230 }
3231 encoded = unicode_encoder(module_name);
3232 if (encoded == NULL) {
3233 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003234 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235 "can't pickle module identifier '%S' using "
3236 "pickle protocol %i",
3237 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003238 goto error;
3239 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003240 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3241 PyBytes_GET_SIZE(encoded)) < 0) {
3242 Py_DECREF(encoded);
3243 goto error;
3244 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003245 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003246 if(_Pickler_Write(self, "\n", 1) < 0)
3247 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003248
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003249 /* Save the name of the module. */
3250 encoded = unicode_encoder(global_name);
3251 if (encoded == NULL) {
3252 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003253 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003254 "can't pickle global identifier '%S' using "
3255 "pickle protocol %i",
3256 global_name, self->proto);
3257 goto error;
3258 }
3259 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3260 PyBytes_GET_SIZE(encoded)) < 0) {
3261 Py_DECREF(encoded);
3262 goto error;
3263 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003264 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003265 if (_Pickler_Write(self, "\n", 1) < 0)
3266 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003267 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003268 /* Memoize the object. */
3269 if (memo_put(self, obj) < 0)
3270 goto error;
3271 }
3272
3273 if (0) {
3274 error:
3275 status = -1;
3276 }
3277 Py_XDECREF(module_name);
3278 Py_XDECREF(global_name);
3279 Py_XDECREF(module);
3280
3281 return status;
3282}
3283
3284static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003285save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3286{
3287 PyObject *reduce_value;
3288 int status;
3289
3290 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3291 if (reduce_value == NULL) {
3292 return -1;
3293 }
3294 status = save_reduce(self, reduce_value, obj);
3295 Py_DECREF(reduce_value);
3296 return status;
3297}
3298
3299static int
3300save_type(PicklerObject *self, PyObject *obj)
3301{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003302 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003303 return save_singleton_type(self, obj, Py_None);
3304 }
3305 else if (obj == (PyObject *)&PyEllipsis_Type) {
3306 return save_singleton_type(self, obj, Py_Ellipsis);
3307 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003308 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003309 return save_singleton_type(self, obj, Py_NotImplemented);
3310 }
3311 return save_global(self, obj, NULL);
3312}
3313
3314static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003315save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3316{
3317 PyObject *pid = NULL;
3318 int status = 0;
3319
3320 const char persid_op = PERSID;
3321 const char binpersid_op = BINPERSID;
3322
3323 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003324 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003325 if (pid == NULL)
3326 return -1;
3327
3328 if (pid != Py_None) {
3329 if (self->bin) {
3330 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003331 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 goto error;
3333 }
3334 else {
3335 PyObject *pid_str = NULL;
3336 char *pid_ascii_bytes;
3337 Py_ssize_t size;
3338
3339 pid_str = PyObject_Str(pid);
3340 if (pid_str == NULL)
3341 goto error;
3342
3343 /* XXX: Should it check whether the persistent id only contains
3344 ASCII characters? And what if the pid contains embedded
3345 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003346 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003347 Py_DECREF(pid_str);
3348 if (pid_ascii_bytes == NULL)
3349 goto error;
3350
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003351 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3352 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3353 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 goto error;
3355 }
3356 status = 1;
3357 }
3358
3359 if (0) {
3360 error:
3361 status = -1;
3362 }
3363 Py_XDECREF(pid);
3364
3365 return status;
3366}
3367
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003368static PyObject *
3369get_class(PyObject *obj)
3370{
3371 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003372 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003373
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003374 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003375 if (cls == NULL) {
3376 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3377 PyErr_Clear();
3378 cls = (PyObject *) Py_TYPE(obj);
3379 Py_INCREF(cls);
3380 }
3381 }
3382 return cls;
3383}
3384
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003385/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3386 * appropriate __reduce__ method for obj.
3387 */
3388static int
3389save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3390{
3391 PyObject *callable;
3392 PyObject *argtup;
3393 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003394 PyObject *listitems = Py_None;
3395 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003396 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003397 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003398 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003399
3400 const char reduce_op = REDUCE;
3401 const char build_op = BUILD;
3402 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003403 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003404
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003405 size = PyTuple_Size(args);
3406 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003407 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003408 "__reduce__ must contain 2 through 5 elements");
3409 return -1;
3410 }
3411
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003412 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3413 &callable, &argtup, &state, &listitems, &dictitems))
3414 return -1;
3415
3416 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003417 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003418 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 return -1;
3420 }
3421 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003422 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003423 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003424 return -1;
3425 }
3426
3427 if (state == Py_None)
3428 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003429
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003430 if (listitems == Py_None)
3431 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003432 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003433 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003434 "returned by __reduce__ must be an iterator, not %s",
3435 Py_TYPE(listitems)->tp_name);
3436 return -1;
3437 }
3438
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003439 if (dictitems == Py_None)
3440 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003441 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003442 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003443 "returned by __reduce__ must be an iterator, not %s",
3444 Py_TYPE(dictitems)->tp_name);
3445 return -1;
3446 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003447
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003448 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003449 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003450 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003451
Victor Stinner804e05e2013-11-14 01:26:17 +01003452 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003453 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003454 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003455 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003456 }
3457 PyErr_Clear();
3458 }
3459 else if (self->proto >= 4) {
3460 _Py_IDENTIFIER(__newobj_ex__);
3461 use_newobj_ex = PyUnicode_Check(name) &&
3462 PyUnicode_Compare(
3463 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3464 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003465 }
3466 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003468 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003469 PyUnicode_Compare(
3470 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003471 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003472 }
3473 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474
3475 if (use_newobj_ex) {
3476 PyObject *cls;
3477 PyObject *args;
3478 PyObject *kwargs;
3479
3480 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003481 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003482 "length of the NEWOBJ_EX argument tuple must be "
3483 "exactly 3, not %zd", Py_SIZE(argtup));
3484 return -1;
3485 }
3486
3487 cls = PyTuple_GET_ITEM(argtup, 0);
3488 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003489 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003490 "first item from NEWOBJ_EX argument tuple must "
3491 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3492 return -1;
3493 }
3494 args = PyTuple_GET_ITEM(argtup, 1);
3495 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003496 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 "second item from NEWOBJ_EX argument tuple must "
3498 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3499 return -1;
3500 }
3501 kwargs = PyTuple_GET_ITEM(argtup, 2);
3502 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003503 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 "third item from NEWOBJ_EX argument tuple must "
3505 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3506 return -1;
3507 }
3508
3509 if (save(self, cls, 0) < 0 ||
3510 save(self, args, 0) < 0 ||
3511 save(self, kwargs, 0) < 0 ||
3512 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3513 return -1;
3514 }
3515 }
3516 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003517 PyObject *cls;
3518 PyObject *newargtup;
3519 PyObject *obj_class;
3520 int p;
3521
3522 /* Sanity checks. */
3523 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003524 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 return -1;
3526 }
3527
3528 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003529 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003530 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003531 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 return -1;
3533 }
3534
3535 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003536 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003537 p = obj_class != cls; /* true iff a problem */
3538 Py_DECREF(obj_class);
3539 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003540 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541 "__newobj__ args has the wrong class");
3542 return -1;
3543 }
3544 }
3545 /* XXX: These calls save() are prone to infinite recursion. Imagine
3546 what happen if the value returned by the __reduce__() method of
3547 some extension type contains another object of the same type. Ouch!
3548
3549 Here is a quick example, that I ran into, to illustrate what I
3550 mean:
3551
3552 >>> import pickle, copyreg
3553 >>> copyreg.dispatch_table.pop(complex)
3554 >>> pickle.dumps(1+2j)
3555 Traceback (most recent call last):
3556 ...
3557 RuntimeError: maximum recursion depth exceeded
3558
3559 Removing the complex class from copyreg.dispatch_table made the
3560 __reduce_ex__() method emit another complex object:
3561
3562 >>> (1+1j).__reduce_ex__(2)
3563 (<function __newobj__ at 0xb7b71c3c>,
3564 (<class 'complex'>, (1+1j)), None, None, None)
3565
3566 Thus when save() was called on newargstup (the 2nd item) recursion
3567 ensued. Of course, the bug was in the complex class which had a
3568 broken __getnewargs__() that emitted another complex object. But,
3569 the point, here, is it is quite easy to end up with a broken reduce
3570 function. */
3571
3572 /* Save the class and its __new__ arguments. */
3573 if (save(self, cls, 0) < 0)
3574 return -1;
3575
3576 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3577 if (newargtup == NULL)
3578 return -1;
3579
3580 p = save(self, newargtup, 0);
3581 Py_DECREF(newargtup);
3582 if (p < 0)
3583 return -1;
3584
3585 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003586 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003587 return -1;
3588 }
3589 else { /* Not using NEWOBJ. */
3590 if (save(self, callable, 0) < 0 ||
3591 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003592 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003593 return -1;
3594 }
3595
3596 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3597 the caller do not want to memoize the object. Not particularly useful,
3598 but that is to mimic the behavior save_reduce() in pickle.py when
3599 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003600 if (obj != NULL) {
3601 /* If the object is already in the memo, this means it is
3602 recursive. In this case, throw away everything we put on the
3603 stack, and fetch the object back from the memo. */
3604 if (PyMemoTable_Get(self->memo, obj)) {
3605 const char pop_op = POP;
3606
3607 if (_Pickler_Write(self, &pop_op, 1) < 0)
3608 return -1;
3609 if (memo_get(self, obj) < 0)
3610 return -1;
3611
3612 return 0;
3613 }
3614 else if (memo_put(self, obj) < 0)
3615 return -1;
3616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003617
3618 if (listitems && batch_list(self, listitems) < 0)
3619 return -1;
3620
3621 if (dictitems && batch_dict(self, dictitems) < 0)
3622 return -1;
3623
3624 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003625 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003626 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003627 return -1;
3628 }
3629
3630 return 0;
3631}
3632
3633static int
3634save(PicklerObject *self, PyObject *obj, int pers_save)
3635{
3636 PyTypeObject *type;
3637 PyObject *reduce_func = NULL;
3638 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003639 int status = 0;
3640
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003641 if (_Pickler_OpcodeBoundary(self) < 0)
3642 return -1;
3643
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003644 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003645 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646
3647 /* The extra pers_save argument is necessary to avoid calling save_pers()
3648 on its returned object. */
3649 if (!pers_save && self->pers_func) {
3650 /* save_pers() returns:
3651 -1 to signal an error;
3652 0 if it did nothing successfully;
3653 1 if a persistent id was saved.
3654 */
3655 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3656 goto done;
3657 }
3658
3659 type = Py_TYPE(obj);
3660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003661 /* The old cPickle had an optimization that used switch-case statement
3662 dispatching on the first letter of the type name. This has was removed
3663 since benchmarks shown that this optimization was actually slowing
3664 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665
3666 /* Atom types; these aren't memoized, so don't check the memo. */
3667
3668 if (obj == Py_None) {
3669 status = save_none(self, obj);
3670 goto done;
3671 }
3672 else if (obj == Py_False || obj == Py_True) {
3673 status = save_bool(self, obj);
3674 goto done;
3675 }
3676 else if (type == &PyLong_Type) {
3677 status = save_long(self, obj);
3678 goto done;
3679 }
3680 else if (type == &PyFloat_Type) {
3681 status = save_float(self, obj);
3682 goto done;
3683 }
3684
3685 /* Check the memo to see if it has the object. If so, generate
3686 a GET (or BINGET) opcode, instead of pickling the object
3687 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003688 if (PyMemoTable_Get(self->memo, obj)) {
3689 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003690 goto error;
3691 goto done;
3692 }
3693
3694 if (type == &PyBytes_Type) {
3695 status = save_bytes(self, obj);
3696 goto done;
3697 }
3698 else if (type == &PyUnicode_Type) {
3699 status = save_unicode(self, obj);
3700 goto done;
3701 }
3702 else if (type == &PyDict_Type) {
3703 status = save_dict(self, obj);
3704 goto done;
3705 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003706 else if (type == &PySet_Type) {
3707 status = save_set(self, obj);
3708 goto done;
3709 }
3710 else if (type == &PyFrozenSet_Type) {
3711 status = save_frozenset(self, obj);
3712 goto done;
3713 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714 else if (type == &PyList_Type) {
3715 status = save_list(self, obj);
3716 goto done;
3717 }
3718 else if (type == &PyTuple_Type) {
3719 status = save_tuple(self, obj);
3720 goto done;
3721 }
3722 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003723 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003724 goto done;
3725 }
3726 else if (type == &PyFunction_Type) {
3727 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003728 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003729 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730
3731 /* XXX: This part needs some unit tests. */
3732
3733 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003734 * self.dispatch_table, copyreg.dispatch_table, the object's
3735 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003736 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003737 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003738 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003739 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3740 (PyObject *)type);
3741 if (reduce_func == NULL) {
3742 if (PyErr_Occurred()) {
3743 goto error;
3744 }
3745 } else {
3746 /* PyDict_GetItemWithError() returns a borrowed reference.
3747 Increase the reference count to be consistent with
3748 PyObject_GetItem and _PyObject_GetAttrId used below. */
3749 Py_INCREF(reduce_func);
3750 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003751 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003752 reduce_func = PyObject_GetItem(self->dispatch_table,
3753 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003754 if (reduce_func == NULL) {
3755 if (PyErr_ExceptionMatches(PyExc_KeyError))
3756 PyErr_Clear();
3757 else
3758 goto error;
3759 }
3760 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003761 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003763 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003764 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003765 else if (PyType_IsSubtype(type, &PyType_Type)) {
3766 status = save_global(self, obj, NULL);
3767 goto done;
3768 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003769 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003770 _Py_IDENTIFIER(__reduce__);
3771 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003773
3774 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3775 automatically defined as __reduce__. While this is convenient, this
3776 make it impossible to know which method was actually called. Of
3777 course, this is not a big deal. But still, it would be nice to let
3778 the user know which method was called when something go
3779 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3780 don't actually have to check for a __reduce__ method. */
3781
3782 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003783 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003784 if (reduce_func != NULL) {
3785 PyObject *proto;
3786 proto = PyLong_FromLong(self->proto);
3787 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003788 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003789 }
3790 }
3791 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003792 PickleState *st = _Pickle_GetGlobalState();
3793
3794 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003795 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003796 }
3797 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003798 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003799 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003800 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003801 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003803 PyObject *empty_tuple = PyTuple_New(0);
3804 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003805 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003806 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003807 }
3808 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003809 PyErr_Format(st->PicklingError,
3810 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003811 type->tp_name, obj);
3812 goto error;
3813 }
3814 }
3815 }
3816
3817 if (reduce_value == NULL)
3818 goto error;
3819
3820 if (PyUnicode_Check(reduce_value)) {
3821 status = save_global(self, obj, reduce_value);
3822 goto done;
3823 }
3824
3825 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003826 PickleState *st = _Pickle_GetGlobalState();
3827 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003828 "__reduce__ must return a string or tuple");
3829 goto error;
3830 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003831
3832 status = save_reduce(self, reduce_value, obj);
3833
3834 if (0) {
3835 error:
3836 status = -1;
3837 }
3838 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003839
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003840 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841 Py_XDECREF(reduce_func);
3842 Py_XDECREF(reduce_value);
3843
3844 return status;
3845}
3846
3847static int
3848dump(PicklerObject *self, PyObject *obj)
3849{
3850 const char stop_op = STOP;
3851
3852 if (self->proto >= 2) {
3853 char header[2];
3854
3855 header[0] = PROTO;
3856 assert(self->proto >= 0 && self->proto < 256);
3857 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003858 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003860 if (self->proto >= 4)
3861 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862 }
3863
3864 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003865 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 return -1;
3867
3868 return 0;
3869}
3870
Larry Hastings61272b72014-01-07 12:41:53 -08003871/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003872
3873_pickle.Pickler.clear_memo
3874
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003875Clears the pickler's "memo".
3876
3877The memo is the data structure that remembers which objects the
3878pickler has already seen, so that shared or recursive objects are
3879pickled by reference and not by value. This method is useful when
3880re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003881[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003882
Larry Hastings3cceb382014-01-04 11:09:09 -08003883static PyObject *
3884_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003885/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003886{
3887 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003888 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003889
3890 Py_RETURN_NONE;
3891}
3892
Larry Hastings61272b72014-01-07 12:41:53 -08003893/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003894
3895_pickle.Pickler.dump
3896
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003897 obj: object
3898 /
3899
3900Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003901[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003902
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003904_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003905/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003906{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003907 /* Check whether the Pickler was initialized correctly (issue3664).
3908 Developers often forget to call __init__() in their subclasses, which
3909 would trigger a segfault without this check. */
3910 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003911 PickleState *st = _Pickle_GetGlobalState();
3912 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003913 "Pickler.__init__() was not called by %s.__init__()",
3914 Py_TYPE(self)->tp_name);
3915 return NULL;
3916 }
3917
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003918 if (_Pickler_ClearBuffer(self) < 0)
3919 return NULL;
3920
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003921 if (dump(self, obj) < 0)
3922 return NULL;
3923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003924 if (_Pickler_FlushToFile(self) < 0)
3925 return NULL;
3926
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 Py_RETURN_NONE;
3928}
3929
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003930/*[clinic input]
3931
3932_pickle.Pickler.__sizeof__ -> Py_ssize_t
3933
3934Returns size in memory, in bytes.
3935[clinic start generated code]*/
3936
3937static Py_ssize_t
3938_pickle_Pickler___sizeof___impl(PicklerObject *self)
3939/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
3940{
3941 Py_ssize_t res, s;
3942
3943 res = sizeof(PicklerObject);
3944 if (self->memo != NULL) {
3945 res += sizeof(PyMemoTable);
3946 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
3947 }
3948 if (self->output_buffer != NULL) {
3949 s = _PySys_GetSizeOf(self->output_buffer);
3950 if (s == -1)
3951 return -1;
3952 res += s;
3953 }
3954 return res;
3955}
3956
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003958 _PICKLE_PICKLER_DUMP_METHODDEF
3959 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003960 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961 {NULL, NULL} /* sentinel */
3962};
3963
3964static void
3965Pickler_dealloc(PicklerObject *self)
3966{
3967 PyObject_GC_UnTrack(self);
3968
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003969 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003970 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003972 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 Py_XDECREF(self->fast_memo);
3974
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003975 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976
3977 Py_TYPE(self)->tp_free((PyObject *)self);
3978}
3979
3980static int
3981Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3982{
3983 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003984 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003985 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 Py_VISIT(self->fast_memo);
3987 return 0;
3988}
3989
3990static int
3991Pickler_clear(PicklerObject *self)
3992{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003993 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003994 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003995 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003996 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 Py_CLEAR(self->fast_memo);
3998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003999 if (self->memo != NULL) {
4000 PyMemoTable *memo = self->memo;
4001 self->memo = NULL;
4002 PyMemoTable_Del(memo);
4003 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 return 0;
4005}
4006
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004007
Larry Hastings61272b72014-01-07 12:41:53 -08004008/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004009
4010_pickle.Pickler.__init__
4011
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004012 file: object
4013 protocol: object = NULL
4014 fix_imports: bool = True
4015
4016This takes a binary file for writing a pickle data stream.
4017
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004018The optional *protocol* argument tells the pickler to use the given
4019protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4020protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004021
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004022Specifying a negative protocol version selects the highest protocol
4023version supported. The higher the protocol used, the more recent the
4024version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004025
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004026The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004027bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004028writing, a io.BytesIO instance, or any other custom object that meets
4029this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004031If *fix_imports* is True and protocol is less than 3, pickle will try
4032to map the new Python 3 names to the old module names used in Python
40332, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004034[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004035
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004036static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08004038/*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004040 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004041 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043 /* In case of multiple __init__() calls, clear previous content. */
4044 if (self->write != NULL)
4045 (void)Pickler_clear(self);
4046
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004047 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004048 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004049
4050 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004051 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004052
4053 /* memo and output_buffer may have already been created in _Pickler_New */
4054 if (self->memo == NULL) {
4055 self->memo = PyMemoTable_New();
4056 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004057 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004058 }
4059 self->output_len = 0;
4060 if (self->output_buffer == NULL) {
4061 self->max_output_len = WRITE_BUF_SIZE;
4062 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4063 self->max_output_len);
4064 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004065 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004066 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004068 self->fast = 0;
4069 self->fast_nesting = 0;
4070 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004071 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004072 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4073 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4074 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004076 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004077 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004078 self->dispatch_table = NULL;
4079 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4080 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4081 &PyId_dispatch_table);
4082 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004083 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004084 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004085
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004086 return 0;
4087}
4088
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004090/* Define a proxy object for the Pickler's internal memo object. This is to
4091 * avoid breaking code like:
4092 * pickler.memo.clear()
4093 * and
4094 * pickler.memo = saved_memo
4095 * Is this a good idea? Not really, but we don't want to break code that uses
4096 * it. Note that we don't implement the entire mapping API here. This is
4097 * intentional, as these should be treated as black-box implementation details.
4098 */
4099
Larry Hastings61272b72014-01-07 12:41:53 -08004100/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004101_pickle.PicklerMemoProxy.clear
4102
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004103Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004104[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004105
Larry Hastings3cceb382014-01-04 11:09:09 -08004106static PyObject *
4107_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004108/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004109{
4110 if (self->pickler->memo)
4111 PyMemoTable_Clear(self->pickler->memo);
4112 Py_RETURN_NONE;
4113}
4114
Larry Hastings61272b72014-01-07 12:41:53 -08004115/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004116_pickle.PicklerMemoProxy.copy
4117
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004118Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004119[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004120
Larry Hastings3cceb382014-01-04 11:09:09 -08004121static PyObject *
4122_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004123/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004124{
4125 Py_ssize_t i;
4126 PyMemoTable *memo;
4127 PyObject *new_memo = PyDict_New();
4128 if (new_memo == NULL)
4129 return NULL;
4130
4131 memo = self->pickler->memo;
4132 for (i = 0; i < memo->mt_allocated; ++i) {
4133 PyMemoEntry entry = memo->mt_table[i];
4134 if (entry.me_key != NULL) {
4135 int status;
4136 PyObject *key, *value;
4137
4138 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004139 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004140
4141 if (key == NULL || value == NULL) {
4142 Py_XDECREF(key);
4143 Py_XDECREF(value);
4144 goto error;
4145 }
4146 status = PyDict_SetItem(new_memo, key, value);
4147 Py_DECREF(key);
4148 Py_DECREF(value);
4149 if (status < 0)
4150 goto error;
4151 }
4152 }
4153 return new_memo;
4154
4155 error:
4156 Py_XDECREF(new_memo);
4157 return NULL;
4158}
4159
Larry Hastings61272b72014-01-07 12:41:53 -08004160/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004161_pickle.PicklerMemoProxy.__reduce__
4162
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004163Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004164[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004165
Larry Hastings3cceb382014-01-04 11:09:09 -08004166static PyObject *
4167_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004168/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004169{
4170 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004171 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004172 if (contents == NULL)
4173 return NULL;
4174
4175 reduce_value = PyTuple_New(2);
4176 if (reduce_value == NULL) {
4177 Py_DECREF(contents);
4178 return NULL;
4179 }
4180 dict_args = PyTuple_New(1);
4181 if (dict_args == NULL) {
4182 Py_DECREF(contents);
4183 Py_DECREF(reduce_value);
4184 return NULL;
4185 }
4186 PyTuple_SET_ITEM(dict_args, 0, contents);
4187 Py_INCREF((PyObject *)&PyDict_Type);
4188 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4189 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4190 return reduce_value;
4191}
4192
4193static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004194 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4195 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4196 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004197 {NULL, NULL} /* sentinel */
4198};
4199
4200static void
4201PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4202{
4203 PyObject_GC_UnTrack(self);
4204 Py_XDECREF(self->pickler);
4205 PyObject_GC_Del((PyObject *)self);
4206}
4207
4208static int
4209PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4210 visitproc visit, void *arg)
4211{
4212 Py_VISIT(self->pickler);
4213 return 0;
4214}
4215
4216static int
4217PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4218{
4219 Py_CLEAR(self->pickler);
4220 return 0;
4221}
4222
4223static PyTypeObject PicklerMemoProxyType = {
4224 PyVarObject_HEAD_INIT(NULL, 0)
4225 "_pickle.PicklerMemoProxy", /*tp_name*/
4226 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4227 0,
4228 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4229 0, /* tp_print */
4230 0, /* tp_getattr */
4231 0, /* tp_setattr */
4232 0, /* tp_compare */
4233 0, /* tp_repr */
4234 0, /* tp_as_number */
4235 0, /* tp_as_sequence */
4236 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004237 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004238 0, /* tp_call */
4239 0, /* tp_str */
4240 PyObject_GenericGetAttr, /* tp_getattro */
4241 PyObject_GenericSetAttr, /* tp_setattro */
4242 0, /* tp_as_buffer */
4243 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4244 0, /* tp_doc */
4245 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4246 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4247 0, /* tp_richcompare */
4248 0, /* tp_weaklistoffset */
4249 0, /* tp_iter */
4250 0, /* tp_iternext */
4251 picklerproxy_methods, /* tp_methods */
4252};
4253
4254static PyObject *
4255PicklerMemoProxy_New(PicklerObject *pickler)
4256{
4257 PicklerMemoProxyObject *self;
4258
4259 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4260 if (self == NULL)
4261 return NULL;
4262 Py_INCREF(pickler);
4263 self->pickler = pickler;
4264 PyObject_GC_Track(self);
4265 return (PyObject *)self;
4266}
4267
4268/*****************************************************************************/
4269
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004270static PyObject *
4271Pickler_get_memo(PicklerObject *self)
4272{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004273 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004274}
4275
4276static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004277Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004278{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004279 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004280
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004281 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004282 PyErr_SetString(PyExc_TypeError,
4283 "attribute deletion is not supported");
4284 return -1;
4285 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286
4287 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4288 PicklerObject *pickler =
4289 ((PicklerMemoProxyObject *)obj)->pickler;
4290
4291 new_memo = PyMemoTable_Copy(pickler->memo);
4292 if (new_memo == NULL)
4293 return -1;
4294 }
4295 else if (PyDict_Check(obj)) {
4296 Py_ssize_t i = 0;
4297 PyObject *key, *value;
4298
4299 new_memo = PyMemoTable_New();
4300 if (new_memo == NULL)
4301 return -1;
4302
4303 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004304 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004305 PyObject *memo_obj;
4306
4307 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4308 PyErr_SetString(PyExc_TypeError,
4309 "'memo' values must be 2-item tuples");
4310 goto error;
4311 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004312 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004313 if (memo_id == -1 && PyErr_Occurred())
4314 goto error;
4315 memo_obj = PyTuple_GET_ITEM(value, 1);
4316 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4317 goto error;
4318 }
4319 }
4320 else {
4321 PyErr_Format(PyExc_TypeError,
4322 "'memo' attribute must be an PicklerMemoProxy object"
4323 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004324 return -1;
4325 }
4326
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004327 PyMemoTable_Del(self->memo);
4328 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004329
4330 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004331
4332 error:
4333 if (new_memo)
4334 PyMemoTable_Del(new_memo);
4335 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004336}
4337
4338static PyObject *
4339Pickler_get_persid(PicklerObject *self)
4340{
4341 if (self->pers_func == NULL)
4342 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4343 else
4344 Py_INCREF(self->pers_func);
4345 return self->pers_func;
4346}
4347
4348static int
4349Pickler_set_persid(PicklerObject *self, PyObject *value)
4350{
4351 PyObject *tmp;
4352
4353 if (value == NULL) {
4354 PyErr_SetString(PyExc_TypeError,
4355 "attribute deletion is not supported");
4356 return -1;
4357 }
4358 if (!PyCallable_Check(value)) {
4359 PyErr_SetString(PyExc_TypeError,
4360 "persistent_id must be a callable taking one argument");
4361 return -1;
4362 }
4363
4364 tmp = self->pers_func;
4365 Py_INCREF(value);
4366 self->pers_func = value;
4367 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4368
4369 return 0;
4370}
4371
4372static PyMemberDef Pickler_members[] = {
4373 {"bin", T_INT, offsetof(PicklerObject, bin)},
4374 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004375 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004376 {NULL}
4377};
4378
4379static PyGetSetDef Pickler_getsets[] = {
4380 {"memo", (getter)Pickler_get_memo,
4381 (setter)Pickler_set_memo},
4382 {"persistent_id", (getter)Pickler_get_persid,
4383 (setter)Pickler_set_persid},
4384 {NULL}
4385};
4386
4387static PyTypeObject Pickler_Type = {
4388 PyVarObject_HEAD_INIT(NULL, 0)
4389 "_pickle.Pickler" , /*tp_name*/
4390 sizeof(PicklerObject), /*tp_basicsize*/
4391 0, /*tp_itemsize*/
4392 (destructor)Pickler_dealloc, /*tp_dealloc*/
4393 0, /*tp_print*/
4394 0, /*tp_getattr*/
4395 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004396 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004397 0, /*tp_repr*/
4398 0, /*tp_as_number*/
4399 0, /*tp_as_sequence*/
4400 0, /*tp_as_mapping*/
4401 0, /*tp_hash*/
4402 0, /*tp_call*/
4403 0, /*tp_str*/
4404 0, /*tp_getattro*/
4405 0, /*tp_setattro*/
4406 0, /*tp_as_buffer*/
4407 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004408 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409 (traverseproc)Pickler_traverse, /*tp_traverse*/
4410 (inquiry)Pickler_clear, /*tp_clear*/
4411 0, /*tp_richcompare*/
4412 0, /*tp_weaklistoffset*/
4413 0, /*tp_iter*/
4414 0, /*tp_iternext*/
4415 Pickler_methods, /*tp_methods*/
4416 Pickler_members, /*tp_members*/
4417 Pickler_getsets, /*tp_getset*/
4418 0, /*tp_base*/
4419 0, /*tp_dict*/
4420 0, /*tp_descr_get*/
4421 0, /*tp_descr_set*/
4422 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004423 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004424 PyType_GenericAlloc, /*tp_alloc*/
4425 PyType_GenericNew, /*tp_new*/
4426 PyObject_GC_Del, /*tp_free*/
4427 0, /*tp_is_gc*/
4428};
4429
Victor Stinner121aab42011-09-29 23:40:53 +02004430/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431
4432 XXX: It would be nice to able to avoid Python function call overhead, by
4433 using directly the C version of find_class(), when find_class() is not
4434 overridden by a subclass. Although, this could become rather hackish. A
4435 simpler optimization would be to call the C function when self is not a
4436 subclass instance. */
4437static PyObject *
4438find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4439{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004440 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004441
4442 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4443 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004444}
4445
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004446static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004447marker(UnpicklerObject *self)
4448{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004449 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004450 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004451 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004452 return -1;
4453 }
4454
4455 return self->marks[--self->num_marks];
4456}
4457
4458static int
4459load_none(UnpicklerObject *self)
4460{
4461 PDATA_APPEND(self->stack, Py_None, -1);
4462 return 0;
4463}
4464
4465static int
4466bad_readline(void)
4467{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004468 PickleState *st = _Pickle_GetGlobalState();
4469 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 return -1;
4471}
4472
4473static int
4474load_int(UnpicklerObject *self)
4475{
4476 PyObject *value;
4477 char *endptr, *s;
4478 Py_ssize_t len;
4479 long x;
4480
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004481 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004482 return -1;
4483 if (len < 2)
4484 return bad_readline();
4485
4486 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004487 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004488 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004489 x = strtol(s, &endptr, 0);
4490
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004491 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004492 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004493 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004494 errno = 0;
4495 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004496 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497 if (value == NULL) {
4498 PyErr_SetString(PyExc_ValueError,
4499 "could not convert string to int");
4500 return -1;
4501 }
4502 }
4503 else {
4504 if (len == 3 && (x == 0 || x == 1)) {
4505 if ((value = PyBool_FromLong(x)) == NULL)
4506 return -1;
4507 }
4508 else {
4509 if ((value = PyLong_FromLong(x)) == NULL)
4510 return -1;
4511 }
4512 }
4513
4514 PDATA_PUSH(self->stack, value, -1);
4515 return 0;
4516}
4517
4518static int
4519load_bool(UnpicklerObject *self, PyObject *boolean)
4520{
4521 assert(boolean == Py_True || boolean == Py_False);
4522 PDATA_APPEND(self->stack, boolean, -1);
4523 return 0;
4524}
4525
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004526/* s contains x bytes of an unsigned little-endian integer. Return its value
4527 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4528 */
4529static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004530calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004531{
4532 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004533 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004534 size_t x = 0;
4535
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004536 for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004537 x |= (size_t) s[i] << (8 * i);
4538 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004539
4540 if (x > PY_SSIZE_T_MAX)
4541 return -1;
4542 else
4543 return (Py_ssize_t) x;
4544}
4545
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004546/* s contains x bytes of a little-endian integer. Return its value as a
4547 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4548 * int, but when x is 4 it's a signed one. This is an historical source
4549 * of x-platform bugs.
4550 */
4551static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004552calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004553{
4554 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004555 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004556 long x = 0;
4557
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004558 for (i = 0; i < nbytes; i++) {
4559 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004560 }
4561
4562 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4563 * is signed, so on a box with longs bigger than 4 bytes we need
4564 * to extend a BININT's sign bit to the full width.
4565 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004566 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567 x |= -(x & (1L << 31));
4568 }
4569
4570 return x;
4571}
4572
4573static int
4574load_binintx(UnpicklerObject *self, char *s, int size)
4575{
4576 PyObject *value;
4577 long x;
4578
4579 x = calc_binint(s, size);
4580
4581 if ((value = PyLong_FromLong(x)) == NULL)
4582 return -1;
4583
4584 PDATA_PUSH(self->stack, value, -1);
4585 return 0;
4586}
4587
4588static int
4589load_binint(UnpicklerObject *self)
4590{
4591 char *s;
4592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004593 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004594 return -1;
4595
4596 return load_binintx(self, s, 4);
4597}
4598
4599static int
4600load_binint1(UnpicklerObject *self)
4601{
4602 char *s;
4603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004604 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004605 return -1;
4606
4607 return load_binintx(self, s, 1);
4608}
4609
4610static int
4611load_binint2(UnpicklerObject *self)
4612{
4613 char *s;
4614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 return -1;
4617
4618 return load_binintx(self, s, 2);
4619}
4620
4621static int
4622load_long(UnpicklerObject *self)
4623{
4624 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004625 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004626 Py_ssize_t len;
4627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004628 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629 return -1;
4630 if (len < 2)
4631 return bad_readline();
4632
Mark Dickinson8dd05142009-01-20 20:43:58 +00004633 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4634 the 'L' before calling PyLong_FromString. In order to maintain
4635 compatibility with Python 3.0.0, we don't actually *require*
4636 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004637 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004638 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004639 /* XXX: Should the base argument explicitly set to 10? */
4640 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004641 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642 return -1;
4643
4644 PDATA_PUSH(self->stack, value, -1);
4645 return 0;
4646}
4647
4648/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4649 * data following.
4650 */
4651static int
4652load_counted_long(UnpicklerObject *self, int size)
4653{
4654 PyObject *value;
4655 char *nbytes;
4656 char *pdata;
4657
4658 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004659 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004660 return -1;
4661
4662 size = calc_binint(nbytes, size);
4663 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004664 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004665 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004666 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 "LONG pickle has negative byte count");
4668 return -1;
4669 }
4670
4671 if (size == 0)
4672 value = PyLong_FromLong(0L);
4673 else {
4674 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004675 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004676 return -1;
4677 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4678 1 /* little endian */ , 1 /* signed */ );
4679 }
4680 if (value == NULL)
4681 return -1;
4682 PDATA_PUSH(self->stack, value, -1);
4683 return 0;
4684}
4685
4686static int
4687load_float(UnpicklerObject *self)
4688{
4689 PyObject *value;
4690 char *endptr, *s;
4691 Py_ssize_t len;
4692 double d;
4693
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004694 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695 return -1;
4696 if (len < 2)
4697 return bad_readline();
4698
4699 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004700 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4701 if (d == -1.0 && PyErr_Occurred())
4702 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004703 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4705 return -1;
4706 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004707 value = PyFloat_FromDouble(d);
4708 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004709 return -1;
4710
4711 PDATA_PUSH(self->stack, value, -1);
4712 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004713}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004714
4715static int
4716load_binfloat(UnpicklerObject *self)
4717{
4718 PyObject *value;
4719 double x;
4720 char *s;
4721
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004722 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004723 return -1;
4724
4725 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4726 if (x == -1.0 && PyErr_Occurred())
4727 return -1;
4728
4729 if ((value = PyFloat_FromDouble(x)) == NULL)
4730 return -1;
4731
4732 PDATA_PUSH(self->stack, value, -1);
4733 return 0;
4734}
4735
4736static int
4737load_string(UnpicklerObject *self)
4738{
4739 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004740 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004741 Py_ssize_t len;
4742 char *s, *p;
4743
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004744 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004746 /* Strip the newline */
4747 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004749 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004750 p = s + 1;
4751 len -= 2;
4752 }
4753 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004754 PickleState *st = _Pickle_GetGlobalState();
4755 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004756 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004757 return -1;
4758 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004759 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004760
4761 /* Use the PyBytes API to decode the string, since that is what is used
4762 to encode, and then coerce the result to Unicode. */
4763 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 if (bytes == NULL)
4765 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004766
4767 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4768 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4769 if (strcmp(self->encoding, "bytes") == 0) {
4770 obj = bytes;
4771 }
4772 else {
4773 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4774 Py_DECREF(bytes);
4775 if (obj == NULL) {
4776 return -1;
4777 }
4778 }
4779
4780 PDATA_PUSH(self->stack, obj, -1);
4781 return 0;
4782}
4783
4784static int
4785load_counted_binstring(UnpicklerObject *self, int nbytes)
4786{
4787 PyObject *obj;
4788 Py_ssize_t size;
4789 char *s;
4790
4791 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004792 return -1;
4793
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004794 size = calc_binsize(s, nbytes);
4795 if (size < 0) {
4796 PickleState *st = _Pickle_GetGlobalState();
4797 PyErr_Format(st->UnpicklingError,
4798 "BINSTRING exceeds system's maximum size of %zd bytes",
4799 PY_SSIZE_T_MAX);
4800 return -1;
4801 }
4802
4803 if (_Unpickler_Read(self, &s, size) < 0)
4804 return -1;
4805
4806 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4807 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4808 if (strcmp(self->encoding, "bytes") == 0) {
4809 obj = PyBytes_FromStringAndSize(s, size);
4810 }
4811 else {
4812 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4813 }
4814 if (obj == NULL) {
4815 return -1;
4816 }
4817
4818 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004819 return 0;
4820}
4821
4822static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004823load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004824{
4825 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004826 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004827 char *s;
4828
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004829 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004830 return -1;
4831
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004832 size = calc_binsize(s, nbytes);
4833 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004834 PyErr_Format(PyExc_OverflowError,
4835 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004836 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004837 return -1;
4838 }
4839
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004840 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004842
4843 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 if (bytes == NULL)
4845 return -1;
4846
4847 PDATA_PUSH(self->stack, bytes, -1);
4848 return 0;
4849}
4850
4851static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852load_unicode(UnpicklerObject *self)
4853{
4854 PyObject *str;
4855 Py_ssize_t len;
4856 char *s;
4857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004858 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004859 return -1;
4860 if (len < 1)
4861 return bad_readline();
4862
4863 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4864 if (str == NULL)
4865 return -1;
4866
4867 PDATA_PUSH(self->stack, str, -1);
4868 return 0;
4869}
4870
4871static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004872load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004873{
4874 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004875 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004876 char *s;
4877
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004878 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004879 return -1;
4880
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004881 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004882 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004883 PyErr_Format(PyExc_OverflowError,
4884 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004885 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004886 return -1;
4887 }
4888
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004889 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004890 return -1;
4891
Victor Stinner485fb562010-04-13 11:07:24 +00004892 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893 if (str == NULL)
4894 return -1;
4895
4896 PDATA_PUSH(self->stack, str, -1);
4897 return 0;
4898}
4899
4900static int
4901load_tuple(UnpicklerObject *self)
4902{
4903 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004904 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004905
4906 if ((i = marker(self)) < 0)
4907 return -1;
4908
4909 tuple = Pdata_poptuple(self->stack, i);
4910 if (tuple == NULL)
4911 return -1;
4912 PDATA_PUSH(self->stack, tuple, -1);
4913 return 0;
4914}
4915
4916static int
4917load_counted_tuple(UnpicklerObject *self, int len)
4918{
4919 PyObject *tuple;
4920
4921 tuple = PyTuple_New(len);
4922 if (tuple == NULL)
4923 return -1;
4924
4925 while (--len >= 0) {
4926 PyObject *item;
4927
4928 PDATA_POP(self->stack, item);
4929 if (item == NULL)
4930 return -1;
4931 PyTuple_SET_ITEM(tuple, len, item);
4932 }
4933 PDATA_PUSH(self->stack, tuple, -1);
4934 return 0;
4935}
4936
4937static int
4938load_empty_list(UnpicklerObject *self)
4939{
4940 PyObject *list;
4941
4942 if ((list = PyList_New(0)) == NULL)
4943 return -1;
4944 PDATA_PUSH(self->stack, list, -1);
4945 return 0;
4946}
4947
4948static int
4949load_empty_dict(UnpicklerObject *self)
4950{
4951 PyObject *dict;
4952
4953 if ((dict = PyDict_New()) == NULL)
4954 return -1;
4955 PDATA_PUSH(self->stack, dict, -1);
4956 return 0;
4957}
4958
4959static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004960load_empty_set(UnpicklerObject *self)
4961{
4962 PyObject *set;
4963
4964 if ((set = PySet_New(NULL)) == NULL)
4965 return -1;
4966 PDATA_PUSH(self->stack, set, -1);
4967 return 0;
4968}
4969
4970static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004971load_list(UnpicklerObject *self)
4972{
4973 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004974 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975
4976 if ((i = marker(self)) < 0)
4977 return -1;
4978
4979 list = Pdata_poplist(self->stack, i);
4980 if (list == NULL)
4981 return -1;
4982 PDATA_PUSH(self->stack, list, -1);
4983 return 0;
4984}
4985
4986static int
4987load_dict(UnpicklerObject *self)
4988{
4989 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004990 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004991
4992 if ((i = marker(self)) < 0)
4993 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004994 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004995
4996 if ((dict = PyDict_New()) == NULL)
4997 return -1;
4998
4999 for (k = i + 1; k < j; k += 2) {
5000 key = self->stack->data[k - 1];
5001 value = self->stack->data[k];
5002 if (PyDict_SetItem(dict, key, value) < 0) {
5003 Py_DECREF(dict);
5004 return -1;
5005 }
5006 }
5007 Pdata_clear(self->stack, i);
5008 PDATA_PUSH(self->stack, dict, -1);
5009 return 0;
5010}
5011
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005012static int
5013load_frozenset(UnpicklerObject *self)
5014{
5015 PyObject *items;
5016 PyObject *frozenset;
5017 Py_ssize_t i;
5018
5019 if ((i = marker(self)) < 0)
5020 return -1;
5021
5022 items = Pdata_poptuple(self->stack, i);
5023 if (items == NULL)
5024 return -1;
5025
5026 frozenset = PyFrozenSet_New(items);
5027 Py_DECREF(items);
5028 if (frozenset == NULL)
5029 return -1;
5030
5031 PDATA_PUSH(self->stack, frozenset, -1);
5032 return 0;
5033}
5034
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035static PyObject *
5036instantiate(PyObject *cls, PyObject *args)
5037{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005038 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005039 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005040 /* Caller must assure args are a tuple. Normally, args come from
5041 Pdata_poptuple which packs objects from the top of the stack
5042 into a newly created tuple. */
5043 assert(PyTuple_Check(args));
5044 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005045 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005046 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005048 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005049 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005050
5051 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005052 }
5053 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054}
5055
5056static int
5057load_obj(UnpicklerObject *self)
5058{
5059 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005060 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005061
5062 if ((i = marker(self)) < 0)
5063 return -1;
5064
5065 args = Pdata_poptuple(self->stack, i + 1);
5066 if (args == NULL)
5067 return -1;
5068
5069 PDATA_POP(self->stack, cls);
5070 if (cls) {
5071 obj = instantiate(cls, args);
5072 Py_DECREF(cls);
5073 }
5074 Py_DECREF(args);
5075 if (obj == NULL)
5076 return -1;
5077
5078 PDATA_PUSH(self->stack, obj, -1);
5079 return 0;
5080}
5081
5082static int
5083load_inst(UnpicklerObject *self)
5084{
5085 PyObject *cls = NULL;
5086 PyObject *args = NULL;
5087 PyObject *obj = NULL;
5088 PyObject *module_name;
5089 PyObject *class_name;
5090 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005091 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005092 char *s;
5093
5094 if ((i = marker(self)) < 0)
5095 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005096 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005097 return -1;
5098 if (len < 2)
5099 return bad_readline();
5100
5101 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5102 identifiers are permitted in Python 3.0, since the INST opcode is only
5103 supported by older protocols on Python 2.x. */
5104 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5105 if (module_name == NULL)
5106 return -1;
5107
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005108 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005109 if (len < 2)
5110 return bad_readline();
5111 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005112 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005113 cls = find_class(self, module_name, class_name);
5114 Py_DECREF(class_name);
5115 }
5116 }
5117 Py_DECREF(module_name);
5118
5119 if (cls == NULL)
5120 return -1;
5121
5122 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5123 obj = instantiate(cls, args);
5124 Py_DECREF(args);
5125 }
5126 Py_DECREF(cls);
5127
5128 if (obj == NULL)
5129 return -1;
5130
5131 PDATA_PUSH(self->stack, obj, -1);
5132 return 0;
5133}
5134
5135static int
5136load_newobj(UnpicklerObject *self)
5137{
5138 PyObject *args = NULL;
5139 PyObject *clsraw = NULL;
5140 PyTypeObject *cls; /* clsraw cast to its true type */
5141 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005142 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005143
5144 /* Stack is ... cls argtuple, and we want to call
5145 * cls.__new__(cls, *argtuple).
5146 */
5147 PDATA_POP(self->stack, args);
5148 if (args == NULL)
5149 goto error;
5150 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005151 PyErr_SetString(st->UnpicklingError,
5152 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153 goto error;
5154 }
5155
5156 PDATA_POP(self->stack, clsraw);
5157 cls = (PyTypeObject *)clsraw;
5158 if (cls == NULL)
5159 goto error;
5160 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005161 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005162 "isn't a type object");
5163 goto error;
5164 }
5165 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005166 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005167 "has NULL tp_new");
5168 goto error;
5169 }
5170
5171 /* Call __new__. */
5172 obj = cls->tp_new(cls, args, NULL);
5173 if (obj == NULL)
5174 goto error;
5175
5176 Py_DECREF(args);
5177 Py_DECREF(clsraw);
5178 PDATA_PUSH(self->stack, obj, -1);
5179 return 0;
5180
5181 error:
5182 Py_XDECREF(args);
5183 Py_XDECREF(clsraw);
5184 return -1;
5185}
5186
5187static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005188load_newobj_ex(UnpicklerObject *self)
5189{
5190 PyObject *cls, *args, *kwargs;
5191 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005192 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005193
5194 PDATA_POP(self->stack, kwargs);
5195 if (kwargs == NULL) {
5196 return -1;
5197 }
5198 PDATA_POP(self->stack, args);
5199 if (args == NULL) {
5200 Py_DECREF(kwargs);
5201 return -1;
5202 }
5203 PDATA_POP(self->stack, cls);
5204 if (cls == NULL) {
5205 Py_DECREF(kwargs);
5206 Py_DECREF(args);
5207 return -1;
5208 }
Larry Hastings61272b72014-01-07 12:41:53 -08005209
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005210 if (!PyType_Check(cls)) {
5211 Py_DECREF(kwargs);
5212 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005213 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005214 "NEWOBJ_EX class argument must be a type, not %.200s",
5215 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005216 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005217 return -1;
5218 }
5219
5220 if (((PyTypeObject *)cls)->tp_new == NULL) {
5221 Py_DECREF(kwargs);
5222 Py_DECREF(args);
5223 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005224 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005225 "NEWOBJ_EX class argument doesn't have __new__");
5226 return -1;
5227 }
5228 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5229 Py_DECREF(kwargs);
5230 Py_DECREF(args);
5231 Py_DECREF(cls);
5232 if (obj == NULL) {
5233 return -1;
5234 }
5235 PDATA_PUSH(self->stack, obj, -1);
5236 return 0;
5237}
5238
5239static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005240load_global(UnpicklerObject *self)
5241{
5242 PyObject *global = NULL;
5243 PyObject *module_name;
5244 PyObject *global_name;
5245 Py_ssize_t len;
5246 char *s;
5247
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005248 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005249 return -1;
5250 if (len < 2)
5251 return bad_readline();
5252 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5253 if (!module_name)
5254 return -1;
5255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005256 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257 if (len < 2) {
5258 Py_DECREF(module_name);
5259 return bad_readline();
5260 }
5261 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5262 if (global_name) {
5263 global = find_class(self, module_name, global_name);
5264 Py_DECREF(global_name);
5265 }
5266 }
5267 Py_DECREF(module_name);
5268
5269 if (global == NULL)
5270 return -1;
5271 PDATA_PUSH(self->stack, global, -1);
5272 return 0;
5273}
5274
5275static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005276load_stack_global(UnpicklerObject *self)
5277{
5278 PyObject *global;
5279 PyObject *module_name;
5280 PyObject *global_name;
5281
5282 PDATA_POP(self->stack, global_name);
5283 PDATA_POP(self->stack, module_name);
5284 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5285 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005286 PickleState *st = _Pickle_GetGlobalState();
5287 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005288 Py_XDECREF(global_name);
5289 Py_XDECREF(module_name);
5290 return -1;
5291 }
5292 global = find_class(self, module_name, global_name);
5293 Py_DECREF(global_name);
5294 Py_DECREF(module_name);
5295 if (global == NULL)
5296 return -1;
5297 PDATA_PUSH(self->stack, global, -1);
5298 return 0;
5299}
5300
5301static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005302load_persid(UnpicklerObject *self)
5303{
5304 PyObject *pid;
5305 Py_ssize_t len;
5306 char *s;
5307
5308 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005309 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005310 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005311 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005312 return bad_readline();
5313
5314 pid = PyBytes_FromStringAndSize(s, len - 1);
5315 if (pid == NULL)
5316 return -1;
5317
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005318 /* This does not leak since _Pickle_FastCall() steals the reference
5319 to pid first. */
5320 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 if (pid == NULL)
5322 return -1;
5323
5324 PDATA_PUSH(self->stack, pid, -1);
5325 return 0;
5326 }
5327 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005328 PickleState *st = _Pickle_GetGlobalState();
5329 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005330 "A load persistent id instruction was encountered,\n"
5331 "but no persistent_load function was specified.");
5332 return -1;
5333 }
5334}
5335
5336static int
5337load_binpersid(UnpicklerObject *self)
5338{
5339 PyObject *pid;
5340
5341 if (self->pers_func) {
5342 PDATA_POP(self->stack, pid);
5343 if (pid == NULL)
5344 return -1;
5345
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005346 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005347 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005348 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005349 if (pid == NULL)
5350 return -1;
5351
5352 PDATA_PUSH(self->stack, pid, -1);
5353 return 0;
5354 }
5355 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005356 PickleState *st = _Pickle_GetGlobalState();
5357 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005358 "A load persistent id instruction was encountered,\n"
5359 "but no persistent_load function was specified.");
5360 return -1;
5361 }
5362}
5363
5364static int
5365load_pop(UnpicklerObject *self)
5366{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005367 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005368
5369 /* Note that we split the (pickle.py) stack into two stacks,
5370 * an object stack and a mark stack. We have to be clever and
5371 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005372 * mark stack first, and only signalling a stack underflow if
5373 * the object stack is empty and the mark stack doesn't match
5374 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005375 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005376 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005377 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005378 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005379 len--;
5380 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005381 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005382 } else {
5383 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005385 return 0;
5386}
5387
5388static int
5389load_pop_mark(UnpicklerObject *self)
5390{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005391 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005392
5393 if ((i = marker(self)) < 0)
5394 return -1;
5395
5396 Pdata_clear(self->stack, i);
5397
5398 return 0;
5399}
5400
5401static int
5402load_dup(UnpicklerObject *self)
5403{
5404 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005405 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005406
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005407 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005408 return stack_underflow();
5409 last = self->stack->data[len - 1];
5410 PDATA_APPEND(self->stack, last, -1);
5411 return 0;
5412}
5413
5414static int
5415load_get(UnpicklerObject *self)
5416{
5417 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005418 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005419 Py_ssize_t len;
5420 char *s;
5421
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005422 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005423 return -1;
5424 if (len < 2)
5425 return bad_readline();
5426
5427 key = PyLong_FromString(s, NULL, 10);
5428 if (key == NULL)
5429 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005430 idx = PyLong_AsSsize_t(key);
5431 if (idx == -1 && PyErr_Occurred()) {
5432 Py_DECREF(key);
5433 return -1;
5434 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005436 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005437 if (value == NULL) {
5438 if (!PyErr_Occurred())
5439 PyErr_SetObject(PyExc_KeyError, key);
5440 Py_DECREF(key);
5441 return -1;
5442 }
5443 Py_DECREF(key);
5444
5445 PDATA_APPEND(self->stack, value, -1);
5446 return 0;
5447}
5448
5449static int
5450load_binget(UnpicklerObject *self)
5451{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005452 PyObject *value;
5453 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005454 char *s;
5455
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005456 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457 return -1;
5458
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005459 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005460
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005461 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005462 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005463 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005464 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005466 Py_DECREF(key);
5467 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 return -1;
5469 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005470
5471 PDATA_APPEND(self->stack, value, -1);
5472 return 0;
5473}
5474
5475static int
5476load_long_binget(UnpicklerObject *self)
5477{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005478 PyObject *value;
5479 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005481
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005482 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005483 return -1;
5484
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005485 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005486
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005487 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005489 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005490 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005492 Py_DECREF(key);
5493 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005494 return -1;
5495 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496
5497 PDATA_APPEND(self->stack, value, -1);
5498 return 0;
5499}
5500
5501/* Push an object from the extension registry (EXT[124]). nbytes is
5502 * the number of bytes following the opcode, holding the index (code) value.
5503 */
5504static int
5505load_extension(UnpicklerObject *self, int nbytes)
5506{
5507 char *codebytes; /* the nbytes bytes after the opcode */
5508 long code; /* calc_binint returns long */
5509 PyObject *py_code; /* code as a Python int */
5510 PyObject *obj; /* the object to push */
5511 PyObject *pair; /* (module_name, class_name) */
5512 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005513 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005514
5515 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005516 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005517 return -1;
5518 code = calc_binint(codebytes, nbytes);
5519 if (code <= 0) { /* note that 0 is forbidden */
5520 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005521 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005522 return -1;
5523 }
5524
5525 /* Look for the code in the cache. */
5526 py_code = PyLong_FromLong(code);
5527 if (py_code == NULL)
5528 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005529 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005530 if (obj != NULL) {
5531 /* Bingo. */
5532 Py_DECREF(py_code);
5533 PDATA_APPEND(self->stack, obj, -1);
5534 return 0;
5535 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005536 if (PyErr_Occurred()) {
5537 Py_DECREF(py_code);
5538 return -1;
5539 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540
5541 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005542 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543 if (pair == NULL) {
5544 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005545 if (!PyErr_Occurred()) {
5546 PyErr_Format(PyExc_ValueError, "unregistered extension "
5547 "code %ld", code);
5548 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 return -1;
5550 }
5551 /* Since the extension registry is manipulable via Python code,
5552 * confirm that pair is really a 2-tuple of strings.
5553 */
5554 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5555 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5556 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5557 Py_DECREF(py_code);
5558 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5559 "isn't a 2-tuple of strings", code);
5560 return -1;
5561 }
5562 /* Load the object. */
5563 obj = find_class(self, module_name, class_name);
5564 if (obj == NULL) {
5565 Py_DECREF(py_code);
5566 return -1;
5567 }
5568 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005569 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570 Py_DECREF(py_code);
5571 if (code < 0) {
5572 Py_DECREF(obj);
5573 return -1;
5574 }
5575 PDATA_PUSH(self->stack, obj, -1);
5576 return 0;
5577}
5578
5579static int
5580load_put(UnpicklerObject *self)
5581{
5582 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005583 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005584 Py_ssize_t len;
5585 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005586
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005587 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005588 return -1;
5589 if (len < 2)
5590 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005591 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005592 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005593 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005594
5595 key = PyLong_FromString(s, NULL, 10);
5596 if (key == NULL)
5597 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005600 if (idx < 0) {
5601 if (!PyErr_Occurred())
5602 PyErr_SetString(PyExc_ValueError,
5603 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005605 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005606
5607 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005608}
5609
5610static int
5611load_binput(UnpicklerObject *self)
5612{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005613 PyObject *value;
5614 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005617 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005619
5620 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005622 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005624 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627}
5628
5629static int
5630load_long_binput(UnpicklerObject *self)
5631{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005632 PyObject *value;
5633 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005637 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005638
5639 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005641 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005643 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005644 if (idx < 0) {
5645 PyErr_SetString(PyExc_ValueError,
5646 "negative LONG_BINPUT argument");
5647 return -1;
5648 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005650 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651}
5652
5653static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005654load_memoize(UnpicklerObject *self)
5655{
5656 PyObject *value;
5657
5658 if (Py_SIZE(self->stack) <= 0)
5659 return stack_underflow();
5660 value = self->stack->data[Py_SIZE(self->stack) - 1];
5661
5662 return _Unpickler_MemoPut(self, self->memo_len, value);
5663}
5664
5665static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005666do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667{
5668 PyObject *value;
5669 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005670 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005672 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673 if (x > len || x <= 0)
5674 return stack_underflow();
5675 if (len == x) /* nothing to do */
5676 return 0;
5677
5678 list = self->stack->data[x - 1];
5679
5680 if (PyList_Check(list)) {
5681 PyObject *slice;
5682 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005683 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684
5685 slice = Pdata_poplist(self->stack, x);
5686 if (!slice)
5687 return -1;
5688 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005689 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005691 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 }
5693 else {
5694 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005695 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005697 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698 if (append_func == NULL)
5699 return -1;
5700 for (i = x; i < len; i++) {
5701 PyObject *result;
5702
5703 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005704 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005705 if (result == NULL) {
5706 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005707 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005708 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 return -1;
5710 }
5711 Py_DECREF(result);
5712 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005713 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005714 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715 }
5716
5717 return 0;
5718}
5719
5720static int
5721load_append(UnpicklerObject *self)
5722{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005723 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005724}
5725
5726static int
5727load_appends(UnpicklerObject *self)
5728{
5729 return do_append(self, marker(self));
5730}
5731
5732static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005733do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734{
5735 PyObject *value, *key;
5736 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005737 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738 int status = 0;
5739
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741 if (x > len || x <= 0)
5742 return stack_underflow();
5743 if (len == x) /* nothing to do */
5744 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005745 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005746 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005748 PyErr_SetString(st->UnpicklingError,
5749 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750 return -1;
5751 }
5752
5753 /* Here, dict does not actually need to be a PyDict; it could be anything
5754 that supports the __setitem__ attribute. */
5755 dict = self->stack->data[x - 1];
5756
5757 for (i = x + 1; i < len; i += 2) {
5758 key = self->stack->data[i - 1];
5759 value = self->stack->data[i];
5760 if (PyObject_SetItem(dict, key, value) < 0) {
5761 status = -1;
5762 break;
5763 }
5764 }
5765
5766 Pdata_clear(self->stack, x);
5767 return status;
5768}
5769
5770static int
5771load_setitem(UnpicklerObject *self)
5772{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774}
5775
5776static int
5777load_setitems(UnpicklerObject *self)
5778{
5779 return do_setitems(self, marker(self));
5780}
5781
5782static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005783load_additems(UnpicklerObject *self)
5784{
5785 PyObject *set;
5786 Py_ssize_t mark, len, i;
5787
5788 mark = marker(self);
5789 len = Py_SIZE(self->stack);
5790 if (mark > len || mark <= 0)
5791 return stack_underflow();
5792 if (len == mark) /* nothing to do */
5793 return 0;
5794
5795 set = self->stack->data[mark - 1];
5796
5797 if (PySet_Check(set)) {
5798 PyObject *items;
5799 int status;
5800
5801 items = Pdata_poptuple(self->stack, mark);
5802 if (items == NULL)
5803 return -1;
5804
5805 status = _PySet_Update(set, items);
5806 Py_DECREF(items);
5807 return status;
5808 }
5809 else {
5810 PyObject *add_func;
5811 _Py_IDENTIFIER(add);
5812
5813 add_func = _PyObject_GetAttrId(set, &PyId_add);
5814 if (add_func == NULL)
5815 return -1;
5816 for (i = mark; i < len; i++) {
5817 PyObject *result;
5818 PyObject *item;
5819
5820 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005821 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005822 if (result == NULL) {
5823 Pdata_clear(self->stack, i + 1);
5824 Py_SIZE(self->stack) = mark;
5825 return -1;
5826 }
5827 Py_DECREF(result);
5828 }
5829 Py_SIZE(self->stack) = mark;
5830 }
5831
5832 return 0;
5833}
5834
5835static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005836load_build(UnpicklerObject *self)
5837{
5838 PyObject *state, *inst, *slotstate;
5839 PyObject *setstate;
5840 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005841 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005842
5843 /* Stack is ... instance, state. We want to leave instance at
5844 * the stack top, possibly mutated via instance.__setstate__(state).
5845 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005846 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005847 return stack_underflow();
5848
5849 PDATA_POP(self->stack, state);
5850 if (state == NULL)
5851 return -1;
5852
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005853 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005855 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005856 if (setstate == NULL) {
5857 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5858 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005859 else {
5860 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005861 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005862 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863 }
5864 else {
5865 PyObject *result;
5866
5867 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005868 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005869 Py_DECREF(setstate);
5870 if (result == NULL)
5871 return -1;
5872 Py_DECREF(result);
5873 return 0;
5874 }
5875
5876 /* A default __setstate__. First see whether state embeds a
5877 * slot state dict too (a proto 2 addition).
5878 */
5879 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5880 PyObject *tmp = state;
5881
5882 state = PyTuple_GET_ITEM(tmp, 0);
5883 slotstate = PyTuple_GET_ITEM(tmp, 1);
5884 Py_INCREF(state);
5885 Py_INCREF(slotstate);
5886 Py_DECREF(tmp);
5887 }
5888 else
5889 slotstate = NULL;
5890
5891 /* Set inst.__dict__ from the state dict (if any). */
5892 if (state != Py_None) {
5893 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005894 PyObject *d_key, *d_value;
5895 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005896 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897
5898 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005899 PickleState *st = _Pickle_GetGlobalState();
5900 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901 goto error;
5902 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005903 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005904 if (dict == NULL)
5905 goto error;
5906
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005907 i = 0;
5908 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5909 /* normally the keys for instance attributes are
5910 interned. we should try to do that here. */
5911 Py_INCREF(d_key);
5912 if (PyUnicode_CheckExact(d_key))
5913 PyUnicode_InternInPlace(&d_key);
5914 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5915 Py_DECREF(d_key);
5916 goto error;
5917 }
5918 Py_DECREF(d_key);
5919 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 Py_DECREF(dict);
5921 }
5922
5923 /* Also set instance attributes from the slotstate dict (if any). */
5924 if (slotstate != NULL) {
5925 PyObject *d_key, *d_value;
5926 Py_ssize_t i;
5927
5928 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005929 PickleState *st = _Pickle_GetGlobalState();
5930 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005931 "slot state is not a dictionary");
5932 goto error;
5933 }
5934 i = 0;
5935 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5936 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5937 goto error;
5938 }
5939 }
5940
5941 if (0) {
5942 error:
5943 status = -1;
5944 }
5945
5946 Py_DECREF(state);
5947 Py_XDECREF(slotstate);
5948 return status;
5949}
5950
5951static int
5952load_mark(UnpicklerObject *self)
5953{
5954
5955 /* Note that we split the (pickle.py) stack into two stacks, an
5956 * object stack and a mark stack. Here we push a mark onto the
5957 * mark stack.
5958 */
5959
5960 if ((self->num_marks + 1) >= self->marks_size) {
5961 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005962
5963 /* Use the size_t type to check for overflow. */
5964 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005965 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005966 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005967 PyErr_NoMemory();
5968 return -1;
5969 }
5970
5971 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005972 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005974 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
5975 if (self->marks == NULL) {
5976 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005977 PyErr_NoMemory();
5978 return -1;
5979 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980 self->marks_size = (Py_ssize_t)alloc;
5981 }
5982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005983 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984
5985 return 0;
5986}
5987
5988static int
5989load_reduce(UnpicklerObject *self)
5990{
5991 PyObject *callable = NULL;
5992 PyObject *argtup = NULL;
5993 PyObject *obj = NULL;
5994
5995 PDATA_POP(self->stack, argtup);
5996 if (argtup == NULL)
5997 return -1;
5998 PDATA_POP(self->stack, callable);
5999 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006000 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006001 Py_DECREF(callable);
6002 }
6003 Py_DECREF(argtup);
6004
6005 if (obj == NULL)
6006 return -1;
6007
6008 PDATA_PUSH(self->stack, obj, -1);
6009 return 0;
6010}
6011
6012/* Just raises an error if we don't know the protocol specified. PROTO
6013 * is the first opcode for protocols >= 2.
6014 */
6015static int
6016load_proto(UnpicklerObject *self)
6017{
6018 char *s;
6019 int i;
6020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006021 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006022 return -1;
6023
6024 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006025 if (i <= HIGHEST_PROTOCOL) {
6026 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006027 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006028 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006029
6030 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6031 return -1;
6032}
6033
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006034static int
6035load_frame(UnpicklerObject *self)
6036{
6037 char *s;
6038 Py_ssize_t frame_len;
6039
6040 if (_Unpickler_Read(self, &s, 8) < 0)
6041 return -1;
6042
6043 frame_len = calc_binsize(s, 8);
6044 if (frame_len < 0) {
6045 PyErr_Format(PyExc_OverflowError,
6046 "FRAME length exceeds system's maximum of %zd bytes",
6047 PY_SSIZE_T_MAX);
6048 return -1;
6049 }
6050
6051 if (_Unpickler_Read(self, &s, frame_len) < 0)
6052 return -1;
6053
6054 /* Rewind to start of frame */
6055 self->next_read_idx -= frame_len;
6056 return 0;
6057}
6058
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059static PyObject *
6060load(UnpicklerObject *self)
6061{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006062 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006063 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006064
6065 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006066 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006067 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006068 Pdata_clear(self->stack, 0);
6069
6070 /* Convenient macros for the dispatch while-switch loop just below. */
6071#define OP(opcode, load_func) \
6072 case opcode: if (load_func(self) < 0) break; continue;
6073
6074#define OP_ARG(opcode, load_func, arg) \
6075 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6076
6077 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006078 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 break;
6080
6081 switch ((enum opcode)s[0]) {
6082 OP(NONE, load_none)
6083 OP(BININT, load_binint)
6084 OP(BININT1, load_binint1)
6085 OP(BININT2, load_binint2)
6086 OP(INT, load_int)
6087 OP(LONG, load_long)
6088 OP_ARG(LONG1, load_counted_long, 1)
6089 OP_ARG(LONG4, load_counted_long, 4)
6090 OP(FLOAT, load_float)
6091 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006092 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6093 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6094 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6095 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6096 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006097 OP(STRING, load_string)
6098 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006099 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6100 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6101 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006102 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6103 OP_ARG(TUPLE1, load_counted_tuple, 1)
6104 OP_ARG(TUPLE2, load_counted_tuple, 2)
6105 OP_ARG(TUPLE3, load_counted_tuple, 3)
6106 OP(TUPLE, load_tuple)
6107 OP(EMPTY_LIST, load_empty_list)
6108 OP(LIST, load_list)
6109 OP(EMPTY_DICT, load_empty_dict)
6110 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006111 OP(EMPTY_SET, load_empty_set)
6112 OP(ADDITEMS, load_additems)
6113 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006114 OP(OBJ, load_obj)
6115 OP(INST, load_inst)
6116 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006117 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006119 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006120 OP(APPEND, load_append)
6121 OP(APPENDS, load_appends)
6122 OP(BUILD, load_build)
6123 OP(DUP, load_dup)
6124 OP(BINGET, load_binget)
6125 OP(LONG_BINGET, load_long_binget)
6126 OP(GET, load_get)
6127 OP(MARK, load_mark)
6128 OP(BINPUT, load_binput)
6129 OP(LONG_BINPUT, load_long_binput)
6130 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006131 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006132 OP(POP, load_pop)
6133 OP(POP_MARK, load_pop_mark)
6134 OP(SETITEM, load_setitem)
6135 OP(SETITEMS, load_setitems)
6136 OP(PERSID, load_persid)
6137 OP(BINPERSID, load_binpersid)
6138 OP(REDUCE, load_reduce)
6139 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006140 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006141 OP_ARG(EXT1, load_extension, 1)
6142 OP_ARG(EXT2, load_extension, 2)
6143 OP_ARG(EXT4, load_extension, 4)
6144 OP_ARG(NEWTRUE, load_bool, Py_True)
6145 OP_ARG(NEWFALSE, load_bool, Py_False)
6146
6147 case STOP:
6148 break;
6149
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006150 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006151 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006152 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006153 }
6154 else {
6155 PickleState *st = _Pickle_GetGlobalState();
6156 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006157 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006158 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 return NULL;
6160 }
6161
6162 break; /* and we are done! */
6163 }
6164
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006165 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006166 return NULL;
6167 }
6168
Victor Stinner2ae57e32013-10-31 13:39:23 +01006169 if (_Unpickler_SkipConsumed(self) < 0)
6170 return NULL;
6171
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006172 PDATA_POP(self->stack, value);
6173 return value;
6174}
6175
Larry Hastings61272b72014-01-07 12:41:53 -08006176/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006177
6178_pickle.Unpickler.load
6179
6180Load a pickle.
6181
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006182Read a pickled object representation from the open file object given
6183in the constructor, and return the reconstituted object hierarchy
6184specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006185[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006186
Larry Hastings3cceb382014-01-04 11:09:09 -08006187static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006188_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006189/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006190{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006191 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006192
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006193 /* Check whether the Unpickler was initialized correctly. This prevents
6194 segfaulting if a subclass overridden __init__ with a function that does
6195 not call Unpickler.__init__(). Here, we simply ensure that self->read
6196 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006197 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006198 PickleState *st = _Pickle_GetGlobalState();
6199 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006201 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006202 return NULL;
6203 }
6204
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006205 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206}
6207
6208/* The name of find_class() is misleading. In newer pickle protocols, this
6209 function is used for loading any global (i.e., functions), not just
6210 classes. The name is kept only for backward compatibility. */
6211
Larry Hastings61272b72014-01-07 12:41:53 -08006212/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006213
6214_pickle.Unpickler.find_class
6215
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006216 module_name: object
6217 global_name: object
6218 /
6219
6220Return an object from a specified module.
6221
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006222If necessary, the module will be imported. Subclasses may override
6223this method (e.g. to restrict unpickling of arbitrary classes and
6224functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006225
6226This method is called whenever a class or a function object is
6227needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006228[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006229
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006230static PyObject *
6231_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006232/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006233{
6234 PyObject *global;
6235 PyObject *modules_dict;
6236 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006237 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006238
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006239 /* Try to map the old names used in Python 2.x to the new ones used in
6240 Python 3.x. We do this only with old pickle protocols and when the
6241 user has not disabled the feature. */
6242 if (self->proto < 3 && self->fix_imports) {
6243 PyObject *key;
6244 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006245 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006246
6247 /* Check if the global (i.e., a function or a class) was renamed
6248 or moved to another module. */
6249 key = PyTuple_Pack(2, module_name, global_name);
6250 if (key == NULL)
6251 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006252 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006253 Py_DECREF(key);
6254 if (item) {
6255 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6256 PyErr_Format(PyExc_RuntimeError,
6257 "_compat_pickle.NAME_MAPPING values should be "
6258 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6259 return NULL;
6260 }
6261 module_name = PyTuple_GET_ITEM(item, 0);
6262 global_name = PyTuple_GET_ITEM(item, 1);
6263 if (!PyUnicode_Check(module_name) ||
6264 !PyUnicode_Check(global_name)) {
6265 PyErr_Format(PyExc_RuntimeError,
6266 "_compat_pickle.NAME_MAPPING values should be "
6267 "pairs of str, not (%.200s, %.200s)",
6268 Py_TYPE(module_name)->tp_name,
6269 Py_TYPE(global_name)->tp_name);
6270 return NULL;
6271 }
6272 }
6273 else if (PyErr_Occurred()) {
6274 return NULL;
6275 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006276 else {
6277 /* Check if the module was renamed. */
6278 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6279 if (item) {
6280 if (!PyUnicode_Check(item)) {
6281 PyErr_Format(PyExc_RuntimeError,
6282 "_compat_pickle.IMPORT_MAPPING values should be "
6283 "strings, not %.200s", Py_TYPE(item)->tp_name);
6284 return NULL;
6285 }
6286 module_name = item;
6287 }
6288 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006289 return NULL;
6290 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006291 }
6292 }
6293
Victor Stinnerbb520202013-11-06 22:40:41 +01006294 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006295 if (modules_dict == NULL) {
6296 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006297 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006298 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006300 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006302 if (PyErr_Occurred())
6303 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304 module = PyImport_Import(module_name);
6305 if (module == NULL)
6306 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006307 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006308 Py_DECREF(module);
6309 }
Victor Stinner121aab42011-09-29 23:40:53 +02006310 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006311 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312 }
6313 return global;
6314}
6315
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006316/*[clinic input]
6317
6318_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6319
6320Returns size in memory, in bytes.
6321[clinic start generated code]*/
6322
6323static Py_ssize_t
6324_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6325/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6326{
6327 Py_ssize_t res;
6328
6329 res = sizeof(UnpicklerObject);
6330 if (self->memo != NULL)
6331 res += self->memo_size * sizeof(PyObject *);
6332 if (self->marks != NULL)
6333 res += self->marks_size * sizeof(Py_ssize_t);
6334 if (self->input_line != NULL)
6335 res += strlen(self->input_line) + 1;
6336 if (self->encoding != NULL)
6337 res += strlen(self->encoding) + 1;
6338 if (self->errors != NULL)
6339 res += strlen(self->errors) + 1;
6340 return res;
6341}
6342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006343static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006344 _PICKLE_UNPICKLER_LOAD_METHODDEF
6345 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006346 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 {NULL, NULL} /* sentinel */
6348};
6349
6350static void
6351Unpickler_dealloc(UnpicklerObject *self)
6352{
6353 PyObject_GC_UnTrack((PyObject *)self);
6354 Py_XDECREF(self->readline);
6355 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006356 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006357 Py_XDECREF(self->stack);
6358 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006359 if (self->buffer.buf != NULL) {
6360 PyBuffer_Release(&self->buffer);
6361 self->buffer.buf = NULL;
6362 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006363
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006364 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006366 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006367 PyMem_Free(self->encoding);
6368 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006369
6370 Py_TYPE(self)->tp_free((PyObject *)self);
6371}
6372
6373static int
6374Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6375{
6376 Py_VISIT(self->readline);
6377 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006378 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006379 Py_VISIT(self->stack);
6380 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 return 0;
6382}
6383
6384static int
6385Unpickler_clear(UnpicklerObject *self)
6386{
6387 Py_CLEAR(self->readline);
6388 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006389 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006390 Py_CLEAR(self->stack);
6391 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006392 if (self->buffer.buf != NULL) {
6393 PyBuffer_Release(&self->buffer);
6394 self->buffer.buf = NULL;
6395 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006397 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398 PyMem_Free(self->marks);
6399 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006400 PyMem_Free(self->input_line);
6401 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006402 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006404 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006405 self->errors = NULL;
6406
6407 return 0;
6408}
6409
Larry Hastings61272b72014-01-07 12:41:53 -08006410/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006411
6412_pickle.Unpickler.__init__
6413
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006414 file: object
6415 *
6416 fix_imports: bool = True
6417 encoding: str = 'ASCII'
6418 errors: str = 'strict'
6419
6420This takes a binary file for reading a pickle data stream.
6421
6422The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006423protocol argument is needed. Bytes past the pickled object's
6424representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006425
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006426The argument *file* must have two methods, a read() method that takes
6427an integer argument, and a readline() method that requires no
6428arguments. Both methods should return bytes. Thus *file* can be a
6429binary file object opened for reading, a io.BytesIO object, or any
6430other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006431
6432Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6433which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006434generated by Python 2. If *fix_imports* is True, pickle will try to
6435map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006436*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006437instances pickled by Python 2; these default to 'ASCII' and 'strict',
6438respectively. The *encoding* can be 'bytes' to read these 8-bit
6439string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006440[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006441
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006442static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006443_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006444/*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006445{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006446 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006447
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006448 /* In case of multiple __init__() calls, clear previous content. */
6449 if (self->read != NULL)
6450 (void)Unpickler_clear(self);
6451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006452 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006453 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006454
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006455 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006456 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006457
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006458 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006459 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006460 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006461
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006462 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006463 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6464 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006465 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006466 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006467 }
6468 else {
6469 self->pers_func = NULL;
6470 }
6471
6472 self->stack = (Pdata *)Pdata_New();
6473 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006474 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006476 self->memo_size = 32;
6477 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006479 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006480
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006481 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006482
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006483 return 0;
6484}
6485
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006486
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006487/* Define a proxy object for the Unpickler's internal memo object. This is to
6488 * avoid breaking code like:
6489 * unpickler.memo.clear()
6490 * and
6491 * unpickler.memo = saved_memo
6492 * Is this a good idea? Not really, but we don't want to break code that uses
6493 * it. Note that we don't implement the entire mapping API here. This is
6494 * intentional, as these should be treated as black-box implementation details.
6495 *
6496 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006497 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006498 */
6499
Larry Hastings61272b72014-01-07 12:41:53 -08006500/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006501_pickle.UnpicklerMemoProxy.clear
6502
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006503Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006504[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006505
Larry Hastings3cceb382014-01-04 11:09:09 -08006506static PyObject *
6507_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006508/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006509{
6510 _Unpickler_MemoCleanup(self->unpickler);
6511 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6512 if (self->unpickler->memo == NULL)
6513 return NULL;
6514 Py_RETURN_NONE;
6515}
6516
Larry Hastings61272b72014-01-07 12:41:53 -08006517/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518_pickle.UnpicklerMemoProxy.copy
6519
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006520Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006521[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006522
Larry Hastings3cceb382014-01-04 11:09:09 -08006523static PyObject *
6524_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006525/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006526{
6527 Py_ssize_t i;
6528 PyObject *new_memo = PyDict_New();
6529 if (new_memo == NULL)
6530 return NULL;
6531
6532 for (i = 0; i < self->unpickler->memo_size; i++) {
6533 int status;
6534 PyObject *key, *value;
6535
6536 value = self->unpickler->memo[i];
6537 if (value == NULL)
6538 continue;
6539
6540 key = PyLong_FromSsize_t(i);
6541 if (key == NULL)
6542 goto error;
6543 status = PyDict_SetItem(new_memo, key, value);
6544 Py_DECREF(key);
6545 if (status < 0)
6546 goto error;
6547 }
6548 return new_memo;
6549
6550error:
6551 Py_DECREF(new_memo);
6552 return NULL;
6553}
6554
Larry Hastings61272b72014-01-07 12:41:53 -08006555/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006556_pickle.UnpicklerMemoProxy.__reduce__
6557
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006558Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006559[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006560
Larry Hastings3cceb382014-01-04 11:09:09 -08006561static PyObject *
6562_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006563/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006564{
6565 PyObject *reduce_value;
6566 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006567 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006568 if (contents == NULL)
6569 return NULL;
6570
6571 reduce_value = PyTuple_New(2);
6572 if (reduce_value == NULL) {
6573 Py_DECREF(contents);
6574 return NULL;
6575 }
6576 constructor_args = PyTuple_New(1);
6577 if (constructor_args == NULL) {
6578 Py_DECREF(contents);
6579 Py_DECREF(reduce_value);
6580 return NULL;
6581 }
6582 PyTuple_SET_ITEM(constructor_args, 0, contents);
6583 Py_INCREF((PyObject *)&PyDict_Type);
6584 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6585 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6586 return reduce_value;
6587}
6588
6589static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006590 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6591 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6592 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006593 {NULL, NULL} /* sentinel */
6594};
6595
6596static void
6597UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6598{
6599 PyObject_GC_UnTrack(self);
6600 Py_XDECREF(self->unpickler);
6601 PyObject_GC_Del((PyObject *)self);
6602}
6603
6604static int
6605UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6606 visitproc visit, void *arg)
6607{
6608 Py_VISIT(self->unpickler);
6609 return 0;
6610}
6611
6612static int
6613UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6614{
6615 Py_CLEAR(self->unpickler);
6616 return 0;
6617}
6618
6619static PyTypeObject UnpicklerMemoProxyType = {
6620 PyVarObject_HEAD_INIT(NULL, 0)
6621 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6622 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6623 0,
6624 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6625 0, /* tp_print */
6626 0, /* tp_getattr */
6627 0, /* tp_setattr */
6628 0, /* tp_compare */
6629 0, /* tp_repr */
6630 0, /* tp_as_number */
6631 0, /* tp_as_sequence */
6632 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006633 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006634 0, /* tp_call */
6635 0, /* tp_str */
6636 PyObject_GenericGetAttr, /* tp_getattro */
6637 PyObject_GenericSetAttr, /* tp_setattro */
6638 0, /* tp_as_buffer */
6639 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6640 0, /* tp_doc */
6641 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6642 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6643 0, /* tp_richcompare */
6644 0, /* tp_weaklistoffset */
6645 0, /* tp_iter */
6646 0, /* tp_iternext */
6647 unpicklerproxy_methods, /* tp_methods */
6648};
6649
6650static PyObject *
6651UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6652{
6653 UnpicklerMemoProxyObject *self;
6654
6655 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6656 &UnpicklerMemoProxyType);
6657 if (self == NULL)
6658 return NULL;
6659 Py_INCREF(unpickler);
6660 self->unpickler = unpickler;
6661 PyObject_GC_Track(self);
6662 return (PyObject *)self;
6663}
6664
6665/*****************************************************************************/
6666
6667
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006668static PyObject *
6669Unpickler_get_memo(UnpicklerObject *self)
6670{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006671 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006672}
6673
6674static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006675Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006676{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006677 PyObject **new_memo;
6678 Py_ssize_t new_memo_size = 0;
6679 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006681 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006682 PyErr_SetString(PyExc_TypeError,
6683 "attribute deletion is not supported");
6684 return -1;
6685 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006686
6687 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6688 UnpicklerObject *unpickler =
6689 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6690
6691 new_memo_size = unpickler->memo_size;
6692 new_memo = _Unpickler_NewMemo(new_memo_size);
6693 if (new_memo == NULL)
6694 return -1;
6695
6696 for (i = 0; i < new_memo_size; i++) {
6697 Py_XINCREF(unpickler->memo[i]);
6698 new_memo[i] = unpickler->memo[i];
6699 }
6700 }
6701 else if (PyDict_Check(obj)) {
6702 Py_ssize_t i = 0;
6703 PyObject *key, *value;
6704
6705 new_memo_size = PyDict_Size(obj);
6706 new_memo = _Unpickler_NewMemo(new_memo_size);
6707 if (new_memo == NULL)
6708 return -1;
6709
6710 while (PyDict_Next(obj, &i, &key, &value)) {
6711 Py_ssize_t idx;
6712 if (!PyLong_Check(key)) {
6713 PyErr_SetString(PyExc_TypeError,
6714 "memo key must be integers");
6715 goto error;
6716 }
6717 idx = PyLong_AsSsize_t(key);
6718 if (idx == -1 && PyErr_Occurred())
6719 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006720 if (idx < 0) {
6721 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006722 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006723 goto error;
6724 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006725 if (_Unpickler_MemoPut(self, idx, value) < 0)
6726 goto error;
6727 }
6728 }
6729 else {
6730 PyErr_Format(PyExc_TypeError,
6731 "'memo' attribute must be an UnpicklerMemoProxy object"
6732 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006733 return -1;
6734 }
6735
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006736 _Unpickler_MemoCleanup(self);
6737 self->memo_size = new_memo_size;
6738 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006739
6740 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006741
6742 error:
6743 if (new_memo_size) {
6744 i = new_memo_size;
6745 while (--i >= 0) {
6746 Py_XDECREF(new_memo[i]);
6747 }
6748 PyMem_FREE(new_memo);
6749 }
6750 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006751}
6752
6753static PyObject *
6754Unpickler_get_persload(UnpicklerObject *self)
6755{
6756 if (self->pers_func == NULL)
6757 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6758 else
6759 Py_INCREF(self->pers_func);
6760 return self->pers_func;
6761}
6762
6763static int
6764Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6765{
6766 PyObject *tmp;
6767
6768 if (value == NULL) {
6769 PyErr_SetString(PyExc_TypeError,
6770 "attribute deletion is not supported");
6771 return -1;
6772 }
6773 if (!PyCallable_Check(value)) {
6774 PyErr_SetString(PyExc_TypeError,
6775 "persistent_load must be a callable taking "
6776 "one argument");
6777 return -1;
6778 }
6779
6780 tmp = self->pers_func;
6781 Py_INCREF(value);
6782 self->pers_func = value;
6783 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6784
6785 return 0;
6786}
6787
6788static PyGetSetDef Unpickler_getsets[] = {
6789 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6790 {"persistent_load", (getter)Unpickler_get_persload,
6791 (setter)Unpickler_set_persload},
6792 {NULL}
6793};
6794
6795static PyTypeObject Unpickler_Type = {
6796 PyVarObject_HEAD_INIT(NULL, 0)
6797 "_pickle.Unpickler", /*tp_name*/
6798 sizeof(UnpicklerObject), /*tp_basicsize*/
6799 0, /*tp_itemsize*/
6800 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6801 0, /*tp_print*/
6802 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006803 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006804 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006805 0, /*tp_repr*/
6806 0, /*tp_as_number*/
6807 0, /*tp_as_sequence*/
6808 0, /*tp_as_mapping*/
6809 0, /*tp_hash*/
6810 0, /*tp_call*/
6811 0, /*tp_str*/
6812 0, /*tp_getattro*/
6813 0, /*tp_setattro*/
6814 0, /*tp_as_buffer*/
6815 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006816 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006817 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6818 (inquiry)Unpickler_clear, /*tp_clear*/
6819 0, /*tp_richcompare*/
6820 0, /*tp_weaklistoffset*/
6821 0, /*tp_iter*/
6822 0, /*tp_iternext*/
6823 Unpickler_methods, /*tp_methods*/
6824 0, /*tp_members*/
6825 Unpickler_getsets, /*tp_getset*/
6826 0, /*tp_base*/
6827 0, /*tp_dict*/
6828 0, /*tp_descr_get*/
6829 0, /*tp_descr_set*/
6830 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006831 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006832 PyType_GenericAlloc, /*tp_alloc*/
6833 PyType_GenericNew, /*tp_new*/
6834 PyObject_GC_Del, /*tp_free*/
6835 0, /*tp_is_gc*/
6836};
6837
Larry Hastings61272b72014-01-07 12:41:53 -08006838/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006839
6840_pickle.dump
6841
6842 obj: object
6843 file: object
6844 protocol: object = NULL
6845 *
6846 fix_imports: bool = True
6847
6848Write a pickled representation of obj to the open file object file.
6849
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006850This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6851be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006852
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006853The optional *protocol* argument tells the pickler to use the given
6854protocol supported protocols are 0, 1, 2, 3 and 4. The default
6855protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006856
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006857Specifying a negative protocol version selects the highest protocol
6858version supported. The higher the protocol used, the more recent the
6859version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006860
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006861The *file* argument must have a write() method that accepts a single
6862bytes argument. It can thus be a file object opened for binary
6863writing, a io.BytesIO instance, or any other custom object that meets
6864this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006865
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006866If *fix_imports* is True and protocol is less than 3, pickle will try
6867to map the new Python 3 names to the old module names used in Python
68682, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006869[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006870
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006871static PyObject *
6872_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006873/*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006874{
6875 PicklerObject *pickler = _Pickler_New();
6876
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 if (pickler == NULL)
6878 return NULL;
6879
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006880 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006881 goto error;
6882
6883 if (_Pickler_SetOutputStream(pickler, file) < 0)
6884 goto error;
6885
6886 if (dump(pickler, obj) < 0)
6887 goto error;
6888
6889 if (_Pickler_FlushToFile(pickler) < 0)
6890 goto error;
6891
6892 Py_DECREF(pickler);
6893 Py_RETURN_NONE;
6894
6895 error:
6896 Py_XDECREF(pickler);
6897 return NULL;
6898}
6899
Larry Hastings61272b72014-01-07 12:41:53 -08006900/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006901
6902_pickle.dumps
6903
6904 obj: object
6905 protocol: object = NULL
6906 *
6907 fix_imports: bool = True
6908
6909Return the pickled representation of the object as a bytes object.
6910
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006911The optional *protocol* argument tells the pickler to use the given
6912protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6913protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006914
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006915Specifying a negative protocol version selects the highest protocol
6916version supported. The higher the protocol used, the more recent the
6917version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006918
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006919If *fix_imports* is True and *protocol* is less than 3, pickle will
6920try to map the new Python 3 names to the old module names used in
6921Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006922[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006923
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006924static PyObject *
6925_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006926/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006927{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006928 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006929 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006931 if (pickler == NULL)
6932 return NULL;
6933
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006934 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006935 goto error;
6936
6937 if (dump(pickler, obj) < 0)
6938 goto error;
6939
6940 result = _Pickler_GetString(pickler);
6941 Py_DECREF(pickler);
6942 return result;
6943
6944 error:
6945 Py_XDECREF(pickler);
6946 return NULL;
6947}
6948
Larry Hastings61272b72014-01-07 12:41:53 -08006949/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006950
6951_pickle.load
6952
6953 file: object
6954 *
6955 fix_imports: bool = True
6956 encoding: str = 'ASCII'
6957 errors: str = 'strict'
6958
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006959Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006960
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006961This is equivalent to ``Unpickler(file).load()``, but may be more
6962efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006963
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006964The protocol version of the pickle is detected automatically, so no
6965protocol argument is needed. Bytes past the pickled object's
6966representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006967
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006968The argument *file* must have two methods, a read() method that takes
6969an integer argument, and a readline() method that requires no
6970arguments. Both methods should return bytes. Thus *file* can be a
6971binary file object opened for reading, a io.BytesIO object, or any
6972other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006973
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006974Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6975which are used to control compatiblity support for pickle stream
6976generated by Python 2. If *fix_imports* is True, pickle will try to
6977map the old Python 2 names to the new names used in Python 3. The
6978*encoding* and *errors* tell pickle how to decode 8-bit string
6979instances pickled by Python 2; these default to 'ASCII' and 'strict',
6980respectively. The *encoding* can be 'bytes' to read these 8-bit
6981string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006982[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006983
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006984static PyObject *
6985_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006986/*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006987{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006988 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006989 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006990
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006991 if (unpickler == NULL)
6992 return NULL;
6993
6994 if (_Unpickler_SetInputStream(unpickler, file) < 0)
6995 goto error;
6996
6997 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6998 goto error;
6999
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007000 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007001
7002 result = load(unpickler);
7003 Py_DECREF(unpickler);
7004 return result;
7005
7006 error:
7007 Py_XDECREF(unpickler);
7008 return NULL;
7009}
7010
Larry Hastings61272b72014-01-07 12:41:53 -08007011/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007012
7013_pickle.loads
7014
7015 data: object
7016 *
7017 fix_imports: bool = True
7018 encoding: str = 'ASCII'
7019 errors: str = 'strict'
7020
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007021Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007022
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007023The protocol version of the pickle is detected automatically, so no
7024protocol argument is needed. Bytes past the pickled object's
7025representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007026
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007027Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7028which are used to control compatiblity support for pickle stream
7029generated by Python 2. If *fix_imports* is True, pickle will try to
7030map the old Python 2 names to the new names used in Python 3. The
7031*encoding* and *errors* tell pickle how to decode 8-bit string
7032instances pickled by Python 2; these default to 'ASCII' and 'strict',
7033respectively. The *encoding* can be 'bytes' to read these 8-bit
7034string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007035[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007036
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007037static PyObject *
7038_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007039/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007040{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007041 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007042 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007043
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007044 if (unpickler == NULL)
7045 return NULL;
7046
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007048 goto error;
7049
7050 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7051 goto error;
7052
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007053 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007054
7055 result = load(unpickler);
7056 Py_DECREF(unpickler);
7057 return result;
7058
7059 error:
7060 Py_XDECREF(unpickler);
7061 return NULL;
7062}
7063
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065 _PICKLE_DUMP_METHODDEF
7066 _PICKLE_DUMPS_METHODDEF
7067 _PICKLE_LOAD_METHODDEF
7068 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007069 {NULL, NULL} /* sentinel */
7070};
7071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007072static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007073pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007074{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007075 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007076 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007077}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007078
Stefan Krahf483b0f2013-12-14 13:43:10 +01007079static void
7080pickle_free(PyObject *m)
7081{
7082 _Pickle_ClearState(_Pickle_GetState(m));
7083}
7084
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007085static int
7086pickle_traverse(PyObject *m, visitproc visit, void *arg)
7087{
7088 PickleState *st = _Pickle_GetState(m);
7089 Py_VISIT(st->PickleError);
7090 Py_VISIT(st->PicklingError);
7091 Py_VISIT(st->UnpicklingError);
7092 Py_VISIT(st->dispatch_table);
7093 Py_VISIT(st->extension_registry);
7094 Py_VISIT(st->extension_cache);
7095 Py_VISIT(st->inverted_registry);
7096 Py_VISIT(st->name_mapping_2to3);
7097 Py_VISIT(st->import_mapping_2to3);
7098 Py_VISIT(st->name_mapping_3to2);
7099 Py_VISIT(st->import_mapping_3to2);
7100 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007101 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007102}
7103
7104static struct PyModuleDef _picklemodule = {
7105 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007106 "_pickle", /* m_name */
7107 pickle_module_doc, /* m_doc */
7108 sizeof(PickleState), /* m_size */
7109 pickle_methods, /* m_methods */
7110 NULL, /* m_reload */
7111 pickle_traverse, /* m_traverse */
7112 pickle_clear, /* m_clear */
7113 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007114};
7115
7116PyMODINIT_FUNC
7117PyInit__pickle(void)
7118{
7119 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007120 PickleState *st;
7121
7122 m = PyState_FindModule(&_picklemodule);
7123 if (m) {
7124 Py_INCREF(m);
7125 return m;
7126 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007127
7128 if (PyType_Ready(&Unpickler_Type) < 0)
7129 return NULL;
7130 if (PyType_Ready(&Pickler_Type) < 0)
7131 return NULL;
7132 if (PyType_Ready(&Pdata_Type) < 0)
7133 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007134 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7135 return NULL;
7136 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7137 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007138
7139 /* Create the module and add the functions. */
7140 m = PyModule_Create(&_picklemodule);
7141 if (m == NULL)
7142 return NULL;
7143
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007144 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007145 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7146 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007147 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7149 return NULL;
7150
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007151 st = _Pickle_GetState(m);
7152
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007153 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007154 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7155 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007156 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007157 st->PicklingError = \
7158 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7159 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007160 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007161 st->UnpicklingError = \
7162 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7163 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007164 return NULL;
7165
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007166 Py_INCREF(st->PickleError);
7167 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007168 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007169 Py_INCREF(st->PicklingError);
7170 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007171 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007172 Py_INCREF(st->UnpicklingError);
7173 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007174 return NULL;
7175
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007176 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007177 return NULL;
7178
7179 return m;
7180}