blob: 830479fe9490d854761567df816adf9452b352d4 [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +02008output preset file
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08009module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -080010class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
11class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
12class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
13class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080014[clinic start generated code]*/
Larry Hastings581ee362014-01-28 05:00:08 -080015/*[clinic end generated code: output=da39a3ee5e6b4b0d input=11c45248a41dd3fc]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080016
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000017/* Bump this when new opcodes are added to the pickle protocol. */
18enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010019 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000020 DEFAULT_PROTOCOL = 3
21};
22
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000023/* Pickle opcodes. These must be kept updated with pickle.py.
24 Extensive docs are in pickletools.py. */
25enum opcode {
26 MARK = '(',
27 STOP = '.',
28 POP = '0',
29 POP_MARK = '1',
30 DUP = '2',
31 FLOAT = 'F',
32 INT = 'I',
33 BININT = 'J',
34 BININT1 = 'K',
35 LONG = 'L',
36 BININT2 = 'M',
37 NONE = 'N',
38 PERSID = 'P',
39 BINPERSID = 'Q',
40 REDUCE = 'R',
41 STRING = 'S',
42 BINSTRING = 'T',
43 SHORT_BINSTRING = 'U',
44 UNICODE = 'V',
45 BINUNICODE = 'X',
46 APPEND = 'a',
47 BUILD = 'b',
48 GLOBAL = 'c',
49 DICT = 'd',
50 EMPTY_DICT = '}',
51 APPENDS = 'e',
52 GET = 'g',
53 BINGET = 'h',
54 INST = 'i',
55 LONG_BINGET = 'j',
56 LIST = 'l',
57 EMPTY_LIST = ']',
58 OBJ = 'o',
59 PUT = 'p',
60 BINPUT = 'q',
61 LONG_BINPUT = 'r',
62 SETITEM = 's',
63 TUPLE = 't',
64 EMPTY_TUPLE = ')',
65 SETITEMS = 'u',
66 BINFLOAT = 'G',
67
68 /* Protocol 2. */
69 PROTO = '\x80',
70 NEWOBJ = '\x81',
71 EXT1 = '\x82',
72 EXT2 = '\x83',
73 EXT4 = '\x84',
74 TUPLE1 = '\x85',
75 TUPLE2 = '\x86',
76 TUPLE3 = '\x87',
77 NEWTRUE = '\x88',
78 NEWFALSE = '\x89',
79 LONG1 = '\x8a',
80 LONG4 = '\x8b',
81
82 /* Protocol 3 (Python 3.x) */
83 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010084 SHORT_BINBYTES = 'C',
85
86 /* Protocol 4 */
87 SHORT_BINUNICODE = '\x8c',
88 BINUNICODE8 = '\x8d',
89 BINBYTES8 = '\x8e',
90 EMPTY_SET = '\x8f',
91 ADDITEMS = '\x90',
92 FROZENSET = '\x91',
93 NEWOBJ_EX = '\x92',
94 STACK_GLOBAL = '\x93',
95 MEMOIZE = '\x94',
96 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000097};
98
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000099enum {
100 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
101 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
102 break if this gets out of synch with pickle.py, but it's unclear that would
103 help anything either. */
104 BATCHSIZE = 1000,
105
106 /* Nesting limit until Pickler, when running in "fast mode", starts
107 checking for self-referential data-structures. */
108 FAST_NESTING_LIMIT = 50,
109
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000110 /* Initial size of the write buffer of Pickler. */
111 WRITE_BUF_SIZE = 4096,
112
Antoine Pitrou04248a82010-10-12 20:51:21 +0000113 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100114 PREFETCH = 8192 * 16,
115
116 FRAME_SIZE_TARGET = 64 * 1024,
117
118 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000119};
120
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800121/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000122
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800123/* State of the pickle module, per PEP 3121. */
124typedef struct {
125 /* Exception classes for pickle. */
126 PyObject *PickleError;
127 PyObject *PicklingError;
128 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800129
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800130 /* copyreg.dispatch_table, {type_object: pickling_function} */
131 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000132
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800133 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000134
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800135 /* copyreg._extension_registry, {(module_name, function_name): code} */
136 PyObject *extension_registry;
137 /* copyreg._extension_cache, {code: object} */
138 PyObject *extension_cache;
139 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
140 PyObject *inverted_registry;
141
142 /* Import mappings for compatibility with Python 2.x */
143
144 /* _compat_pickle.NAME_MAPPING,
145 {(oldmodule, oldname): (newmodule, newname)} */
146 PyObject *name_mapping_2to3;
147 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
148 PyObject *import_mapping_2to3;
149 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
150 PyObject *name_mapping_3to2;
151 PyObject *import_mapping_3to2;
152
153 /* codecs.encode, used for saving bytes in older protocols */
154 PyObject *codecs_encode;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800155} PickleState;
156
157/* Forward declaration of the _pickle module definition. */
158static struct PyModuleDef _picklemodule;
159
160/* Given a module object, get its per-module state. */
161static PickleState *
162_Pickle_GetState(PyObject *module)
163{
164 return (PickleState *)PyModule_GetState(module);
165}
166
167/* Find the module instance imported in the currently running sub-interpreter
168 and get its state. */
169static PickleState *
170_Pickle_GetGlobalState(void)
171{
172 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
173}
174
175/* Clear the given pickle module state. */
176static void
177_Pickle_ClearState(PickleState *st)
178{
179 Py_CLEAR(st->PickleError);
180 Py_CLEAR(st->PicklingError);
181 Py_CLEAR(st->UnpicklingError);
182 Py_CLEAR(st->dispatch_table);
183 Py_CLEAR(st->extension_registry);
184 Py_CLEAR(st->extension_cache);
185 Py_CLEAR(st->inverted_registry);
186 Py_CLEAR(st->name_mapping_2to3);
187 Py_CLEAR(st->import_mapping_2to3);
188 Py_CLEAR(st->name_mapping_3to2);
189 Py_CLEAR(st->import_mapping_3to2);
190 Py_CLEAR(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800191}
192
193/* Initialize the given pickle module state. */
194static int
195_Pickle_InitState(PickleState *st)
196{
197 PyObject *copyreg = NULL;
198 PyObject *compat_pickle = NULL;
199 PyObject *codecs = NULL;
200
201 copyreg = PyImport_ImportModule("copyreg");
202 if (!copyreg)
203 goto error;
204 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
205 if (!st->dispatch_table)
206 goto error;
207 if (!PyDict_CheckExact(st->dispatch_table)) {
208 PyErr_Format(PyExc_RuntimeError,
209 "copyreg.dispatch_table should be a dict, not %.200s",
210 Py_TYPE(st->dispatch_table)->tp_name);
211 goto error;
212 }
213 st->extension_registry = \
214 PyObject_GetAttrString(copyreg, "_extension_registry");
215 if (!st->extension_registry)
216 goto error;
217 if (!PyDict_CheckExact(st->extension_registry)) {
218 PyErr_Format(PyExc_RuntimeError,
219 "copyreg._extension_registry should be a dict, "
220 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
221 goto error;
222 }
223 st->inverted_registry = \
224 PyObject_GetAttrString(copyreg, "_inverted_registry");
225 if (!st->inverted_registry)
226 goto error;
227 if (!PyDict_CheckExact(st->inverted_registry)) {
228 PyErr_Format(PyExc_RuntimeError,
229 "copyreg._inverted_registry should be a dict, "
230 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
231 goto error;
232 }
233 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
234 if (!st->extension_cache)
235 goto error;
236 if (!PyDict_CheckExact(st->extension_cache)) {
237 PyErr_Format(PyExc_RuntimeError,
238 "copyreg._extension_cache should be a dict, "
239 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
240 goto error;
241 }
242 Py_CLEAR(copyreg);
243
244 /* Load the 2.x -> 3.x stdlib module mapping tables */
245 compat_pickle = PyImport_ImportModule("_compat_pickle");
246 if (!compat_pickle)
247 goto error;
248 st->name_mapping_2to3 = \
249 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
250 if (!st->name_mapping_2to3)
251 goto error;
252 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
255 Py_TYPE(st->name_mapping_2to3)->tp_name);
256 goto error;
257 }
258 st->import_mapping_2to3 = \
259 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
260 if (!st->import_mapping_2to3)
261 goto error;
262 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
263 PyErr_Format(PyExc_RuntimeError,
264 "_compat_pickle.IMPORT_MAPPING should be a dict, "
265 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
266 goto error;
267 }
268 /* ... and the 3.x -> 2.x mapping tables */
269 st->name_mapping_3to2 = \
270 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
271 if (!st->name_mapping_3to2)
272 goto error;
273 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
274 PyErr_Format(PyExc_RuntimeError,
275 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
276 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
277 goto error;
278 }
279 st->import_mapping_3to2 = \
280 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
281 if (!st->import_mapping_3to2)
282 goto error;
283 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
284 PyErr_Format(PyExc_RuntimeError,
285 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
286 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
287 goto error;
288 }
289 Py_CLEAR(compat_pickle);
290
291 codecs = PyImport_ImportModule("codecs");
292 if (codecs == NULL)
293 goto error;
294 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
295 if (st->codecs_encode == NULL) {
296 goto error;
297 }
298 if (!PyCallable_Check(st->codecs_encode)) {
299 PyErr_Format(PyExc_RuntimeError,
300 "codecs.encode should be a callable, not %.200s",
301 Py_TYPE(st->codecs_encode)->tp_name);
302 goto error;
303 }
304 Py_CLEAR(codecs);
305
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800306 return 0;
307
308 error:
309 Py_CLEAR(copyreg);
310 Py_CLEAR(compat_pickle);
311 Py_CLEAR(codecs);
312 _Pickle_ClearState(st);
313 return -1;
314}
315
316/* Helper for calling a function with a single argument quickly.
317
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800318 This function steals the reference of the given argument. */
319static PyObject *
320_Pickle_FastCall(PyObject *func, PyObject *obj)
321{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800322 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800323 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800324
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800325 /* Note: this function used to reuse the argument tuple. This used to give
326 a slight performance boost with older pickle implementations where many
327 unbuffered reads occurred (thus needing many function calls).
328
329 However, this optimization was removed because it was too complicated
330 to get right. It abused the C API for tuples to mutate them which led
331 to subtle reference counting and concurrency bugs. Furthermore, the
332 introduction of protocol 4 and the prefetching optimization via peek()
333 significantly reduced the number of function calls we do. Thus, the
334 benefits became marginal at best. */
335
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800336 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800337 Py_DECREF(obj);
338 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800339 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800340 PyTuple_SET_ITEM(arg_tuple, 0, obj);
341 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800342 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 return result;
344}
345
346/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000347
348static int
349stack_underflow(void)
350{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 PickleState *st = _Pickle_GetGlobalState();
352 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000353 return -1;
354}
355
356/* Internal data type used as the unpickling stack. */
357typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000358 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000359 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000360 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000361} Pdata;
362
363static void
364Pdata_dealloc(Pdata *self)
365{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200366 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000367 while (--i >= 0) {
368 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000369 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000370 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000371 PyObject_Del(self);
372}
373
374static PyTypeObject Pdata_Type = {
375 PyVarObject_HEAD_INIT(NULL, 0)
376 "_pickle.Pdata", /*tp_name*/
377 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200378 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000379 (destructor)Pdata_dealloc, /*tp_dealloc*/
380};
381
382static PyObject *
383Pdata_New(void)
384{
385 Pdata *self;
386
387 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
388 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000389 Py_SIZE(self) = 0;
390 self->allocated = 8;
391 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000392 if (self->data)
393 return (PyObject *)self;
394 Py_DECREF(self);
395 return PyErr_NoMemory();
396}
397
398
399/* Retain only the initial clearto items. If clearto >= the current
400 * number of items, this is a (non-erroneous) NOP.
401 */
402static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200403Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000404{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200405 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000406
407 if (clearto < 0)
408 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000409 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000410 return 0;
411
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000412 while (--i >= clearto) {
413 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000414 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000415 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000416 return 0;
417}
418
419static int
420Pdata_grow(Pdata *self)
421{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000422 PyObject **data = self->data;
423 Py_ssize_t allocated = self->allocated;
424 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 new_allocated = (allocated >> 3) + 6;
427 /* check for integer overflow */
428 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000429 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000430 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500431 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000433 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000434
435 self->data = data;
436 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000437 return 0;
438
439 nomemory:
440 PyErr_NoMemory();
441 return -1;
442}
443
444/* D is a Pdata*. Pop the topmost element and store it into V, which
445 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
446 * is raised and V is set to NULL.
447 */
448static PyObject *
449Pdata_pop(Pdata *self)
450{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800451 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000452 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800453 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000454 return NULL;
455 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000456 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457}
458#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
459
460static int
461Pdata_push(Pdata *self, PyObject *obj)
462{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000463 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000464 return -1;
465 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000466 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000467 return 0;
468}
469
470/* Push an object on stack, transferring its ownership to the stack. */
471#define PDATA_PUSH(D, O, ER) do { \
472 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
473
474/* Push an object on stack, adding a new reference to the object. */
475#define PDATA_APPEND(D, O, ER) do { \
476 Py_INCREF((O)); \
477 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
478
479static PyObject *
480Pdata_poptuple(Pdata *self, Py_ssize_t start)
481{
482 PyObject *tuple;
483 Py_ssize_t len, i, j;
484
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000485 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000486 tuple = PyTuple_New(len);
487 if (tuple == NULL)
488 return NULL;
489 for (i = start, j = 0; j < len; i++, j++)
490 PyTuple_SET_ITEM(tuple, j, self->data[i]);
491
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000492 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000493 return tuple;
494}
495
496static PyObject *
497Pdata_poplist(Pdata *self, Py_ssize_t start)
498{
499 PyObject *list;
500 Py_ssize_t len, i, j;
501
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000502 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000503 list = PyList_New(len);
504 if (list == NULL)
505 return NULL;
506 for (i = start, j = 0; j < len; i++, j++)
507 PyList_SET_ITEM(list, j, self->data[i]);
508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 return list;
511}
512
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000513typedef struct {
514 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200515 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516} PyMemoEntry;
517
518typedef struct {
519 Py_ssize_t mt_mask;
520 Py_ssize_t mt_used;
521 Py_ssize_t mt_allocated;
522 PyMemoEntry *mt_table;
523} PyMemoTable;
524
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000525typedef struct PicklerObject {
526 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000527 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000528 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100531 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532
533 PyObject *write; /* write() method of the output stream. */
534 PyObject *output_buffer; /* Write into a local bytearray buffer before
535 flushing to the stream. */
536 Py_ssize_t output_len; /* Length of output_buffer. */
537 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538 int proto; /* Pickle protocol number, >= 0 */
539 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100540 int framing; /* True when framing is enabled, proto >= 4 */
541 Py_ssize_t frame_start; /* Position in output_buffer where the
542 where the current frame begins. -1 if there
543 is no frame currently open. */
544
545 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000546 int fast; /* Enable fast mode if set to a true value.
547 The fast mode disable the usage of memo,
548 therefore speeding the pickling process by
549 not generating superfluous PUT opcodes. It
550 should not be used if with self-referential
551 objects. */
552 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000553 int fix_imports; /* Indicate whether Pickler should fix
554 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000555 PyObject *fast_memo;
556} PicklerObject;
557
558typedef struct UnpicklerObject {
559 PyObject_HEAD
560 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000561
562 /* The unpickler memo is just an array of PyObject *s. Using a dict
563 is unnecessary, since the keys are contiguous ints. */
564 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100565 Py_ssize_t memo_size; /* Capacity of the memo array */
566 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000567
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000568 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000569
570 Py_buffer buffer;
571 char *input_buffer;
572 char *input_line;
573 Py_ssize_t input_len;
574 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000575 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000577 PyObject *read; /* read() method of the input stream. */
578 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000579 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000580
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000581 char *encoding; /* Name of the encoding to be used for
582 decoding strings pickled using Python
583 2.x. The default value is "ASCII" */
584 char *errors; /* Name of errors handling scheme to used when
585 decoding strings. The default value is
586 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500587 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000588 objects. */
589 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
590 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000591 int proto; /* Protocol of the pickle loaded. */
592 int fix_imports; /* Indicate whether Unpickler should fix
593 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594} UnpicklerObject;
595
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200596typedef struct {
597 PyObject_HEAD
598 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
599} PicklerMemoProxyObject;
600
601typedef struct {
602 PyObject_HEAD
603 UnpicklerObject *unpickler;
604} UnpicklerMemoProxyObject;
605
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000606/* Forward declarations */
607static int save(PicklerObject *, PyObject *, int);
608static int save_reduce(PicklerObject *, PyObject *, PyObject *);
609static PyTypeObject Pickler_Type;
610static PyTypeObject Unpickler_Type;
611
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200612#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000613
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300615 A custom hashtable mapping void* to Python ints. This is used by the pickler
616 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000617 a bunch of unnecessary object creation. This makes a huge performance
618 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000620#define MT_MINSIZE 8
621#define PERTURB_SHIFT 5
622
623
624static PyMemoTable *
625PyMemoTable_New(void)
626{
627 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
628 if (memo == NULL) {
629 PyErr_NoMemory();
630 return NULL;
631 }
632
633 memo->mt_used = 0;
634 memo->mt_allocated = MT_MINSIZE;
635 memo->mt_mask = MT_MINSIZE - 1;
636 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
637 if (memo->mt_table == NULL) {
638 PyMem_FREE(memo);
639 PyErr_NoMemory();
640 return NULL;
641 }
642 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
643
644 return memo;
645}
646
647static PyMemoTable *
648PyMemoTable_Copy(PyMemoTable *self)
649{
650 Py_ssize_t i;
651 PyMemoTable *new = PyMemoTable_New();
652 if (new == NULL)
653 return NULL;
654
655 new->mt_used = self->mt_used;
656 new->mt_allocated = self->mt_allocated;
657 new->mt_mask = self->mt_mask;
658 /* The table we get from _New() is probably smaller than we wanted.
659 Free it and allocate one that's the right size. */
660 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500661 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000662 if (new->mt_table == NULL) {
663 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200664 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000665 return NULL;
666 }
667 for (i = 0; i < self->mt_allocated; i++) {
668 Py_XINCREF(self->mt_table[i].me_key);
669 }
670 memcpy(new->mt_table, self->mt_table,
671 sizeof(PyMemoEntry) * self->mt_allocated);
672
673 return new;
674}
675
676static Py_ssize_t
677PyMemoTable_Size(PyMemoTable *self)
678{
679 return self->mt_used;
680}
681
682static int
683PyMemoTable_Clear(PyMemoTable *self)
684{
685 Py_ssize_t i = self->mt_allocated;
686
687 while (--i >= 0) {
688 Py_XDECREF(self->mt_table[i].me_key);
689 }
690 self->mt_used = 0;
691 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
692 return 0;
693}
694
695static void
696PyMemoTable_Del(PyMemoTable *self)
697{
698 if (self == NULL)
699 return;
700 PyMemoTable_Clear(self);
701
702 PyMem_FREE(self->mt_table);
703 PyMem_FREE(self);
704}
705
706/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
707 can be considerably simpler than dictobject.c's lookdict(). */
708static PyMemoEntry *
709_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
710{
711 size_t i;
712 size_t perturb;
713 size_t mask = (size_t)self->mt_mask;
714 PyMemoEntry *table = self->mt_table;
715 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000716 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000717
718 i = hash & mask;
719 entry = &table[i];
720 if (entry->me_key == NULL || entry->me_key == key)
721 return entry;
722
723 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
724 i = (i << 2) + i + perturb + 1;
725 entry = &table[i & mask];
726 if (entry->me_key == NULL || entry->me_key == key)
727 return entry;
728 }
729 assert(0); /* Never reached */
730 return NULL;
731}
732
733/* Returns -1 on failure, 0 on success. */
734static int
735_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
736{
737 PyMemoEntry *oldtable = NULL;
738 PyMemoEntry *oldentry, *newentry;
739 Py_ssize_t new_size = MT_MINSIZE;
740 Py_ssize_t to_process;
741
742 assert(min_size > 0);
743
744 /* Find the smallest valid table size >= min_size. */
745 while (new_size < min_size && new_size > 0)
746 new_size <<= 1;
747 if (new_size <= 0) {
748 PyErr_NoMemory();
749 return -1;
750 }
751 /* new_size needs to be a power of two. */
752 assert((new_size & (new_size - 1)) == 0);
753
754 /* Allocate new table. */
755 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500756 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000757 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200758 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000759 PyErr_NoMemory();
760 return -1;
761 }
762 self->mt_allocated = new_size;
763 self->mt_mask = new_size - 1;
764 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
765
766 /* Copy entries from the old table. */
767 to_process = self->mt_used;
768 for (oldentry = oldtable; to_process > 0; oldentry++) {
769 if (oldentry->me_key != NULL) {
770 to_process--;
771 /* newentry is a pointer to a chunk of the new
772 mt_table, so we're setting the key:value pair
773 in-place. */
774 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
775 newentry->me_key = oldentry->me_key;
776 newentry->me_value = oldentry->me_value;
777 }
778 }
779
780 /* Deallocate the old table. */
781 PyMem_FREE(oldtable);
782 return 0;
783}
784
785/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200786static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000787PyMemoTable_Get(PyMemoTable *self, PyObject *key)
788{
789 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
790 if (entry->me_key == NULL)
791 return NULL;
792 return &entry->me_value;
793}
794
795/* Returns -1 on failure, 0 on success. */
796static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200797PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000798{
799 PyMemoEntry *entry;
800
801 assert(key != NULL);
802
803 entry = _PyMemoTable_Lookup(self, key);
804 if (entry->me_key != NULL) {
805 entry->me_value = value;
806 return 0;
807 }
808 Py_INCREF(key);
809 entry->me_key = key;
810 entry->me_value = value;
811 self->mt_used++;
812
813 /* If we added a key, we can safely resize. Otherwise just return!
814 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
815 *
816 * Quadrupling the size improves average table sparseness
817 * (reducing collisions) at the cost of some memory. It also halves
818 * the number of expensive resize operations in a growing memo table.
819 *
820 * Very large memo tables (over 50K items) use doubling instead.
821 * This may help applications with severe memory constraints.
822 */
823 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
824 return 0;
825 return _PyMemoTable_ResizeTable(self,
826 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
827}
828
829#undef MT_MINSIZE
830#undef PERTURB_SHIFT
831
832/*************************************************************************/
833
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000834
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000835static int
836_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000837{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000838 Py_CLEAR(self->output_buffer);
839 self->output_buffer =
840 PyBytes_FromStringAndSize(NULL, self->max_output_len);
841 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000842 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100844 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 return 0;
846}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000847
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100848static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100849_write_size64(char *out, size_t value)
850{
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800851 int i;
852
853 assert(sizeof(size_t) <= 8);
854
855 for (i = 0; i < sizeof(size_t); i++) {
856 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
857 }
858 for (i = sizeof(size_t); i < 8; i++) {
859 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800860 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100861}
862
863static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100864_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
865{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100866 qdata[0] = FRAME;
867 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100868}
869
870static int
871_Pickler_CommitFrame(PicklerObject *self)
872{
873 size_t frame_len;
874 char *qdata;
875
876 if (!self->framing || self->frame_start == -1)
877 return 0;
878 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
879 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
880 _Pickler_WriteFrameHeader(self, qdata, frame_len);
881 self->frame_start = -1;
882 return 0;
883}
884
885static int
886_Pickler_OpcodeBoundary(PicklerObject *self)
887{
888 Py_ssize_t frame_len;
889
890 if (!self->framing || self->frame_start == -1)
891 return 0;
892 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
893 if (frame_len >= FRAME_SIZE_TARGET)
894 return _Pickler_CommitFrame(self);
895 else
896 return 0;
897}
898
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000899static PyObject *
900_Pickler_GetString(PicklerObject *self)
901{
902 PyObject *output_buffer = self->output_buffer;
903
904 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100905
906 if (_Pickler_CommitFrame(self))
907 return NULL;
908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000909 self->output_buffer = NULL;
910 /* Resize down to exact size */
911 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
912 return NULL;
913 return output_buffer;
914}
915
916static int
917_Pickler_FlushToFile(PicklerObject *self)
918{
919 PyObject *output, *result;
920
921 assert(self->write != NULL);
922
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100923 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000924 output = _Pickler_GetString(self);
925 if (output == NULL)
926 return -1;
927
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800928 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 Py_XDECREF(result);
930 return (result == NULL) ? -1 : 0;
931}
932
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200933static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100934_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000935{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000939
940 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100941 need_new_frame = (self->framing && self->frame_start == -1);
942
943 if (need_new_frame)
944 n = data_len + FRAME_HEADER_SIZE;
945 else
946 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000947
948 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949 if (required > self->max_output_len) {
950 /* Make place in buffer for the pickle chunk */
951 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
952 PyErr_NoMemory();
953 return -1;
954 }
955 self->max_output_len = (self->output_len + n) / 2 * 3;
956 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
957 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000958 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 if (need_new_frame) {
961 /* Setup new frame */
962 Py_ssize_t frame_start = self->output_len;
963 self->frame_start = frame_start;
964 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
965 /* Write an invalid value, for debugging */
966 buffer[frame_start + i] = 0xFE;
967 }
968 self->output_len += FRAME_HEADER_SIZE;
969 }
970 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000971 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100972 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000973 buffer[self->output_len + i] = s[i];
974 }
975 }
976 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100977 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000978 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100979 self->output_len += data_len;
980 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000981}
982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983static PicklerObject *
984_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000985{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
989 if (self == NULL)
990 return NULL;
991
992 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100993 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000994 self->write = NULL;
995 self->proto = 0;
996 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100997 self->framing = 0;
998 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 self->fast = 0;
1000 self->fast_nesting = 0;
1001 self->fix_imports = 0;
1002 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001003 self->max_output_len = WRITE_BUF_SIZE;
1004 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001005
1006 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1008 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001009
1010 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001011 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 return NULL;
1013 }
1014 return self;
1015}
1016
1017static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001018_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001020 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001022 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001024 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001026 proto = PyLong_AsLong(protocol);
1027 if (proto < 0) {
1028 if (proto == -1 && PyErr_Occurred())
1029 return -1;
1030 proto = HIGHEST_PROTOCOL;
1031 }
1032 else if (proto > HIGHEST_PROTOCOL) {
1033 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1034 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001035 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001036 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001038 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 self->bin = proto > 0;
1040 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 return 0;
1042}
1043
1044/* Returns -1 (with an exception set) on failure, 0 on success. This may
1045 be called once on a freshly created Pickler. */
1046static int
1047_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1048{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001049 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001051 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 if (self->write == NULL) {
1053 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1054 PyErr_SetString(PyExc_TypeError,
1055 "file must have a 'write' attribute");
1056 return -1;
1057 }
1058
1059 return 0;
1060}
1061
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062/* Returns the size of the input on success, -1 on failure. This takes its
1063 own reference to `input`. */
1064static Py_ssize_t
1065_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1066{
1067 if (self->buffer.buf != NULL)
1068 PyBuffer_Release(&self->buffer);
1069 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1070 return -1;
1071 self->input_buffer = self->buffer.buf;
1072 self->input_len = self->buffer.len;
1073 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001074 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 return self->input_len;
1076}
1077
Antoine Pitrou04248a82010-10-12 20:51:21 +00001078static int
1079_Unpickler_SkipConsumed(UnpicklerObject *self)
1080{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001081 Py_ssize_t consumed;
1082 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001083
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001084 consumed = self->next_read_idx - self->prefetched_idx;
1085 if (consumed <= 0)
1086 return 0;
1087
1088 assert(self->peek); /* otherwise we did something wrong */
1089 /* This makes an useless copy... */
1090 r = PyObject_CallFunction(self->read, "n", consumed);
1091 if (r == NULL)
1092 return -1;
1093 Py_DECREF(r);
1094
1095 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096 return 0;
1097}
1098
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001099static const Py_ssize_t READ_WHOLE_LINE = -1;
1100
1101/* If reading from a file, we need to only pull the bytes we need, since there
1102 may be multiple pickle objects arranged contiguously in the same input
1103 buffer.
1104
1105 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1106 bytes from the input stream/buffer.
1107
1108 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1109 failure; on success, returns the number of bytes read from the file.
1110
1111 On success, self->input_len will be 0; this is intentional so that when
1112 unpickling from a file, the "we've run out of data" code paths will trigger,
1113 causing the Unpickler to go back to the file for more data. Use the returned
1114 size to tell you how much data you can process. */
1115static Py_ssize_t
1116_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1117{
1118 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001119 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001120
1121 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001122
Antoine Pitrou04248a82010-10-12 20:51:21 +00001123 if (_Unpickler_SkipConsumed(self) < 0)
1124 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001126 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001127 PyObject *empty_tuple = PyTuple_New(0);
1128 data = PyObject_Call(self->readline, empty_tuple, NULL);
1129 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001130 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001131 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001132 PyObject *len;
1133 /* Prefetch some data without advancing the file pointer, if possible */
1134 if (self->peek && n < PREFETCH) {
1135 len = PyLong_FromSsize_t(PREFETCH);
1136 if (len == NULL)
1137 return -1;
1138 data = _Pickle_FastCall(self->peek, len);
1139 if (data == NULL) {
1140 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1141 return -1;
1142 /* peek() is probably not supported by the given file object */
1143 PyErr_Clear();
1144 Py_CLEAR(self->peek);
1145 }
1146 else {
1147 read_size = _Unpickler_SetStringInput(self, data);
1148 Py_DECREF(data);
1149 self->prefetched_idx = 0;
1150 if (n <= read_size)
1151 return n;
1152 }
1153 }
1154 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 if (len == NULL)
1156 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001157 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001159 if (data == NULL)
1160 return -1;
1161
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001162 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001163 Py_DECREF(data);
1164 return read_size;
1165}
1166
1167/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1168
1169 This should be used for all data reads, rather than accessing the unpickler's
1170 input buffer directly. This method deals correctly with reading from input
1171 streams, which the input buffer doesn't deal with.
1172
1173 Note that when reading from a file-like object, self->next_read_idx won't
1174 be updated (it should remain at 0 for the entire unpickling process). You
1175 should use this function's return value to know how many bytes you can
1176 consume.
1177
1178 Returns -1 (with an exception set) on failure. On success, return the
1179 number of chars read. */
1180static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001181_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001182{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001183 Py_ssize_t num_read;
1184
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001185 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001186 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1187 PickleState *st = _Pickle_GetGlobalState();
1188 PyErr_SetString(st->UnpicklingError,
1189 "read would overflow (invalid bytecode)");
1190 return -1;
1191 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001192 if (self->next_read_idx + n <= self->input_len) {
1193 *s = self->input_buffer + self->next_read_idx;
1194 self->next_read_idx += n;
1195 return n;
1196 }
1197 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001198 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001199 return -1;
1200 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001201 num_read = _Unpickler_ReadFromFile(self, n);
1202 if (num_read < 0)
1203 return -1;
1204 if (num_read < n) {
1205 PyErr_Format(PyExc_EOFError, "Ran out of input");
1206 return -1;
1207 }
1208 *s = self->input_buffer;
1209 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001210 return n;
1211}
1212
1213static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001214_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1215 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001216{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001217 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001218 if (input_line == NULL) {
1219 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001220 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001221 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223 memcpy(input_line, line, len);
1224 input_line[len] = '\0';
1225 self->input_line = input_line;
1226 *result = self->input_line;
1227 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001228}
1229
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001230/* Read a line from the input stream/buffer. If we run off the end of the input
1231 before hitting \n, return the data we found.
1232
1233 Returns the number of chars read, or -1 on failure. */
1234static Py_ssize_t
1235_Unpickler_Readline(UnpicklerObject *self, char **result)
1236{
1237 Py_ssize_t i, num_read;
1238
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001239 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240 if (self->input_buffer[i] == '\n') {
1241 char *line_start = self->input_buffer + self->next_read_idx;
1242 num_read = i - self->next_read_idx + 1;
1243 self->next_read_idx = i + 1;
1244 return _Unpickler_CopyLine(self, line_start, num_read, result);
1245 }
1246 }
1247 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1249 if (num_read < 0)
1250 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001251 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001252 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001253 }
Victor Stinner121aab42011-09-29 23:40:53 +02001254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255 /* If we get here, we've run off the end of the input string. Return the
1256 remaining string and let the caller figure it out. */
1257 *result = self->input_buffer + self->next_read_idx;
1258 num_read = i - self->next_read_idx;
1259 self->next_read_idx = i;
1260 return num_read;
1261}
1262
1263/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1264 will be modified in place. */
1265static int
1266_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1267{
1268 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001269
1270 assert(new_size > self->memo_size);
1271
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001272 PyMem_RESIZE(self->memo, PyObject *, new_size);
1273 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001274 PyErr_NoMemory();
1275 return -1;
1276 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001277 for (i = self->memo_size; i < new_size; i++)
1278 self->memo[i] = NULL;
1279 self->memo_size = new_size;
1280 return 0;
1281}
1282
1283/* Returns NULL if idx is out of bounds. */
1284static PyObject *
1285_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1286{
1287 if (idx < 0 || idx >= self->memo_size)
1288 return NULL;
1289
1290 return self->memo[idx];
1291}
1292
1293/* Returns -1 (with an exception set) on failure, 0 on success.
1294 This takes its own reference to `value`. */
1295static int
1296_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1297{
1298 PyObject *old_item;
1299
1300 if (idx >= self->memo_size) {
1301 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1302 return -1;
1303 assert(idx < self->memo_size);
1304 }
1305 Py_INCREF(value);
1306 old_item = self->memo[idx];
1307 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001308 if (old_item != NULL) {
1309 Py_DECREF(old_item);
1310 }
1311 else {
1312 self->memo_len++;
1313 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001314 return 0;
1315}
1316
1317static PyObject **
1318_Unpickler_NewMemo(Py_ssize_t new_size)
1319{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001320 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001321 if (memo == NULL) {
1322 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001323 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001324 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001325 memset(memo, 0, new_size * sizeof(PyObject *));
1326 return memo;
1327}
1328
1329/* Free the unpickler's memo, taking care to decref any items left in it. */
1330static void
1331_Unpickler_MemoCleanup(UnpicklerObject *self)
1332{
1333 Py_ssize_t i;
1334 PyObject **memo = self->memo;
1335
1336 if (self->memo == NULL)
1337 return;
1338 self->memo = NULL;
1339 i = self->memo_size;
1340 while (--i >= 0) {
1341 Py_XDECREF(memo[i]);
1342 }
1343 PyMem_FREE(memo);
1344}
1345
1346static UnpicklerObject *
1347_Unpickler_New(void)
1348{
1349 UnpicklerObject *self;
1350
1351 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1352 if (self == NULL)
1353 return NULL;
1354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001355 self->pers_func = NULL;
1356 self->input_buffer = NULL;
1357 self->input_line = NULL;
1358 self->input_len = 0;
1359 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001360 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001361 self->read = NULL;
1362 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001363 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001364 self->encoding = NULL;
1365 self->errors = NULL;
1366 self->marks = NULL;
1367 self->num_marks = 0;
1368 self->marks_size = 0;
1369 self->proto = 0;
1370 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001371 memset(&self->buffer, 0, sizeof(Py_buffer));
1372 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001373 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001374 self->memo = _Unpickler_NewMemo(self->memo_size);
1375 self->stack = (Pdata *)Pdata_New();
1376
1377 if (self->memo == NULL || self->stack == NULL) {
1378 Py_DECREF(self);
1379 return NULL;
1380 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001381
1382 return self;
1383}
1384
1385/* Returns -1 (with an exception set) on failure, 0 on success. This may
1386 be called once on a freshly created Pickler. */
1387static int
1388_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1389{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001390 _Py_IDENTIFIER(peek);
1391 _Py_IDENTIFIER(read);
1392 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001393
1394 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001395 if (self->peek == NULL) {
1396 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1397 PyErr_Clear();
1398 else
1399 return -1;
1400 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001401 self->read = _PyObject_GetAttrId(file, &PyId_read);
1402 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001403 if (self->readline == NULL || self->read == NULL) {
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1405 PyErr_SetString(PyExc_TypeError,
1406 "file must have 'read' and 'readline' attributes");
1407 Py_CLEAR(self->read);
1408 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001409 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001410 return -1;
1411 }
1412 return 0;
1413}
1414
1415/* Returns -1 (with an exception set) on failure, 0 on success. This may
1416 be called once on a freshly created Pickler. */
1417static int
1418_Unpickler_SetInputEncoding(UnpicklerObject *self,
1419 const char *encoding,
1420 const char *errors)
1421{
1422 if (encoding == NULL)
1423 encoding = "ASCII";
1424 if (errors == NULL)
1425 errors = "strict";
1426
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001427 self->encoding = _PyMem_Strdup(encoding);
1428 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001429 if (self->encoding == NULL || self->errors == NULL) {
1430 PyErr_NoMemory();
1431 return -1;
1432 }
1433 return 0;
1434}
1435
1436/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001437static int
1438memo_get(PicklerObject *self, PyObject *key)
1439{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001440 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001442 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001444 value = PyMemoTable_Get(self->memo, key);
1445 if (value == NULL) {
1446 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447 return -1;
1448 }
1449
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001450 if (!self->bin) {
1451 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001452 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1453 "%" PY_FORMAT_SIZE_T "d\n", *value);
1454 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 }
1456 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001457 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001459 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001460 len = 2;
1461 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001462 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001464 pdata[1] = (unsigned char)(*value & 0xff);
1465 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1466 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1467 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 len = 5;
1469 }
1470 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001471 PickleState *st = _Pickle_GetGlobalState();
1472 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 "memo id too large for LONG_BINGET");
1474 return -1;
1475 }
1476 }
1477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001478 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 return -1;
1480
1481 return 0;
1482}
1483
1484/* Store an object in the memo, assign it a new unique ID based on the number
1485 of objects currently stored in the memo and generate a PUT opcode. */
1486static int
1487memo_put(PicklerObject *self, PyObject *obj)
1488{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001490 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001491 Py_ssize_t idx;
1492
1493 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001494
1495 if (self->fast)
1496 return 0;
1497
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001498 idx = PyMemoTable_Size(self->memo);
1499 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1500 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001502 if (self->proto >= 4) {
1503 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1504 return -1;
1505 return 0;
1506 }
1507 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001509 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001510 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511 len = strlen(pdata);
1512 }
1513 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001514 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001515 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001516 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 len = 2;
1518 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001519 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001520 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001521 pdata[1] = (unsigned char)(idx & 0xff);
1522 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1523 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1524 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 len = 5;
1526 }
1527 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001528 PickleState *st = _Pickle_GetGlobalState();
1529 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530 "memo id too large for LONG_BINPUT");
1531 return -1;
1532 }
1533 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001534 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001535 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001537 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538}
1539
1540static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1542 PyObject *dotted_path;
1543 Py_ssize_t i;
1544 _Py_static_string(PyId_dot, ".");
1545 _Py_static_string(PyId_locals, "<locals>");
1546
1547 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1548 if (dotted_path == NULL) {
1549 return NULL;
1550 }
1551 assert(Py_SIZE(dotted_path) >= 1);
1552 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1553 PyErr_Format(PyExc_AttributeError,
1554 "Can't get qualified attribute %R on %R;"
1555 "use protocols >= 4 to enable support",
1556 name, obj);
1557 Py_DECREF(dotted_path);
1558 return NULL;
1559 }
1560 Py_INCREF(obj);
1561 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1562 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1563 PyObject *tmp;
1564 PyObject *result = PyUnicode_RichCompare(
1565 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1566 int is_equal = (result == Py_True);
1567 assert(PyBool_Check(result));
1568 Py_DECREF(result);
1569 if (is_equal) {
1570 PyErr_Format(PyExc_AttributeError,
1571 "Can't get local attribute %R on %R", name, obj);
1572 Py_DECREF(dotted_path);
1573 Py_DECREF(obj);
1574 return NULL;
1575 }
1576 tmp = PyObject_GetAttr(obj, subpath);
1577 Py_DECREF(obj);
1578 if (tmp == NULL) {
1579 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1580 PyErr_Clear();
1581 PyErr_Format(PyExc_AttributeError,
1582 "Can't get attribute %R on %R", name, obj);
1583 }
1584 Py_DECREF(dotted_path);
1585 return NULL;
1586 }
1587 obj = tmp;
1588 }
1589 Py_DECREF(dotted_path);
1590 return obj;
1591}
1592
1593static PyObject *
1594whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001595{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001596 PyObject *module_name;
1597 PyObject *modules_dict;
1598 PyObject *module;
1599 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 Py_ssize_t i, j;
1601 _Py_IDENTIFIER(__module__);
1602 _Py_IDENTIFIER(modules);
1603 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001605 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1606
1607 if (module_name == NULL) {
1608 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001609 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001610 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001611 }
1612 else {
1613 /* In some rare cases (e.g., bound methods of extension types),
1614 __module__ can be None. If it is so, then search sys.modules for
1615 the module of global. */
1616 if (module_name != Py_None)
1617 return module_name;
1618 Py_CLEAR(module_name);
1619 }
1620 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001621
Victor Stinnerbb520202013-11-06 22:40:41 +01001622 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001623 if (modules_dict == NULL) {
1624 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001625 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001626 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001627
1628 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001630 PyObject *result = PyUnicode_RichCompare(
1631 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1632 int is_equal = (result == Py_True);
1633 assert(PyBool_Check(result));
1634 Py_DECREF(result);
1635 if (is_equal)
1636 continue;
1637 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 continue;
1639
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001640 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001641 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001642 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001645 continue;
1646 }
1647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 Py_INCREF(module_name);
1651 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001652 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001654 }
1655
1656 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001657 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001658 Py_INCREF(module_name);
1659 return module_name;
1660}
1661
1662/* fast_save_enter() and fast_save_leave() are guards against recursive
1663 objects when Pickler is used with the "fast mode" (i.e., with object
1664 memoization disabled). If the nesting of a list or dict object exceed
1665 FAST_NESTING_LIMIT, these guards will start keeping an internal
1666 reference to the seen list or dict objects and check whether these objects
1667 are recursive. These are not strictly necessary, since save() has a
1668 hard-coded recursion limit, but they give a nicer error message than the
1669 typical RuntimeError. */
1670static int
1671fast_save_enter(PicklerObject *self, PyObject *obj)
1672{
1673 /* if fast_nesting < 0, we're doing an error exit. */
1674 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1675 PyObject *key = NULL;
1676 if (self->fast_memo == NULL) {
1677 self->fast_memo = PyDict_New();
1678 if (self->fast_memo == NULL) {
1679 self->fast_nesting = -1;
1680 return 0;
1681 }
1682 }
1683 key = PyLong_FromVoidPtr(obj);
1684 if (key == NULL)
1685 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001686 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001687 Py_DECREF(key);
1688 PyErr_Format(PyExc_ValueError,
1689 "fast mode: can't pickle cyclic objects "
1690 "including object type %.200s at %p",
1691 obj->ob_type->tp_name, obj);
1692 self->fast_nesting = -1;
1693 return 0;
1694 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001695 if (PyErr_Occurred()) {
1696 return 0;
1697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001698 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1699 Py_DECREF(key);
1700 self->fast_nesting = -1;
1701 return 0;
1702 }
1703 Py_DECREF(key);
1704 }
1705 return 1;
1706}
1707
1708static int
1709fast_save_leave(PicklerObject *self, PyObject *obj)
1710{
1711 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1712 PyObject *key = PyLong_FromVoidPtr(obj);
1713 if (key == NULL)
1714 return 0;
1715 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1716 Py_DECREF(key);
1717 return 0;
1718 }
1719 Py_DECREF(key);
1720 }
1721 return 1;
1722}
1723
1724static int
1725save_none(PicklerObject *self, PyObject *obj)
1726{
1727 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001728 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001729 return -1;
1730
1731 return 0;
1732}
1733
1734static int
1735save_bool(PicklerObject *self, PyObject *obj)
1736{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001738 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001739 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001740 return -1;
1741 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001742 else {
1743 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1744 * so that unpicklers written before bools were introduced unpickle them
1745 * as ints, but unpicklers after can recognize that bools were intended.
1746 * Note that protocol 2 added direct ways to pickle bools.
1747 */
1748 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1749 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1750 return -1;
1751 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001752 return 0;
1753}
1754
1755static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001756save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001757{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001758 PyObject *repr = NULL;
1759 Py_ssize_t size;
1760 long val;
1761 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001762
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001763 const char long_op = LONG;
1764
1765 val= PyLong_AsLong(obj);
1766 if (val == -1 && PyErr_Occurred()) {
1767 /* out of range for int pickling */
1768 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001769 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001770 else if (self->bin &&
1771 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001772 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001773 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001774
1775 Note: we can't use -0x80000000L in the above condition because some
1776 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1777 before applying the unary minus when sizeof(long) <= 4. The
1778 resulting value stays unsigned which is commonly not what we want,
1779 so MSVC happily warns us about it. However, that result would have
1780 been fine because we guard for sizeof(long) <= 4 which turns the
1781 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001782 char pdata[32];
1783 Py_ssize_t len = 0;
1784
1785 pdata[1] = (unsigned char)(val & 0xff);
1786 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1787 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1788 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789
1790 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1791 if (pdata[2] == 0) {
1792 pdata[0] = BININT1;
1793 len = 2;
1794 }
1795 else {
1796 pdata[0] = BININT2;
1797 len = 3;
1798 }
1799 }
1800 else {
1801 pdata[0] = BININT;
1802 len = 5;
1803 }
1804
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001805 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001807
1808 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001809 }
1810
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001811 if (self->proto >= 2) {
1812 /* Linear-time pickling. */
1813 size_t nbits;
1814 size_t nbytes;
1815 unsigned char *pdata;
1816 char header[5];
1817 int i;
1818 int sign = _PyLong_Sign(obj);
1819
1820 if (sign == 0) {
1821 header[0] = LONG1;
1822 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001823 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001824 goto error;
1825 return 0;
1826 }
1827 nbits = _PyLong_NumBits(obj);
1828 if (nbits == (size_t)-1 && PyErr_Occurred())
1829 goto error;
1830 /* How many bytes do we need? There are nbits >> 3 full
1831 * bytes of data, and nbits & 7 leftover bits. If there
1832 * are any leftover bits, then we clearly need another
1833 * byte. Wnat's not so obvious is that we *probably*
1834 * need another byte even if there aren't any leftovers:
1835 * the most-significant bit of the most-significant byte
1836 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001837 * opposite of the one we need. The exception is ints
1838 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001839 * its own 256's-complement, so has the right sign bit
1840 * even without the extra byte. That's a pain to check
1841 * for in advance, though, so we always grab an extra
1842 * byte at the start, and cut it back later if possible.
1843 */
1844 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001845 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001847 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 goto error;
1849 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001850 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 if (repr == NULL)
1852 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001853 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854 i = _PyLong_AsByteArray((PyLongObject *)obj,
1855 pdata, nbytes,
1856 1 /* little endian */ , 1 /* signed */ );
1857 if (i < 0)
1858 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001859 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001860 * needed. This is so iff the MSB is all redundant sign
1861 * bits.
1862 */
1863 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001864 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 pdata[nbytes - 1] == 0xff &&
1866 (pdata[nbytes - 2] & 0x80) != 0) {
1867 nbytes--;
1868 }
1869
1870 if (nbytes < 256) {
1871 header[0] = LONG1;
1872 header[1] = (unsigned char)nbytes;
1873 size = 2;
1874 }
1875 else {
1876 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001877 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001878 for (i = 1; i < 5; i++) {
1879 header[i] = (unsigned char)(size & 0xff);
1880 size >>= 8;
1881 }
1882 size = 5;
1883 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001884 if (_Pickler_Write(self, header, size) < 0 ||
1885 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 goto error;
1887 }
1888 else {
1889 char *string;
1890
Mark Dickinson8dd05142009-01-20 20:43:58 +00001891 /* proto < 2: write the repr and newline. This is quadratic-time (in
1892 the number of digits), in both directions. We add a trailing 'L'
1893 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001894
1895 repr = PyObject_Repr(obj);
1896 if (repr == NULL)
1897 goto error;
1898
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001899 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001900 if (string == NULL)
1901 goto error;
1902
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001903 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1904 _Pickler_Write(self, string, size) < 0 ||
1905 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 goto error;
1907 }
1908
1909 if (0) {
1910 error:
1911 status = -1;
1912 }
1913 Py_XDECREF(repr);
1914
1915 return status;
1916}
1917
1918static int
1919save_float(PicklerObject *self, PyObject *obj)
1920{
1921 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1922
1923 if (self->bin) {
1924 char pdata[9];
1925 pdata[0] = BINFLOAT;
1926 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1927 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001928 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001929 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001930 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001932 int result = -1;
1933 char *buf = NULL;
1934 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001936 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001937 goto done;
1938
Mark Dickinson3e09f432009-04-17 08:41:23 +00001939 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001940 if (!buf) {
1941 PyErr_NoMemory();
1942 goto done;
1943 }
1944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001945 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001946 goto done;
1947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001948 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001949 goto done;
1950
1951 result = 0;
1952done:
1953 PyMem_Free(buf);
1954 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001955 }
1956
1957 return 0;
1958}
1959
1960static int
1961save_bytes(PicklerObject *self, PyObject *obj)
1962{
1963 if (self->proto < 3) {
1964 /* Older pickle protocols do not have an opcode for pickling bytes
1965 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001966 the __reduce__ method) to permit bytes object unpickling.
1967
1968 Here we use a hack to be compatible with Python 2. Since in Python
1969 2 'bytes' is just an alias for 'str' (which has different
1970 parameters than the actual bytes object), we use codecs.encode
1971 to create the appropriate 'str' object when unpickled using
1972 Python 2 *and* the appropriate 'bytes' object when unpickled
1973 using Python 3. Again this is a hack and we don't need to do this
1974 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001975 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976 int status;
1977
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001978 if (PyBytes_GET_SIZE(obj) == 0) {
1979 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1980 }
1981 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001982 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001983 PyObject *unicode_str =
1984 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1985 PyBytes_GET_SIZE(obj),
1986 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001987 _Py_IDENTIFIER(latin1);
1988
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001989 if (unicode_str == NULL)
1990 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001991 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001992 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001993 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001994 Py_DECREF(unicode_str);
1995 }
1996
1997 if (reduce_value == NULL)
1998 return -1;
1999
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000 /* save_reduce() will memoize the object automatically. */
2001 status = save_reduce(self, reduce_value, obj);
2002 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002003 return status;
2004 }
2005 else {
2006 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002007 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002008 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002009
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002010 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 if (size < 0)
2012 return -1;
2013
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002014 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002015 header[0] = SHORT_BINBYTES;
2016 header[1] = (unsigned char)size;
2017 len = 2;
2018 }
2019 else if (size <= 0xffffffffL) {
2020 header[0] = BINBYTES;
2021 header[1] = (unsigned char)(size & 0xff);
2022 header[2] = (unsigned char)((size >> 8) & 0xff);
2023 header[3] = (unsigned char)((size >> 16) & 0xff);
2024 header[4] = (unsigned char)((size >> 24) & 0xff);
2025 len = 5;
2026 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002027 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002029 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002030 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002031 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002033 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002034 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002035 return -1; /* string too large */
2036 }
2037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002038 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002039 return -1;
2040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002041 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042 return -1;
2043
2044 if (memo_put(self, obj) < 0)
2045 return -1;
2046
2047 return 0;
2048 }
2049}
2050
2051/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2052 backslash and newline characters to \uXXXX escapes. */
2053static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002054raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002055{
2056 PyObject *repr, *result;
2057 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002058 Py_ssize_t i, size, expandsize;
2059 void *data;
2060 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002062 if (PyUnicode_READY(obj))
2063 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002064
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002065 size = PyUnicode_GET_LENGTH(obj);
2066 data = PyUnicode_DATA(obj);
2067 kind = PyUnicode_KIND(obj);
2068 if (kind == PyUnicode_4BYTE_KIND)
2069 expandsize = 10;
2070 else
2071 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002072
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002073 if (size > PY_SSIZE_T_MAX / expandsize)
2074 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002075 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 if (repr == NULL)
2077 return NULL;
2078 if (size == 0)
2079 goto done;
2080
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002081 p = PyByteArray_AS_STRING(repr);
2082 for (i=0; i < size; i++) {
2083 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 /* Map 32-bit characters to '\Uxxxxxxxx' */
2085 if (ch >= 0x10000) {
2086 *p++ = '\\';
2087 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002088 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2089 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2090 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2091 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2092 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2093 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2094 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2095 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002098 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 *p++ = '\\';
2100 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002101 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2102 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2103 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2104 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002106 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 else
2108 *p++ = (char) ch;
2109 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002110 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002111
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002113 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 Py_DECREF(repr);
2115 return result;
2116}
2117
2118static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002119write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2120{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002121 char header[9];
2122 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002123
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002124 if (size <= 0xff && self->proto >= 4) {
2125 header[0] = SHORT_BINUNICODE;
2126 header[1] = (unsigned char)(size & 0xff);
2127 len = 2;
2128 }
2129 else if (size <= 0xffffffffUL) {
2130 header[0] = BINUNICODE;
2131 header[1] = (unsigned char)(size & 0xff);
2132 header[2] = (unsigned char)((size >> 8) & 0xff);
2133 header[3] = (unsigned char)((size >> 16) & 0xff);
2134 header[4] = (unsigned char)((size >> 24) & 0xff);
2135 len = 5;
2136 }
2137 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002138 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002139 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002140 len = 9;
2141 }
2142 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002143 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002144 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002145 return -1;
2146 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002147
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002148 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002149 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002150 if (_Pickler_Write(self, data, size) < 0)
2151 return -1;
2152
2153 return 0;
2154}
2155
2156static int
2157write_unicode_binary(PicklerObject *self, PyObject *obj)
2158{
2159 PyObject *encoded = NULL;
2160 Py_ssize_t size;
2161 char *data;
2162 int r;
2163
2164 if (PyUnicode_READY(obj))
2165 return -1;
2166
2167 data = PyUnicode_AsUTF8AndSize(obj, &size);
2168 if (data != NULL)
2169 return write_utf8(self, data, size);
2170
2171 /* Issue #8383: for strings with lone surrogates, fallback on the
2172 "surrogatepass" error handler. */
2173 PyErr_Clear();
2174 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2175 if (encoded == NULL)
2176 return -1;
2177
2178 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2179 PyBytes_GET_SIZE(encoded));
2180 Py_DECREF(encoded);
2181 return r;
2182}
2183
2184static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185save_unicode(PicklerObject *self, PyObject *obj)
2186{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002187 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002188 if (write_unicode_binary(self, obj) < 0)
2189 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002190 }
2191 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192 PyObject *encoded;
2193 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002194 const char unicode_op = UNICODE;
2195
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002196 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002197 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002199
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2201 Py_DECREF(encoded);
2202 return -1;
2203 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002204
2205 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2207 Py_DECREF(encoded);
2208 return -1;
2209 }
2210 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002211
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002212 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002213 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002214 }
2215 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002216 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002218 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219}
2220
2221/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2222static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002223store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002225 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002226
2227 assert(PyTuple_Size(t) == len);
2228
2229 for (i = 0; i < len; i++) {
2230 PyObject *element = PyTuple_GET_ITEM(t, i);
2231
2232 if (element == NULL)
2233 return -1;
2234 if (save(self, element, 0) < 0)
2235 return -1;
2236 }
2237
2238 return 0;
2239}
2240
2241/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2242 * used across protocols to minimize the space needed to pickle them.
2243 * Tuples are also the only builtin immutable type that can be recursive
2244 * (a tuple can be reached from itself), and that requires some subtle
2245 * magic so that it works in all cases. IOW, this is a long routine.
2246 */
2247static int
2248save_tuple(PicklerObject *self, PyObject *obj)
2249{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002250 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002251
2252 const char mark_op = MARK;
2253 const char tuple_op = TUPLE;
2254 const char pop_op = POP;
2255 const char pop_mark_op = POP_MARK;
2256 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2257
2258 if ((len = PyTuple_Size(obj)) < 0)
2259 return -1;
2260
2261 if (len == 0) {
2262 char pdata[2];
2263
2264 if (self->proto) {
2265 pdata[0] = EMPTY_TUPLE;
2266 len = 1;
2267 }
2268 else {
2269 pdata[0] = MARK;
2270 pdata[1] = TUPLE;
2271 len = 2;
2272 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002273 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274 return -1;
2275 return 0;
2276 }
2277
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002278 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279 * saving the tuple elements, the tuple must be recursive, in
2280 * which case we'll pop everything we put on the stack, and fetch
2281 * its value from the memo.
2282 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002283 if (len <= 3 && self->proto >= 2) {
2284 /* Use TUPLE{1,2,3} opcodes. */
2285 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002286 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002288 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289 /* pop the len elements */
2290 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002291 if (_Pickler_Write(self, &pop_op, 1) < 0)
2292 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002294 if (memo_get(self, obj) < 0)
2295 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002296
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 return 0;
2298 }
2299 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002300 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2301 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002302 }
2303 goto memoize;
2304 }
2305
2306 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2307 * Generate MARK e1 e2 ... TUPLE
2308 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002309 if (_Pickler_Write(self, &mark_op, 1) < 0)
2310 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002311
2312 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316 /* pop the stack stuff we pushed */
2317 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002318 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2319 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002320 }
2321 else {
2322 /* Note that we pop one more than len, to remove
2323 * the MARK too.
2324 */
2325 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002326 if (_Pickler_Write(self, &pop_op, 1) < 0)
2327 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 }
2329 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (memo_get(self, obj) < 0)
2331 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002333 return 0;
2334 }
2335 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338 }
2339
2340 memoize:
2341 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345}
2346
2347/* iter is an iterator giving items, and we batch up chunks of
2348 * MARK item item ... item APPENDS
2349 * opcode sequences. Calling code should have arranged to first create an
2350 * empty list, or list-like object, for the APPENDS to operate on.
2351 * Returns 0 on success, <0 on error.
2352 */
2353static int
2354batch_list(PicklerObject *self, PyObject *iter)
2355{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002356 PyObject *obj = NULL;
2357 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 int i, n;
2359
2360 const char mark_op = MARK;
2361 const char append_op = APPEND;
2362 const char appends_op = APPENDS;
2363
2364 assert(iter != NULL);
2365
2366 /* XXX: I think this function could be made faster by avoiding the
2367 iterator interface and fetching objects directly from list using
2368 PyList_GET_ITEM.
2369 */
2370
2371 if (self->proto == 0) {
2372 /* APPENDS isn't available; do one at a time. */
2373 for (;;) {
2374 obj = PyIter_Next(iter);
2375 if (obj == NULL) {
2376 if (PyErr_Occurred())
2377 return -1;
2378 break;
2379 }
2380 i = save(self, obj, 0);
2381 Py_DECREF(obj);
2382 if (i < 0)
2383 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385 return -1;
2386 }
2387 return 0;
2388 }
2389
2390 /* proto > 0: write in batches of BATCHSIZE. */
2391 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002392 /* Get first item */
2393 firstitem = PyIter_Next(iter);
2394 if (firstitem == NULL) {
2395 if (PyErr_Occurred())
2396 goto error;
2397
2398 /* nothing more to add */
2399 break;
2400 }
2401
2402 /* Try to get a second item */
2403 obj = PyIter_Next(iter);
2404 if (obj == NULL) {
2405 if (PyErr_Occurred())
2406 goto error;
2407
2408 /* Only one item to write */
2409 if (save(self, firstitem, 0) < 0)
2410 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002411 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002412 goto error;
2413 Py_CLEAR(firstitem);
2414 break;
2415 }
2416
2417 /* More than one item to write */
2418
2419 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002420 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002421 goto error;
2422
2423 if (save(self, firstitem, 0) < 0)
2424 goto error;
2425 Py_CLEAR(firstitem);
2426 n = 1;
2427
2428 /* Fetch and save up to BATCHSIZE items */
2429 while (obj) {
2430 if (save(self, obj, 0) < 0)
2431 goto error;
2432 Py_CLEAR(obj);
2433 n += 1;
2434
2435 if (n == BATCHSIZE)
2436 break;
2437
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002438 obj = PyIter_Next(iter);
2439 if (obj == NULL) {
2440 if (PyErr_Occurred())
2441 goto error;
2442 break;
2443 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002444 }
2445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002446 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002447 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002449 } while (n == BATCHSIZE);
2450 return 0;
2451
2452 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002453 Py_XDECREF(firstitem);
2454 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002455 return -1;
2456}
2457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002458/* This is a variant of batch_list() above, specialized for lists (with no
2459 * support for list subclasses). Like batch_list(), we batch up chunks of
2460 * MARK item item ... item APPENDS
2461 * opcode sequences. Calling code should have arranged to first create an
2462 * empty list, or list-like object, for the APPENDS to operate on.
2463 * Returns 0 on success, -1 on error.
2464 *
2465 * This version is considerably faster than batch_list(), if less general.
2466 *
2467 * Note that this only works for protocols > 0.
2468 */
2469static int
2470batch_list_exact(PicklerObject *self, PyObject *obj)
2471{
2472 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002473 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002474
2475 const char append_op = APPEND;
2476 const char appends_op = APPENDS;
2477 const char mark_op = MARK;
2478
2479 assert(obj != NULL);
2480 assert(self->proto > 0);
2481 assert(PyList_CheckExact(obj));
2482
2483 if (PyList_GET_SIZE(obj) == 1) {
2484 item = PyList_GET_ITEM(obj, 0);
2485 if (save(self, item, 0) < 0)
2486 return -1;
2487 if (_Pickler_Write(self, &append_op, 1) < 0)
2488 return -1;
2489 return 0;
2490 }
2491
2492 /* Write in batches of BATCHSIZE. */
2493 total = 0;
2494 do {
2495 this_batch = 0;
2496 if (_Pickler_Write(self, &mark_op, 1) < 0)
2497 return -1;
2498 while (total < PyList_GET_SIZE(obj)) {
2499 item = PyList_GET_ITEM(obj, total);
2500 if (save(self, item, 0) < 0)
2501 return -1;
2502 total++;
2503 if (++this_batch == BATCHSIZE)
2504 break;
2505 }
2506 if (_Pickler_Write(self, &appends_op, 1) < 0)
2507 return -1;
2508
2509 } while (total < PyList_GET_SIZE(obj));
2510
2511 return 0;
2512}
2513
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002514static int
2515save_list(PicklerObject *self, PyObject *obj)
2516{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002518 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519 int status = 0;
2520
2521 if (self->fast && !fast_save_enter(self, obj))
2522 goto error;
2523
2524 /* Create an empty list. */
2525 if (self->bin) {
2526 header[0] = EMPTY_LIST;
2527 len = 1;
2528 }
2529 else {
2530 header[0] = MARK;
2531 header[1] = LIST;
2532 len = 2;
2533 }
2534
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002535 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002536 goto error;
2537
2538 /* Get list length, and bow out early if empty. */
2539 if ((len = PyList_Size(obj)) < 0)
2540 goto error;
2541
2542 if (memo_put(self, obj) < 0)
2543 goto error;
2544
2545 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002546 /* Materialize the list elements. */
2547 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002548 if (Py_EnterRecursiveCall(" while pickling an object"))
2549 goto error;
2550 status = batch_list_exact(self, obj);
2551 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002552 } else {
2553 PyObject *iter = PyObject_GetIter(obj);
2554 if (iter == NULL)
2555 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002557 if (Py_EnterRecursiveCall(" while pickling an object")) {
2558 Py_DECREF(iter);
2559 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002560 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002561 status = batch_list(self, iter);
2562 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002563 Py_DECREF(iter);
2564 }
2565 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002566 if (0) {
2567 error:
2568 status = -1;
2569 }
2570
2571 if (self->fast && !fast_save_leave(self, obj))
2572 status = -1;
2573
2574 return status;
2575}
2576
2577/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2578 * MARK key value ... key value SETITEMS
2579 * opcode sequences. Calling code should have arranged to first create an
2580 * empty dict, or dict-like object, for the SETITEMS to operate on.
2581 * Returns 0 on success, <0 on error.
2582 *
2583 * This is very much like batch_list(). The difference between saving
2584 * elements directly, and picking apart two-tuples, is so long-winded at
2585 * the C level, though, that attempts to combine these routines were too
2586 * ugly to bear.
2587 */
2588static int
2589batch_dict(PicklerObject *self, PyObject *iter)
2590{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002591 PyObject *obj = NULL;
2592 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002593 int i, n;
2594
2595 const char mark_op = MARK;
2596 const char setitem_op = SETITEM;
2597 const char setitems_op = SETITEMS;
2598
2599 assert(iter != NULL);
2600
2601 if (self->proto == 0) {
2602 /* SETITEMS isn't available; do one at a time. */
2603 for (;;) {
2604 obj = PyIter_Next(iter);
2605 if (obj == NULL) {
2606 if (PyErr_Occurred())
2607 return -1;
2608 break;
2609 }
2610 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2611 PyErr_SetString(PyExc_TypeError, "dict items "
2612 "iterator must return 2-tuples");
2613 return -1;
2614 }
2615 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2616 if (i >= 0)
2617 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2618 Py_DECREF(obj);
2619 if (i < 0)
2620 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002622 return -1;
2623 }
2624 return 0;
2625 }
2626
2627 /* proto > 0: write in batches of BATCHSIZE. */
2628 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002629 /* Get first item */
2630 firstitem = PyIter_Next(iter);
2631 if (firstitem == NULL) {
2632 if (PyErr_Occurred())
2633 goto error;
2634
2635 /* nothing more to add */
2636 break;
2637 }
2638 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2639 PyErr_SetString(PyExc_TypeError, "dict items "
2640 "iterator must return 2-tuples");
2641 goto error;
2642 }
2643
2644 /* Try to get a second item */
2645 obj = PyIter_Next(iter);
2646 if (obj == NULL) {
2647 if (PyErr_Occurred())
2648 goto error;
2649
2650 /* Only one item to write */
2651 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2652 goto error;
2653 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2654 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002655 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002656 goto error;
2657 Py_CLEAR(firstitem);
2658 break;
2659 }
2660
2661 /* More than one item to write */
2662
2663 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002664 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002665 goto error;
2666
2667 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2668 goto error;
2669 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2670 goto error;
2671 Py_CLEAR(firstitem);
2672 n = 1;
2673
2674 /* Fetch and save up to BATCHSIZE items */
2675 while (obj) {
2676 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2677 PyErr_SetString(PyExc_TypeError, "dict items "
2678 "iterator must return 2-tuples");
2679 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002680 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002681 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2682 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2683 goto error;
2684 Py_CLEAR(obj);
2685 n += 1;
2686
2687 if (n == BATCHSIZE)
2688 break;
2689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002690 obj = PyIter_Next(iter);
2691 if (obj == NULL) {
2692 if (PyErr_Occurred())
2693 goto error;
2694 break;
2695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002696 }
2697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002698 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002699 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002700
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002701 } while (n == BATCHSIZE);
2702 return 0;
2703
2704 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002705 Py_XDECREF(firstitem);
2706 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002707 return -1;
2708}
2709
Collin Winter5c9b02d2009-05-25 05:43:30 +00002710/* This is a variant of batch_dict() above that specializes for dicts, with no
2711 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2712 * MARK key value ... key value SETITEMS
2713 * opcode sequences. Calling code should have arranged to first create an
2714 * empty dict, or dict-like object, for the SETITEMS to operate on.
2715 * Returns 0 on success, -1 on error.
2716 *
2717 * Note that this currently doesn't work for protocol 0.
2718 */
2719static int
2720batch_dict_exact(PicklerObject *self, PyObject *obj)
2721{
2722 PyObject *key = NULL, *value = NULL;
2723 int i;
2724 Py_ssize_t dict_size, ppos = 0;
2725
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002726 const char mark_op = MARK;
2727 const char setitem_op = SETITEM;
2728 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002729
2730 assert(obj != NULL);
2731 assert(self->proto > 0);
2732
2733 dict_size = PyDict_Size(obj);
2734
2735 /* Special-case len(d) == 1 to save space. */
2736 if (dict_size == 1) {
2737 PyDict_Next(obj, &ppos, &key, &value);
2738 if (save(self, key, 0) < 0)
2739 return -1;
2740 if (save(self, value, 0) < 0)
2741 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002743 return -1;
2744 return 0;
2745 }
2746
2747 /* Write in batches of BATCHSIZE. */
2748 do {
2749 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002750 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002751 return -1;
2752 while (PyDict_Next(obj, &ppos, &key, &value)) {
2753 if (save(self, key, 0) < 0)
2754 return -1;
2755 if (save(self, value, 0) < 0)
2756 return -1;
2757 if (++i == BATCHSIZE)
2758 break;
2759 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002761 return -1;
2762 if (PyDict_Size(obj) != dict_size) {
2763 PyErr_Format(
2764 PyExc_RuntimeError,
2765 "dictionary changed size during iteration");
2766 return -1;
2767 }
2768
2769 } while (i == BATCHSIZE);
2770 return 0;
2771}
2772
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002773static int
2774save_dict(PicklerObject *self, PyObject *obj)
2775{
2776 PyObject *items, *iter;
2777 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002778 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002779 int status = 0;
2780
2781 if (self->fast && !fast_save_enter(self, obj))
2782 goto error;
2783
2784 /* Create an empty dict. */
2785 if (self->bin) {
2786 header[0] = EMPTY_DICT;
2787 len = 1;
2788 }
2789 else {
2790 header[0] = MARK;
2791 header[1] = DICT;
2792 len = 2;
2793 }
2794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002795 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002796 goto error;
2797
2798 /* Get dict size, and bow out early if empty. */
2799 if ((len = PyDict_Size(obj)) < 0)
2800 goto error;
2801
2802 if (memo_put(self, obj) < 0)
2803 goto error;
2804
2805 if (len != 0) {
2806 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002807 if (PyDict_CheckExact(obj) && self->proto > 0) {
2808 /* We can take certain shortcuts if we know this is a dict and
2809 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002810 if (Py_EnterRecursiveCall(" while pickling an object"))
2811 goto error;
2812 status = batch_dict_exact(self, obj);
2813 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002814 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002815 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002816
2817 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002818 if (items == NULL)
2819 goto error;
2820 iter = PyObject_GetIter(items);
2821 Py_DECREF(items);
2822 if (iter == NULL)
2823 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002824 if (Py_EnterRecursiveCall(" while pickling an object")) {
2825 Py_DECREF(iter);
2826 goto error;
2827 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002828 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002829 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002830 Py_DECREF(iter);
2831 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002832 }
2833
2834 if (0) {
2835 error:
2836 status = -1;
2837 }
2838
2839 if (self->fast && !fast_save_leave(self, obj))
2840 status = -1;
2841
2842 return status;
2843}
2844
2845static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002846save_set(PicklerObject *self, PyObject *obj)
2847{
2848 PyObject *item;
2849 int i;
2850 Py_ssize_t set_size, ppos = 0;
2851 Py_hash_t hash;
2852
2853 const char empty_set_op = EMPTY_SET;
2854 const char mark_op = MARK;
2855 const char additems_op = ADDITEMS;
2856
2857 if (self->proto < 4) {
2858 PyObject *items;
2859 PyObject *reduce_value;
2860 int status;
2861
2862 items = PySequence_List(obj);
2863 if (items == NULL) {
2864 return -1;
2865 }
2866 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2867 Py_DECREF(items);
2868 if (reduce_value == NULL) {
2869 return -1;
2870 }
2871 /* save_reduce() will memoize the object automatically. */
2872 status = save_reduce(self, reduce_value, obj);
2873 Py_DECREF(reduce_value);
2874 return status;
2875 }
2876
2877 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2878 return -1;
2879
2880 if (memo_put(self, obj) < 0)
2881 return -1;
2882
2883 set_size = PySet_GET_SIZE(obj);
2884 if (set_size == 0)
2885 return 0; /* nothing to do */
2886
2887 /* Write in batches of BATCHSIZE. */
2888 do {
2889 i = 0;
2890 if (_Pickler_Write(self, &mark_op, 1) < 0)
2891 return -1;
2892 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2893 if (save(self, item, 0) < 0)
2894 return -1;
2895 if (++i == BATCHSIZE)
2896 break;
2897 }
2898 if (_Pickler_Write(self, &additems_op, 1) < 0)
2899 return -1;
2900 if (PySet_GET_SIZE(obj) != set_size) {
2901 PyErr_Format(
2902 PyExc_RuntimeError,
2903 "set changed size during iteration");
2904 return -1;
2905 }
2906 } while (i == BATCHSIZE);
2907
2908 return 0;
2909}
2910
2911static int
2912save_frozenset(PicklerObject *self, PyObject *obj)
2913{
2914 PyObject *iter;
2915
2916 const char mark_op = MARK;
2917 const char frozenset_op = FROZENSET;
2918
2919 if (self->fast && !fast_save_enter(self, obj))
2920 return -1;
2921
2922 if (self->proto < 4) {
2923 PyObject *items;
2924 PyObject *reduce_value;
2925 int status;
2926
2927 items = PySequence_List(obj);
2928 if (items == NULL) {
2929 return -1;
2930 }
2931 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2932 items);
2933 Py_DECREF(items);
2934 if (reduce_value == NULL) {
2935 return -1;
2936 }
2937 /* save_reduce() will memoize the object automatically. */
2938 status = save_reduce(self, reduce_value, obj);
2939 Py_DECREF(reduce_value);
2940 return status;
2941 }
2942
2943 if (_Pickler_Write(self, &mark_op, 1) < 0)
2944 return -1;
2945
2946 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002947 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002948 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002949 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002950 for (;;) {
2951 PyObject *item;
2952
2953 item = PyIter_Next(iter);
2954 if (item == NULL) {
2955 if (PyErr_Occurred()) {
2956 Py_DECREF(iter);
2957 return -1;
2958 }
2959 break;
2960 }
2961 if (save(self, item, 0) < 0) {
2962 Py_DECREF(item);
2963 Py_DECREF(iter);
2964 return -1;
2965 }
2966 Py_DECREF(item);
2967 }
2968 Py_DECREF(iter);
2969
2970 /* If the object is already in the memo, this means it is
2971 recursive. In this case, throw away everything we put on the
2972 stack, and fetch the object back from the memo. */
2973 if (PyMemoTable_Get(self->memo, obj)) {
2974 const char pop_mark_op = POP_MARK;
2975
2976 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2977 return -1;
2978 if (memo_get(self, obj) < 0)
2979 return -1;
2980 return 0;
2981 }
2982
2983 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2984 return -1;
2985 if (memo_put(self, obj) < 0)
2986 return -1;
2987
2988 return 0;
2989}
2990
2991static int
2992fix_imports(PyObject **module_name, PyObject **global_name)
2993{
2994 PyObject *key;
2995 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002996 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002997
2998 key = PyTuple_Pack(2, *module_name, *global_name);
2999 if (key == NULL)
3000 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003001 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003002 Py_DECREF(key);
3003 if (item) {
3004 PyObject *fixed_module_name;
3005 PyObject *fixed_global_name;
3006
3007 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3008 PyErr_Format(PyExc_RuntimeError,
3009 "_compat_pickle.REVERSE_NAME_MAPPING values "
3010 "should be 2-tuples, not %.200s",
3011 Py_TYPE(item)->tp_name);
3012 return -1;
3013 }
3014 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3015 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3016 if (!PyUnicode_Check(fixed_module_name) ||
3017 !PyUnicode_Check(fixed_global_name)) {
3018 PyErr_Format(PyExc_RuntimeError,
3019 "_compat_pickle.REVERSE_NAME_MAPPING values "
3020 "should be pairs of str, not (%.200s, %.200s)",
3021 Py_TYPE(fixed_module_name)->tp_name,
3022 Py_TYPE(fixed_global_name)->tp_name);
3023 return -1;
3024 }
3025
3026 Py_CLEAR(*module_name);
3027 Py_CLEAR(*global_name);
3028 Py_INCREF(fixed_module_name);
3029 Py_INCREF(fixed_global_name);
3030 *module_name = fixed_module_name;
3031 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003032 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003033 }
3034 else if (PyErr_Occurred()) {
3035 return -1;
3036 }
3037
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003038 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003039 if (item) {
3040 if (!PyUnicode_Check(item)) {
3041 PyErr_Format(PyExc_RuntimeError,
3042 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3043 "should be strings, not %.200s",
3044 Py_TYPE(item)->tp_name);
3045 return -1;
3046 }
3047 Py_CLEAR(*module_name);
3048 Py_INCREF(item);
3049 *module_name = item;
3050 }
3051 else if (PyErr_Occurred()) {
3052 return -1;
3053 }
3054
3055 return 0;
3056}
3057
3058static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003059save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3060{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003061 PyObject *global_name = NULL;
3062 PyObject *module_name = NULL;
3063 PyObject *module = NULL;
3064 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003065 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003066 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003067 _Py_IDENTIFIER(__name__);
3068 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003069
3070 const char global_op = GLOBAL;
3071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003072 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003073 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003074 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003075 }
3076 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003077 if (self->proto >= 4) {
3078 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3079 if (global_name == NULL) {
3080 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3081 goto error;
3082 PyErr_Clear();
3083 }
3084 }
3085 if (global_name == NULL) {
3086 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3087 if (global_name == NULL)
3088 goto error;
3089 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003090 }
3091
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003092 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003093 if (module_name == NULL)
3094 goto error;
3095
3096 /* XXX: Change to use the import C API directly with level=0 to disallow
3097 relative imports.
3098
3099 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3100 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3101 custom import functions (IMHO, this would be a nice security
3102 feature). The import C API would need to be extended to support the
3103 extra parameters of __import__ to fix that. */
3104 module = PyImport_Import(module_name);
3105 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003106 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003107 "Can't pickle %R: import of module %R failed",
3108 obj, module_name);
3109 goto error;
3110 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003111 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003112 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003113 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003114 "Can't pickle %R: attribute lookup %S on %S failed",
3115 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003116 goto error;
3117 }
3118 if (cls != obj) {
3119 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003120 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003121 "Can't pickle %R: it's not the same object as %S.%S",
3122 obj, module_name, global_name);
3123 goto error;
3124 }
3125 Py_DECREF(cls);
3126
3127 if (self->proto >= 2) {
3128 /* See whether this is in the extension registry, and if
3129 * so generate an EXT opcode.
3130 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003131 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003132 PyObject *code_obj; /* extension code as Python object */
3133 long code; /* extension code as C value */
3134 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003135 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003137 extension_key = PyTuple_Pack(2, module_name, global_name);
3138 if (extension_key == NULL) {
3139 goto error;
3140 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003141 code_obj = PyDict_GetItemWithError(st->extension_registry,
3142 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003143 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003144 /* The object is not registered in the extension registry.
3145 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003146 if (code_obj == NULL) {
3147 if (PyErr_Occurred()) {
3148 goto error;
3149 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003150 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003151 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003152
3153 /* XXX: pickle.py doesn't check neither the type, nor the range
3154 of the value returned by the extension_registry. It should for
3155 consistency. */
3156
3157 /* Verify code_obj has the right type and value. */
3158 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003159 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003160 "Can't pickle %R: extension code %R isn't an integer",
3161 obj, code_obj);
3162 goto error;
3163 }
3164 code = PyLong_AS_LONG(code_obj);
3165 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003166 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003167 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3168 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169 goto error;
3170 }
3171
3172 /* Generate an EXT opcode. */
3173 if (code <= 0xff) {
3174 pdata[0] = EXT1;
3175 pdata[1] = (unsigned char)code;
3176 n = 2;
3177 }
3178 else if (code <= 0xffff) {
3179 pdata[0] = EXT2;
3180 pdata[1] = (unsigned char)(code & 0xff);
3181 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3182 n = 3;
3183 }
3184 else {
3185 pdata[0] = EXT4;
3186 pdata[1] = (unsigned char)(code & 0xff);
3187 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3188 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3189 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3190 n = 5;
3191 }
3192
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003193 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194 goto error;
3195 }
3196 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003198 if (self->proto >= 4) {
3199 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003200
Christian Heimese8b1ba12013-11-23 21:13:39 +01003201 if (save(self, module_name, 0) < 0)
3202 goto error;
3203 if (save(self, global_name, 0) < 0)
3204 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003205
3206 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3207 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003208 }
3209 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003210 /* Generate a normal global opcode if we are using a pickle
3211 protocol < 4, or if the object is not registered in the
3212 extension registry. */
3213 PyObject *encoded;
3214 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003216 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003217 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003218
3219 /* For protocol < 3 and if the user didn't request against doing
3220 so, we convert module names to the old 2.x module names. */
3221 if (self->proto < 3 && self->fix_imports) {
3222 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003223 goto error;
3224 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003225 }
3226
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003227 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3228 both the module name and the global name using UTF-8. We do so
3229 only when we are using the pickle protocol newer than version
3230 3. This is to ensure compatibility with older Unpickler running
3231 on Python 2.x. */
3232 if (self->proto == 3) {
3233 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003234 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003235 else {
3236 unicode_encoder = PyUnicode_AsASCIIString;
3237 }
3238 encoded = unicode_encoder(module_name);
3239 if (encoded == NULL) {
3240 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003241 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003242 "can't pickle module identifier '%S' using "
3243 "pickle protocol %i",
3244 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003245 goto error;
3246 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003247 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3248 PyBytes_GET_SIZE(encoded)) < 0) {
3249 Py_DECREF(encoded);
3250 goto error;
3251 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003252 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003253 if(_Pickler_Write(self, "\n", 1) < 0)
3254 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003255
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003256 /* Save the name of the module. */
3257 encoded = unicode_encoder(global_name);
3258 if (encoded == NULL) {
3259 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003260 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003261 "can't pickle global identifier '%S' using "
3262 "pickle protocol %i",
3263 global_name, self->proto);
3264 goto error;
3265 }
3266 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3267 PyBytes_GET_SIZE(encoded)) < 0) {
3268 Py_DECREF(encoded);
3269 goto error;
3270 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003272 if (_Pickler_Write(self, "\n", 1) < 0)
3273 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003275 /* Memoize the object. */
3276 if (memo_put(self, obj) < 0)
3277 goto error;
3278 }
3279
3280 if (0) {
3281 error:
3282 status = -1;
3283 }
3284 Py_XDECREF(module_name);
3285 Py_XDECREF(global_name);
3286 Py_XDECREF(module);
3287
3288 return status;
3289}
3290
3291static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003292save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3293{
3294 PyObject *reduce_value;
3295 int status;
3296
3297 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3298 if (reduce_value == NULL) {
3299 return -1;
3300 }
3301 status = save_reduce(self, reduce_value, obj);
3302 Py_DECREF(reduce_value);
3303 return status;
3304}
3305
3306static int
3307save_type(PicklerObject *self, PyObject *obj)
3308{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003309 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003310 return save_singleton_type(self, obj, Py_None);
3311 }
3312 else if (obj == (PyObject *)&PyEllipsis_Type) {
3313 return save_singleton_type(self, obj, Py_Ellipsis);
3314 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003315 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003316 return save_singleton_type(self, obj, Py_NotImplemented);
3317 }
3318 return save_global(self, obj, NULL);
3319}
3320
3321static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003322save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3323{
3324 PyObject *pid = NULL;
3325 int status = 0;
3326
3327 const char persid_op = PERSID;
3328 const char binpersid_op = BINPERSID;
3329
3330 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003331 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003332 if (pid == NULL)
3333 return -1;
3334
3335 if (pid != Py_None) {
3336 if (self->bin) {
3337 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003338 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003339 goto error;
3340 }
3341 else {
3342 PyObject *pid_str = NULL;
3343 char *pid_ascii_bytes;
3344 Py_ssize_t size;
3345
3346 pid_str = PyObject_Str(pid);
3347 if (pid_str == NULL)
3348 goto error;
3349
3350 /* XXX: Should it check whether the persistent id only contains
3351 ASCII characters? And what if the pid contains embedded
3352 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003353 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 Py_DECREF(pid_str);
3355 if (pid_ascii_bytes == NULL)
3356 goto error;
3357
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003358 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3359 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3360 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003361 goto error;
3362 }
3363 status = 1;
3364 }
3365
3366 if (0) {
3367 error:
3368 status = -1;
3369 }
3370 Py_XDECREF(pid);
3371
3372 return status;
3373}
3374
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003375static PyObject *
3376get_class(PyObject *obj)
3377{
3378 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003379 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003380
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003381 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003382 if (cls == NULL) {
3383 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3384 PyErr_Clear();
3385 cls = (PyObject *) Py_TYPE(obj);
3386 Py_INCREF(cls);
3387 }
3388 }
3389 return cls;
3390}
3391
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003392/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3393 * appropriate __reduce__ method for obj.
3394 */
3395static int
3396save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3397{
3398 PyObject *callable;
3399 PyObject *argtup;
3400 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003401 PyObject *listitems = Py_None;
3402 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003403 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003404 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003405 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003406
3407 const char reduce_op = REDUCE;
3408 const char build_op = BUILD;
3409 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003410 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003411
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003412 size = PyTuple_Size(args);
3413 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003414 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003415 "__reduce__ must contain 2 through 5 elements");
3416 return -1;
3417 }
3418
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3420 &callable, &argtup, &state, &listitems, &dictitems))
3421 return -1;
3422
3423 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003424 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003425 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003426 return -1;
3427 }
3428 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003429 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003430 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431 return -1;
3432 }
3433
3434 if (state == Py_None)
3435 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003436
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003437 if (listitems == Py_None)
3438 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003439 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003440 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003441 "returned by __reduce__ must be an iterator, not %s",
3442 Py_TYPE(listitems)->tp_name);
3443 return -1;
3444 }
3445
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003446 if (dictitems == Py_None)
3447 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003448 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003449 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003450 "returned by __reduce__ must be an iterator, not %s",
3451 Py_TYPE(dictitems)->tp_name);
3452 return -1;
3453 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003454
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003455 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003456 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003457 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458
Victor Stinner804e05e2013-11-14 01:26:17 +01003459 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003460 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003461 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003462 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003463 }
3464 PyErr_Clear();
3465 }
3466 else if (self->proto >= 4) {
3467 _Py_IDENTIFIER(__newobj_ex__);
3468 use_newobj_ex = PyUnicode_Check(name) &&
3469 PyUnicode_Compare(
3470 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3471 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003472 }
3473 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003475 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 PyUnicode_Compare(
3477 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003478 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479 }
3480 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003481
3482 if (use_newobj_ex) {
3483 PyObject *cls;
3484 PyObject *args;
3485 PyObject *kwargs;
3486
3487 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003488 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003489 "length of the NEWOBJ_EX argument tuple must be "
3490 "exactly 3, not %zd", Py_SIZE(argtup));
3491 return -1;
3492 }
3493
3494 cls = PyTuple_GET_ITEM(argtup, 0);
3495 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003496 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 "first item from NEWOBJ_EX argument tuple must "
3498 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3499 return -1;
3500 }
3501 args = PyTuple_GET_ITEM(argtup, 1);
3502 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003503 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 "second item from NEWOBJ_EX argument tuple must "
3505 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3506 return -1;
3507 }
3508 kwargs = PyTuple_GET_ITEM(argtup, 2);
3509 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003510 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003511 "third item from NEWOBJ_EX argument tuple must "
3512 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3513 return -1;
3514 }
3515
3516 if (save(self, cls, 0) < 0 ||
3517 save(self, args, 0) < 0 ||
3518 save(self, kwargs, 0) < 0 ||
3519 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3520 return -1;
3521 }
3522 }
3523 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 PyObject *cls;
3525 PyObject *newargtup;
3526 PyObject *obj_class;
3527 int p;
3528
3529 /* Sanity checks. */
3530 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003531 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 return -1;
3533 }
3534
3535 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003536 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003537 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003538 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539 return -1;
3540 }
3541
3542 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003543 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 p = obj_class != cls; /* true iff a problem */
3545 Py_DECREF(obj_class);
3546 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003547 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548 "__newobj__ args has the wrong class");
3549 return -1;
3550 }
3551 }
3552 /* XXX: These calls save() are prone to infinite recursion. Imagine
3553 what happen if the value returned by the __reduce__() method of
3554 some extension type contains another object of the same type. Ouch!
3555
3556 Here is a quick example, that I ran into, to illustrate what I
3557 mean:
3558
3559 >>> import pickle, copyreg
3560 >>> copyreg.dispatch_table.pop(complex)
3561 >>> pickle.dumps(1+2j)
3562 Traceback (most recent call last):
3563 ...
3564 RuntimeError: maximum recursion depth exceeded
3565
3566 Removing the complex class from copyreg.dispatch_table made the
3567 __reduce_ex__() method emit another complex object:
3568
3569 >>> (1+1j).__reduce_ex__(2)
3570 (<function __newobj__ at 0xb7b71c3c>,
3571 (<class 'complex'>, (1+1j)), None, None, None)
3572
3573 Thus when save() was called on newargstup (the 2nd item) recursion
3574 ensued. Of course, the bug was in the complex class which had a
3575 broken __getnewargs__() that emitted another complex object. But,
3576 the point, here, is it is quite easy to end up with a broken reduce
3577 function. */
3578
3579 /* Save the class and its __new__ arguments. */
3580 if (save(self, cls, 0) < 0)
3581 return -1;
3582
3583 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3584 if (newargtup == NULL)
3585 return -1;
3586
3587 p = save(self, newargtup, 0);
3588 Py_DECREF(newargtup);
3589 if (p < 0)
3590 return -1;
3591
3592 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003593 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003594 return -1;
3595 }
3596 else { /* Not using NEWOBJ. */
3597 if (save(self, callable, 0) < 0 ||
3598 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003599 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600 return -1;
3601 }
3602
3603 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3604 the caller do not want to memoize the object. Not particularly useful,
3605 but that is to mimic the behavior save_reduce() in pickle.py when
3606 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003607 if (obj != NULL) {
3608 /* If the object is already in the memo, this means it is
3609 recursive. In this case, throw away everything we put on the
3610 stack, and fetch the object back from the memo. */
3611 if (PyMemoTable_Get(self->memo, obj)) {
3612 const char pop_op = POP;
3613
3614 if (_Pickler_Write(self, &pop_op, 1) < 0)
3615 return -1;
3616 if (memo_get(self, obj) < 0)
3617 return -1;
3618
3619 return 0;
3620 }
3621 else if (memo_put(self, obj) < 0)
3622 return -1;
3623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003624
3625 if (listitems && batch_list(self, listitems) < 0)
3626 return -1;
3627
3628 if (dictitems && batch_dict(self, dictitems) < 0)
3629 return -1;
3630
3631 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003632 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003633 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003634 return -1;
3635 }
3636
3637 return 0;
3638}
3639
3640static int
3641save(PicklerObject *self, PyObject *obj, int pers_save)
3642{
3643 PyTypeObject *type;
3644 PyObject *reduce_func = NULL;
3645 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003646 int status = 0;
3647
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003648 if (_Pickler_OpcodeBoundary(self) < 0)
3649 return -1;
3650
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003651 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003652 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003653
3654 /* The extra pers_save argument is necessary to avoid calling save_pers()
3655 on its returned object. */
3656 if (!pers_save && self->pers_func) {
3657 /* save_pers() returns:
3658 -1 to signal an error;
3659 0 if it did nothing successfully;
3660 1 if a persistent id was saved.
3661 */
3662 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3663 goto done;
3664 }
3665
3666 type = Py_TYPE(obj);
3667
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003668 /* The old cPickle had an optimization that used switch-case statement
3669 dispatching on the first letter of the type name. This has was removed
3670 since benchmarks shown that this optimization was actually slowing
3671 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003672
3673 /* Atom types; these aren't memoized, so don't check the memo. */
3674
3675 if (obj == Py_None) {
3676 status = save_none(self, obj);
3677 goto done;
3678 }
3679 else if (obj == Py_False || obj == Py_True) {
3680 status = save_bool(self, obj);
3681 goto done;
3682 }
3683 else if (type == &PyLong_Type) {
3684 status = save_long(self, obj);
3685 goto done;
3686 }
3687 else if (type == &PyFloat_Type) {
3688 status = save_float(self, obj);
3689 goto done;
3690 }
3691
3692 /* Check the memo to see if it has the object. If so, generate
3693 a GET (or BINGET) opcode, instead of pickling the object
3694 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003695 if (PyMemoTable_Get(self->memo, obj)) {
3696 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003697 goto error;
3698 goto done;
3699 }
3700
3701 if (type == &PyBytes_Type) {
3702 status = save_bytes(self, obj);
3703 goto done;
3704 }
3705 else if (type == &PyUnicode_Type) {
3706 status = save_unicode(self, obj);
3707 goto done;
3708 }
3709 else if (type == &PyDict_Type) {
3710 status = save_dict(self, obj);
3711 goto done;
3712 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003713 else if (type == &PySet_Type) {
3714 status = save_set(self, obj);
3715 goto done;
3716 }
3717 else if (type == &PyFrozenSet_Type) {
3718 status = save_frozenset(self, obj);
3719 goto done;
3720 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003721 else if (type == &PyList_Type) {
3722 status = save_list(self, obj);
3723 goto done;
3724 }
3725 else if (type == &PyTuple_Type) {
3726 status = save_tuple(self, obj);
3727 goto done;
3728 }
3729 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003730 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003731 goto done;
3732 }
3733 else if (type == &PyFunction_Type) {
3734 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003735 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003736 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003737
3738 /* XXX: This part needs some unit tests. */
3739
3740 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003741 * self.dispatch_table, copyreg.dispatch_table, the object's
3742 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003743 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003744 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003745 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003746 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3747 (PyObject *)type);
3748 if (reduce_func == NULL) {
3749 if (PyErr_Occurred()) {
3750 goto error;
3751 }
3752 } else {
3753 /* PyDict_GetItemWithError() returns a borrowed reference.
3754 Increase the reference count to be consistent with
3755 PyObject_GetItem and _PyObject_GetAttrId used below. */
3756 Py_INCREF(reduce_func);
3757 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003758 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003759 reduce_func = PyObject_GetItem(self->dispatch_table,
3760 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003761 if (reduce_func == NULL) {
3762 if (PyErr_ExceptionMatches(PyExc_KeyError))
3763 PyErr_Clear();
3764 else
3765 goto error;
3766 }
3767 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003769 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003770 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003771 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003772 else if (PyType_IsSubtype(type, &PyType_Type)) {
3773 status = save_global(self, obj, NULL);
3774 goto done;
3775 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003776 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003777 _Py_IDENTIFIER(__reduce__);
3778 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003779
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003780
3781 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3782 automatically defined as __reduce__. While this is convenient, this
3783 make it impossible to know which method was actually called. Of
3784 course, this is not a big deal. But still, it would be nice to let
3785 the user know which method was called when something go
3786 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3787 don't actually have to check for a __reduce__ method. */
3788
3789 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003790 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003791 if (reduce_func != NULL) {
3792 PyObject *proto;
3793 proto = PyLong_FromLong(self->proto);
3794 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003795 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796 }
3797 }
3798 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003799 PickleState *st = _Pickle_GetGlobalState();
3800
3801 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003803 }
3804 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003805 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003806 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003807 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003808 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003809 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003810 PyObject *empty_tuple = PyTuple_New(0);
3811 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003812 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003813 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003814 }
3815 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003816 PyErr_Format(st->PicklingError,
3817 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003818 type->tp_name, obj);
3819 goto error;
3820 }
3821 }
3822 }
3823
3824 if (reduce_value == NULL)
3825 goto error;
3826
3827 if (PyUnicode_Check(reduce_value)) {
3828 status = save_global(self, obj, reduce_value);
3829 goto done;
3830 }
3831
3832 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003833 PickleState *st = _Pickle_GetGlobalState();
3834 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 "__reduce__ must return a string or tuple");
3836 goto error;
3837 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003838
3839 status = save_reduce(self, reduce_value, obj);
3840
3841 if (0) {
3842 error:
3843 status = -1;
3844 }
3845 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003846
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003847 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003848 Py_XDECREF(reduce_func);
3849 Py_XDECREF(reduce_value);
3850
3851 return status;
3852}
3853
3854static int
3855dump(PicklerObject *self, PyObject *obj)
3856{
3857 const char stop_op = STOP;
3858
3859 if (self->proto >= 2) {
3860 char header[2];
3861
3862 header[0] = PROTO;
3863 assert(self->proto >= 0 && self->proto < 256);
3864 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003865 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003867 if (self->proto >= 4)
3868 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 }
3870
3871 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003872 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003873 return -1;
3874
3875 return 0;
3876}
3877
Larry Hastings61272b72014-01-07 12:41:53 -08003878/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003879
3880_pickle.Pickler.clear_memo
3881
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003882Clears the pickler's "memo".
3883
3884The memo is the data structure that remembers which objects the
3885pickler has already seen, so that shared or recursive objects are
3886pickled by reference and not by value. This method is useful when
3887re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003888[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003889
Larry Hastings3cceb382014-01-04 11:09:09 -08003890static PyObject *
3891_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003892/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893{
3894 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003895 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896
3897 Py_RETURN_NONE;
3898}
3899
Larry Hastings61272b72014-01-07 12:41:53 -08003900/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003901
3902_pickle.Pickler.dump
3903
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003904 obj: object
3905 /
3906
3907Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003908[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003909
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003911_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003912/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003914 /* Check whether the Pickler was initialized correctly (issue3664).
3915 Developers often forget to call __init__() in their subclasses, which
3916 would trigger a segfault without this check. */
3917 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003918 PickleState *st = _Pickle_GetGlobalState();
3919 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003920 "Pickler.__init__() was not called by %s.__init__()",
3921 Py_TYPE(self)->tp_name);
3922 return NULL;
3923 }
3924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003925 if (_Pickler_ClearBuffer(self) < 0)
3926 return NULL;
3927
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928 if (dump(self, obj) < 0)
3929 return NULL;
3930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003931 if (_Pickler_FlushToFile(self) < 0)
3932 return NULL;
3933
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 Py_RETURN_NONE;
3935}
3936
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003937/*[clinic input]
3938
3939_pickle.Pickler.__sizeof__ -> Py_ssize_t
3940
3941Returns size in memory, in bytes.
3942[clinic start generated code]*/
3943
3944static Py_ssize_t
3945_pickle_Pickler___sizeof___impl(PicklerObject *self)
3946/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
3947{
3948 Py_ssize_t res, s;
3949
3950 res = sizeof(PicklerObject);
3951 if (self->memo != NULL) {
3952 res += sizeof(PyMemoTable);
3953 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
3954 }
3955 if (self->output_buffer != NULL) {
3956 s = _PySys_GetSizeOf(self->output_buffer);
3957 if (s == -1)
3958 return -1;
3959 res += s;
3960 }
3961 return res;
3962}
3963
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003965 _PICKLE_PICKLER_DUMP_METHODDEF
3966 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003967 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968 {NULL, NULL} /* sentinel */
3969};
3970
3971static void
3972Pickler_dealloc(PicklerObject *self)
3973{
3974 PyObject_GC_UnTrack(self);
3975
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003976 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003979 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003980 Py_XDECREF(self->fast_memo);
3981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003982 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003983
3984 Py_TYPE(self)->tp_free((PyObject *)self);
3985}
3986
3987static int
3988Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3989{
3990 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003991 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003992 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 Py_VISIT(self->fast_memo);
3994 return 0;
3995}
3996
3997static int
3998Pickler_clear(PicklerObject *self)
3999{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004000 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004001 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004002 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004003 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 Py_CLEAR(self->fast_memo);
4005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004006 if (self->memo != NULL) {
4007 PyMemoTable *memo = self->memo;
4008 self->memo = NULL;
4009 PyMemoTable_Del(memo);
4010 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004011 return 0;
4012}
4013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004014
Larry Hastings61272b72014-01-07 12:41:53 -08004015/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004016
4017_pickle.Pickler.__init__
4018
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004019 file: object
4020 protocol: object = NULL
4021 fix_imports: bool = True
4022
4023This takes a binary file for writing a pickle data stream.
4024
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004025The optional *protocol* argument tells the pickler to use the given
4026protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4027protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004028
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004029Specifying a negative protocol version selects the highest protocol
4030version supported. The higher the protocol used, the more recent the
4031version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004032
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004033The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004034bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004035writing, a io.BytesIO instance, or any other custom object that meets
4036this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004038If *fix_imports* is True and protocol is less than 3, pickle will try
4039to map the new Python 3 names to the old module names used in Python
40402, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004041[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004042
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004043static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004044_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08004045/*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004046{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004047 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004048 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004049
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004050 /* In case of multiple __init__() calls, clear previous content. */
4051 if (self->write != NULL)
4052 (void)Pickler_clear(self);
4053
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004054 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004055 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004056
4057 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004058 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004059
4060 /* memo and output_buffer may have already been created in _Pickler_New */
4061 if (self->memo == NULL) {
4062 self->memo = PyMemoTable_New();
4063 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004064 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004065 }
4066 self->output_len = 0;
4067 if (self->output_buffer == NULL) {
4068 self->max_output_len = WRITE_BUF_SIZE;
4069 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4070 self->max_output_len);
4071 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004072 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004073 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004074
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004075 self->fast = 0;
4076 self->fast_nesting = 0;
4077 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004078 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004079 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4080 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4081 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004082 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004083 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004084 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004085 self->dispatch_table = NULL;
4086 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4087 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4088 &PyId_dispatch_table);
4089 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004090 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004091 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004093 return 0;
4094}
4095
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004096
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004097/* Define a proxy object for the Pickler's internal memo object. This is to
4098 * avoid breaking code like:
4099 * pickler.memo.clear()
4100 * and
4101 * pickler.memo = saved_memo
4102 * Is this a good idea? Not really, but we don't want to break code that uses
4103 * it. Note that we don't implement the entire mapping API here. This is
4104 * intentional, as these should be treated as black-box implementation details.
4105 */
4106
Larry Hastings61272b72014-01-07 12:41:53 -08004107/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004108_pickle.PicklerMemoProxy.clear
4109
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004110Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004111[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004112
Larry Hastings3cceb382014-01-04 11:09:09 -08004113static PyObject *
4114_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004115/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004116{
4117 if (self->pickler->memo)
4118 PyMemoTable_Clear(self->pickler->memo);
4119 Py_RETURN_NONE;
4120}
4121
Larry Hastings61272b72014-01-07 12:41:53 -08004122/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004123_pickle.PicklerMemoProxy.copy
4124
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004125Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004126[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004127
Larry Hastings3cceb382014-01-04 11:09:09 -08004128static PyObject *
4129_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004130/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131{
4132 Py_ssize_t i;
4133 PyMemoTable *memo;
4134 PyObject *new_memo = PyDict_New();
4135 if (new_memo == NULL)
4136 return NULL;
4137
4138 memo = self->pickler->memo;
4139 for (i = 0; i < memo->mt_allocated; ++i) {
4140 PyMemoEntry entry = memo->mt_table[i];
4141 if (entry.me_key != NULL) {
4142 int status;
4143 PyObject *key, *value;
4144
4145 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004146 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004147
4148 if (key == NULL || value == NULL) {
4149 Py_XDECREF(key);
4150 Py_XDECREF(value);
4151 goto error;
4152 }
4153 status = PyDict_SetItem(new_memo, key, value);
4154 Py_DECREF(key);
4155 Py_DECREF(value);
4156 if (status < 0)
4157 goto error;
4158 }
4159 }
4160 return new_memo;
4161
4162 error:
4163 Py_XDECREF(new_memo);
4164 return NULL;
4165}
4166
Larry Hastings61272b72014-01-07 12:41:53 -08004167/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168_pickle.PicklerMemoProxy.__reduce__
4169
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004171[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172
Larry Hastings3cceb382014-01-04 11:09:09 -08004173static PyObject *
4174_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004175/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004176{
4177 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004178 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004179 if (contents == NULL)
4180 return NULL;
4181
4182 reduce_value = PyTuple_New(2);
4183 if (reduce_value == NULL) {
4184 Py_DECREF(contents);
4185 return NULL;
4186 }
4187 dict_args = PyTuple_New(1);
4188 if (dict_args == NULL) {
4189 Py_DECREF(contents);
4190 Py_DECREF(reduce_value);
4191 return NULL;
4192 }
4193 PyTuple_SET_ITEM(dict_args, 0, contents);
4194 Py_INCREF((PyObject *)&PyDict_Type);
4195 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4196 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4197 return reduce_value;
4198}
4199
4200static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004201 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4202 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4203 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004204 {NULL, NULL} /* sentinel */
4205};
4206
4207static void
4208PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4209{
4210 PyObject_GC_UnTrack(self);
4211 Py_XDECREF(self->pickler);
4212 PyObject_GC_Del((PyObject *)self);
4213}
4214
4215static int
4216PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4217 visitproc visit, void *arg)
4218{
4219 Py_VISIT(self->pickler);
4220 return 0;
4221}
4222
4223static int
4224PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4225{
4226 Py_CLEAR(self->pickler);
4227 return 0;
4228}
4229
4230static PyTypeObject PicklerMemoProxyType = {
4231 PyVarObject_HEAD_INIT(NULL, 0)
4232 "_pickle.PicklerMemoProxy", /*tp_name*/
4233 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4234 0,
4235 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4236 0, /* tp_print */
4237 0, /* tp_getattr */
4238 0, /* tp_setattr */
4239 0, /* tp_compare */
4240 0, /* tp_repr */
4241 0, /* tp_as_number */
4242 0, /* tp_as_sequence */
4243 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004244 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004245 0, /* tp_call */
4246 0, /* tp_str */
4247 PyObject_GenericGetAttr, /* tp_getattro */
4248 PyObject_GenericSetAttr, /* tp_setattro */
4249 0, /* tp_as_buffer */
4250 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4251 0, /* tp_doc */
4252 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4253 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4254 0, /* tp_richcompare */
4255 0, /* tp_weaklistoffset */
4256 0, /* tp_iter */
4257 0, /* tp_iternext */
4258 picklerproxy_methods, /* tp_methods */
4259};
4260
4261static PyObject *
4262PicklerMemoProxy_New(PicklerObject *pickler)
4263{
4264 PicklerMemoProxyObject *self;
4265
4266 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4267 if (self == NULL)
4268 return NULL;
4269 Py_INCREF(pickler);
4270 self->pickler = pickler;
4271 PyObject_GC_Track(self);
4272 return (PyObject *)self;
4273}
4274
4275/*****************************************************************************/
4276
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004277static PyObject *
4278Pickler_get_memo(PicklerObject *self)
4279{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004280 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004281}
4282
4283static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004284Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004285{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004288 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004289 PyErr_SetString(PyExc_TypeError,
4290 "attribute deletion is not supported");
4291 return -1;
4292 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004293
4294 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4295 PicklerObject *pickler =
4296 ((PicklerMemoProxyObject *)obj)->pickler;
4297
4298 new_memo = PyMemoTable_Copy(pickler->memo);
4299 if (new_memo == NULL)
4300 return -1;
4301 }
4302 else if (PyDict_Check(obj)) {
4303 Py_ssize_t i = 0;
4304 PyObject *key, *value;
4305
4306 new_memo = PyMemoTable_New();
4307 if (new_memo == NULL)
4308 return -1;
4309
4310 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004311 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004312 PyObject *memo_obj;
4313
4314 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4315 PyErr_SetString(PyExc_TypeError,
4316 "'memo' values must be 2-item tuples");
4317 goto error;
4318 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004319 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004320 if (memo_id == -1 && PyErr_Occurred())
4321 goto error;
4322 memo_obj = PyTuple_GET_ITEM(value, 1);
4323 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4324 goto error;
4325 }
4326 }
4327 else {
4328 PyErr_Format(PyExc_TypeError,
4329 "'memo' attribute must be an PicklerMemoProxy object"
4330 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004331 return -1;
4332 }
4333
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004334 PyMemoTable_Del(self->memo);
4335 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004336
4337 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004338
4339 error:
4340 if (new_memo)
4341 PyMemoTable_Del(new_memo);
4342 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343}
4344
4345static PyObject *
4346Pickler_get_persid(PicklerObject *self)
4347{
4348 if (self->pers_func == NULL)
4349 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4350 else
4351 Py_INCREF(self->pers_func);
4352 return self->pers_func;
4353}
4354
4355static int
4356Pickler_set_persid(PicklerObject *self, PyObject *value)
4357{
4358 PyObject *tmp;
4359
4360 if (value == NULL) {
4361 PyErr_SetString(PyExc_TypeError,
4362 "attribute deletion is not supported");
4363 return -1;
4364 }
4365 if (!PyCallable_Check(value)) {
4366 PyErr_SetString(PyExc_TypeError,
4367 "persistent_id must be a callable taking one argument");
4368 return -1;
4369 }
4370
4371 tmp = self->pers_func;
4372 Py_INCREF(value);
4373 self->pers_func = value;
4374 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4375
4376 return 0;
4377}
4378
4379static PyMemberDef Pickler_members[] = {
4380 {"bin", T_INT, offsetof(PicklerObject, bin)},
4381 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004382 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004383 {NULL}
4384};
4385
4386static PyGetSetDef Pickler_getsets[] = {
4387 {"memo", (getter)Pickler_get_memo,
4388 (setter)Pickler_set_memo},
4389 {"persistent_id", (getter)Pickler_get_persid,
4390 (setter)Pickler_set_persid},
4391 {NULL}
4392};
4393
4394static PyTypeObject Pickler_Type = {
4395 PyVarObject_HEAD_INIT(NULL, 0)
4396 "_pickle.Pickler" , /*tp_name*/
4397 sizeof(PicklerObject), /*tp_basicsize*/
4398 0, /*tp_itemsize*/
4399 (destructor)Pickler_dealloc, /*tp_dealloc*/
4400 0, /*tp_print*/
4401 0, /*tp_getattr*/
4402 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004403 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004404 0, /*tp_repr*/
4405 0, /*tp_as_number*/
4406 0, /*tp_as_sequence*/
4407 0, /*tp_as_mapping*/
4408 0, /*tp_hash*/
4409 0, /*tp_call*/
4410 0, /*tp_str*/
4411 0, /*tp_getattro*/
4412 0, /*tp_setattro*/
4413 0, /*tp_as_buffer*/
4414 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004415 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416 (traverseproc)Pickler_traverse, /*tp_traverse*/
4417 (inquiry)Pickler_clear, /*tp_clear*/
4418 0, /*tp_richcompare*/
4419 0, /*tp_weaklistoffset*/
4420 0, /*tp_iter*/
4421 0, /*tp_iternext*/
4422 Pickler_methods, /*tp_methods*/
4423 Pickler_members, /*tp_members*/
4424 Pickler_getsets, /*tp_getset*/
4425 0, /*tp_base*/
4426 0, /*tp_dict*/
4427 0, /*tp_descr_get*/
4428 0, /*tp_descr_set*/
4429 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004430 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004431 PyType_GenericAlloc, /*tp_alloc*/
4432 PyType_GenericNew, /*tp_new*/
4433 PyObject_GC_Del, /*tp_free*/
4434 0, /*tp_is_gc*/
4435};
4436
Victor Stinner121aab42011-09-29 23:40:53 +02004437/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004438
4439 XXX: It would be nice to able to avoid Python function call overhead, by
4440 using directly the C version of find_class(), when find_class() is not
4441 overridden by a subclass. Although, this could become rather hackish. A
4442 simpler optimization would be to call the C function when self is not a
4443 subclass instance. */
4444static PyObject *
4445find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4446{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004447 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004448
4449 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4450 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004451}
4452
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004453static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004454marker(UnpicklerObject *self)
4455{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004456 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004457 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004458 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004459 return -1;
4460 }
4461
4462 return self->marks[--self->num_marks];
4463}
4464
4465static int
4466load_none(UnpicklerObject *self)
4467{
4468 PDATA_APPEND(self->stack, Py_None, -1);
4469 return 0;
4470}
4471
4472static int
4473bad_readline(void)
4474{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004475 PickleState *st = _Pickle_GetGlobalState();
4476 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004477 return -1;
4478}
4479
4480static int
4481load_int(UnpicklerObject *self)
4482{
4483 PyObject *value;
4484 char *endptr, *s;
4485 Py_ssize_t len;
4486 long x;
4487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004488 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004489 return -1;
4490 if (len < 2)
4491 return bad_readline();
4492
4493 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004494 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004495 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004496 x = strtol(s, &endptr, 0);
4497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004498 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004499 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004500 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004501 errno = 0;
4502 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004503 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004504 if (value == NULL) {
4505 PyErr_SetString(PyExc_ValueError,
4506 "could not convert string to int");
4507 return -1;
4508 }
4509 }
4510 else {
4511 if (len == 3 && (x == 0 || x == 1)) {
4512 if ((value = PyBool_FromLong(x)) == NULL)
4513 return -1;
4514 }
4515 else {
4516 if ((value = PyLong_FromLong(x)) == NULL)
4517 return -1;
4518 }
4519 }
4520
4521 PDATA_PUSH(self->stack, value, -1);
4522 return 0;
4523}
4524
4525static int
4526load_bool(UnpicklerObject *self, PyObject *boolean)
4527{
4528 assert(boolean == Py_True || boolean == Py_False);
4529 PDATA_APPEND(self->stack, boolean, -1);
4530 return 0;
4531}
4532
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004533/* s contains x bytes of an unsigned little-endian integer. Return its value
4534 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4535 */
4536static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004537calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004538{
4539 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004540 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004541 size_t x = 0;
4542
Serhiy Storchakae0606192015-09-29 22:10:07 +03004543 if (nbytes > (int)sizeof(size_t)) {
4544 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4545 * have 64-bit size that can't be represented on 32-bit platform.
4546 */
4547 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4548 if (s[i])
4549 return -1;
4550 }
4551 nbytes = (int)sizeof(size_t);
4552 }
4553 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004554 x |= (size_t) s[i] << (8 * i);
4555 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004556
4557 if (x > PY_SSIZE_T_MAX)
4558 return -1;
4559 else
4560 return (Py_ssize_t) x;
4561}
4562
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563/* s contains x bytes of a little-endian integer. Return its value as a
4564 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4565 * int, but when x is 4 it's a signed one. This is an historical source
4566 * of x-platform bugs.
4567 */
4568static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004569calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570{
4571 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004572 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573 long x = 0;
4574
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004575 for (i = 0; i < nbytes; i++) {
4576 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577 }
4578
4579 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4580 * is signed, so on a box with longs bigger than 4 bytes we need
4581 * to extend a BININT's sign bit to the full width.
4582 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004583 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004584 x |= -(x & (1L << 31));
4585 }
4586
4587 return x;
4588}
4589
4590static int
4591load_binintx(UnpicklerObject *self, char *s, int size)
4592{
4593 PyObject *value;
4594 long x;
4595
4596 x = calc_binint(s, size);
4597
4598 if ((value = PyLong_FromLong(x)) == NULL)
4599 return -1;
4600
4601 PDATA_PUSH(self->stack, value, -1);
4602 return 0;
4603}
4604
4605static int
4606load_binint(UnpicklerObject *self)
4607{
4608 char *s;
4609
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004610 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004611 return -1;
4612
4613 return load_binintx(self, s, 4);
4614}
4615
4616static int
4617load_binint1(UnpicklerObject *self)
4618{
4619 char *s;
4620
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004621 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622 return -1;
4623
4624 return load_binintx(self, s, 1);
4625}
4626
4627static int
4628load_binint2(UnpicklerObject *self)
4629{
4630 char *s;
4631
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004632 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633 return -1;
4634
4635 return load_binintx(self, s, 2);
4636}
4637
4638static int
4639load_long(UnpicklerObject *self)
4640{
4641 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004642 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004643 Py_ssize_t len;
4644
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004645 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004646 return -1;
4647 if (len < 2)
4648 return bad_readline();
4649
Mark Dickinson8dd05142009-01-20 20:43:58 +00004650 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4651 the 'L' before calling PyLong_FromString. In order to maintain
4652 compatibility with Python 3.0.0, we don't actually *require*
4653 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004654 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004655 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004656 /* XXX: Should the base argument explicitly set to 10? */
4657 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004658 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004659 return -1;
4660
4661 PDATA_PUSH(self->stack, value, -1);
4662 return 0;
4663}
4664
4665/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4666 * data following.
4667 */
4668static int
4669load_counted_long(UnpicklerObject *self, int size)
4670{
4671 PyObject *value;
4672 char *nbytes;
4673 char *pdata;
4674
4675 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004676 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004677 return -1;
4678
4679 size = calc_binint(nbytes, size);
4680 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004681 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004682 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004683 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004684 "LONG pickle has negative byte count");
4685 return -1;
4686 }
4687
4688 if (size == 0)
4689 value = PyLong_FromLong(0L);
4690 else {
4691 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004692 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004693 return -1;
4694 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4695 1 /* little endian */ , 1 /* signed */ );
4696 }
4697 if (value == NULL)
4698 return -1;
4699 PDATA_PUSH(self->stack, value, -1);
4700 return 0;
4701}
4702
4703static int
4704load_float(UnpicklerObject *self)
4705{
4706 PyObject *value;
4707 char *endptr, *s;
4708 Py_ssize_t len;
4709 double d;
4710
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004711 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004712 return -1;
4713 if (len < 2)
4714 return bad_readline();
4715
4716 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004717 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4718 if (d == -1.0 && PyErr_Occurred())
4719 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004720 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4722 return -1;
4723 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004724 value = PyFloat_FromDouble(d);
4725 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004726 return -1;
4727
4728 PDATA_PUSH(self->stack, value, -1);
4729 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004730}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004731
4732static int
4733load_binfloat(UnpicklerObject *self)
4734{
4735 PyObject *value;
4736 double x;
4737 char *s;
4738
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004739 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 return -1;
4741
4742 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4743 if (x == -1.0 && PyErr_Occurred())
4744 return -1;
4745
4746 if ((value = PyFloat_FromDouble(x)) == NULL)
4747 return -1;
4748
4749 PDATA_PUSH(self->stack, value, -1);
4750 return 0;
4751}
4752
4753static int
4754load_string(UnpicklerObject *self)
4755{
4756 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004757 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004758 Py_ssize_t len;
4759 char *s, *p;
4760
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004761 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004762 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004763 /* Strip the newline */
4764 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004765 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004766 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767 p = s + 1;
4768 len -= 2;
4769 }
4770 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004771 PickleState *st = _Pickle_GetGlobalState();
4772 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004773 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 return -1;
4775 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004776 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777
4778 /* Use the PyBytes API to decode the string, since that is what is used
4779 to encode, and then coerce the result to Unicode. */
4780 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 if (bytes == NULL)
4782 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004783
4784 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4785 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4786 if (strcmp(self->encoding, "bytes") == 0) {
4787 obj = bytes;
4788 }
4789 else {
4790 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4791 Py_DECREF(bytes);
4792 if (obj == NULL) {
4793 return -1;
4794 }
4795 }
4796
4797 PDATA_PUSH(self->stack, obj, -1);
4798 return 0;
4799}
4800
4801static int
4802load_counted_binstring(UnpicklerObject *self, int nbytes)
4803{
4804 PyObject *obj;
4805 Py_ssize_t size;
4806 char *s;
4807
4808 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004809 return -1;
4810
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004811 size = calc_binsize(s, nbytes);
4812 if (size < 0) {
4813 PickleState *st = _Pickle_GetGlobalState();
4814 PyErr_Format(st->UnpicklingError,
4815 "BINSTRING exceeds system's maximum size of %zd bytes",
4816 PY_SSIZE_T_MAX);
4817 return -1;
4818 }
4819
4820 if (_Unpickler_Read(self, &s, size) < 0)
4821 return -1;
4822
4823 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4824 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4825 if (strcmp(self->encoding, "bytes") == 0) {
4826 obj = PyBytes_FromStringAndSize(s, size);
4827 }
4828 else {
4829 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4830 }
4831 if (obj == NULL) {
4832 return -1;
4833 }
4834
4835 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004836 return 0;
4837}
4838
4839static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004840load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004841{
4842 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004843 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 char *s;
4845
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004846 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004847 return -1;
4848
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004849 size = calc_binsize(s, nbytes);
4850 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004851 PyErr_Format(PyExc_OverflowError,
4852 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004853 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004854 return -1;
4855 }
4856
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004857 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004859
4860 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 if (bytes == NULL)
4862 return -1;
4863
4864 PDATA_PUSH(self->stack, bytes, -1);
4865 return 0;
4866}
4867
4868static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004869load_unicode(UnpicklerObject *self)
4870{
4871 PyObject *str;
4872 Py_ssize_t len;
4873 char *s;
4874
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004875 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004876 return -1;
4877 if (len < 1)
4878 return bad_readline();
4879
4880 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4881 if (str == NULL)
4882 return -1;
4883
4884 PDATA_PUSH(self->stack, str, -1);
4885 return 0;
4886}
4887
4888static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004889load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004890{
4891 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004892 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893 char *s;
4894
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004895 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 return -1;
4897
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004898 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004900 PyErr_Format(PyExc_OverflowError,
4901 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004902 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 return -1;
4904 }
4905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004906 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004907 return -1;
4908
Victor Stinner485fb562010-04-13 11:07:24 +00004909 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 if (str == NULL)
4911 return -1;
4912
4913 PDATA_PUSH(self->stack, str, -1);
4914 return 0;
4915}
4916
4917static int
4918load_tuple(UnpicklerObject *self)
4919{
4920 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004921 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922
4923 if ((i = marker(self)) < 0)
4924 return -1;
4925
4926 tuple = Pdata_poptuple(self->stack, i);
4927 if (tuple == NULL)
4928 return -1;
4929 PDATA_PUSH(self->stack, tuple, -1);
4930 return 0;
4931}
4932
4933static int
4934load_counted_tuple(UnpicklerObject *self, int len)
4935{
4936 PyObject *tuple;
4937
4938 tuple = PyTuple_New(len);
4939 if (tuple == NULL)
4940 return -1;
4941
4942 while (--len >= 0) {
4943 PyObject *item;
4944
4945 PDATA_POP(self->stack, item);
4946 if (item == NULL)
4947 return -1;
4948 PyTuple_SET_ITEM(tuple, len, item);
4949 }
4950 PDATA_PUSH(self->stack, tuple, -1);
4951 return 0;
4952}
4953
4954static int
4955load_empty_list(UnpicklerObject *self)
4956{
4957 PyObject *list;
4958
4959 if ((list = PyList_New(0)) == NULL)
4960 return -1;
4961 PDATA_PUSH(self->stack, list, -1);
4962 return 0;
4963}
4964
4965static int
4966load_empty_dict(UnpicklerObject *self)
4967{
4968 PyObject *dict;
4969
4970 if ((dict = PyDict_New()) == NULL)
4971 return -1;
4972 PDATA_PUSH(self->stack, dict, -1);
4973 return 0;
4974}
4975
4976static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004977load_empty_set(UnpicklerObject *self)
4978{
4979 PyObject *set;
4980
4981 if ((set = PySet_New(NULL)) == NULL)
4982 return -1;
4983 PDATA_PUSH(self->stack, set, -1);
4984 return 0;
4985}
4986
4987static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988load_list(UnpicklerObject *self)
4989{
4990 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004991 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004992
4993 if ((i = marker(self)) < 0)
4994 return -1;
4995
4996 list = Pdata_poplist(self->stack, i);
4997 if (list == NULL)
4998 return -1;
4999 PDATA_PUSH(self->stack, list, -1);
5000 return 0;
5001}
5002
5003static int
5004load_dict(UnpicklerObject *self)
5005{
5006 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005007 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008
5009 if ((i = marker(self)) < 0)
5010 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005011 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005012
5013 if ((dict = PyDict_New()) == NULL)
5014 return -1;
5015
5016 for (k = i + 1; k < j; k += 2) {
5017 key = self->stack->data[k - 1];
5018 value = self->stack->data[k];
5019 if (PyDict_SetItem(dict, key, value) < 0) {
5020 Py_DECREF(dict);
5021 return -1;
5022 }
5023 }
5024 Pdata_clear(self->stack, i);
5025 PDATA_PUSH(self->stack, dict, -1);
5026 return 0;
5027}
5028
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005029static int
5030load_frozenset(UnpicklerObject *self)
5031{
5032 PyObject *items;
5033 PyObject *frozenset;
5034 Py_ssize_t i;
5035
5036 if ((i = marker(self)) < 0)
5037 return -1;
5038
5039 items = Pdata_poptuple(self->stack, i);
5040 if (items == NULL)
5041 return -1;
5042
5043 frozenset = PyFrozenSet_New(items);
5044 Py_DECREF(items);
5045 if (frozenset == NULL)
5046 return -1;
5047
5048 PDATA_PUSH(self->stack, frozenset, -1);
5049 return 0;
5050}
5051
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005052static PyObject *
5053instantiate(PyObject *cls, PyObject *args)
5054{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005055 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005056 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005057 /* Caller must assure args are a tuple. Normally, args come from
5058 Pdata_poptuple which packs objects from the top of the stack
5059 into a newly created tuple. */
5060 assert(PyTuple_Check(args));
5061 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005062 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005063 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005065 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005066 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005067
5068 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005069 }
5070 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071}
5072
5073static int
5074load_obj(UnpicklerObject *self)
5075{
5076 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005077 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005078
5079 if ((i = marker(self)) < 0)
5080 return -1;
5081
5082 args = Pdata_poptuple(self->stack, i + 1);
5083 if (args == NULL)
5084 return -1;
5085
5086 PDATA_POP(self->stack, cls);
5087 if (cls) {
5088 obj = instantiate(cls, args);
5089 Py_DECREF(cls);
5090 }
5091 Py_DECREF(args);
5092 if (obj == NULL)
5093 return -1;
5094
5095 PDATA_PUSH(self->stack, obj, -1);
5096 return 0;
5097}
5098
5099static int
5100load_inst(UnpicklerObject *self)
5101{
5102 PyObject *cls = NULL;
5103 PyObject *args = NULL;
5104 PyObject *obj = NULL;
5105 PyObject *module_name;
5106 PyObject *class_name;
5107 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005108 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005109 char *s;
5110
5111 if ((i = marker(self)) < 0)
5112 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005113 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005114 return -1;
5115 if (len < 2)
5116 return bad_readline();
5117
5118 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5119 identifiers are permitted in Python 3.0, since the INST opcode is only
5120 supported by older protocols on Python 2.x. */
5121 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5122 if (module_name == NULL)
5123 return -1;
5124
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005125 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126 if (len < 2)
5127 return bad_readline();
5128 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005129 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005130 cls = find_class(self, module_name, class_name);
5131 Py_DECREF(class_name);
5132 }
5133 }
5134 Py_DECREF(module_name);
5135
5136 if (cls == NULL)
5137 return -1;
5138
5139 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5140 obj = instantiate(cls, args);
5141 Py_DECREF(args);
5142 }
5143 Py_DECREF(cls);
5144
5145 if (obj == NULL)
5146 return -1;
5147
5148 PDATA_PUSH(self->stack, obj, -1);
5149 return 0;
5150}
5151
5152static int
5153load_newobj(UnpicklerObject *self)
5154{
5155 PyObject *args = NULL;
5156 PyObject *clsraw = NULL;
5157 PyTypeObject *cls; /* clsraw cast to its true type */
5158 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005159 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005160
5161 /* Stack is ... cls argtuple, and we want to call
5162 * cls.__new__(cls, *argtuple).
5163 */
5164 PDATA_POP(self->stack, args);
5165 if (args == NULL)
5166 goto error;
5167 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005168 PyErr_SetString(st->UnpicklingError,
5169 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005170 goto error;
5171 }
5172
5173 PDATA_POP(self->stack, clsraw);
5174 cls = (PyTypeObject *)clsraw;
5175 if (cls == NULL)
5176 goto error;
5177 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005178 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179 "isn't a type object");
5180 goto error;
5181 }
5182 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005183 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005184 "has NULL tp_new");
5185 goto error;
5186 }
5187
5188 /* Call __new__. */
5189 obj = cls->tp_new(cls, args, NULL);
5190 if (obj == NULL)
5191 goto error;
5192
5193 Py_DECREF(args);
5194 Py_DECREF(clsraw);
5195 PDATA_PUSH(self->stack, obj, -1);
5196 return 0;
5197
5198 error:
5199 Py_XDECREF(args);
5200 Py_XDECREF(clsraw);
5201 return -1;
5202}
5203
5204static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005205load_newobj_ex(UnpicklerObject *self)
5206{
5207 PyObject *cls, *args, *kwargs;
5208 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005209 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005210
5211 PDATA_POP(self->stack, kwargs);
5212 if (kwargs == NULL) {
5213 return -1;
5214 }
5215 PDATA_POP(self->stack, args);
5216 if (args == NULL) {
5217 Py_DECREF(kwargs);
5218 return -1;
5219 }
5220 PDATA_POP(self->stack, cls);
5221 if (cls == NULL) {
5222 Py_DECREF(kwargs);
5223 Py_DECREF(args);
5224 return -1;
5225 }
Larry Hastings61272b72014-01-07 12:41:53 -08005226
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005227 if (!PyType_Check(cls)) {
5228 Py_DECREF(kwargs);
5229 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005230 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005231 "NEWOBJ_EX class argument must be a type, not %.200s",
5232 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005233 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005234 return -1;
5235 }
5236
5237 if (((PyTypeObject *)cls)->tp_new == NULL) {
5238 Py_DECREF(kwargs);
5239 Py_DECREF(args);
5240 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005241 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005242 "NEWOBJ_EX class argument doesn't have __new__");
5243 return -1;
5244 }
5245 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5246 Py_DECREF(kwargs);
5247 Py_DECREF(args);
5248 Py_DECREF(cls);
5249 if (obj == NULL) {
5250 return -1;
5251 }
5252 PDATA_PUSH(self->stack, obj, -1);
5253 return 0;
5254}
5255
5256static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257load_global(UnpicklerObject *self)
5258{
5259 PyObject *global = NULL;
5260 PyObject *module_name;
5261 PyObject *global_name;
5262 Py_ssize_t len;
5263 char *s;
5264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005265 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005266 return -1;
5267 if (len < 2)
5268 return bad_readline();
5269 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5270 if (!module_name)
5271 return -1;
5272
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005273 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005274 if (len < 2) {
5275 Py_DECREF(module_name);
5276 return bad_readline();
5277 }
5278 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5279 if (global_name) {
5280 global = find_class(self, module_name, global_name);
5281 Py_DECREF(global_name);
5282 }
5283 }
5284 Py_DECREF(module_name);
5285
5286 if (global == NULL)
5287 return -1;
5288 PDATA_PUSH(self->stack, global, -1);
5289 return 0;
5290}
5291
5292static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005293load_stack_global(UnpicklerObject *self)
5294{
5295 PyObject *global;
5296 PyObject *module_name;
5297 PyObject *global_name;
5298
5299 PDATA_POP(self->stack, global_name);
5300 PDATA_POP(self->stack, module_name);
5301 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5302 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005303 PickleState *st = _Pickle_GetGlobalState();
5304 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005305 Py_XDECREF(global_name);
5306 Py_XDECREF(module_name);
5307 return -1;
5308 }
5309 global = find_class(self, module_name, global_name);
5310 Py_DECREF(global_name);
5311 Py_DECREF(module_name);
5312 if (global == NULL)
5313 return -1;
5314 PDATA_PUSH(self->stack, global, -1);
5315 return 0;
5316}
5317
5318static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005319load_persid(UnpicklerObject *self)
5320{
5321 PyObject *pid;
5322 Py_ssize_t len;
5323 char *s;
5324
5325 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005326 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005327 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005328 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005329 return bad_readline();
5330
5331 pid = PyBytes_FromStringAndSize(s, len - 1);
5332 if (pid == NULL)
5333 return -1;
5334
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005335 /* This does not leak since _Pickle_FastCall() steals the reference
5336 to pid first. */
5337 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005338 if (pid == NULL)
5339 return -1;
5340
5341 PDATA_PUSH(self->stack, pid, -1);
5342 return 0;
5343 }
5344 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005345 PickleState *st = _Pickle_GetGlobalState();
5346 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005347 "A load persistent id instruction was encountered,\n"
5348 "but no persistent_load function was specified.");
5349 return -1;
5350 }
5351}
5352
5353static int
5354load_binpersid(UnpicklerObject *self)
5355{
5356 PyObject *pid;
5357
5358 if (self->pers_func) {
5359 PDATA_POP(self->stack, pid);
5360 if (pid == NULL)
5361 return -1;
5362
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005363 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005364 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005365 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005366 if (pid == NULL)
5367 return -1;
5368
5369 PDATA_PUSH(self->stack, pid, -1);
5370 return 0;
5371 }
5372 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005373 PickleState *st = _Pickle_GetGlobalState();
5374 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005375 "A load persistent id instruction was encountered,\n"
5376 "but no persistent_load function was specified.");
5377 return -1;
5378 }
5379}
5380
5381static int
5382load_pop(UnpicklerObject *self)
5383{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005384 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005385
5386 /* Note that we split the (pickle.py) stack into two stacks,
5387 * an object stack and a mark stack. We have to be clever and
5388 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005389 * mark stack first, and only signalling a stack underflow if
5390 * the object stack is empty and the mark stack doesn't match
5391 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005392 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005393 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005394 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005395 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005396 len--;
5397 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005398 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005399 } else {
5400 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005401 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005402 return 0;
5403}
5404
5405static int
5406load_pop_mark(UnpicklerObject *self)
5407{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005408 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005409
5410 if ((i = marker(self)) < 0)
5411 return -1;
5412
5413 Pdata_clear(self->stack, i);
5414
5415 return 0;
5416}
5417
5418static int
5419load_dup(UnpicklerObject *self)
5420{
5421 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005422 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005423
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005424 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005425 return stack_underflow();
5426 last = self->stack->data[len - 1];
5427 PDATA_APPEND(self->stack, last, -1);
5428 return 0;
5429}
5430
5431static int
5432load_get(UnpicklerObject *self)
5433{
5434 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005435 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005436 Py_ssize_t len;
5437 char *s;
5438
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005439 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440 return -1;
5441 if (len < 2)
5442 return bad_readline();
5443
5444 key = PyLong_FromString(s, NULL, 10);
5445 if (key == NULL)
5446 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005447 idx = PyLong_AsSsize_t(key);
5448 if (idx == -1 && PyErr_Occurred()) {
5449 Py_DECREF(key);
5450 return -1;
5451 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005452
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005453 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005454 if (value == NULL) {
5455 if (!PyErr_Occurred())
5456 PyErr_SetObject(PyExc_KeyError, key);
5457 Py_DECREF(key);
5458 return -1;
5459 }
5460 Py_DECREF(key);
5461
5462 PDATA_APPEND(self->stack, value, -1);
5463 return 0;
5464}
5465
5466static int
5467load_binget(UnpicklerObject *self)
5468{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005469 PyObject *value;
5470 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471 char *s;
5472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005473 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474 return -1;
5475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005476 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005478 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005479 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005480 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005481 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005482 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005483 Py_DECREF(key);
5484 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005485 return -1;
5486 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005487
5488 PDATA_APPEND(self->stack, value, -1);
5489 return 0;
5490}
5491
5492static int
5493load_long_binget(UnpicklerObject *self)
5494{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005495 PyObject *value;
5496 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005497 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005498
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005499 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005500 return -1;
5501
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005502 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005504 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005506 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005507 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005508 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005509 Py_DECREF(key);
5510 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005511 return -1;
5512 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005513
5514 PDATA_APPEND(self->stack, value, -1);
5515 return 0;
5516}
5517
5518/* Push an object from the extension registry (EXT[124]). nbytes is
5519 * the number of bytes following the opcode, holding the index (code) value.
5520 */
5521static int
5522load_extension(UnpicklerObject *self, int nbytes)
5523{
5524 char *codebytes; /* the nbytes bytes after the opcode */
5525 long code; /* calc_binint returns long */
5526 PyObject *py_code; /* code as a Python int */
5527 PyObject *obj; /* the object to push */
5528 PyObject *pair; /* (module_name, class_name) */
5529 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005530 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531
5532 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005533 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 return -1;
5535 code = calc_binint(codebytes, nbytes);
5536 if (code <= 0) { /* note that 0 is forbidden */
5537 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005538 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539 return -1;
5540 }
5541
5542 /* Look for the code in the cache. */
5543 py_code = PyLong_FromLong(code);
5544 if (py_code == NULL)
5545 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005546 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 if (obj != NULL) {
5548 /* Bingo. */
5549 Py_DECREF(py_code);
5550 PDATA_APPEND(self->stack, obj, -1);
5551 return 0;
5552 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005553 if (PyErr_Occurred()) {
5554 Py_DECREF(py_code);
5555 return -1;
5556 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557
5558 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005559 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 if (pair == NULL) {
5561 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005562 if (!PyErr_Occurred()) {
5563 PyErr_Format(PyExc_ValueError, "unregistered extension "
5564 "code %ld", code);
5565 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005566 return -1;
5567 }
5568 /* Since the extension registry is manipulable via Python code,
5569 * confirm that pair is really a 2-tuple of strings.
5570 */
5571 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5572 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5573 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5574 Py_DECREF(py_code);
5575 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5576 "isn't a 2-tuple of strings", code);
5577 return -1;
5578 }
5579 /* Load the object. */
5580 obj = find_class(self, module_name, class_name);
5581 if (obj == NULL) {
5582 Py_DECREF(py_code);
5583 return -1;
5584 }
5585 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005586 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587 Py_DECREF(py_code);
5588 if (code < 0) {
5589 Py_DECREF(obj);
5590 return -1;
5591 }
5592 PDATA_PUSH(self->stack, obj, -1);
5593 return 0;
5594}
5595
5596static int
5597load_put(UnpicklerObject *self)
5598{
5599 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 Py_ssize_t len;
5602 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 return -1;
5606 if (len < 2)
5607 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005610 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611
5612 key = PyLong_FromString(s, NULL, 10);
5613 if (key == NULL)
5614 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005615 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005617 if (idx < 0) {
5618 if (!PyErr_Occurred())
5619 PyErr_SetString(PyExc_ValueError,
5620 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005621 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005622 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005623
5624 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625}
5626
5627static int
5628load_binput(UnpicklerObject *self)
5629{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005630 PyObject *value;
5631 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005633
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005634 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636
5637 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005639 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005641 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005643 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644}
5645
5646static int
5647load_long_binput(UnpicklerObject *self)
5648{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005649 PyObject *value;
5650 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005652
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005653 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655
5656 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005658 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005660 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005661 if (idx < 0) {
5662 PyErr_SetString(PyExc_ValueError,
5663 "negative LONG_BINPUT argument");
5664 return -1;
5665 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005667 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668}
5669
5670static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005671load_memoize(UnpicklerObject *self)
5672{
5673 PyObject *value;
5674
5675 if (Py_SIZE(self->stack) <= 0)
5676 return stack_underflow();
5677 value = self->stack->data[Py_SIZE(self->stack) - 1];
5678
5679 return _Unpickler_MemoPut(self, self->memo_len, value);
5680}
5681
5682static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005683do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684{
5685 PyObject *value;
5686 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005687 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005689 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 if (x > len || x <= 0)
5691 return stack_underflow();
5692 if (len == x) /* nothing to do */
5693 return 0;
5694
5695 list = self->stack->data[x - 1];
5696
5697 if (PyList_Check(list)) {
5698 PyObject *slice;
5699 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005700 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005701
5702 slice = Pdata_poplist(self->stack, x);
5703 if (!slice)
5704 return -1;
5705 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005706 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005708 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 }
5710 else {
5711 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005712 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005713
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005714 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715 if (append_func == NULL)
5716 return -1;
5717 for (i = x; i < len; i++) {
5718 PyObject *result;
5719
5720 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005721 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722 if (result == NULL) {
5723 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005724 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005725 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005726 return -1;
5727 }
5728 Py_DECREF(result);
5729 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005730 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005731 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 }
5733
5734 return 0;
5735}
5736
5737static int
5738load_append(UnpicklerObject *self)
5739{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741}
5742
5743static int
5744load_appends(UnpicklerObject *self)
5745{
5746 return do_append(self, marker(self));
5747}
5748
5749static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005750do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005751{
5752 PyObject *value, *key;
5753 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005754 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755 int status = 0;
5756
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005757 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005758 if (x > len || x <= 0)
5759 return stack_underflow();
5760 if (len == x) /* nothing to do */
5761 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005762 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005763 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005765 PyErr_SetString(st->UnpicklingError,
5766 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 return -1;
5768 }
5769
5770 /* Here, dict does not actually need to be a PyDict; it could be anything
5771 that supports the __setitem__ attribute. */
5772 dict = self->stack->data[x - 1];
5773
5774 for (i = x + 1; i < len; i += 2) {
5775 key = self->stack->data[i - 1];
5776 value = self->stack->data[i];
5777 if (PyObject_SetItem(dict, key, value) < 0) {
5778 status = -1;
5779 break;
5780 }
5781 }
5782
5783 Pdata_clear(self->stack, x);
5784 return status;
5785}
5786
5787static int
5788load_setitem(UnpicklerObject *self)
5789{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791}
5792
5793static int
5794load_setitems(UnpicklerObject *self)
5795{
5796 return do_setitems(self, marker(self));
5797}
5798
5799static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005800load_additems(UnpicklerObject *self)
5801{
5802 PyObject *set;
5803 Py_ssize_t mark, len, i;
5804
5805 mark = marker(self);
5806 len = Py_SIZE(self->stack);
5807 if (mark > len || mark <= 0)
5808 return stack_underflow();
5809 if (len == mark) /* nothing to do */
5810 return 0;
5811
5812 set = self->stack->data[mark - 1];
5813
5814 if (PySet_Check(set)) {
5815 PyObject *items;
5816 int status;
5817
5818 items = Pdata_poptuple(self->stack, mark);
5819 if (items == NULL)
5820 return -1;
5821
5822 status = _PySet_Update(set, items);
5823 Py_DECREF(items);
5824 return status;
5825 }
5826 else {
5827 PyObject *add_func;
5828 _Py_IDENTIFIER(add);
5829
5830 add_func = _PyObject_GetAttrId(set, &PyId_add);
5831 if (add_func == NULL)
5832 return -1;
5833 for (i = mark; i < len; i++) {
5834 PyObject *result;
5835 PyObject *item;
5836
5837 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005838 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005839 if (result == NULL) {
5840 Pdata_clear(self->stack, i + 1);
5841 Py_SIZE(self->stack) = mark;
5842 return -1;
5843 }
5844 Py_DECREF(result);
5845 }
5846 Py_SIZE(self->stack) = mark;
5847 }
5848
5849 return 0;
5850}
5851
5852static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005853load_build(UnpicklerObject *self)
5854{
5855 PyObject *state, *inst, *slotstate;
5856 PyObject *setstate;
5857 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005858 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005859
5860 /* Stack is ... instance, state. We want to leave instance at
5861 * the stack top, possibly mutated via instance.__setstate__(state).
5862 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005863 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864 return stack_underflow();
5865
5866 PDATA_POP(self->stack, state);
5867 if (state == NULL)
5868 return -1;
5869
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005870 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005872 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005873 if (setstate == NULL) {
5874 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5875 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005876 else {
5877 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005878 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005879 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880 }
5881 else {
5882 PyObject *result;
5883
5884 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005885 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886 Py_DECREF(setstate);
5887 if (result == NULL)
5888 return -1;
5889 Py_DECREF(result);
5890 return 0;
5891 }
5892
5893 /* A default __setstate__. First see whether state embeds a
5894 * slot state dict too (a proto 2 addition).
5895 */
5896 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5897 PyObject *tmp = state;
5898
5899 state = PyTuple_GET_ITEM(tmp, 0);
5900 slotstate = PyTuple_GET_ITEM(tmp, 1);
5901 Py_INCREF(state);
5902 Py_INCREF(slotstate);
5903 Py_DECREF(tmp);
5904 }
5905 else
5906 slotstate = NULL;
5907
5908 /* Set inst.__dict__ from the state dict (if any). */
5909 if (state != Py_None) {
5910 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005911 PyObject *d_key, *d_value;
5912 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005913 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914
5915 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005916 PickleState *st = _Pickle_GetGlobalState();
5917 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918 goto error;
5919 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005920 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005921 if (dict == NULL)
5922 goto error;
5923
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005924 i = 0;
5925 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5926 /* normally the keys for instance attributes are
5927 interned. we should try to do that here. */
5928 Py_INCREF(d_key);
5929 if (PyUnicode_CheckExact(d_key))
5930 PyUnicode_InternInPlace(&d_key);
5931 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5932 Py_DECREF(d_key);
5933 goto error;
5934 }
5935 Py_DECREF(d_key);
5936 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005937 Py_DECREF(dict);
5938 }
5939
5940 /* Also set instance attributes from the slotstate dict (if any). */
5941 if (slotstate != NULL) {
5942 PyObject *d_key, *d_value;
5943 Py_ssize_t i;
5944
5945 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005946 PickleState *st = _Pickle_GetGlobalState();
5947 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005948 "slot state is not a dictionary");
5949 goto error;
5950 }
5951 i = 0;
5952 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5953 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5954 goto error;
5955 }
5956 }
5957
5958 if (0) {
5959 error:
5960 status = -1;
5961 }
5962
5963 Py_DECREF(state);
5964 Py_XDECREF(slotstate);
5965 return status;
5966}
5967
5968static int
5969load_mark(UnpicklerObject *self)
5970{
5971
5972 /* Note that we split the (pickle.py) stack into two stacks, an
5973 * object stack and a mark stack. Here we push a mark onto the
5974 * mark stack.
5975 */
5976
5977 if ((self->num_marks + 1) >= self->marks_size) {
5978 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005979
5980 /* Use the size_t type to check for overflow. */
5981 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005982 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005983 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984 PyErr_NoMemory();
5985 return -1;
5986 }
5987
5988 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005989 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005990 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005991 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
5992 if (self->marks == NULL) {
5993 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 PyErr_NoMemory();
5995 return -1;
5996 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005997 self->marks_size = (Py_ssize_t)alloc;
5998 }
5999
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006000 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006001
6002 return 0;
6003}
6004
6005static int
6006load_reduce(UnpicklerObject *self)
6007{
6008 PyObject *callable = NULL;
6009 PyObject *argtup = NULL;
6010 PyObject *obj = NULL;
6011
6012 PDATA_POP(self->stack, argtup);
6013 if (argtup == NULL)
6014 return -1;
6015 PDATA_POP(self->stack, callable);
6016 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006017 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006018 Py_DECREF(callable);
6019 }
6020 Py_DECREF(argtup);
6021
6022 if (obj == NULL)
6023 return -1;
6024
6025 PDATA_PUSH(self->stack, obj, -1);
6026 return 0;
6027}
6028
6029/* Just raises an error if we don't know the protocol specified. PROTO
6030 * is the first opcode for protocols >= 2.
6031 */
6032static int
6033load_proto(UnpicklerObject *self)
6034{
6035 char *s;
6036 int i;
6037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006038 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006039 return -1;
6040
6041 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006042 if (i <= HIGHEST_PROTOCOL) {
6043 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006044 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006045 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046
6047 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6048 return -1;
6049}
6050
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006051static int
6052load_frame(UnpicklerObject *self)
6053{
6054 char *s;
6055 Py_ssize_t frame_len;
6056
6057 if (_Unpickler_Read(self, &s, 8) < 0)
6058 return -1;
6059
6060 frame_len = calc_binsize(s, 8);
6061 if (frame_len < 0) {
6062 PyErr_Format(PyExc_OverflowError,
6063 "FRAME length exceeds system's maximum of %zd bytes",
6064 PY_SSIZE_T_MAX);
6065 return -1;
6066 }
6067
6068 if (_Unpickler_Read(self, &s, frame_len) < 0)
6069 return -1;
6070
6071 /* Rewind to start of frame */
6072 self->next_read_idx -= frame_len;
6073 return 0;
6074}
6075
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076static PyObject *
6077load(UnpicklerObject *self)
6078{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006080 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006081
6082 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006083 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006084 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085 Pdata_clear(self->stack, 0);
6086
6087 /* Convenient macros for the dispatch while-switch loop just below. */
6088#define OP(opcode, load_func) \
6089 case opcode: if (load_func(self) < 0) break; continue;
6090
6091#define OP_ARG(opcode, load_func, arg) \
6092 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6093
6094 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006095 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096 break;
6097
6098 switch ((enum opcode)s[0]) {
6099 OP(NONE, load_none)
6100 OP(BININT, load_binint)
6101 OP(BININT1, load_binint1)
6102 OP(BININT2, load_binint2)
6103 OP(INT, load_int)
6104 OP(LONG, load_long)
6105 OP_ARG(LONG1, load_counted_long, 1)
6106 OP_ARG(LONG4, load_counted_long, 4)
6107 OP(FLOAT, load_float)
6108 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006109 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6110 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6111 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6112 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6113 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006114 OP(STRING, load_string)
6115 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006116 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6117 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6118 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6120 OP_ARG(TUPLE1, load_counted_tuple, 1)
6121 OP_ARG(TUPLE2, load_counted_tuple, 2)
6122 OP_ARG(TUPLE3, load_counted_tuple, 3)
6123 OP(TUPLE, load_tuple)
6124 OP(EMPTY_LIST, load_empty_list)
6125 OP(LIST, load_list)
6126 OP(EMPTY_DICT, load_empty_dict)
6127 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006128 OP(EMPTY_SET, load_empty_set)
6129 OP(ADDITEMS, load_additems)
6130 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006131 OP(OBJ, load_obj)
6132 OP(INST, load_inst)
6133 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006134 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006135 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006136 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 OP(APPEND, load_append)
6138 OP(APPENDS, load_appends)
6139 OP(BUILD, load_build)
6140 OP(DUP, load_dup)
6141 OP(BINGET, load_binget)
6142 OP(LONG_BINGET, load_long_binget)
6143 OP(GET, load_get)
6144 OP(MARK, load_mark)
6145 OP(BINPUT, load_binput)
6146 OP(LONG_BINPUT, load_long_binput)
6147 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006148 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149 OP(POP, load_pop)
6150 OP(POP_MARK, load_pop_mark)
6151 OP(SETITEM, load_setitem)
6152 OP(SETITEMS, load_setitems)
6153 OP(PERSID, load_persid)
6154 OP(BINPERSID, load_binpersid)
6155 OP(REDUCE, load_reduce)
6156 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006157 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006158 OP_ARG(EXT1, load_extension, 1)
6159 OP_ARG(EXT2, load_extension, 2)
6160 OP_ARG(EXT4, load_extension, 4)
6161 OP_ARG(NEWTRUE, load_bool, Py_True)
6162 OP_ARG(NEWFALSE, load_bool, Py_False)
6163
6164 case STOP:
6165 break;
6166
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006167 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006168 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006169 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006170 }
6171 else {
6172 PickleState *st = _Pickle_GetGlobalState();
6173 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006174 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006175 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006176 return NULL;
6177 }
6178
6179 break; /* and we are done! */
6180 }
6181
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006182 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006183 return NULL;
6184 }
6185
Victor Stinner2ae57e32013-10-31 13:39:23 +01006186 if (_Unpickler_SkipConsumed(self) < 0)
6187 return NULL;
6188
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189 PDATA_POP(self->stack, value);
6190 return value;
6191}
6192
Larry Hastings61272b72014-01-07 12:41:53 -08006193/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006194
6195_pickle.Unpickler.load
6196
6197Load a pickle.
6198
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006199Read a pickled object representation from the open file object given
6200in the constructor, and return the reconstituted object hierarchy
6201specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006202[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006203
Larry Hastings3cceb382014-01-04 11:09:09 -08006204static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006205_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006206/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006208 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006209
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 /* Check whether the Unpickler was initialized correctly. This prevents
6211 segfaulting if a subclass overridden __init__ with a function that does
6212 not call Unpickler.__init__(). Here, we simply ensure that self->read
6213 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006214 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006215 PickleState *st = _Pickle_GetGlobalState();
6216 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006218 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006219 return NULL;
6220 }
6221
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006222 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006223}
6224
6225/* The name of find_class() is misleading. In newer pickle protocols, this
6226 function is used for loading any global (i.e., functions), not just
6227 classes. The name is kept only for backward compatibility. */
6228
Larry Hastings61272b72014-01-07 12:41:53 -08006229/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006230
6231_pickle.Unpickler.find_class
6232
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006233 module_name: object
6234 global_name: object
6235 /
6236
6237Return an object from a specified module.
6238
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006239If necessary, the module will be imported. Subclasses may override
6240this method (e.g. to restrict unpickling of arbitrary classes and
6241functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006242
6243This method is called whenever a class or a function object is
6244needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006245[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006246
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006247static PyObject *
6248_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006249/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006250{
6251 PyObject *global;
6252 PyObject *modules_dict;
6253 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006254 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006255
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006256 /* Try to map the old names used in Python 2.x to the new ones used in
6257 Python 3.x. We do this only with old pickle protocols and when the
6258 user has not disabled the feature. */
6259 if (self->proto < 3 && self->fix_imports) {
6260 PyObject *key;
6261 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006262 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006263
6264 /* Check if the global (i.e., a function or a class) was renamed
6265 or moved to another module. */
6266 key = PyTuple_Pack(2, module_name, global_name);
6267 if (key == NULL)
6268 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006269 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006270 Py_DECREF(key);
6271 if (item) {
6272 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6273 PyErr_Format(PyExc_RuntimeError,
6274 "_compat_pickle.NAME_MAPPING values should be "
6275 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6276 return NULL;
6277 }
6278 module_name = PyTuple_GET_ITEM(item, 0);
6279 global_name = PyTuple_GET_ITEM(item, 1);
6280 if (!PyUnicode_Check(module_name) ||
6281 !PyUnicode_Check(global_name)) {
6282 PyErr_Format(PyExc_RuntimeError,
6283 "_compat_pickle.NAME_MAPPING values should be "
6284 "pairs of str, not (%.200s, %.200s)",
6285 Py_TYPE(module_name)->tp_name,
6286 Py_TYPE(global_name)->tp_name);
6287 return NULL;
6288 }
6289 }
6290 else if (PyErr_Occurred()) {
6291 return NULL;
6292 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006293 else {
6294 /* Check if the module was renamed. */
6295 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6296 if (item) {
6297 if (!PyUnicode_Check(item)) {
6298 PyErr_Format(PyExc_RuntimeError,
6299 "_compat_pickle.IMPORT_MAPPING values should be "
6300 "strings, not %.200s", Py_TYPE(item)->tp_name);
6301 return NULL;
6302 }
6303 module_name = item;
6304 }
6305 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006306 return NULL;
6307 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006308 }
6309 }
6310
Victor Stinnerbb520202013-11-06 22:40:41 +01006311 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006312 if (modules_dict == NULL) {
6313 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006315 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006316
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006317 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006319 if (PyErr_Occurred())
6320 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006321 module = PyImport_Import(module_name);
6322 if (module == NULL)
6323 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006324 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006325 Py_DECREF(module);
6326 }
Victor Stinner121aab42011-09-29 23:40:53 +02006327 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006328 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 }
6330 return global;
6331}
6332
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006333/*[clinic input]
6334
6335_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6336
6337Returns size in memory, in bytes.
6338[clinic start generated code]*/
6339
6340static Py_ssize_t
6341_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6342/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6343{
6344 Py_ssize_t res;
6345
6346 res = sizeof(UnpicklerObject);
6347 if (self->memo != NULL)
6348 res += self->memo_size * sizeof(PyObject *);
6349 if (self->marks != NULL)
6350 res += self->marks_size * sizeof(Py_ssize_t);
6351 if (self->input_line != NULL)
6352 res += strlen(self->input_line) + 1;
6353 if (self->encoding != NULL)
6354 res += strlen(self->encoding) + 1;
6355 if (self->errors != NULL)
6356 res += strlen(self->errors) + 1;
6357 return res;
6358}
6359
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006360static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006361 _PICKLE_UNPICKLER_LOAD_METHODDEF
6362 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006363 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364 {NULL, NULL} /* sentinel */
6365};
6366
6367static void
6368Unpickler_dealloc(UnpicklerObject *self)
6369{
6370 PyObject_GC_UnTrack((PyObject *)self);
6371 Py_XDECREF(self->readline);
6372 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006373 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006374 Py_XDECREF(self->stack);
6375 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006376 if (self->buffer.buf != NULL) {
6377 PyBuffer_Release(&self->buffer);
6378 self->buffer.buf = NULL;
6379 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006380
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006381 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006382 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006383 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006384 PyMem_Free(self->encoding);
6385 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006386
6387 Py_TYPE(self)->tp_free((PyObject *)self);
6388}
6389
6390static int
6391Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6392{
6393 Py_VISIT(self->readline);
6394 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006395 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396 Py_VISIT(self->stack);
6397 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398 return 0;
6399}
6400
6401static int
6402Unpickler_clear(UnpicklerObject *self)
6403{
6404 Py_CLEAR(self->readline);
6405 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006406 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006407 Py_CLEAR(self->stack);
6408 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006409 if (self->buffer.buf != NULL) {
6410 PyBuffer_Release(&self->buffer);
6411 self->buffer.buf = NULL;
6412 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006413
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006414 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006415 PyMem_Free(self->marks);
6416 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006417 PyMem_Free(self->input_line);
6418 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006419 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006420 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006421 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006422 self->errors = NULL;
6423
6424 return 0;
6425}
6426
Larry Hastings61272b72014-01-07 12:41:53 -08006427/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006428
6429_pickle.Unpickler.__init__
6430
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006431 file: object
6432 *
6433 fix_imports: bool = True
6434 encoding: str = 'ASCII'
6435 errors: str = 'strict'
6436
6437This takes a binary file for reading a pickle data stream.
6438
6439The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006440protocol argument is needed. Bytes past the pickled object's
6441representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006442
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006443The argument *file* must have two methods, a read() method that takes
6444an integer argument, and a readline() method that requires no
6445arguments. Both methods should return bytes. Thus *file* can be a
6446binary file object opened for reading, a io.BytesIO object, or any
6447other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006448
6449Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6450which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006451generated by Python 2. If *fix_imports* is True, pickle will try to
6452map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006453*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006454instances pickled by Python 2; these default to 'ASCII' and 'strict',
6455respectively. The *encoding* can be 'bytes' to read these 8-bit
6456string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006457[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006458
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006459static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006460_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006461/*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006462{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006463 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006464
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006465 /* In case of multiple __init__() calls, clear previous content. */
6466 if (self->read != NULL)
6467 (void)Unpickler_clear(self);
6468
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006469 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006470 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006472 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006473 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006474
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006475 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006476 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006477 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006479 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006480 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6481 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006483 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006484 }
6485 else {
6486 self->pers_func = NULL;
6487 }
6488
6489 self->stack = (Pdata *)Pdata_New();
6490 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006491 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006493 self->memo_size = 32;
6494 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006495 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006496 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006497
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006498 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006499
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006500 return 0;
6501}
6502
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006504/* Define a proxy object for the Unpickler's internal memo object. This is to
6505 * avoid breaking code like:
6506 * unpickler.memo.clear()
6507 * and
6508 * unpickler.memo = saved_memo
6509 * Is this a good idea? Not really, but we don't want to break code that uses
6510 * it. Note that we don't implement the entire mapping API here. This is
6511 * intentional, as these should be treated as black-box implementation details.
6512 *
6513 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006514 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006515 */
6516
Larry Hastings61272b72014-01-07 12:41:53 -08006517/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518_pickle.UnpicklerMemoProxy.clear
6519
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006520Remove all items from memo.
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_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006525/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006526{
6527 _Unpickler_MemoCleanup(self->unpickler);
6528 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6529 if (self->unpickler->memo == NULL)
6530 return NULL;
6531 Py_RETURN_NONE;
6532}
6533
Larry Hastings61272b72014-01-07 12:41:53 -08006534/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006535_pickle.UnpicklerMemoProxy.copy
6536
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006538[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006539
Larry Hastings3cceb382014-01-04 11:09:09 -08006540static PyObject *
6541_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006542/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006543{
6544 Py_ssize_t i;
6545 PyObject *new_memo = PyDict_New();
6546 if (new_memo == NULL)
6547 return NULL;
6548
6549 for (i = 0; i < self->unpickler->memo_size; i++) {
6550 int status;
6551 PyObject *key, *value;
6552
6553 value = self->unpickler->memo[i];
6554 if (value == NULL)
6555 continue;
6556
6557 key = PyLong_FromSsize_t(i);
6558 if (key == NULL)
6559 goto error;
6560 status = PyDict_SetItem(new_memo, key, value);
6561 Py_DECREF(key);
6562 if (status < 0)
6563 goto error;
6564 }
6565 return new_memo;
6566
6567error:
6568 Py_DECREF(new_memo);
6569 return NULL;
6570}
6571
Larry Hastings61272b72014-01-07 12:41:53 -08006572/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006573_pickle.UnpicklerMemoProxy.__reduce__
6574
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006576[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577
Larry Hastings3cceb382014-01-04 11:09:09 -08006578static PyObject *
6579_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006580/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006581{
6582 PyObject *reduce_value;
6583 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006584 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006585 if (contents == NULL)
6586 return NULL;
6587
6588 reduce_value = PyTuple_New(2);
6589 if (reduce_value == NULL) {
6590 Py_DECREF(contents);
6591 return NULL;
6592 }
6593 constructor_args = PyTuple_New(1);
6594 if (constructor_args == NULL) {
6595 Py_DECREF(contents);
6596 Py_DECREF(reduce_value);
6597 return NULL;
6598 }
6599 PyTuple_SET_ITEM(constructor_args, 0, contents);
6600 Py_INCREF((PyObject *)&PyDict_Type);
6601 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6602 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6603 return reduce_value;
6604}
6605
6606static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006607 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6608 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6609 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006610 {NULL, NULL} /* sentinel */
6611};
6612
6613static void
6614UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6615{
6616 PyObject_GC_UnTrack(self);
6617 Py_XDECREF(self->unpickler);
6618 PyObject_GC_Del((PyObject *)self);
6619}
6620
6621static int
6622UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6623 visitproc visit, void *arg)
6624{
6625 Py_VISIT(self->unpickler);
6626 return 0;
6627}
6628
6629static int
6630UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6631{
6632 Py_CLEAR(self->unpickler);
6633 return 0;
6634}
6635
6636static PyTypeObject UnpicklerMemoProxyType = {
6637 PyVarObject_HEAD_INIT(NULL, 0)
6638 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6639 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6640 0,
6641 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6642 0, /* tp_print */
6643 0, /* tp_getattr */
6644 0, /* tp_setattr */
6645 0, /* tp_compare */
6646 0, /* tp_repr */
6647 0, /* tp_as_number */
6648 0, /* tp_as_sequence */
6649 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006650 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006651 0, /* tp_call */
6652 0, /* tp_str */
6653 PyObject_GenericGetAttr, /* tp_getattro */
6654 PyObject_GenericSetAttr, /* tp_setattro */
6655 0, /* tp_as_buffer */
6656 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6657 0, /* tp_doc */
6658 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6659 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6660 0, /* tp_richcompare */
6661 0, /* tp_weaklistoffset */
6662 0, /* tp_iter */
6663 0, /* tp_iternext */
6664 unpicklerproxy_methods, /* tp_methods */
6665};
6666
6667static PyObject *
6668UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6669{
6670 UnpicklerMemoProxyObject *self;
6671
6672 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6673 &UnpicklerMemoProxyType);
6674 if (self == NULL)
6675 return NULL;
6676 Py_INCREF(unpickler);
6677 self->unpickler = unpickler;
6678 PyObject_GC_Track(self);
6679 return (PyObject *)self;
6680}
6681
6682/*****************************************************************************/
6683
6684
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006685static PyObject *
6686Unpickler_get_memo(UnpicklerObject *self)
6687{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006688 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689}
6690
6691static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006692Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006694 PyObject **new_memo;
6695 Py_ssize_t new_memo_size = 0;
6696 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006698 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006699 PyErr_SetString(PyExc_TypeError,
6700 "attribute deletion is not supported");
6701 return -1;
6702 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006703
6704 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6705 UnpicklerObject *unpickler =
6706 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6707
6708 new_memo_size = unpickler->memo_size;
6709 new_memo = _Unpickler_NewMemo(new_memo_size);
6710 if (new_memo == NULL)
6711 return -1;
6712
6713 for (i = 0; i < new_memo_size; i++) {
6714 Py_XINCREF(unpickler->memo[i]);
6715 new_memo[i] = unpickler->memo[i];
6716 }
6717 }
6718 else if (PyDict_Check(obj)) {
6719 Py_ssize_t i = 0;
6720 PyObject *key, *value;
6721
6722 new_memo_size = PyDict_Size(obj);
6723 new_memo = _Unpickler_NewMemo(new_memo_size);
6724 if (new_memo == NULL)
6725 return -1;
6726
6727 while (PyDict_Next(obj, &i, &key, &value)) {
6728 Py_ssize_t idx;
6729 if (!PyLong_Check(key)) {
6730 PyErr_SetString(PyExc_TypeError,
6731 "memo key must be integers");
6732 goto error;
6733 }
6734 idx = PyLong_AsSsize_t(key);
6735 if (idx == -1 && PyErr_Occurred())
6736 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006737 if (idx < 0) {
6738 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006739 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006740 goto error;
6741 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006742 if (_Unpickler_MemoPut(self, idx, value) < 0)
6743 goto error;
6744 }
6745 }
6746 else {
6747 PyErr_Format(PyExc_TypeError,
6748 "'memo' attribute must be an UnpicklerMemoProxy object"
6749 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006750 return -1;
6751 }
6752
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006753 _Unpickler_MemoCleanup(self);
6754 self->memo_size = new_memo_size;
6755 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006756
6757 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006758
6759 error:
6760 if (new_memo_size) {
6761 i = new_memo_size;
6762 while (--i >= 0) {
6763 Py_XDECREF(new_memo[i]);
6764 }
6765 PyMem_FREE(new_memo);
6766 }
6767 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006768}
6769
6770static PyObject *
6771Unpickler_get_persload(UnpicklerObject *self)
6772{
6773 if (self->pers_func == NULL)
6774 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6775 else
6776 Py_INCREF(self->pers_func);
6777 return self->pers_func;
6778}
6779
6780static int
6781Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6782{
6783 PyObject *tmp;
6784
6785 if (value == NULL) {
6786 PyErr_SetString(PyExc_TypeError,
6787 "attribute deletion is not supported");
6788 return -1;
6789 }
6790 if (!PyCallable_Check(value)) {
6791 PyErr_SetString(PyExc_TypeError,
6792 "persistent_load must be a callable taking "
6793 "one argument");
6794 return -1;
6795 }
6796
6797 tmp = self->pers_func;
6798 Py_INCREF(value);
6799 self->pers_func = value;
6800 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6801
6802 return 0;
6803}
6804
6805static PyGetSetDef Unpickler_getsets[] = {
6806 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6807 {"persistent_load", (getter)Unpickler_get_persload,
6808 (setter)Unpickler_set_persload},
6809 {NULL}
6810};
6811
6812static PyTypeObject Unpickler_Type = {
6813 PyVarObject_HEAD_INIT(NULL, 0)
6814 "_pickle.Unpickler", /*tp_name*/
6815 sizeof(UnpicklerObject), /*tp_basicsize*/
6816 0, /*tp_itemsize*/
6817 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6818 0, /*tp_print*/
6819 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006820 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006821 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006822 0, /*tp_repr*/
6823 0, /*tp_as_number*/
6824 0, /*tp_as_sequence*/
6825 0, /*tp_as_mapping*/
6826 0, /*tp_hash*/
6827 0, /*tp_call*/
6828 0, /*tp_str*/
6829 0, /*tp_getattro*/
6830 0, /*tp_setattro*/
6831 0, /*tp_as_buffer*/
6832 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006833 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006834 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6835 (inquiry)Unpickler_clear, /*tp_clear*/
6836 0, /*tp_richcompare*/
6837 0, /*tp_weaklistoffset*/
6838 0, /*tp_iter*/
6839 0, /*tp_iternext*/
6840 Unpickler_methods, /*tp_methods*/
6841 0, /*tp_members*/
6842 Unpickler_getsets, /*tp_getset*/
6843 0, /*tp_base*/
6844 0, /*tp_dict*/
6845 0, /*tp_descr_get*/
6846 0, /*tp_descr_set*/
6847 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006848 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006849 PyType_GenericAlloc, /*tp_alloc*/
6850 PyType_GenericNew, /*tp_new*/
6851 PyObject_GC_Del, /*tp_free*/
6852 0, /*tp_is_gc*/
6853};
6854
Larry Hastings61272b72014-01-07 12:41:53 -08006855/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006856
6857_pickle.dump
6858
6859 obj: object
6860 file: object
6861 protocol: object = NULL
6862 *
6863 fix_imports: bool = True
6864
6865Write a pickled representation of obj to the open file object file.
6866
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006867This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6868be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006869
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006870The optional *protocol* argument tells the pickler to use the given
6871protocol supported protocols are 0, 1, 2, 3 and 4. The default
6872protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006873
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006874Specifying a negative protocol version selects the highest protocol
6875version supported. The higher the protocol used, the more recent the
6876version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006877
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006878The *file* argument must have a write() method that accepts a single
6879bytes argument. It can thus be a file object opened for binary
6880writing, a io.BytesIO instance, or any other custom object that meets
6881this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006882
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006883If *fix_imports* is True and protocol is less than 3, pickle will try
6884to map the new Python 3 names to the old module names used in Python
68852, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006886[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006887
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006888static PyObject *
6889_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006890/*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891{
6892 PicklerObject *pickler = _Pickler_New();
6893
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006894 if (pickler == NULL)
6895 return NULL;
6896
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006897 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006898 goto error;
6899
6900 if (_Pickler_SetOutputStream(pickler, file) < 0)
6901 goto error;
6902
6903 if (dump(pickler, obj) < 0)
6904 goto error;
6905
6906 if (_Pickler_FlushToFile(pickler) < 0)
6907 goto error;
6908
6909 Py_DECREF(pickler);
6910 Py_RETURN_NONE;
6911
6912 error:
6913 Py_XDECREF(pickler);
6914 return NULL;
6915}
6916
Larry Hastings61272b72014-01-07 12:41:53 -08006917/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006918
6919_pickle.dumps
6920
6921 obj: object
6922 protocol: object = NULL
6923 *
6924 fix_imports: bool = True
6925
6926Return the pickled representation of the object as a bytes object.
6927
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006928The optional *protocol* argument tells the pickler to use the given
6929protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6930protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006931
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006932Specifying a negative protocol version selects the highest protocol
6933version supported. The higher the protocol used, the more recent the
6934version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006935
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006936If *fix_imports* is True and *protocol* is less than 3, pickle will
6937try to map the new Python 3 names to the old module names used in
6938Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006939[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006940
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006941static PyObject *
6942_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006943/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006944{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006945 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006946 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006947
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006948 if (pickler == NULL)
6949 return NULL;
6950
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006951 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006952 goto error;
6953
6954 if (dump(pickler, obj) < 0)
6955 goto error;
6956
6957 result = _Pickler_GetString(pickler);
6958 Py_DECREF(pickler);
6959 return result;
6960
6961 error:
6962 Py_XDECREF(pickler);
6963 return NULL;
6964}
6965
Larry Hastings61272b72014-01-07 12:41:53 -08006966/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006967
6968_pickle.load
6969
6970 file: object
6971 *
6972 fix_imports: bool = True
6973 encoding: str = 'ASCII'
6974 errors: str = 'strict'
6975
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006976Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006977
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006978This is equivalent to ``Unpickler(file).load()``, but may be more
6979efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006980
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006981The protocol version of the pickle is detected automatically, so no
6982protocol argument is needed. Bytes past the pickled object's
6983representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006984
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006985The argument *file* must have two methods, a read() method that takes
6986an integer argument, and a readline() method that requires no
6987arguments. Both methods should return bytes. Thus *file* can be a
6988binary file object opened for reading, a io.BytesIO object, or any
6989other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006990
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006991Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6992which are used to control compatiblity support for pickle stream
6993generated by Python 2. If *fix_imports* is True, pickle will try to
6994map the old Python 2 names to the new names used in Python 3. The
6995*encoding* and *errors* tell pickle how to decode 8-bit string
6996instances pickled by Python 2; these default to 'ASCII' and 'strict',
6997respectively. The *encoding* can be 'bytes' to read these 8-bit
6998string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006999[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007000
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007001static PyObject *
7002_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007003/*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007005 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007006 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007007
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007008 if (unpickler == NULL)
7009 return NULL;
7010
7011 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7012 goto error;
7013
7014 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7015 goto error;
7016
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007017 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007018
7019 result = load(unpickler);
7020 Py_DECREF(unpickler);
7021 return result;
7022
7023 error:
7024 Py_XDECREF(unpickler);
7025 return NULL;
7026}
7027
Larry Hastings61272b72014-01-07 12:41:53 -08007028/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007029
7030_pickle.loads
7031
7032 data: object
7033 *
7034 fix_imports: bool = True
7035 encoding: str = 'ASCII'
7036 errors: str = 'strict'
7037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007038Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007039
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007040The protocol version of the pickle is detected automatically, so no
7041protocol argument is needed. Bytes past the pickled object's
7042representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007044Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7045which are used to control compatiblity support for pickle stream
7046generated by Python 2. If *fix_imports* is True, pickle will try to
7047map the old Python 2 names to the new names used in Python 3. The
7048*encoding* and *errors* tell pickle how to decode 8-bit string
7049instances pickled by Python 2; these default to 'ASCII' and 'strict',
7050respectively. The *encoding* can be 'bytes' to read these 8-bit
7051string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007052[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007053
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007054static PyObject *
7055_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007056/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007057{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007058 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007059 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007061 if (unpickler == NULL)
7062 return NULL;
7063
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007064 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065 goto error;
7066
7067 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7068 goto error;
7069
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007070 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007071
7072 result = load(unpickler);
7073 Py_DECREF(unpickler);
7074 return result;
7075
7076 error:
7077 Py_XDECREF(unpickler);
7078 return NULL;
7079}
7080
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007081static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082 _PICKLE_DUMP_METHODDEF
7083 _PICKLE_DUMPS_METHODDEF
7084 _PICKLE_LOAD_METHODDEF
7085 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007086 {NULL, NULL} /* sentinel */
7087};
7088
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007089static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007090pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007091{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007092 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007093 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007094}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095
Stefan Krahf483b0f2013-12-14 13:43:10 +01007096static void
7097pickle_free(PyObject *m)
7098{
7099 _Pickle_ClearState(_Pickle_GetState(m));
7100}
7101
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007102static int
7103pickle_traverse(PyObject *m, visitproc visit, void *arg)
7104{
7105 PickleState *st = _Pickle_GetState(m);
7106 Py_VISIT(st->PickleError);
7107 Py_VISIT(st->PicklingError);
7108 Py_VISIT(st->UnpicklingError);
7109 Py_VISIT(st->dispatch_table);
7110 Py_VISIT(st->extension_registry);
7111 Py_VISIT(st->extension_cache);
7112 Py_VISIT(st->inverted_registry);
7113 Py_VISIT(st->name_mapping_2to3);
7114 Py_VISIT(st->import_mapping_2to3);
7115 Py_VISIT(st->name_mapping_3to2);
7116 Py_VISIT(st->import_mapping_3to2);
7117 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007118 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007119}
7120
7121static struct PyModuleDef _picklemodule = {
7122 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007123 "_pickle", /* m_name */
7124 pickle_module_doc, /* m_doc */
7125 sizeof(PickleState), /* m_size */
7126 pickle_methods, /* m_methods */
7127 NULL, /* m_reload */
7128 pickle_traverse, /* m_traverse */
7129 pickle_clear, /* m_clear */
7130 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007131};
7132
7133PyMODINIT_FUNC
7134PyInit__pickle(void)
7135{
7136 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007137 PickleState *st;
7138
7139 m = PyState_FindModule(&_picklemodule);
7140 if (m) {
7141 Py_INCREF(m);
7142 return m;
7143 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007144
7145 if (PyType_Ready(&Unpickler_Type) < 0)
7146 return NULL;
7147 if (PyType_Ready(&Pickler_Type) < 0)
7148 return NULL;
7149 if (PyType_Ready(&Pdata_Type) < 0)
7150 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007151 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7152 return NULL;
7153 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7154 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007155
7156 /* Create the module and add the functions. */
7157 m = PyModule_Create(&_picklemodule);
7158 if (m == NULL)
7159 return NULL;
7160
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007161 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007162 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7163 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007164 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007165 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7166 return NULL;
7167
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007168 st = _Pickle_GetState(m);
7169
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007170 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007171 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7172 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007173 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007174 st->PicklingError = \
7175 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7176 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007177 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007178 st->UnpicklingError = \
7179 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7180 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007181 return NULL;
7182
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007183 Py_INCREF(st->PickleError);
7184 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007185 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007186 Py_INCREF(st->PicklingError);
7187 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007188 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007189 Py_INCREF(st->UnpicklingError);
7190 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007191 return NULL;
7192
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007193 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007194 return NULL;
7195
7196 return m;
7197}