blob: c357dcc219473a3610da1d9cc9bad76ea71e8032 [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{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000451 if (Py_SIZE(self) == 0) {
Serhiy Storchakae9b30742015-11-23 15:17:43 +0200452 PickleState *st = _Pickle_GetGlobalState();
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
Martin Panter7462b6492015-11-02 03:37:02 +00004035writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004036this 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)
Martin Panter7462b6492015-11-02 03:37:02 +00004045/*[clinic end generated code: output=56e229f3b1f4332f input=4faabdbc763c2389]*/
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
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004918load_counted_tuple(UnpicklerObject *self, int len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004919{
4920 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004921
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004922 if (Py_SIZE(self->stack) < len)
4923 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004924
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004925 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 if (tuple == NULL)
4927 return -1;
4928 PDATA_PUSH(self->stack, tuple, -1);
4929 return 0;
4930}
4931
4932static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004933load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004934{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004935 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004936
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004937 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004938 return -1;
4939
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004940 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941}
4942
4943static int
4944load_empty_list(UnpicklerObject *self)
4945{
4946 PyObject *list;
4947
4948 if ((list = PyList_New(0)) == NULL)
4949 return -1;
4950 PDATA_PUSH(self->stack, list, -1);
4951 return 0;
4952}
4953
4954static int
4955load_empty_dict(UnpicklerObject *self)
4956{
4957 PyObject *dict;
4958
4959 if ((dict = PyDict_New()) == NULL)
4960 return -1;
4961 PDATA_PUSH(self->stack, dict, -1);
4962 return 0;
4963}
4964
4965static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004966load_empty_set(UnpicklerObject *self)
4967{
4968 PyObject *set;
4969
4970 if ((set = PySet_New(NULL)) == NULL)
4971 return -1;
4972 PDATA_PUSH(self->stack, set, -1);
4973 return 0;
4974}
4975
4976static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977load_list(UnpicklerObject *self)
4978{
4979 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004980 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981
4982 if ((i = marker(self)) < 0)
4983 return -1;
4984
4985 list = Pdata_poplist(self->stack, i);
4986 if (list == NULL)
4987 return -1;
4988 PDATA_PUSH(self->stack, list, -1);
4989 return 0;
4990}
4991
4992static int
4993load_dict(UnpicklerObject *self)
4994{
4995 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004996 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004997
4998 if ((i = marker(self)) < 0)
4999 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005000 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001
5002 if ((dict = PyDict_New()) == NULL)
5003 return -1;
5004
5005 for (k = i + 1; k < j; k += 2) {
5006 key = self->stack->data[k - 1];
5007 value = self->stack->data[k];
5008 if (PyDict_SetItem(dict, key, value) < 0) {
5009 Py_DECREF(dict);
5010 return -1;
5011 }
5012 }
5013 Pdata_clear(self->stack, i);
5014 PDATA_PUSH(self->stack, dict, -1);
5015 return 0;
5016}
5017
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005018static int
5019load_frozenset(UnpicklerObject *self)
5020{
5021 PyObject *items;
5022 PyObject *frozenset;
5023 Py_ssize_t i;
5024
5025 if ((i = marker(self)) < 0)
5026 return -1;
5027
5028 items = Pdata_poptuple(self->stack, i);
5029 if (items == NULL)
5030 return -1;
5031
5032 frozenset = PyFrozenSet_New(items);
5033 Py_DECREF(items);
5034 if (frozenset == NULL)
5035 return -1;
5036
5037 PDATA_PUSH(self->stack, frozenset, -1);
5038 return 0;
5039}
5040
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041static PyObject *
5042instantiate(PyObject *cls, PyObject *args)
5043{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005044 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005045 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005046 /* Caller must assure args are a tuple. Normally, args come from
5047 Pdata_poptuple which packs objects from the top of the stack
5048 into a newly created tuple. */
5049 assert(PyTuple_Check(args));
5050 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005051 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005052 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005054 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005055 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005056
5057 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005058 }
5059 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060}
5061
5062static int
5063load_obj(UnpicklerObject *self)
5064{
5065 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005066 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067
5068 if ((i = marker(self)) < 0)
5069 return -1;
5070
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005071 if (Py_SIZE(self->stack) - i < 1)
5072 return stack_underflow();
5073
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005074 args = Pdata_poptuple(self->stack, i + 1);
5075 if (args == NULL)
5076 return -1;
5077
5078 PDATA_POP(self->stack, cls);
5079 if (cls) {
5080 obj = instantiate(cls, args);
5081 Py_DECREF(cls);
5082 }
5083 Py_DECREF(args);
5084 if (obj == NULL)
5085 return -1;
5086
5087 PDATA_PUSH(self->stack, obj, -1);
5088 return 0;
5089}
5090
5091static int
5092load_inst(UnpicklerObject *self)
5093{
5094 PyObject *cls = NULL;
5095 PyObject *args = NULL;
5096 PyObject *obj = NULL;
5097 PyObject *module_name;
5098 PyObject *class_name;
5099 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005100 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005101 char *s;
5102
5103 if ((i = marker(self)) < 0)
5104 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005105 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005106 return -1;
5107 if (len < 2)
5108 return bad_readline();
5109
5110 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5111 identifiers are permitted in Python 3.0, since the INST opcode is only
5112 supported by older protocols on Python 2.x. */
5113 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5114 if (module_name == NULL)
5115 return -1;
5116
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005117 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005118 if (len < 2) {
5119 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005121 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005123 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124 cls = find_class(self, module_name, class_name);
5125 Py_DECREF(class_name);
5126 }
5127 }
5128 Py_DECREF(module_name);
5129
5130 if (cls == NULL)
5131 return -1;
5132
5133 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5134 obj = instantiate(cls, args);
5135 Py_DECREF(args);
5136 }
5137 Py_DECREF(cls);
5138
5139 if (obj == NULL)
5140 return -1;
5141
5142 PDATA_PUSH(self->stack, obj, -1);
5143 return 0;
5144}
5145
5146static int
5147load_newobj(UnpicklerObject *self)
5148{
5149 PyObject *args = NULL;
5150 PyObject *clsraw = NULL;
5151 PyTypeObject *cls; /* clsraw cast to its true type */
5152 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005153 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005154
5155 /* Stack is ... cls argtuple, and we want to call
5156 * cls.__new__(cls, *argtuple).
5157 */
5158 PDATA_POP(self->stack, args);
5159 if (args == NULL)
5160 goto error;
5161 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005162 PyErr_SetString(st->UnpicklingError,
5163 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005164 goto error;
5165 }
5166
5167 PDATA_POP(self->stack, clsraw);
5168 cls = (PyTypeObject *)clsraw;
5169 if (cls == NULL)
5170 goto error;
5171 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005172 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005173 "isn't a type object");
5174 goto error;
5175 }
5176 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005177 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005178 "has NULL tp_new");
5179 goto error;
5180 }
5181
5182 /* Call __new__. */
5183 obj = cls->tp_new(cls, args, NULL);
5184 if (obj == NULL)
5185 goto error;
5186
5187 Py_DECREF(args);
5188 Py_DECREF(clsraw);
5189 PDATA_PUSH(self->stack, obj, -1);
5190 return 0;
5191
5192 error:
5193 Py_XDECREF(args);
5194 Py_XDECREF(clsraw);
5195 return -1;
5196}
5197
5198static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005199load_newobj_ex(UnpicklerObject *self)
5200{
5201 PyObject *cls, *args, *kwargs;
5202 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005203 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005204
5205 PDATA_POP(self->stack, kwargs);
5206 if (kwargs == NULL) {
5207 return -1;
5208 }
5209 PDATA_POP(self->stack, args);
5210 if (args == NULL) {
5211 Py_DECREF(kwargs);
5212 return -1;
5213 }
5214 PDATA_POP(self->stack, cls);
5215 if (cls == NULL) {
5216 Py_DECREF(kwargs);
5217 Py_DECREF(args);
5218 return -1;
5219 }
Larry Hastings61272b72014-01-07 12:41:53 -08005220
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005221 if (!PyType_Check(cls)) {
5222 Py_DECREF(kwargs);
5223 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005224 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005225 "NEWOBJ_EX class argument must be a type, not %.200s",
5226 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005227 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005228 return -1;
5229 }
5230
5231 if (((PyTypeObject *)cls)->tp_new == NULL) {
5232 Py_DECREF(kwargs);
5233 Py_DECREF(args);
5234 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005235 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005236 "NEWOBJ_EX class argument doesn't have __new__");
5237 return -1;
5238 }
5239 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5240 Py_DECREF(kwargs);
5241 Py_DECREF(args);
5242 Py_DECREF(cls);
5243 if (obj == NULL) {
5244 return -1;
5245 }
5246 PDATA_PUSH(self->stack, obj, -1);
5247 return 0;
5248}
5249
5250static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005251load_global(UnpicklerObject *self)
5252{
5253 PyObject *global = NULL;
5254 PyObject *module_name;
5255 PyObject *global_name;
5256 Py_ssize_t len;
5257 char *s;
5258
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005259 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005260 return -1;
5261 if (len < 2)
5262 return bad_readline();
5263 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5264 if (!module_name)
5265 return -1;
5266
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005267 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005268 if (len < 2) {
5269 Py_DECREF(module_name);
5270 return bad_readline();
5271 }
5272 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5273 if (global_name) {
5274 global = find_class(self, module_name, global_name);
5275 Py_DECREF(global_name);
5276 }
5277 }
5278 Py_DECREF(module_name);
5279
5280 if (global == NULL)
5281 return -1;
5282 PDATA_PUSH(self->stack, global, -1);
5283 return 0;
5284}
5285
5286static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005287load_stack_global(UnpicklerObject *self)
5288{
5289 PyObject *global;
5290 PyObject *module_name;
5291 PyObject *global_name;
5292
5293 PDATA_POP(self->stack, global_name);
5294 PDATA_POP(self->stack, module_name);
5295 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5296 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005297 PickleState *st = _Pickle_GetGlobalState();
5298 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005299 Py_XDECREF(global_name);
5300 Py_XDECREF(module_name);
5301 return -1;
5302 }
5303 global = find_class(self, module_name, global_name);
5304 Py_DECREF(global_name);
5305 Py_DECREF(module_name);
5306 if (global == NULL)
5307 return -1;
5308 PDATA_PUSH(self->stack, global, -1);
5309 return 0;
5310}
5311
5312static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005313load_persid(UnpicklerObject *self)
5314{
5315 PyObject *pid;
5316 Py_ssize_t len;
5317 char *s;
5318
5319 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005320 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005322 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323 return bad_readline();
5324
5325 pid = PyBytes_FromStringAndSize(s, len - 1);
5326 if (pid == NULL)
5327 return -1;
5328
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005329 /* This does not leak since _Pickle_FastCall() steals the reference
5330 to pid first. */
5331 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005332 if (pid == NULL)
5333 return -1;
5334
5335 PDATA_PUSH(self->stack, pid, -1);
5336 return 0;
5337 }
5338 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005339 PickleState *st = _Pickle_GetGlobalState();
5340 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005341 "A load persistent id instruction was encountered,\n"
5342 "but no persistent_load function was specified.");
5343 return -1;
5344 }
5345}
5346
5347static int
5348load_binpersid(UnpicklerObject *self)
5349{
5350 PyObject *pid;
5351
5352 if (self->pers_func) {
5353 PDATA_POP(self->stack, pid);
5354 if (pid == NULL)
5355 return -1;
5356
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005357 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005358 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005359 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005360 if (pid == NULL)
5361 return -1;
5362
5363 PDATA_PUSH(self->stack, pid, -1);
5364 return 0;
5365 }
5366 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005367 PickleState *st = _Pickle_GetGlobalState();
5368 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005369 "A load persistent id instruction was encountered,\n"
5370 "but no persistent_load function was specified.");
5371 return -1;
5372 }
5373}
5374
5375static int
5376load_pop(UnpicklerObject *self)
5377{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005378 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005379
5380 /* Note that we split the (pickle.py) stack into two stacks,
5381 * an object stack and a mark stack. We have to be clever and
5382 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005383 * mark stack first, and only signalling a stack underflow if
5384 * the object stack is empty and the mark stack doesn't match
5385 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005386 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005387 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005388 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005389 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390 len--;
5391 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005392 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005393 } else {
5394 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005396 return 0;
5397}
5398
5399static int
5400load_pop_mark(UnpicklerObject *self)
5401{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005402 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403
5404 if ((i = marker(self)) < 0)
5405 return -1;
5406
5407 Pdata_clear(self->stack, i);
5408
5409 return 0;
5410}
5411
5412static int
5413load_dup(UnpicklerObject *self)
5414{
5415 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005416 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005417
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005418 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005419 return stack_underflow();
5420 last = self->stack->data[len - 1];
5421 PDATA_APPEND(self->stack, last, -1);
5422 return 0;
5423}
5424
5425static int
5426load_get(UnpicklerObject *self)
5427{
5428 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005429 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005430 Py_ssize_t len;
5431 char *s;
5432
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005433 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005434 return -1;
5435 if (len < 2)
5436 return bad_readline();
5437
5438 key = PyLong_FromString(s, NULL, 10);
5439 if (key == NULL)
5440 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005441 idx = PyLong_AsSsize_t(key);
5442 if (idx == -1 && PyErr_Occurred()) {
5443 Py_DECREF(key);
5444 return -1;
5445 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005447 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448 if (value == NULL) {
5449 if (!PyErr_Occurred())
5450 PyErr_SetObject(PyExc_KeyError, key);
5451 Py_DECREF(key);
5452 return -1;
5453 }
5454 Py_DECREF(key);
5455
5456 PDATA_APPEND(self->stack, value, -1);
5457 return 0;
5458}
5459
5460static int
5461load_binget(UnpicklerObject *self)
5462{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005463 PyObject *value;
5464 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 char *s;
5466
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005467 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005468 return -1;
5469
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005470 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005472 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005473 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005474 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005475 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005476 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005477 Py_DECREF(key);
5478 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005479 return -1;
5480 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005481
5482 PDATA_APPEND(self->stack, value, -1);
5483 return 0;
5484}
5485
5486static int
5487load_long_binget(UnpicklerObject *self)
5488{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005489 PyObject *value;
5490 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005493 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005494 return -1;
5495
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005496 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005498 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005499 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005500 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005501 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005502 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005503 Py_DECREF(key);
5504 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 return -1;
5506 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005507
5508 PDATA_APPEND(self->stack, value, -1);
5509 return 0;
5510}
5511
5512/* Push an object from the extension registry (EXT[124]). nbytes is
5513 * the number of bytes following the opcode, holding the index (code) value.
5514 */
5515static int
5516load_extension(UnpicklerObject *self, int nbytes)
5517{
5518 char *codebytes; /* the nbytes bytes after the opcode */
5519 long code; /* calc_binint returns long */
5520 PyObject *py_code; /* code as a Python int */
5521 PyObject *obj; /* the object to push */
5522 PyObject *pair; /* (module_name, class_name) */
5523 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005524 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525
5526 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005527 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005528 return -1;
5529 code = calc_binint(codebytes, nbytes);
5530 if (code <= 0) { /* note that 0 is forbidden */
5531 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005532 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533 return -1;
5534 }
5535
5536 /* Look for the code in the cache. */
5537 py_code = PyLong_FromLong(code);
5538 if (py_code == NULL)
5539 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005540 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541 if (obj != NULL) {
5542 /* Bingo. */
5543 Py_DECREF(py_code);
5544 PDATA_APPEND(self->stack, obj, -1);
5545 return 0;
5546 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005547 if (PyErr_Occurred()) {
5548 Py_DECREF(py_code);
5549 return -1;
5550 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551
5552 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005553 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554 if (pair == NULL) {
5555 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005556 if (!PyErr_Occurred()) {
5557 PyErr_Format(PyExc_ValueError, "unregistered extension "
5558 "code %ld", code);
5559 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 return -1;
5561 }
5562 /* Since the extension registry is manipulable via Python code,
5563 * confirm that pair is really a 2-tuple of strings.
5564 */
5565 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5566 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5567 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5568 Py_DECREF(py_code);
5569 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5570 "isn't a 2-tuple of strings", code);
5571 return -1;
5572 }
5573 /* Load the object. */
5574 obj = find_class(self, module_name, class_name);
5575 if (obj == NULL) {
5576 Py_DECREF(py_code);
5577 return -1;
5578 }
5579 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005580 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005581 Py_DECREF(py_code);
5582 if (code < 0) {
5583 Py_DECREF(obj);
5584 return -1;
5585 }
5586 PDATA_PUSH(self->stack, obj, -1);
5587 return 0;
5588}
5589
5590static int
5591load_put(UnpicklerObject *self)
5592{
5593 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005594 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005595 Py_ssize_t len;
5596 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 return -1;
5600 if (len < 2)
5601 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605
5606 key = PyLong_FromString(s, NULL, 10);
5607 if (key == NULL)
5608 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005611 if (idx < 0) {
5612 if (!PyErr_Occurred())
5613 PyErr_SetString(PyExc_ValueError,
5614 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005615 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005616 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005617
5618 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619}
5620
5621static int
5622load_binput(UnpicklerObject *self)
5623{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005624 PyObject *value;
5625 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005628 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005630
5631 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005633 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005635 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005637 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638}
5639
5640static int
5641load_long_binput(UnpicklerObject *self)
5642{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005643 PyObject *value;
5644 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005645 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005646
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005647 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005649
5650 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005651 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005652 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005654 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005655 if (idx < 0) {
5656 PyErr_SetString(PyExc_ValueError,
5657 "negative LONG_BINPUT argument");
5658 return -1;
5659 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005661 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662}
5663
5664static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005665load_memoize(UnpicklerObject *self)
5666{
5667 PyObject *value;
5668
5669 if (Py_SIZE(self->stack) <= 0)
5670 return stack_underflow();
5671 value = self->stack->data[Py_SIZE(self->stack) - 1];
5672
5673 return _Unpickler_MemoPut(self, self->memo_len, value);
5674}
5675
5676static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005677do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678{
5679 PyObject *value;
5680 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005681 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005683 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 if (x > len || x <= 0)
5685 return stack_underflow();
5686 if (len == x) /* nothing to do */
5687 return 0;
5688
5689 list = self->stack->data[x - 1];
5690
5691 if (PyList_Check(list)) {
5692 PyObject *slice;
5693 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005694 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695
5696 slice = Pdata_poplist(self->stack, x);
5697 if (!slice)
5698 return -1;
5699 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005700 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005701 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005702 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005703 }
5704 else {
5705 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005706 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005708 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 if (append_func == NULL)
5710 return -1;
5711 for (i = x; i < len; i++) {
5712 PyObject *result;
5713
5714 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005715 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716 if (result == NULL) {
5717 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005718 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005719 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720 return -1;
5721 }
5722 Py_DECREF(result);
5723 }
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 }
5727
5728 return 0;
5729}
5730
5731static int
5732load_append(UnpicklerObject *self)
5733{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005734 if (Py_SIZE(self->stack) - 1 <= 0)
5735 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005736 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005737}
5738
5739static int
5740load_appends(UnpicklerObject *self)
5741{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005742 Py_ssize_t i = marker(self);
5743 if (i < 0)
5744 return -1;
5745 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746}
5747
5748static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005749do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750{
5751 PyObject *value, *key;
5752 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005753 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754 int status = 0;
5755
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005756 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757 if (x > len || x <= 0)
5758 return stack_underflow();
5759 if (len == x) /* nothing to do */
5760 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005761 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005762 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005764 PyErr_SetString(st->UnpicklingError,
5765 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 return -1;
5767 }
5768
5769 /* Here, dict does not actually need to be a PyDict; it could be anything
5770 that supports the __setitem__ attribute. */
5771 dict = self->stack->data[x - 1];
5772
5773 for (i = x + 1; i < len; i += 2) {
5774 key = self->stack->data[i - 1];
5775 value = self->stack->data[i];
5776 if (PyObject_SetItem(dict, key, value) < 0) {
5777 status = -1;
5778 break;
5779 }
5780 }
5781
5782 Pdata_clear(self->stack, x);
5783 return status;
5784}
5785
5786static int
5787load_setitem(UnpicklerObject *self)
5788{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790}
5791
5792static int
5793load_setitems(UnpicklerObject *self)
5794{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005795 Py_ssize_t i = marker(self);
5796 if (i < 0)
5797 return -1;
5798 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799}
5800
5801static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005802load_additems(UnpicklerObject *self)
5803{
5804 PyObject *set;
5805 Py_ssize_t mark, len, i;
5806
5807 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005808 if (mark < 0)
5809 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005810 len = Py_SIZE(self->stack);
5811 if (mark > len || mark <= 0)
5812 return stack_underflow();
5813 if (len == mark) /* nothing to do */
5814 return 0;
5815
5816 set = self->stack->data[mark - 1];
5817
5818 if (PySet_Check(set)) {
5819 PyObject *items;
5820 int status;
5821
5822 items = Pdata_poptuple(self->stack, mark);
5823 if (items == NULL)
5824 return -1;
5825
5826 status = _PySet_Update(set, items);
5827 Py_DECREF(items);
5828 return status;
5829 }
5830 else {
5831 PyObject *add_func;
5832 _Py_IDENTIFIER(add);
5833
5834 add_func = _PyObject_GetAttrId(set, &PyId_add);
5835 if (add_func == NULL)
5836 return -1;
5837 for (i = mark; i < len; i++) {
5838 PyObject *result;
5839 PyObject *item;
5840
5841 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005842 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005843 if (result == NULL) {
5844 Pdata_clear(self->stack, i + 1);
5845 Py_SIZE(self->stack) = mark;
5846 return -1;
5847 }
5848 Py_DECREF(result);
5849 }
5850 Py_SIZE(self->stack) = mark;
5851 }
5852
5853 return 0;
5854}
5855
5856static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005857load_build(UnpicklerObject *self)
5858{
5859 PyObject *state, *inst, *slotstate;
5860 PyObject *setstate;
5861 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005862 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863
5864 /* Stack is ... instance, state. We want to leave instance at
5865 * the stack top, possibly mutated via instance.__setstate__(state).
5866 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005867 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005868 return stack_underflow();
5869
5870 PDATA_POP(self->stack, state);
5871 if (state == NULL)
5872 return -1;
5873
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005874 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005876 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005877 if (setstate == NULL) {
5878 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5879 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005880 else {
5881 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005882 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005883 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005884 }
5885 else {
5886 PyObject *result;
5887
5888 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005889 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890 Py_DECREF(setstate);
5891 if (result == NULL)
5892 return -1;
5893 Py_DECREF(result);
5894 return 0;
5895 }
5896
5897 /* A default __setstate__. First see whether state embeds a
5898 * slot state dict too (a proto 2 addition).
5899 */
5900 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5901 PyObject *tmp = state;
5902
5903 state = PyTuple_GET_ITEM(tmp, 0);
5904 slotstate = PyTuple_GET_ITEM(tmp, 1);
5905 Py_INCREF(state);
5906 Py_INCREF(slotstate);
5907 Py_DECREF(tmp);
5908 }
5909 else
5910 slotstate = NULL;
5911
5912 /* Set inst.__dict__ from the state dict (if any). */
5913 if (state != Py_None) {
5914 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005915 PyObject *d_key, *d_value;
5916 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005917 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918
5919 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005920 PickleState *st = _Pickle_GetGlobalState();
5921 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005922 goto error;
5923 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005924 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005925 if (dict == NULL)
5926 goto error;
5927
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005928 i = 0;
5929 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5930 /* normally the keys for instance attributes are
5931 interned. we should try to do that here. */
5932 Py_INCREF(d_key);
5933 if (PyUnicode_CheckExact(d_key))
5934 PyUnicode_InternInPlace(&d_key);
5935 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5936 Py_DECREF(d_key);
5937 goto error;
5938 }
5939 Py_DECREF(d_key);
5940 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005941 Py_DECREF(dict);
5942 }
5943
5944 /* Also set instance attributes from the slotstate dict (if any). */
5945 if (slotstate != NULL) {
5946 PyObject *d_key, *d_value;
5947 Py_ssize_t i;
5948
5949 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005950 PickleState *st = _Pickle_GetGlobalState();
5951 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952 "slot state is not a dictionary");
5953 goto error;
5954 }
5955 i = 0;
5956 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5957 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5958 goto error;
5959 }
5960 }
5961
5962 if (0) {
5963 error:
5964 status = -1;
5965 }
5966
5967 Py_DECREF(state);
5968 Py_XDECREF(slotstate);
5969 return status;
5970}
5971
5972static int
5973load_mark(UnpicklerObject *self)
5974{
5975
5976 /* Note that we split the (pickle.py) stack into two stacks, an
5977 * object stack and a mark stack. Here we push a mark onto the
5978 * mark stack.
5979 */
5980
5981 if ((self->num_marks + 1) >= self->marks_size) {
5982 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005983
5984 /* Use the size_t type to check for overflow. */
5985 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005986 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005987 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005988 PyErr_NoMemory();
5989 return -1;
5990 }
5991
5992 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005993 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005994 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05005995 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
5996 if (self->marks == NULL) {
5997 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005998 PyErr_NoMemory();
5999 return -1;
6000 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006001 self->marks_size = (Py_ssize_t)alloc;
6002 }
6003
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006004 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005
6006 return 0;
6007}
6008
6009static int
6010load_reduce(UnpicklerObject *self)
6011{
6012 PyObject *callable = NULL;
6013 PyObject *argtup = NULL;
6014 PyObject *obj = NULL;
6015
6016 PDATA_POP(self->stack, argtup);
6017 if (argtup == NULL)
6018 return -1;
6019 PDATA_POP(self->stack, callable);
6020 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006021 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006022 Py_DECREF(callable);
6023 }
6024 Py_DECREF(argtup);
6025
6026 if (obj == NULL)
6027 return -1;
6028
6029 PDATA_PUSH(self->stack, obj, -1);
6030 return 0;
6031}
6032
6033/* Just raises an error if we don't know the protocol specified. PROTO
6034 * is the first opcode for protocols >= 2.
6035 */
6036static int
6037load_proto(UnpicklerObject *self)
6038{
6039 char *s;
6040 int i;
6041
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006042 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006043 return -1;
6044
6045 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006046 if (i <= HIGHEST_PROTOCOL) {
6047 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006049 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006050
6051 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6052 return -1;
6053}
6054
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006055static int
6056load_frame(UnpicklerObject *self)
6057{
6058 char *s;
6059 Py_ssize_t frame_len;
6060
6061 if (_Unpickler_Read(self, &s, 8) < 0)
6062 return -1;
6063
6064 frame_len = calc_binsize(s, 8);
6065 if (frame_len < 0) {
6066 PyErr_Format(PyExc_OverflowError,
6067 "FRAME length exceeds system's maximum of %zd bytes",
6068 PY_SSIZE_T_MAX);
6069 return -1;
6070 }
6071
6072 if (_Unpickler_Read(self, &s, frame_len) < 0)
6073 return -1;
6074
6075 /* Rewind to start of frame */
6076 self->next_read_idx -= frame_len;
6077 return 0;
6078}
6079
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080static PyObject *
6081load(UnpicklerObject *self)
6082{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006083 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006084 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006085
6086 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006087 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006088 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006089 Pdata_clear(self->stack, 0);
6090
6091 /* Convenient macros for the dispatch while-switch loop just below. */
6092#define OP(opcode, load_func) \
6093 case opcode: if (load_func(self) < 0) break; continue;
6094
6095#define OP_ARG(opcode, load_func, arg) \
6096 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6097
6098 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006099 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006100 break;
6101
6102 switch ((enum opcode)s[0]) {
6103 OP(NONE, load_none)
6104 OP(BININT, load_binint)
6105 OP(BININT1, load_binint1)
6106 OP(BININT2, load_binint2)
6107 OP(INT, load_int)
6108 OP(LONG, load_long)
6109 OP_ARG(LONG1, load_counted_long, 1)
6110 OP_ARG(LONG4, load_counted_long, 4)
6111 OP(FLOAT, load_float)
6112 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006113 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6114 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6115 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6116 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6117 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006118 OP(STRING, load_string)
6119 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006120 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6121 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6122 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006123 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6124 OP_ARG(TUPLE1, load_counted_tuple, 1)
6125 OP_ARG(TUPLE2, load_counted_tuple, 2)
6126 OP_ARG(TUPLE3, load_counted_tuple, 3)
6127 OP(TUPLE, load_tuple)
6128 OP(EMPTY_LIST, load_empty_list)
6129 OP(LIST, load_list)
6130 OP(EMPTY_DICT, load_empty_dict)
6131 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006132 OP(EMPTY_SET, load_empty_set)
6133 OP(ADDITEMS, load_additems)
6134 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006135 OP(OBJ, load_obj)
6136 OP(INST, load_inst)
6137 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006138 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006139 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006140 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006141 OP(APPEND, load_append)
6142 OP(APPENDS, load_appends)
6143 OP(BUILD, load_build)
6144 OP(DUP, load_dup)
6145 OP(BINGET, load_binget)
6146 OP(LONG_BINGET, load_long_binget)
6147 OP(GET, load_get)
6148 OP(MARK, load_mark)
6149 OP(BINPUT, load_binput)
6150 OP(LONG_BINPUT, load_long_binput)
6151 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006152 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006153 OP(POP, load_pop)
6154 OP(POP_MARK, load_pop_mark)
6155 OP(SETITEM, load_setitem)
6156 OP(SETITEMS, load_setitems)
6157 OP(PERSID, load_persid)
6158 OP(BINPERSID, load_binpersid)
6159 OP(REDUCE, load_reduce)
6160 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006161 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006162 OP_ARG(EXT1, load_extension, 1)
6163 OP_ARG(EXT2, load_extension, 2)
6164 OP_ARG(EXT4, load_extension, 4)
6165 OP_ARG(NEWTRUE, load_bool, Py_True)
6166 OP_ARG(NEWFALSE, load_bool, Py_False)
6167
6168 case STOP:
6169 break;
6170
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006172 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006173 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006174 }
6175 else {
6176 PickleState *st = _Pickle_GetGlobalState();
6177 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006178 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006179 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 return NULL;
6181 }
6182
6183 break; /* and we are done! */
6184 }
6185
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006186 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006187 return NULL;
6188 }
6189
Victor Stinner2ae57e32013-10-31 13:39:23 +01006190 if (_Unpickler_SkipConsumed(self) < 0)
6191 return NULL;
6192
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006193 PDATA_POP(self->stack, value);
6194 return value;
6195}
6196
Larry Hastings61272b72014-01-07 12:41:53 -08006197/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006198
6199_pickle.Unpickler.load
6200
6201Load a pickle.
6202
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006203Read a pickled object representation from the open file object given
6204in the constructor, and return the reconstituted object hierarchy
6205specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006206[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006207
Larry Hastings3cceb382014-01-04 11:09:09 -08006208static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006209_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006210/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006212 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006213
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006214 /* Check whether the Unpickler was initialized correctly. This prevents
6215 segfaulting if a subclass overridden __init__ with a function that does
6216 not call Unpickler.__init__(). Here, we simply ensure that self->read
6217 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006218 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006219 PickleState *st = _Pickle_GetGlobalState();
6220 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006221 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006222 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006223 return NULL;
6224 }
6225
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006226 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006227}
6228
6229/* The name of find_class() is misleading. In newer pickle protocols, this
6230 function is used for loading any global (i.e., functions), not just
6231 classes. The name is kept only for backward compatibility. */
6232
Larry Hastings61272b72014-01-07 12:41:53 -08006233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006234
6235_pickle.Unpickler.find_class
6236
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006237 module_name: object
6238 global_name: object
6239 /
6240
6241Return an object from a specified module.
6242
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006243If necessary, the module will be imported. Subclasses may override
6244this method (e.g. to restrict unpickling of arbitrary classes and
6245functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006246
6247This method is called whenever a class or a function object is
6248needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006249[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006250
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006251static PyObject *
6252_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006253/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254{
6255 PyObject *global;
6256 PyObject *modules_dict;
6257 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006258 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006259
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006260 /* Try to map the old names used in Python 2.x to the new ones used in
6261 Python 3.x. We do this only with old pickle protocols and when the
6262 user has not disabled the feature. */
6263 if (self->proto < 3 && self->fix_imports) {
6264 PyObject *key;
6265 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006266 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006267
6268 /* Check if the global (i.e., a function or a class) was renamed
6269 or moved to another module. */
6270 key = PyTuple_Pack(2, module_name, global_name);
6271 if (key == NULL)
6272 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006273 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006274 Py_DECREF(key);
6275 if (item) {
6276 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6277 PyErr_Format(PyExc_RuntimeError,
6278 "_compat_pickle.NAME_MAPPING values should be "
6279 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6280 return NULL;
6281 }
6282 module_name = PyTuple_GET_ITEM(item, 0);
6283 global_name = PyTuple_GET_ITEM(item, 1);
6284 if (!PyUnicode_Check(module_name) ||
6285 !PyUnicode_Check(global_name)) {
6286 PyErr_Format(PyExc_RuntimeError,
6287 "_compat_pickle.NAME_MAPPING values should be "
6288 "pairs of str, not (%.200s, %.200s)",
6289 Py_TYPE(module_name)->tp_name,
6290 Py_TYPE(global_name)->tp_name);
6291 return NULL;
6292 }
6293 }
6294 else if (PyErr_Occurred()) {
6295 return NULL;
6296 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006297 else {
6298 /* Check if the module was renamed. */
6299 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6300 if (item) {
6301 if (!PyUnicode_Check(item)) {
6302 PyErr_Format(PyExc_RuntimeError,
6303 "_compat_pickle.IMPORT_MAPPING values should be "
6304 "strings, not %.200s", Py_TYPE(item)->tp_name);
6305 return NULL;
6306 }
6307 module_name = item;
6308 }
6309 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006310 return NULL;
6311 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006312 }
6313 }
6314
Victor Stinnerbb520202013-11-06 22:40:41 +01006315 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006316 if (modules_dict == NULL) {
6317 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006319 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006320
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006321 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006322 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006323 if (PyErr_Occurred())
6324 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006325 module = PyImport_Import(module_name);
6326 if (module == NULL)
6327 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006328 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 Py_DECREF(module);
6330 }
Victor Stinner121aab42011-09-29 23:40:53 +02006331 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006332 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006333 }
6334 return global;
6335}
6336
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006337/*[clinic input]
6338
6339_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6340
6341Returns size in memory, in bytes.
6342[clinic start generated code]*/
6343
6344static Py_ssize_t
6345_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6346/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6347{
6348 Py_ssize_t res;
6349
6350 res = sizeof(UnpicklerObject);
6351 if (self->memo != NULL)
6352 res += self->memo_size * sizeof(PyObject *);
6353 if (self->marks != NULL)
6354 res += self->marks_size * sizeof(Py_ssize_t);
6355 if (self->input_line != NULL)
6356 res += strlen(self->input_line) + 1;
6357 if (self->encoding != NULL)
6358 res += strlen(self->encoding) + 1;
6359 if (self->errors != NULL)
6360 res += strlen(self->errors) + 1;
6361 return res;
6362}
6363
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006364static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006365 _PICKLE_UNPICKLER_LOAD_METHODDEF
6366 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006367 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006368 {NULL, NULL} /* sentinel */
6369};
6370
6371static void
6372Unpickler_dealloc(UnpicklerObject *self)
6373{
6374 PyObject_GC_UnTrack((PyObject *)self);
6375 Py_XDECREF(self->readline);
6376 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006377 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006378 Py_XDECREF(self->stack);
6379 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006380 if (self->buffer.buf != NULL) {
6381 PyBuffer_Release(&self->buffer);
6382 self->buffer.buf = NULL;
6383 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006384
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006385 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006386 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006387 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006388 PyMem_Free(self->encoding);
6389 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006390
6391 Py_TYPE(self)->tp_free((PyObject *)self);
6392}
6393
6394static int
6395Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6396{
6397 Py_VISIT(self->readline);
6398 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006399 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006400 Py_VISIT(self->stack);
6401 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402 return 0;
6403}
6404
6405static int
6406Unpickler_clear(UnpicklerObject *self)
6407{
6408 Py_CLEAR(self->readline);
6409 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006410 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006411 Py_CLEAR(self->stack);
6412 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006413 if (self->buffer.buf != NULL) {
6414 PyBuffer_Release(&self->buffer);
6415 self->buffer.buf = NULL;
6416 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006417
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006418 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006419 PyMem_Free(self->marks);
6420 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006421 PyMem_Free(self->input_line);
6422 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006423 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006424 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006425 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006426 self->errors = NULL;
6427
6428 return 0;
6429}
6430
Larry Hastings61272b72014-01-07 12:41:53 -08006431/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006432
6433_pickle.Unpickler.__init__
6434
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006435 file: object
6436 *
6437 fix_imports: bool = True
6438 encoding: str = 'ASCII'
6439 errors: str = 'strict'
6440
6441This takes a binary file for reading a pickle data stream.
6442
6443The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006444protocol argument is needed. Bytes past the pickled object's
6445representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006446
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006447The argument *file* must have two methods, a read() method that takes
6448an integer argument, and a readline() method that requires no
6449arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006450binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006451other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006452
6453Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6454which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006455generated by Python 2. If *fix_imports* is True, pickle will try to
6456map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006457*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006458instances pickled by Python 2; these default to 'ASCII' and 'strict',
6459respectively. The *encoding* can be 'bytes' to read these 8-bit
6460string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006461[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006462
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006463static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006464_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Martin Panter7462b6492015-11-02 03:37:02 +00006465/*[clinic end generated code: output=b9ed1d84d315f3b5 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006466{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006467 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006469 /* In case of multiple __init__() calls, clear previous content. */
6470 if (self->read != NULL)
6471 (void)Unpickler_clear(self);
6472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006473 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006474 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006476 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006477 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006478
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006479 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006480 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006481 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006483 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006484 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6485 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006486 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006487 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006488 }
6489 else {
6490 self->pers_func = NULL;
6491 }
6492
6493 self->stack = (Pdata *)Pdata_New();
6494 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006495 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006496
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006497 self->memo_size = 32;
6498 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006499 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006500 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006501
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006502 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006503
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006504 return 0;
6505}
6506
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006508/* Define a proxy object for the Unpickler's internal memo object. This is to
6509 * avoid breaking code like:
6510 * unpickler.memo.clear()
6511 * and
6512 * unpickler.memo = saved_memo
6513 * Is this a good idea? Not really, but we don't want to break code that uses
6514 * it. Note that we don't implement the entire mapping API here. This is
6515 * intentional, as these should be treated as black-box implementation details.
6516 *
6517 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006518 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006519 */
6520
Larry Hastings61272b72014-01-07 12:41:53 -08006521/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006522_pickle.UnpicklerMemoProxy.clear
6523
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006524Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006525[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006526
Larry Hastings3cceb382014-01-04 11:09:09 -08006527static PyObject *
6528_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006529/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006530{
6531 _Unpickler_MemoCleanup(self->unpickler);
6532 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6533 if (self->unpickler->memo == NULL)
6534 return NULL;
6535 Py_RETURN_NONE;
6536}
6537
Larry Hastings61272b72014-01-07 12:41:53 -08006538/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006539_pickle.UnpicklerMemoProxy.copy
6540
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006541Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006542[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006543
Larry Hastings3cceb382014-01-04 11:09:09 -08006544static PyObject *
6545_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006546/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006547{
6548 Py_ssize_t i;
6549 PyObject *new_memo = PyDict_New();
6550 if (new_memo == NULL)
6551 return NULL;
6552
6553 for (i = 0; i < self->unpickler->memo_size; i++) {
6554 int status;
6555 PyObject *key, *value;
6556
6557 value = self->unpickler->memo[i];
6558 if (value == NULL)
6559 continue;
6560
6561 key = PyLong_FromSsize_t(i);
6562 if (key == NULL)
6563 goto error;
6564 status = PyDict_SetItem(new_memo, key, value);
6565 Py_DECREF(key);
6566 if (status < 0)
6567 goto error;
6568 }
6569 return new_memo;
6570
6571error:
6572 Py_DECREF(new_memo);
6573 return NULL;
6574}
6575
Larry Hastings61272b72014-01-07 12:41:53 -08006576/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577_pickle.UnpicklerMemoProxy.__reduce__
6578
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006580[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006581
Larry Hastings3cceb382014-01-04 11:09:09 -08006582static PyObject *
6583_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006584/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006585{
6586 PyObject *reduce_value;
6587 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006588 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006589 if (contents == NULL)
6590 return NULL;
6591
6592 reduce_value = PyTuple_New(2);
6593 if (reduce_value == NULL) {
6594 Py_DECREF(contents);
6595 return NULL;
6596 }
6597 constructor_args = PyTuple_New(1);
6598 if (constructor_args == NULL) {
6599 Py_DECREF(contents);
6600 Py_DECREF(reduce_value);
6601 return NULL;
6602 }
6603 PyTuple_SET_ITEM(constructor_args, 0, contents);
6604 Py_INCREF((PyObject *)&PyDict_Type);
6605 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6606 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6607 return reduce_value;
6608}
6609
6610static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006611 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6612 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6613 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006614 {NULL, NULL} /* sentinel */
6615};
6616
6617static void
6618UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6619{
6620 PyObject_GC_UnTrack(self);
6621 Py_XDECREF(self->unpickler);
6622 PyObject_GC_Del((PyObject *)self);
6623}
6624
6625static int
6626UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6627 visitproc visit, void *arg)
6628{
6629 Py_VISIT(self->unpickler);
6630 return 0;
6631}
6632
6633static int
6634UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6635{
6636 Py_CLEAR(self->unpickler);
6637 return 0;
6638}
6639
6640static PyTypeObject UnpicklerMemoProxyType = {
6641 PyVarObject_HEAD_INIT(NULL, 0)
6642 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6643 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6644 0,
6645 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6646 0, /* tp_print */
6647 0, /* tp_getattr */
6648 0, /* tp_setattr */
6649 0, /* tp_compare */
6650 0, /* tp_repr */
6651 0, /* tp_as_number */
6652 0, /* tp_as_sequence */
6653 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006654 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006655 0, /* tp_call */
6656 0, /* tp_str */
6657 PyObject_GenericGetAttr, /* tp_getattro */
6658 PyObject_GenericSetAttr, /* tp_setattro */
6659 0, /* tp_as_buffer */
6660 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6661 0, /* tp_doc */
6662 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6663 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6664 0, /* tp_richcompare */
6665 0, /* tp_weaklistoffset */
6666 0, /* tp_iter */
6667 0, /* tp_iternext */
6668 unpicklerproxy_methods, /* tp_methods */
6669};
6670
6671static PyObject *
6672UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6673{
6674 UnpicklerMemoProxyObject *self;
6675
6676 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6677 &UnpicklerMemoProxyType);
6678 if (self == NULL)
6679 return NULL;
6680 Py_INCREF(unpickler);
6681 self->unpickler = unpickler;
6682 PyObject_GC_Track(self);
6683 return (PyObject *)self;
6684}
6685
6686/*****************************************************************************/
6687
6688
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006689static PyObject *
6690Unpickler_get_memo(UnpicklerObject *self)
6691{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006692 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006693}
6694
6695static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006696Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006697{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006698 PyObject **new_memo;
6699 Py_ssize_t new_memo_size = 0;
6700 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006701
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006702 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006703 PyErr_SetString(PyExc_TypeError,
6704 "attribute deletion is not supported");
6705 return -1;
6706 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006707
6708 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6709 UnpicklerObject *unpickler =
6710 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6711
6712 new_memo_size = unpickler->memo_size;
6713 new_memo = _Unpickler_NewMemo(new_memo_size);
6714 if (new_memo == NULL)
6715 return -1;
6716
6717 for (i = 0; i < new_memo_size; i++) {
6718 Py_XINCREF(unpickler->memo[i]);
6719 new_memo[i] = unpickler->memo[i];
6720 }
6721 }
6722 else if (PyDict_Check(obj)) {
6723 Py_ssize_t i = 0;
6724 PyObject *key, *value;
6725
6726 new_memo_size = PyDict_Size(obj);
6727 new_memo = _Unpickler_NewMemo(new_memo_size);
6728 if (new_memo == NULL)
6729 return -1;
6730
6731 while (PyDict_Next(obj, &i, &key, &value)) {
6732 Py_ssize_t idx;
6733 if (!PyLong_Check(key)) {
6734 PyErr_SetString(PyExc_TypeError,
6735 "memo key must be integers");
6736 goto error;
6737 }
6738 idx = PyLong_AsSsize_t(key);
6739 if (idx == -1 && PyErr_Occurred())
6740 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006741 if (idx < 0) {
6742 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006743 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006744 goto error;
6745 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006746 if (_Unpickler_MemoPut(self, idx, value) < 0)
6747 goto error;
6748 }
6749 }
6750 else {
6751 PyErr_Format(PyExc_TypeError,
6752 "'memo' attribute must be an UnpicklerMemoProxy object"
6753 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006754 return -1;
6755 }
6756
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006757 _Unpickler_MemoCleanup(self);
6758 self->memo_size = new_memo_size;
6759 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006760
6761 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006762
6763 error:
6764 if (new_memo_size) {
6765 i = new_memo_size;
6766 while (--i >= 0) {
6767 Py_XDECREF(new_memo[i]);
6768 }
6769 PyMem_FREE(new_memo);
6770 }
6771 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006772}
6773
6774static PyObject *
6775Unpickler_get_persload(UnpicklerObject *self)
6776{
6777 if (self->pers_func == NULL)
6778 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6779 else
6780 Py_INCREF(self->pers_func);
6781 return self->pers_func;
6782}
6783
6784static int
6785Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6786{
6787 PyObject *tmp;
6788
6789 if (value == NULL) {
6790 PyErr_SetString(PyExc_TypeError,
6791 "attribute deletion is not supported");
6792 return -1;
6793 }
6794 if (!PyCallable_Check(value)) {
6795 PyErr_SetString(PyExc_TypeError,
6796 "persistent_load must be a callable taking "
6797 "one argument");
6798 return -1;
6799 }
6800
6801 tmp = self->pers_func;
6802 Py_INCREF(value);
6803 self->pers_func = value;
6804 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6805
6806 return 0;
6807}
6808
6809static PyGetSetDef Unpickler_getsets[] = {
6810 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6811 {"persistent_load", (getter)Unpickler_get_persload,
6812 (setter)Unpickler_set_persload},
6813 {NULL}
6814};
6815
6816static PyTypeObject Unpickler_Type = {
6817 PyVarObject_HEAD_INIT(NULL, 0)
6818 "_pickle.Unpickler", /*tp_name*/
6819 sizeof(UnpicklerObject), /*tp_basicsize*/
6820 0, /*tp_itemsize*/
6821 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6822 0, /*tp_print*/
6823 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006824 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006825 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006826 0, /*tp_repr*/
6827 0, /*tp_as_number*/
6828 0, /*tp_as_sequence*/
6829 0, /*tp_as_mapping*/
6830 0, /*tp_hash*/
6831 0, /*tp_call*/
6832 0, /*tp_str*/
6833 0, /*tp_getattro*/
6834 0, /*tp_setattro*/
6835 0, /*tp_as_buffer*/
6836 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006837 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006838 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6839 (inquiry)Unpickler_clear, /*tp_clear*/
6840 0, /*tp_richcompare*/
6841 0, /*tp_weaklistoffset*/
6842 0, /*tp_iter*/
6843 0, /*tp_iternext*/
6844 Unpickler_methods, /*tp_methods*/
6845 0, /*tp_members*/
6846 Unpickler_getsets, /*tp_getset*/
6847 0, /*tp_base*/
6848 0, /*tp_dict*/
6849 0, /*tp_descr_get*/
6850 0, /*tp_descr_set*/
6851 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006852 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006853 PyType_GenericAlloc, /*tp_alloc*/
6854 PyType_GenericNew, /*tp_new*/
6855 PyObject_GC_Del, /*tp_free*/
6856 0, /*tp_is_gc*/
6857};
6858
Larry Hastings61272b72014-01-07 12:41:53 -08006859/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006860
6861_pickle.dump
6862
6863 obj: object
6864 file: object
6865 protocol: object = NULL
6866 *
6867 fix_imports: bool = True
6868
6869Write a pickled representation of obj to the open file object file.
6870
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006871This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6872be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006873
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006874The optional *protocol* argument tells the pickler to use the given
6875protocol supported protocols are 0, 1, 2, 3 and 4. The default
6876protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006877
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006878Specifying a negative protocol version selects the highest protocol
6879version supported. The higher the protocol used, the more recent the
6880version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006881
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006882The *file* argument must have a write() method that accepts a single
6883bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00006884writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006885this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006886
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006887If *fix_imports* is True and protocol is less than 3, pickle will try
6888to map the new Python 3 names to the old module names used in Python
68892, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006890[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006891
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006892static PyObject *
6893_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Martin Panter7462b6492015-11-02 03:37:02 +00006894/*[clinic end generated code: output=a606e626d553850d input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006895{
6896 PicklerObject *pickler = _Pickler_New();
6897
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006898 if (pickler == NULL)
6899 return NULL;
6900
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006901 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006902 goto error;
6903
6904 if (_Pickler_SetOutputStream(pickler, file) < 0)
6905 goto error;
6906
6907 if (dump(pickler, obj) < 0)
6908 goto error;
6909
6910 if (_Pickler_FlushToFile(pickler) < 0)
6911 goto error;
6912
6913 Py_DECREF(pickler);
6914 Py_RETURN_NONE;
6915
6916 error:
6917 Py_XDECREF(pickler);
6918 return NULL;
6919}
6920
Larry Hastings61272b72014-01-07 12:41:53 -08006921/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006922
6923_pickle.dumps
6924
6925 obj: object
6926 protocol: object = NULL
6927 *
6928 fix_imports: bool = True
6929
6930Return the pickled representation of the object as a bytes object.
6931
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006932The optional *protocol* argument tells the pickler to use the given
6933protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6934protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006935
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006936Specifying a negative protocol version selects the highest protocol
6937version supported. The higher the protocol used, the more recent the
6938version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006939
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006940If *fix_imports* is True and *protocol* is less than 3, pickle will
6941try to map the new Python 3 names to the old module names used in
6942Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006943[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006944
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006945static PyObject *
6946_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006947/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006948{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006949 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006950 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006951
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006952 if (pickler == NULL)
6953 return NULL;
6954
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006955 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006956 goto error;
6957
6958 if (dump(pickler, obj) < 0)
6959 goto error;
6960
6961 result = _Pickler_GetString(pickler);
6962 Py_DECREF(pickler);
6963 return result;
6964
6965 error:
6966 Py_XDECREF(pickler);
6967 return NULL;
6968}
6969
Larry Hastings61272b72014-01-07 12:41:53 -08006970/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006971
6972_pickle.load
6973
6974 file: object
6975 *
6976 fix_imports: bool = True
6977 encoding: str = 'ASCII'
6978 errors: str = 'strict'
6979
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006980Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006981
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006982This is equivalent to ``Unpickler(file).load()``, but may be more
6983efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006984
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006985The protocol version of the pickle is detected automatically, so no
6986protocol argument is needed. Bytes past the pickled object's
6987representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006988
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006989The argument *file* must have two methods, a read() method that takes
6990an integer argument, and a readline() method that requires no
6991arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006992binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006993other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006994
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006995Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6996which are used to control compatiblity support for pickle stream
6997generated by Python 2. If *fix_imports* is True, pickle will try to
6998map the old Python 2 names to the new names used in Python 3. The
6999*encoding* and *errors* tell pickle how to decode 8-bit string
7000instances pickled by Python 2; these default to 'ASCII' and 'strict',
7001respectively. The *encoding* can be 'bytes' to read these 8-bit
7002string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007003[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007005static PyObject *
7006_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Martin Panter7462b6492015-11-02 03:37:02 +00007007/*[clinic end generated code: output=568c61356c172654 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007010 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 if (unpickler == NULL)
7013 return NULL;
7014
7015 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7016 goto error;
7017
7018 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7019 goto error;
7020
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007021 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007022
7023 result = load(unpickler);
7024 Py_DECREF(unpickler);
7025 return result;
7026
7027 error:
7028 Py_XDECREF(unpickler);
7029 return NULL;
7030}
7031
Larry Hastings61272b72014-01-07 12:41:53 -08007032/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007033
7034_pickle.loads
7035
7036 data: object
7037 *
7038 fix_imports: bool = True
7039 encoding: str = 'ASCII'
7040 errors: str = 'strict'
7041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007044The protocol version of the pickle is detected automatically, so no
7045protocol argument is needed. Bytes past the pickled object's
7046representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007048Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7049which are used to control compatiblity support for pickle stream
7050generated by Python 2. If *fix_imports* is True, pickle will try to
7051map the old Python 2 names to the new names used in Python 3. The
7052*encoding* and *errors* tell pickle how to decode 8-bit string
7053instances pickled by Python 2; these default to 'ASCII' and 'strict',
7054respectively. The *encoding* can be 'bytes' to read these 8-bit
7055string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007056[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007057
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058static PyObject *
7059_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007060/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007062 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007063 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065 if (unpickler == NULL)
7066 return NULL;
7067
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007068 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007069 goto error;
7070
7071 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7072 goto error;
7073
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007074 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007075
7076 result = load(unpickler);
7077 Py_DECREF(unpickler);
7078 return result;
7079
7080 error:
7081 Py_XDECREF(unpickler);
7082 return NULL;
7083}
7084
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007085static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007086 _PICKLE_DUMP_METHODDEF
7087 _PICKLE_DUMPS_METHODDEF
7088 _PICKLE_LOAD_METHODDEF
7089 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007090 {NULL, NULL} /* sentinel */
7091};
7092
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007093static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007094pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007095{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007096 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007097 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007098}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007099
Stefan Krahf483b0f2013-12-14 13:43:10 +01007100static void
7101pickle_free(PyObject *m)
7102{
7103 _Pickle_ClearState(_Pickle_GetState(m));
7104}
7105
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007106static int
7107pickle_traverse(PyObject *m, visitproc visit, void *arg)
7108{
7109 PickleState *st = _Pickle_GetState(m);
7110 Py_VISIT(st->PickleError);
7111 Py_VISIT(st->PicklingError);
7112 Py_VISIT(st->UnpicklingError);
7113 Py_VISIT(st->dispatch_table);
7114 Py_VISIT(st->extension_registry);
7115 Py_VISIT(st->extension_cache);
7116 Py_VISIT(st->inverted_registry);
7117 Py_VISIT(st->name_mapping_2to3);
7118 Py_VISIT(st->import_mapping_2to3);
7119 Py_VISIT(st->name_mapping_3to2);
7120 Py_VISIT(st->import_mapping_3to2);
7121 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007122 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007123}
7124
7125static struct PyModuleDef _picklemodule = {
7126 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007127 "_pickle", /* m_name */
7128 pickle_module_doc, /* m_doc */
7129 sizeof(PickleState), /* m_size */
7130 pickle_methods, /* m_methods */
7131 NULL, /* m_reload */
7132 pickle_traverse, /* m_traverse */
7133 pickle_clear, /* m_clear */
7134 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007135};
7136
7137PyMODINIT_FUNC
7138PyInit__pickle(void)
7139{
7140 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007141 PickleState *st;
7142
7143 m = PyState_FindModule(&_picklemodule);
7144 if (m) {
7145 Py_INCREF(m);
7146 return m;
7147 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148
7149 if (PyType_Ready(&Unpickler_Type) < 0)
7150 return NULL;
7151 if (PyType_Ready(&Pickler_Type) < 0)
7152 return NULL;
7153 if (PyType_Ready(&Pdata_Type) < 0)
7154 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007155 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7156 return NULL;
7157 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7158 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007159
7160 /* Create the module and add the functions. */
7161 m = PyModule_Create(&_picklemodule);
7162 if (m == NULL)
7163 return NULL;
7164
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007165 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007166 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7167 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007168 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007169 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7170 return NULL;
7171
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007172 st = _Pickle_GetState(m);
7173
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007174 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007175 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7176 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007177 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007178 st->PicklingError = \
7179 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7180 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007181 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007182 st->UnpicklingError = \
7183 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7184 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007185 return NULL;
7186
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007187 Py_INCREF(st->PickleError);
7188 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007189 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007190 Py_INCREF(st->PicklingError);
7191 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007192 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007193 Py_INCREF(st->UnpicklingError);
7194 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007195 return NULL;
7196
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007197 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007198 return NULL;
7199
7200 return m;
7201}