blob: 24524a6ca5a63d399ecb9f90bfd31fc040c076e5 [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*/
378 0, /*tp_itemsize*/
379 (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;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200423 size_t allocated = (size_t)self->allocated;
424 size_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 */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200428 if (new_allocated > (size_t)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;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200431 if (new_allocated > ((size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000432 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000433 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
434 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000435 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436
437 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200438 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 return 0;
440
441 nomemory:
442 PyErr_NoMemory();
443 return -1;
444}
445
446/* D is a Pdata*. Pop the topmost element and store it into V, which
447 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
448 * is raised and V is set to NULL.
449 */
450static PyObject *
451Pdata_pop(Pdata *self)
452{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800453 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800455 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000456 return NULL;
457 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000458 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000459}
460#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
461
462static int
463Pdata_push(Pdata *self, PyObject *obj)
464{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000465 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000466 return -1;
467 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000468 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000469 return 0;
470}
471
472/* Push an object on stack, transferring its ownership to the stack. */
473#define PDATA_PUSH(D, O, ER) do { \
474 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
475
476/* Push an object on stack, adding a new reference to the object. */
477#define PDATA_APPEND(D, O, ER) do { \
478 Py_INCREF((O)); \
479 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
480
481static PyObject *
482Pdata_poptuple(Pdata *self, Py_ssize_t start)
483{
484 PyObject *tuple;
485 Py_ssize_t len, i, j;
486
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000487 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000488 tuple = PyTuple_New(len);
489 if (tuple == NULL)
490 return NULL;
491 for (i = start, j = 0; j < len; i++, j++)
492 PyTuple_SET_ITEM(tuple, j, self->data[i]);
493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000494 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000495 return tuple;
496}
497
498static PyObject *
499Pdata_poplist(Pdata *self, Py_ssize_t start)
500{
501 PyObject *list;
502 Py_ssize_t len, i, j;
503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000504 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000505 list = PyList_New(len);
506 if (list == NULL)
507 return NULL;
508 for (i = start, j = 0; j < len; i++, j++)
509 PyList_SET_ITEM(list, j, self->data[i]);
510
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000511 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000512 return list;
513}
514
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000515typedef struct {
516 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200517 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000518} PyMemoEntry;
519
520typedef struct {
521 Py_ssize_t mt_mask;
522 Py_ssize_t mt_used;
523 Py_ssize_t mt_allocated;
524 PyMemoEntry *mt_table;
525} PyMemoTable;
526
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527typedef struct PicklerObject {
528 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000530 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000531 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000532 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100533 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000534
535 PyObject *write; /* write() method of the output stream. */
536 PyObject *output_buffer; /* Write into a local bytearray buffer before
537 flushing to the stream. */
538 Py_ssize_t output_len; /* Length of output_buffer. */
539 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000540 int proto; /* Pickle protocol number, >= 0 */
541 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100542 int framing; /* True when framing is enabled, proto >= 4 */
543 Py_ssize_t frame_start; /* Position in output_buffer where the
544 where the current frame begins. -1 if there
545 is no frame currently open. */
546
547 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000548 int fast; /* Enable fast mode if set to a true value.
549 The fast mode disable the usage of memo,
550 therefore speeding the pickling process by
551 not generating superfluous PUT opcodes. It
552 should not be used if with self-referential
553 objects. */
554 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000555 int fix_imports; /* Indicate whether Pickler should fix
556 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000557 PyObject *fast_memo;
558} PicklerObject;
559
560typedef struct UnpicklerObject {
561 PyObject_HEAD
562 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000563
564 /* The unpickler memo is just an array of PyObject *s. Using a dict
565 is unnecessary, since the keys are contiguous ints. */
566 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100567 Py_ssize_t memo_size; /* Capacity of the memo array */
568 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000569
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000571
572 Py_buffer buffer;
573 char *input_buffer;
574 char *input_line;
575 Py_ssize_t input_len;
576 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000577 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100578
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000579 PyObject *read; /* read() method of the input stream. */
580 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000581 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000583 char *encoding; /* Name of the encoding to be used for
584 decoding strings pickled using Python
585 2.x. The default value is "ASCII" */
586 char *errors; /* Name of errors handling scheme to used when
587 decoding strings. The default value is
588 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500589 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000590 objects. */
591 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
592 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000593 int proto; /* Protocol of the pickle loaded. */
594 int fix_imports; /* Indicate whether Unpickler should fix
595 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000596} UnpicklerObject;
597
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200598typedef struct {
599 PyObject_HEAD
600 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
601} PicklerMemoProxyObject;
602
603typedef struct {
604 PyObject_HEAD
605 UnpicklerObject *unpickler;
606} UnpicklerMemoProxyObject;
607
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000608/* Forward declarations */
609static int save(PicklerObject *, PyObject *, int);
610static int save_reduce(PicklerObject *, PyObject *, PyObject *);
611static PyTypeObject Pickler_Type;
612static PyTypeObject Unpickler_Type;
613
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200614#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000616/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300617 A custom hashtable mapping void* to Python ints. This is used by the pickler
618 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000619 a bunch of unnecessary object creation. This makes a huge performance
620 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000621
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000622#define MT_MINSIZE 8
623#define PERTURB_SHIFT 5
624
625
626static PyMemoTable *
627PyMemoTable_New(void)
628{
629 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
630 if (memo == NULL) {
631 PyErr_NoMemory();
632 return NULL;
633 }
634
635 memo->mt_used = 0;
636 memo->mt_allocated = MT_MINSIZE;
637 memo->mt_mask = MT_MINSIZE - 1;
638 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
639 if (memo->mt_table == NULL) {
640 PyMem_FREE(memo);
641 PyErr_NoMemory();
642 return NULL;
643 }
644 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
645
646 return memo;
647}
648
649static PyMemoTable *
650PyMemoTable_Copy(PyMemoTable *self)
651{
652 Py_ssize_t i;
653 PyMemoTable *new = PyMemoTable_New();
654 if (new == NULL)
655 return NULL;
656
657 new->mt_used = self->mt_used;
658 new->mt_allocated = self->mt_allocated;
659 new->mt_mask = self->mt_mask;
660 /* The table we get from _New() is probably smaller than we wanted.
661 Free it and allocate one that's the right size. */
662 PyMem_FREE(new->mt_table);
663 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
664 if (new->mt_table == NULL) {
665 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200666 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000667 return NULL;
668 }
669 for (i = 0; i < self->mt_allocated; i++) {
670 Py_XINCREF(self->mt_table[i].me_key);
671 }
672 memcpy(new->mt_table, self->mt_table,
673 sizeof(PyMemoEntry) * self->mt_allocated);
674
675 return new;
676}
677
678static Py_ssize_t
679PyMemoTable_Size(PyMemoTable *self)
680{
681 return self->mt_used;
682}
683
684static int
685PyMemoTable_Clear(PyMemoTable *self)
686{
687 Py_ssize_t i = self->mt_allocated;
688
689 while (--i >= 0) {
690 Py_XDECREF(self->mt_table[i].me_key);
691 }
692 self->mt_used = 0;
693 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
694 return 0;
695}
696
697static void
698PyMemoTable_Del(PyMemoTable *self)
699{
700 if (self == NULL)
701 return;
702 PyMemoTable_Clear(self);
703
704 PyMem_FREE(self->mt_table);
705 PyMem_FREE(self);
706}
707
708/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
709 can be considerably simpler than dictobject.c's lookdict(). */
710static PyMemoEntry *
711_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
712{
713 size_t i;
714 size_t perturb;
715 size_t mask = (size_t)self->mt_mask;
716 PyMemoEntry *table = self->mt_table;
717 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000718 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000719
720 i = hash & mask;
721 entry = &table[i];
722 if (entry->me_key == NULL || entry->me_key == key)
723 return entry;
724
725 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
726 i = (i << 2) + i + perturb + 1;
727 entry = &table[i & mask];
728 if (entry->me_key == NULL || entry->me_key == key)
729 return entry;
730 }
731 assert(0); /* Never reached */
732 return NULL;
733}
734
735/* Returns -1 on failure, 0 on success. */
736static int
737_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
738{
739 PyMemoEntry *oldtable = NULL;
740 PyMemoEntry *oldentry, *newentry;
741 Py_ssize_t new_size = MT_MINSIZE;
742 Py_ssize_t to_process;
743
744 assert(min_size > 0);
745
746 /* Find the smallest valid table size >= min_size. */
747 while (new_size < min_size && new_size > 0)
748 new_size <<= 1;
749 if (new_size <= 0) {
750 PyErr_NoMemory();
751 return -1;
752 }
753 /* new_size needs to be a power of two. */
754 assert((new_size & (new_size - 1)) == 0);
755
756 /* Allocate new table. */
757 oldtable = self->mt_table;
758 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
759 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200760 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000761 PyErr_NoMemory();
762 return -1;
763 }
764 self->mt_allocated = new_size;
765 self->mt_mask = new_size - 1;
766 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
767
768 /* Copy entries from the old table. */
769 to_process = self->mt_used;
770 for (oldentry = oldtable; to_process > 0; oldentry++) {
771 if (oldentry->me_key != NULL) {
772 to_process--;
773 /* newentry is a pointer to a chunk of the new
774 mt_table, so we're setting the key:value pair
775 in-place. */
776 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
777 newentry->me_key = oldentry->me_key;
778 newentry->me_value = oldentry->me_value;
779 }
780 }
781
782 /* Deallocate the old table. */
783 PyMem_FREE(oldtable);
784 return 0;
785}
786
787/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200788static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000789PyMemoTable_Get(PyMemoTable *self, PyObject *key)
790{
791 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
792 if (entry->me_key == NULL)
793 return NULL;
794 return &entry->me_value;
795}
796
797/* Returns -1 on failure, 0 on success. */
798static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200799PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000800{
801 PyMemoEntry *entry;
802
803 assert(key != NULL);
804
805 entry = _PyMemoTable_Lookup(self, key);
806 if (entry->me_key != NULL) {
807 entry->me_value = value;
808 return 0;
809 }
810 Py_INCREF(key);
811 entry->me_key = key;
812 entry->me_value = value;
813 self->mt_used++;
814
815 /* If we added a key, we can safely resize. Otherwise just return!
816 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
817 *
818 * Quadrupling the size improves average table sparseness
819 * (reducing collisions) at the cost of some memory. It also halves
820 * the number of expensive resize operations in a growing memo table.
821 *
822 * Very large memo tables (over 50K items) use doubling instead.
823 * This may help applications with severe memory constraints.
824 */
825 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
826 return 0;
827 return _PyMemoTable_ResizeTable(self,
828 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
829}
830
831#undef MT_MINSIZE
832#undef PERTURB_SHIFT
833
834/*************************************************************************/
835
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000837static int
838_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000839{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000840 Py_CLEAR(self->output_buffer);
841 self->output_buffer =
842 PyBytes_FromStringAndSize(NULL, self->max_output_len);
843 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000844 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000845 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100846 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000847 return 0;
848}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000849
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100850static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100851_write_size64(char *out, size_t value)
852{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200853 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800854
855 assert(sizeof(size_t) <= 8);
856
857 for (i = 0; i < sizeof(size_t); i++) {
858 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
859 }
860 for (i = sizeof(size_t); i < 8; i++) {
861 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800862 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100863}
864
865static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100866_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
867{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100868 qdata[0] = FRAME;
869 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100870}
871
872static int
873_Pickler_CommitFrame(PicklerObject *self)
874{
875 size_t frame_len;
876 char *qdata;
877
878 if (!self->framing || self->frame_start == -1)
879 return 0;
880 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
881 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
882 _Pickler_WriteFrameHeader(self, qdata, frame_len);
883 self->frame_start = -1;
884 return 0;
885}
886
887static int
888_Pickler_OpcodeBoundary(PicklerObject *self)
889{
890 Py_ssize_t frame_len;
891
892 if (!self->framing || self->frame_start == -1)
893 return 0;
894 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
895 if (frame_len >= FRAME_SIZE_TARGET)
896 return _Pickler_CommitFrame(self);
897 else
898 return 0;
899}
900
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000901static PyObject *
902_Pickler_GetString(PicklerObject *self)
903{
904 PyObject *output_buffer = self->output_buffer;
905
906 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100907
908 if (_Pickler_CommitFrame(self))
909 return NULL;
910
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000911 self->output_buffer = NULL;
912 /* Resize down to exact size */
913 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
914 return NULL;
915 return output_buffer;
916}
917
918static int
919_Pickler_FlushToFile(PicklerObject *self)
920{
921 PyObject *output, *result;
922
923 assert(self->write != NULL);
924
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100925 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000926 output = _Pickler_GetString(self);
927 if (output == NULL)
928 return -1;
929
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800930 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000931 Py_XDECREF(result);
932 return (result == NULL) ? -1 : 0;
933}
934
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200935static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000939 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100940 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000941
942 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100943 need_new_frame = (self->framing && self->frame_start == -1);
944
945 if (need_new_frame)
946 n = data_len + FRAME_HEADER_SIZE;
947 else
948 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000949
950 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951 if (required > self->max_output_len) {
952 /* Make place in buffer for the pickle chunk */
953 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
954 PyErr_NoMemory();
955 return -1;
956 }
957 self->max_output_len = (self->output_len + n) / 2 * 3;
958 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
959 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000960 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000961 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100962 if (need_new_frame) {
963 /* Setup new frame */
964 Py_ssize_t frame_start = self->output_len;
965 self->frame_start = frame_start;
966 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
967 /* Write an invalid value, for debugging */
968 buffer[frame_start + i] = 0xFE;
969 }
970 self->output_len += FRAME_HEADER_SIZE;
971 }
972 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000973 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100974 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000975 buffer[self->output_len + i] = s[i];
976 }
977 }
978 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100979 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100981 self->output_len += data_len;
982 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000983}
984
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000985static PicklerObject *
986_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000987{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000989
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000990 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
991 if (self == NULL)
992 return NULL;
993
994 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100995 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 self->write = NULL;
997 self->proto = 0;
998 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100999 self->framing = 0;
1000 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 self->fast = 0;
1002 self->fast_nesting = 0;
1003 self->fix_imports = 0;
1004 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005 self->max_output_len = WRITE_BUF_SIZE;
1006 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001007
1008 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001009 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1010 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001011
1012 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001013 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001014 return NULL;
1015 }
1016 return self;
1017}
1018
1019static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001020_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001022 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001024 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001026 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001027 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001028 proto = PyLong_AsLong(protocol);
1029 if (proto < 0) {
1030 if (proto == -1 && PyErr_Occurred())
1031 return -1;
1032 proto = HIGHEST_PROTOCOL;
1033 }
1034 else if (proto > HIGHEST_PROTOCOL) {
1035 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1036 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001038 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001039 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001040 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001041 self->bin = proto > 0;
1042 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043 return 0;
1044}
1045
1046/* Returns -1 (with an exception set) on failure, 0 on success. This may
1047 be called once on a freshly created Pickler. */
1048static int
1049_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1050{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001051 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001053 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054 if (self->write == NULL) {
1055 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1056 PyErr_SetString(PyExc_TypeError,
1057 "file must have a 'write' attribute");
1058 return -1;
1059 }
1060
1061 return 0;
1062}
1063
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001064/* Returns the size of the input on success, -1 on failure. This takes its
1065 own reference to `input`. */
1066static Py_ssize_t
1067_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1068{
1069 if (self->buffer.buf != NULL)
1070 PyBuffer_Release(&self->buffer);
1071 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1072 return -1;
1073 self->input_buffer = self->buffer.buf;
1074 self->input_len = self->buffer.len;
1075 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001076 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 return self->input_len;
1078}
1079
Antoine Pitrou04248a82010-10-12 20:51:21 +00001080static int
1081_Unpickler_SkipConsumed(UnpicklerObject *self)
1082{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001083 Py_ssize_t consumed;
1084 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001085
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001086 consumed = self->next_read_idx - self->prefetched_idx;
1087 if (consumed <= 0)
1088 return 0;
1089
1090 assert(self->peek); /* otherwise we did something wrong */
1091 /* This makes an useless copy... */
1092 r = PyObject_CallFunction(self->read, "n", consumed);
1093 if (r == NULL)
1094 return -1;
1095 Py_DECREF(r);
1096
1097 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001098 return 0;
1099}
1100
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001101static const Py_ssize_t READ_WHOLE_LINE = -1;
1102
1103/* If reading from a file, we need to only pull the bytes we need, since there
1104 may be multiple pickle objects arranged contiguously in the same input
1105 buffer.
1106
1107 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1108 bytes from the input stream/buffer.
1109
1110 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1111 failure; on success, returns the number of bytes read from the file.
1112
1113 On success, self->input_len will be 0; this is intentional so that when
1114 unpickling from a file, the "we've run out of data" code paths will trigger,
1115 causing the Unpickler to go back to the file for more data. Use the returned
1116 size to tell you how much data you can process. */
1117static Py_ssize_t
1118_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1119{
1120 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001121 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001122
1123 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001124
Antoine Pitrou04248a82010-10-12 20:51:21 +00001125 if (_Unpickler_SkipConsumed(self) < 0)
1126 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001127
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001128 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001129 PyObject *empty_tuple = PyTuple_New(0);
1130 data = PyObject_Call(self->readline, empty_tuple, NULL);
1131 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001132 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001134 PyObject *len;
1135 /* Prefetch some data without advancing the file pointer, if possible */
1136 if (self->peek && n < PREFETCH) {
1137 len = PyLong_FromSsize_t(PREFETCH);
1138 if (len == NULL)
1139 return -1;
1140 data = _Pickle_FastCall(self->peek, len);
1141 if (data == NULL) {
1142 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1143 return -1;
1144 /* peek() is probably not supported by the given file object */
1145 PyErr_Clear();
1146 Py_CLEAR(self->peek);
1147 }
1148 else {
1149 read_size = _Unpickler_SetStringInput(self, data);
1150 Py_DECREF(data);
1151 self->prefetched_idx = 0;
1152 if (n <= read_size)
1153 return n;
1154 }
1155 }
1156 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001157 if (len == NULL)
1158 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001159 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001160 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001161 if (data == NULL)
1162 return -1;
1163
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001164 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001165 Py_DECREF(data);
1166 return read_size;
1167}
1168
1169/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1170
1171 This should be used for all data reads, rather than accessing the unpickler's
1172 input buffer directly. This method deals correctly with reading from input
1173 streams, which the input buffer doesn't deal with.
1174
1175 Note that when reading from a file-like object, self->next_read_idx won't
1176 be updated (it should remain at 0 for the entire unpickling process). You
1177 should use this function's return value to know how many bytes you can
1178 consume.
1179
1180 Returns -1 (with an exception set) on failure. On success, return the
1181 number of chars read. */
1182static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001183_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001184{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001185 Py_ssize_t num_read;
1186
Antoine Pitrou04248a82010-10-12 20:51:21 +00001187 if (self->next_read_idx + n <= self->input_len) {
1188 *s = self->input_buffer + self->next_read_idx;
1189 self->next_read_idx += n;
1190 return n;
1191 }
1192 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001193 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001194 return -1;
1195 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196 num_read = _Unpickler_ReadFromFile(self, n);
1197 if (num_read < 0)
1198 return -1;
1199 if (num_read < n) {
1200 PyErr_Format(PyExc_EOFError, "Ran out of input");
1201 return -1;
1202 }
1203 *s = self->input_buffer;
1204 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001205 return n;
1206}
1207
1208static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001209_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1210 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001211{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001212 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001213 if (input_line == NULL) {
1214 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001215 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001216 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001217
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001218 memcpy(input_line, line, len);
1219 input_line[len] = '\0';
1220 self->input_line = input_line;
1221 *result = self->input_line;
1222 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001223}
1224
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001225/* Read a line from the input stream/buffer. If we run off the end of the input
1226 before hitting \n, return the data we found.
1227
1228 Returns the number of chars read, or -1 on failure. */
1229static Py_ssize_t
1230_Unpickler_Readline(UnpicklerObject *self, char **result)
1231{
1232 Py_ssize_t i, num_read;
1233
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001234 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001235 if (self->input_buffer[i] == '\n') {
1236 char *line_start = self->input_buffer + self->next_read_idx;
1237 num_read = i - self->next_read_idx + 1;
1238 self->next_read_idx = i + 1;
1239 return _Unpickler_CopyLine(self, line_start, num_read, result);
1240 }
1241 }
1242 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1244 if (num_read < 0)
1245 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001246 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001247 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248 }
Victor Stinner121aab42011-09-29 23:40:53 +02001249
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001250 /* If we get here, we've run off the end of the input string. Return the
1251 remaining string and let the caller figure it out. */
1252 *result = self->input_buffer + self->next_read_idx;
1253 num_read = i - self->next_read_idx;
1254 self->next_read_idx = i;
1255 return num_read;
1256}
1257
1258/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1259 will be modified in place. */
1260static int
1261_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1262{
1263 Py_ssize_t i;
1264 PyObject **memo;
1265
1266 assert(new_size > self->memo_size);
1267
1268 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1269 if (memo == NULL) {
1270 PyErr_NoMemory();
1271 return -1;
1272 }
1273 self->memo = memo;
1274 for (i = self->memo_size; i < new_size; i++)
1275 self->memo[i] = NULL;
1276 self->memo_size = new_size;
1277 return 0;
1278}
1279
1280/* Returns NULL if idx is out of bounds. */
1281static PyObject *
1282_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1283{
1284 if (idx < 0 || idx >= self->memo_size)
1285 return NULL;
1286
1287 return self->memo[idx];
1288}
1289
1290/* Returns -1 (with an exception set) on failure, 0 on success.
1291 This takes its own reference to `value`. */
1292static int
1293_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1294{
1295 PyObject *old_item;
1296
1297 if (idx >= self->memo_size) {
1298 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1299 return -1;
1300 assert(idx < self->memo_size);
1301 }
1302 Py_INCREF(value);
1303 old_item = self->memo[idx];
1304 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001305 if (old_item != NULL) {
1306 Py_DECREF(old_item);
1307 }
1308 else {
1309 self->memo_len++;
1310 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001311 return 0;
1312}
1313
1314static PyObject **
1315_Unpickler_NewMemo(Py_ssize_t new_size)
1316{
1317 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
Victor Stinner42024562013-07-12 00:53:57 +02001318 if (memo == NULL) {
1319 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001320 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001321 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001322 memset(memo, 0, new_size * sizeof(PyObject *));
1323 return memo;
1324}
1325
1326/* Free the unpickler's memo, taking care to decref any items left in it. */
1327static void
1328_Unpickler_MemoCleanup(UnpicklerObject *self)
1329{
1330 Py_ssize_t i;
1331 PyObject **memo = self->memo;
1332
1333 if (self->memo == NULL)
1334 return;
1335 self->memo = NULL;
1336 i = self->memo_size;
1337 while (--i >= 0) {
1338 Py_XDECREF(memo[i]);
1339 }
1340 PyMem_FREE(memo);
1341}
1342
1343static UnpicklerObject *
1344_Unpickler_New(void)
1345{
1346 UnpicklerObject *self;
1347
1348 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1349 if (self == NULL)
1350 return NULL;
1351
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001352 self->pers_func = NULL;
1353 self->input_buffer = NULL;
1354 self->input_line = NULL;
1355 self->input_len = 0;
1356 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001357 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001358 self->read = NULL;
1359 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001360 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001361 self->encoding = NULL;
1362 self->errors = NULL;
1363 self->marks = NULL;
1364 self->num_marks = 0;
1365 self->marks_size = 0;
1366 self->proto = 0;
1367 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001368 memset(&self->buffer, 0, sizeof(Py_buffer));
1369 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001370 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001371 self->memo = _Unpickler_NewMemo(self->memo_size);
1372 self->stack = (Pdata *)Pdata_New();
1373
1374 if (self->memo == NULL || self->stack == NULL) {
1375 Py_DECREF(self);
1376 return NULL;
1377 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001378
1379 return self;
1380}
1381
1382/* Returns -1 (with an exception set) on failure, 0 on success. This may
1383 be called once on a freshly created Pickler. */
1384static int
1385_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1386{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001387 _Py_IDENTIFIER(peek);
1388 _Py_IDENTIFIER(read);
1389 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001390
1391 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001392 if (self->peek == NULL) {
1393 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1394 PyErr_Clear();
1395 else
1396 return -1;
1397 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001398 self->read = _PyObject_GetAttrId(file, &PyId_read);
1399 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001400 if (self->readline == NULL || self->read == NULL) {
1401 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1402 PyErr_SetString(PyExc_TypeError,
1403 "file must have 'read' and 'readline' attributes");
1404 Py_CLEAR(self->read);
1405 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001406 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001407 return -1;
1408 }
1409 return 0;
1410}
1411
1412/* Returns -1 (with an exception set) on failure, 0 on success. This may
1413 be called once on a freshly created Pickler. */
1414static int
1415_Unpickler_SetInputEncoding(UnpicklerObject *self,
1416 const char *encoding,
1417 const char *errors)
1418{
1419 if (encoding == NULL)
1420 encoding = "ASCII";
1421 if (errors == NULL)
1422 errors = "strict";
1423
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001424 self->encoding = _PyMem_Strdup(encoding);
1425 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001426 if (self->encoding == NULL || self->errors == NULL) {
1427 PyErr_NoMemory();
1428 return -1;
1429 }
1430 return 0;
1431}
1432
1433/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001434static int
1435memo_get(PicklerObject *self, PyObject *key)
1436{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001437 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001438 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001439 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001440
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001441 value = PyMemoTable_Get(self->memo, key);
1442 if (value == NULL) {
1443 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001444 return -1;
1445 }
1446
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447 if (!self->bin) {
1448 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001449 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1450 "%" PY_FORMAT_SIZE_T "d\n", *value);
1451 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001452 }
1453 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001454 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001456 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001457 len = 2;
1458 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001459 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001460 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001461 pdata[1] = (unsigned char)(*value & 0xff);
1462 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1463 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1464 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465 len = 5;
1466 }
1467 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001468 PickleState *st = _Pickle_GetGlobalState();
1469 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001470 "memo id too large for LONG_BINGET");
1471 return -1;
1472 }
1473 }
1474
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001475 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001476 return -1;
1477
1478 return 0;
1479}
1480
1481/* Store an object in the memo, assign it a new unique ID based on the number
1482 of objects currently stored in the memo and generate a PUT opcode. */
1483static int
1484memo_put(PicklerObject *self, PyObject *obj)
1485{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001486 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001487 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001488 Py_ssize_t idx;
1489
1490 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491
1492 if (self->fast)
1493 return 0;
1494
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001495 idx = PyMemoTable_Size(self->memo);
1496 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1497 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001498
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001499 if (self->proto >= 4) {
1500 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1501 return -1;
1502 return 0;
1503 }
1504 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001505 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001506 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001507 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001508 len = strlen(pdata);
1509 }
1510 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001511 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001512 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001513 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001514 len = 2;
1515 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001516 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001517 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001518 pdata[1] = (unsigned char)(idx & 0xff);
1519 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1520 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1521 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001522 len = 5;
1523 }
1524 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001525 PickleState *st = _Pickle_GetGlobalState();
1526 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001527 "memo id too large for LONG_BINPUT");
1528 return -1;
1529 }
1530 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001531 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001532 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001534 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001535}
1536
1537static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1539 PyObject *dotted_path;
1540 Py_ssize_t i;
1541 _Py_static_string(PyId_dot, ".");
1542 _Py_static_string(PyId_locals, "<locals>");
1543
1544 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1545 if (dotted_path == NULL) {
1546 return NULL;
1547 }
1548 assert(Py_SIZE(dotted_path) >= 1);
1549 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1550 PyErr_Format(PyExc_AttributeError,
1551 "Can't get qualified attribute %R on %R;"
1552 "use protocols >= 4 to enable support",
1553 name, obj);
1554 Py_DECREF(dotted_path);
1555 return NULL;
1556 }
1557 Py_INCREF(obj);
1558 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1559 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1560 PyObject *tmp;
1561 PyObject *result = PyUnicode_RichCompare(
1562 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1563 int is_equal = (result == Py_True);
1564 assert(PyBool_Check(result));
1565 Py_DECREF(result);
1566 if (is_equal) {
1567 PyErr_Format(PyExc_AttributeError,
1568 "Can't get local attribute %R on %R", name, obj);
1569 Py_DECREF(dotted_path);
1570 Py_DECREF(obj);
1571 return NULL;
1572 }
1573 tmp = PyObject_GetAttr(obj, subpath);
1574 Py_DECREF(obj);
1575 if (tmp == NULL) {
1576 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1577 PyErr_Clear();
1578 PyErr_Format(PyExc_AttributeError,
1579 "Can't get attribute %R on %R", name, obj);
1580 }
1581 Py_DECREF(dotted_path);
1582 return NULL;
1583 }
1584 obj = tmp;
1585 }
1586 Py_DECREF(dotted_path);
1587 return obj;
1588}
1589
1590static PyObject *
1591whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001592{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001593 PyObject *module_name;
1594 PyObject *modules_dict;
1595 PyObject *module;
1596 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001597 Py_ssize_t i, j;
1598 _Py_IDENTIFIER(__module__);
1599 _Py_IDENTIFIER(modules);
1600 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001601
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001602 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1603
1604 if (module_name == NULL) {
1605 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001606 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001607 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001608 }
1609 else {
1610 /* In some rare cases (e.g., bound methods of extension types),
1611 __module__ can be None. If it is so, then search sys.modules for
1612 the module of global. */
1613 if (module_name != Py_None)
1614 return module_name;
1615 Py_CLEAR(module_name);
1616 }
1617 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001618
Victor Stinnerbb520202013-11-06 22:40:41 +01001619 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001620 if (modules_dict == NULL) {
1621 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001622 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001624
1625 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001626 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001627 PyObject *result = PyUnicode_RichCompare(
1628 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1629 int is_equal = (result == Py_True);
1630 assert(PyBool_Check(result));
1631 Py_DECREF(result);
1632 if (is_equal)
1633 continue;
1634 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635 continue;
1636
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001637 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001638 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001639 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001641 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001642 continue;
1643 }
1644
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001645 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001647 Py_INCREF(module_name);
1648 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001650 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651 }
1652
1653 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001655 Py_INCREF(module_name);
1656 return module_name;
1657}
1658
1659/* fast_save_enter() and fast_save_leave() are guards against recursive
1660 objects when Pickler is used with the "fast mode" (i.e., with object
1661 memoization disabled). If the nesting of a list or dict object exceed
1662 FAST_NESTING_LIMIT, these guards will start keeping an internal
1663 reference to the seen list or dict objects and check whether these objects
1664 are recursive. These are not strictly necessary, since save() has a
1665 hard-coded recursion limit, but they give a nicer error message than the
1666 typical RuntimeError. */
1667static int
1668fast_save_enter(PicklerObject *self, PyObject *obj)
1669{
1670 /* if fast_nesting < 0, we're doing an error exit. */
1671 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1672 PyObject *key = NULL;
1673 if (self->fast_memo == NULL) {
1674 self->fast_memo = PyDict_New();
1675 if (self->fast_memo == NULL) {
1676 self->fast_nesting = -1;
1677 return 0;
1678 }
1679 }
1680 key = PyLong_FromVoidPtr(obj);
1681 if (key == NULL)
1682 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001683 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001684 Py_DECREF(key);
1685 PyErr_Format(PyExc_ValueError,
1686 "fast mode: can't pickle cyclic objects "
1687 "including object type %.200s at %p",
1688 obj->ob_type->tp_name, obj);
1689 self->fast_nesting = -1;
1690 return 0;
1691 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001692 if (PyErr_Occurred()) {
1693 return 0;
1694 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1696 Py_DECREF(key);
1697 self->fast_nesting = -1;
1698 return 0;
1699 }
1700 Py_DECREF(key);
1701 }
1702 return 1;
1703}
1704
1705static int
1706fast_save_leave(PicklerObject *self, PyObject *obj)
1707{
1708 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1709 PyObject *key = PyLong_FromVoidPtr(obj);
1710 if (key == NULL)
1711 return 0;
1712 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1713 Py_DECREF(key);
1714 return 0;
1715 }
1716 Py_DECREF(key);
1717 }
1718 return 1;
1719}
1720
1721static int
1722save_none(PicklerObject *self, PyObject *obj)
1723{
1724 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001725 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001726 return -1;
1727
1728 return 0;
1729}
1730
1731static int
1732save_bool(PicklerObject *self, PyObject *obj)
1733{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001734 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001735 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001736 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 return -1;
1738 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001739 else {
1740 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1741 * so that unpicklers written before bools were introduced unpickle them
1742 * as ints, but unpicklers after can recognize that bools were intended.
1743 * Note that protocol 2 added direct ways to pickle bools.
1744 */
1745 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1746 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1747 return -1;
1748 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001749 return 0;
1750}
1751
1752static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001753save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001754{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001755 PyObject *repr = NULL;
1756 Py_ssize_t size;
1757 long val;
1758 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001759
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001760 const char long_op = LONG;
1761
1762 val= PyLong_AsLong(obj);
1763 if (val == -1 && PyErr_Occurred()) {
1764 /* out of range for int pickling */
1765 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001766 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001767 else if (self->bin &&
1768 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001769 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001770 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001771
1772 Note: we can't use -0x80000000L in the above condition because some
1773 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1774 before applying the unary minus when sizeof(long) <= 4. The
1775 resulting value stays unsigned which is commonly not what we want,
1776 so MSVC happily warns us about it. However, that result would have
1777 been fine because we guard for sizeof(long) <= 4 which turns the
1778 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001779 char pdata[32];
1780 Py_ssize_t len = 0;
1781
1782 pdata[1] = (unsigned char)(val & 0xff);
1783 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1784 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1785 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001786
1787 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1788 if (pdata[2] == 0) {
1789 pdata[0] = BININT1;
1790 len = 2;
1791 }
1792 else {
1793 pdata[0] = BININT2;
1794 len = 3;
1795 }
1796 }
1797 else {
1798 pdata[0] = BININT;
1799 len = 5;
1800 }
1801
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001802 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001803 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001804
1805 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 }
1807
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001808 if (self->proto >= 2) {
1809 /* Linear-time pickling. */
1810 size_t nbits;
1811 size_t nbytes;
1812 unsigned char *pdata;
1813 char header[5];
1814 int i;
1815 int sign = _PyLong_Sign(obj);
1816
1817 if (sign == 0) {
1818 header[0] = LONG1;
1819 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001820 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001821 goto error;
1822 return 0;
1823 }
1824 nbits = _PyLong_NumBits(obj);
1825 if (nbits == (size_t)-1 && PyErr_Occurred())
1826 goto error;
1827 /* How many bytes do we need? There are nbits >> 3 full
1828 * bytes of data, and nbits & 7 leftover bits. If there
1829 * are any leftover bits, then we clearly need another
1830 * byte. Wnat's not so obvious is that we *probably*
1831 * need another byte even if there aren't any leftovers:
1832 * the most-significant bit of the most-significant byte
1833 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001834 * opposite of the one we need. The exception is ints
1835 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001836 * its own 256's-complement, so has the right sign bit
1837 * even without the extra byte. That's a pain to check
1838 * for in advance, though, so we always grab an extra
1839 * byte at the start, and cut it back later if possible.
1840 */
1841 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001842 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001843 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001844 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001845 goto error;
1846 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001847 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 if (repr == NULL)
1849 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001850 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001851 i = _PyLong_AsByteArray((PyLongObject *)obj,
1852 pdata, nbytes,
1853 1 /* little endian */ , 1 /* signed */ );
1854 if (i < 0)
1855 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001856 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001857 * needed. This is so iff the MSB is all redundant sign
1858 * bits.
1859 */
1860 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001861 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001862 pdata[nbytes - 1] == 0xff &&
1863 (pdata[nbytes - 2] & 0x80) != 0) {
1864 nbytes--;
1865 }
1866
1867 if (nbytes < 256) {
1868 header[0] = LONG1;
1869 header[1] = (unsigned char)nbytes;
1870 size = 2;
1871 }
1872 else {
1873 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001874 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001875 for (i = 1; i < 5; i++) {
1876 header[i] = (unsigned char)(size & 0xff);
1877 size >>= 8;
1878 }
1879 size = 5;
1880 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001881 if (_Pickler_Write(self, header, size) < 0 ||
1882 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001883 goto error;
1884 }
1885 else {
1886 char *string;
1887
Mark Dickinson8dd05142009-01-20 20:43:58 +00001888 /* proto < 2: write the repr and newline. This is quadratic-time (in
1889 the number of digits), in both directions. We add a trailing 'L'
1890 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001891
1892 repr = PyObject_Repr(obj);
1893 if (repr == NULL)
1894 goto error;
1895
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001896 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 if (string == NULL)
1898 goto error;
1899
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001900 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1901 _Pickler_Write(self, string, size) < 0 ||
1902 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 goto error;
1904 }
1905
1906 if (0) {
1907 error:
1908 status = -1;
1909 }
1910 Py_XDECREF(repr);
1911
1912 return status;
1913}
1914
1915static int
1916save_float(PicklerObject *self, PyObject *obj)
1917{
1918 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1919
1920 if (self->bin) {
1921 char pdata[9];
1922 pdata[0] = BINFLOAT;
1923 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1924 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001925 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001926 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001927 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001928 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001929 int result = -1;
1930 char *buf = NULL;
1931 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001933 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001934 goto done;
1935
Mark Dickinson3e09f432009-04-17 08:41:23 +00001936 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001937 if (!buf) {
1938 PyErr_NoMemory();
1939 goto done;
1940 }
1941
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001942 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001943 goto done;
1944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001945 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001946 goto done;
1947
1948 result = 0;
1949done:
1950 PyMem_Free(buf);
1951 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001952 }
1953
1954 return 0;
1955}
1956
1957static int
1958save_bytes(PicklerObject *self, PyObject *obj)
1959{
1960 if (self->proto < 3) {
1961 /* Older pickle protocols do not have an opcode for pickling bytes
1962 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001963 the __reduce__ method) to permit bytes object unpickling.
1964
1965 Here we use a hack to be compatible with Python 2. Since in Python
1966 2 'bytes' is just an alias for 'str' (which has different
1967 parameters than the actual bytes object), we use codecs.encode
1968 to create the appropriate 'str' object when unpickled using
1969 Python 2 *and* the appropriate 'bytes' object when unpickled
1970 using Python 3. Again this is a hack and we don't need to do this
1971 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973 int status;
1974
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001975 if (PyBytes_GET_SIZE(obj) == 0) {
1976 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1977 }
1978 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001979 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001980 PyObject *unicode_str =
1981 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1982 PyBytes_GET_SIZE(obj),
1983 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001984 _Py_IDENTIFIER(latin1);
1985
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001986 if (unicode_str == NULL)
1987 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001988 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001989 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001990 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001991 Py_DECREF(unicode_str);
1992 }
1993
1994 if (reduce_value == NULL)
1995 return -1;
1996
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001997 /* save_reduce() will memoize the object automatically. */
1998 status = save_reduce(self, reduce_value, obj);
1999 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002000 return status;
2001 }
2002 else {
2003 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002004 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002005 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002006
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002007 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008 if (size < 0)
2009 return -1;
2010
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002011 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 header[0] = SHORT_BINBYTES;
2013 header[1] = (unsigned char)size;
2014 len = 2;
2015 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002016 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 header[0] = BINBYTES;
2018 header[1] = (unsigned char)(size & 0xff);
2019 header[2] = (unsigned char)((size >> 8) & 0xff);
2020 header[3] = (unsigned char)((size >> 16) & 0xff);
2021 header[4] = (unsigned char)((size >> 24) & 0xff);
2022 len = 5;
2023 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002024 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002025 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002026 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002027 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002029 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002030 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002031 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 return -1; /* string too large */
2033 }
2034
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002035 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002036 return -1;
2037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002038 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002039 return -1;
2040
2041 if (memo_put(self, obj) < 0)
2042 return -1;
2043
2044 return 0;
2045 }
2046}
2047
2048/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2049 backslash and newline characters to \uXXXX escapes. */
2050static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002051raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002053 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002054 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002055 Py_ssize_t i, size;
2056 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002057 void *data;
2058 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002059
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002060 if (PyUnicode_READY(obj))
2061 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002062
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002063 size = PyUnicode_GET_LENGTH(obj);
2064 data = PyUnicode_DATA(obj);
2065 kind = PyUnicode_KIND(obj);
2066 if (kind == PyUnicode_4BYTE_KIND)
2067 expandsize = 10;
2068 else
2069 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002070
Victor Stinner049e5092014-08-17 22:20:00 +02002071 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002072 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002073 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002074 if (repr == NULL)
2075 return NULL;
2076 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002077 return repr;
2078 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002079
Victor Stinner7270b7f2014-08-17 21:14:46 +02002080 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002081 for (i=0; i < size; i++) {
2082 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002083 /* Map 32-bit characters to '\Uxxxxxxxx' */
2084 if (ch >= 0x10000) {
2085 *p++ = '\\';
2086 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002087 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2088 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2089 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2090 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2091 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2092 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2093 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2094 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002095 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002097 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 *p++ = '\\';
2099 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002100 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2101 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2102 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2103 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002104 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002105 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106 else
2107 *p++ = (char) ch;
2108 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002109 size = p - PyBytes_AS_STRING(repr);
2110 if (_PyBytes_Resize(&repr, size) < 0)
2111 return NULL;
2112 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113}
2114
2115static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002116write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2117{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002118 char header[9];
2119 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002120
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002121 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002122 if (size <= 0xff && self->proto >= 4) {
2123 header[0] = SHORT_BINUNICODE;
2124 header[1] = (unsigned char)(size & 0xff);
2125 len = 2;
2126 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002127 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002128 header[0] = BINUNICODE;
2129 header[1] = (unsigned char)(size & 0xff);
2130 header[2] = (unsigned char)((size >> 8) & 0xff);
2131 header[3] = (unsigned char)((size >> 16) & 0xff);
2132 header[4] = (unsigned char)((size >> 24) & 0xff);
2133 len = 5;
2134 }
2135 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002136 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002137 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002138 len = 9;
2139 }
2140 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002141 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002142 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002143 return -1;
2144 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002145
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002146 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002147 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002148 if (_Pickler_Write(self, data, size) < 0)
2149 return -1;
2150
2151 return 0;
2152}
2153
2154static int
2155write_unicode_binary(PicklerObject *self, PyObject *obj)
2156{
2157 PyObject *encoded = NULL;
2158 Py_ssize_t size;
2159 char *data;
2160 int r;
2161
2162 if (PyUnicode_READY(obj))
2163 return -1;
2164
2165 data = PyUnicode_AsUTF8AndSize(obj, &size);
2166 if (data != NULL)
2167 return write_utf8(self, data, size);
2168
2169 /* Issue #8383: for strings with lone surrogates, fallback on the
2170 "surrogatepass" error handler. */
2171 PyErr_Clear();
2172 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2173 if (encoded == NULL)
2174 return -1;
2175
2176 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2177 PyBytes_GET_SIZE(encoded));
2178 Py_DECREF(encoded);
2179 return r;
2180}
2181
2182static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002183save_unicode(PicklerObject *self, PyObject *obj)
2184{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002185 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002186 if (write_unicode_binary(self, obj) < 0)
2187 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002188 }
2189 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002190 PyObject *encoded;
2191 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002192 const char unicode_op = UNICODE;
2193
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002194 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002195 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002196 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002197
Antoine Pitrou299978d2013-04-07 17:38:11 +02002198 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2199 Py_DECREF(encoded);
2200 return -1;
2201 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002202
2203 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002204 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2205 Py_DECREF(encoded);
2206 return -1;
2207 }
2208 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002209
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002210 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002211 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002212 }
2213 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002214 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002215
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002216 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002217}
2218
2219/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2220static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002221store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002222{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002223 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224
2225 assert(PyTuple_Size(t) == len);
2226
2227 for (i = 0; i < len; i++) {
2228 PyObject *element = PyTuple_GET_ITEM(t, i);
2229
2230 if (element == NULL)
2231 return -1;
2232 if (save(self, element, 0) < 0)
2233 return -1;
2234 }
2235
2236 return 0;
2237}
2238
2239/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2240 * used across protocols to minimize the space needed to pickle them.
2241 * Tuples are also the only builtin immutable type that can be recursive
2242 * (a tuple can be reached from itself), and that requires some subtle
2243 * magic so that it works in all cases. IOW, this is a long routine.
2244 */
2245static int
2246save_tuple(PicklerObject *self, PyObject *obj)
2247{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002248 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249
2250 const char mark_op = MARK;
2251 const char tuple_op = TUPLE;
2252 const char pop_op = POP;
2253 const char pop_mark_op = POP_MARK;
2254 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2255
2256 if ((len = PyTuple_Size(obj)) < 0)
2257 return -1;
2258
2259 if (len == 0) {
2260 char pdata[2];
2261
2262 if (self->proto) {
2263 pdata[0] = EMPTY_TUPLE;
2264 len = 1;
2265 }
2266 else {
2267 pdata[0] = MARK;
2268 pdata[1] = TUPLE;
2269 len = 2;
2270 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002271 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 return -1;
2273 return 0;
2274 }
2275
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002276 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277 * saving the tuple elements, the tuple must be recursive, in
2278 * which case we'll pop everything we put on the stack, and fetch
2279 * its value from the memo.
2280 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281 if (len <= 3 && self->proto >= 2) {
2282 /* Use TUPLE{1,2,3} opcodes. */
2283 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002284 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002285
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002286 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287 /* pop the len elements */
2288 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002289 if (_Pickler_Write(self, &pop_op, 1) < 0)
2290 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002291 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002292 if (memo_get(self, obj) < 0)
2293 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002294
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295 return 0;
2296 }
2297 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002298 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2299 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002300 }
2301 goto memoize;
2302 }
2303
2304 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2305 * Generate MARK e1 e2 ... TUPLE
2306 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002307 if (_Pickler_Write(self, &mark_op, 1) < 0)
2308 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002309
2310 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002311 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002312
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002313 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314 /* pop the stack stuff we pushed */
2315 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002316 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2317 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318 }
2319 else {
2320 /* Note that we pop one more than len, to remove
2321 * the MARK too.
2322 */
2323 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002324 if (_Pickler_Write(self, &pop_op, 1) < 0)
2325 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326 }
2327 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002328 if (memo_get(self, obj) < 0)
2329 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002330
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 return 0;
2332 }
2333 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002334 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2335 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 }
2337
2338 memoize:
2339 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002340 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002343}
2344
2345/* iter is an iterator giving items, and we batch up chunks of
2346 * MARK item item ... item APPENDS
2347 * opcode sequences. Calling code should have arranged to first create an
2348 * empty list, or list-like object, for the APPENDS to operate on.
2349 * Returns 0 on success, <0 on error.
2350 */
2351static int
2352batch_list(PicklerObject *self, PyObject *iter)
2353{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002354 PyObject *obj = NULL;
2355 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356 int i, n;
2357
2358 const char mark_op = MARK;
2359 const char append_op = APPEND;
2360 const char appends_op = APPENDS;
2361
2362 assert(iter != NULL);
2363
2364 /* XXX: I think this function could be made faster by avoiding the
2365 iterator interface and fetching objects directly from list using
2366 PyList_GET_ITEM.
2367 */
2368
2369 if (self->proto == 0) {
2370 /* APPENDS isn't available; do one at a time. */
2371 for (;;) {
2372 obj = PyIter_Next(iter);
2373 if (obj == NULL) {
2374 if (PyErr_Occurred())
2375 return -1;
2376 break;
2377 }
2378 i = save(self, obj, 0);
2379 Py_DECREF(obj);
2380 if (i < 0)
2381 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002382 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383 return -1;
2384 }
2385 return 0;
2386 }
2387
2388 /* proto > 0: write in batches of BATCHSIZE. */
2389 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002390 /* Get first item */
2391 firstitem = PyIter_Next(iter);
2392 if (firstitem == NULL) {
2393 if (PyErr_Occurred())
2394 goto error;
2395
2396 /* nothing more to add */
2397 break;
2398 }
2399
2400 /* Try to get a second item */
2401 obj = PyIter_Next(iter);
2402 if (obj == NULL) {
2403 if (PyErr_Occurred())
2404 goto error;
2405
2406 /* Only one item to write */
2407 if (save(self, firstitem, 0) < 0)
2408 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002409 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002410 goto error;
2411 Py_CLEAR(firstitem);
2412 break;
2413 }
2414
2415 /* More than one item to write */
2416
2417 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002418 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002419 goto error;
2420
2421 if (save(self, firstitem, 0) < 0)
2422 goto error;
2423 Py_CLEAR(firstitem);
2424 n = 1;
2425
2426 /* Fetch and save up to BATCHSIZE items */
2427 while (obj) {
2428 if (save(self, obj, 0) < 0)
2429 goto error;
2430 Py_CLEAR(obj);
2431 n += 1;
2432
2433 if (n == BATCHSIZE)
2434 break;
2435
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002436 obj = PyIter_Next(iter);
2437 if (obj == NULL) {
2438 if (PyErr_Occurred())
2439 goto error;
2440 break;
2441 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002442 }
2443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002444 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002445 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447 } while (n == BATCHSIZE);
2448 return 0;
2449
2450 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002451 Py_XDECREF(firstitem);
2452 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002453 return -1;
2454}
2455
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002456/* This is a variant of batch_list() above, specialized for lists (with no
2457 * support for list subclasses). Like batch_list(), we batch up chunks of
2458 * MARK item item ... item APPENDS
2459 * opcode sequences. Calling code should have arranged to first create an
2460 * empty list, or list-like object, for the APPENDS to operate on.
2461 * Returns 0 on success, -1 on error.
2462 *
2463 * This version is considerably faster than batch_list(), if less general.
2464 *
2465 * Note that this only works for protocols > 0.
2466 */
2467static int
2468batch_list_exact(PicklerObject *self, PyObject *obj)
2469{
2470 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002471 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002472
2473 const char append_op = APPEND;
2474 const char appends_op = APPENDS;
2475 const char mark_op = MARK;
2476
2477 assert(obj != NULL);
2478 assert(self->proto > 0);
2479 assert(PyList_CheckExact(obj));
2480
2481 if (PyList_GET_SIZE(obj) == 1) {
2482 item = PyList_GET_ITEM(obj, 0);
2483 if (save(self, item, 0) < 0)
2484 return -1;
2485 if (_Pickler_Write(self, &append_op, 1) < 0)
2486 return -1;
2487 return 0;
2488 }
2489
2490 /* Write in batches of BATCHSIZE. */
2491 total = 0;
2492 do {
2493 this_batch = 0;
2494 if (_Pickler_Write(self, &mark_op, 1) < 0)
2495 return -1;
2496 while (total < PyList_GET_SIZE(obj)) {
2497 item = PyList_GET_ITEM(obj, total);
2498 if (save(self, item, 0) < 0)
2499 return -1;
2500 total++;
2501 if (++this_batch == BATCHSIZE)
2502 break;
2503 }
2504 if (_Pickler_Write(self, &appends_op, 1) < 0)
2505 return -1;
2506
2507 } while (total < PyList_GET_SIZE(obj));
2508
2509 return 0;
2510}
2511
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002512static int
2513save_list(PicklerObject *self, PyObject *obj)
2514{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002516 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 int status = 0;
2518
2519 if (self->fast && !fast_save_enter(self, obj))
2520 goto error;
2521
2522 /* Create an empty list. */
2523 if (self->bin) {
2524 header[0] = EMPTY_LIST;
2525 len = 1;
2526 }
2527 else {
2528 header[0] = MARK;
2529 header[1] = LIST;
2530 len = 2;
2531 }
2532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002533 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002534 goto error;
2535
2536 /* Get list length, and bow out early if empty. */
2537 if ((len = PyList_Size(obj)) < 0)
2538 goto error;
2539
2540 if (memo_put(self, obj) < 0)
2541 goto error;
2542
2543 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002544 /* Materialize the list elements. */
2545 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002546 if (Py_EnterRecursiveCall(" while pickling an object"))
2547 goto error;
2548 status = batch_list_exact(self, obj);
2549 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002550 } else {
2551 PyObject *iter = PyObject_GetIter(obj);
2552 if (iter == NULL)
2553 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002554
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002555 if (Py_EnterRecursiveCall(" while pickling an object")) {
2556 Py_DECREF(iter);
2557 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002558 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002559 status = batch_list(self, iter);
2560 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002561 Py_DECREF(iter);
2562 }
2563 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002564 if (0) {
2565 error:
2566 status = -1;
2567 }
2568
2569 if (self->fast && !fast_save_leave(self, obj))
2570 status = -1;
2571
2572 return status;
2573}
2574
2575/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2576 * MARK key value ... key value SETITEMS
2577 * opcode sequences. Calling code should have arranged to first create an
2578 * empty dict, or dict-like object, for the SETITEMS to operate on.
2579 * Returns 0 on success, <0 on error.
2580 *
2581 * This is very much like batch_list(). The difference between saving
2582 * elements directly, and picking apart two-tuples, is so long-winded at
2583 * the C level, though, that attempts to combine these routines were too
2584 * ugly to bear.
2585 */
2586static int
2587batch_dict(PicklerObject *self, PyObject *iter)
2588{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002589 PyObject *obj = NULL;
2590 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002591 int i, n;
2592
2593 const char mark_op = MARK;
2594 const char setitem_op = SETITEM;
2595 const char setitems_op = SETITEMS;
2596
2597 assert(iter != NULL);
2598
2599 if (self->proto == 0) {
2600 /* SETITEMS isn't available; do one at a time. */
2601 for (;;) {
2602 obj = PyIter_Next(iter);
2603 if (obj == NULL) {
2604 if (PyErr_Occurred())
2605 return -1;
2606 break;
2607 }
2608 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2609 PyErr_SetString(PyExc_TypeError, "dict items "
2610 "iterator must return 2-tuples");
2611 return -1;
2612 }
2613 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2614 if (i >= 0)
2615 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2616 Py_DECREF(obj);
2617 if (i < 0)
2618 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002619 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002620 return -1;
2621 }
2622 return 0;
2623 }
2624
2625 /* proto > 0: write in batches of BATCHSIZE. */
2626 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002627 /* Get first item */
2628 firstitem = PyIter_Next(iter);
2629 if (firstitem == NULL) {
2630 if (PyErr_Occurred())
2631 goto error;
2632
2633 /* nothing more to add */
2634 break;
2635 }
2636 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2637 PyErr_SetString(PyExc_TypeError, "dict items "
2638 "iterator must return 2-tuples");
2639 goto error;
2640 }
2641
2642 /* Try to get a second item */
2643 obj = PyIter_Next(iter);
2644 if (obj == NULL) {
2645 if (PyErr_Occurred())
2646 goto error;
2647
2648 /* Only one item to write */
2649 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2650 goto error;
2651 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2652 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002653 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002654 goto error;
2655 Py_CLEAR(firstitem);
2656 break;
2657 }
2658
2659 /* More than one item to write */
2660
2661 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002662 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002663 goto error;
2664
2665 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2666 goto error;
2667 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2668 goto error;
2669 Py_CLEAR(firstitem);
2670 n = 1;
2671
2672 /* Fetch and save up to BATCHSIZE items */
2673 while (obj) {
2674 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2675 PyErr_SetString(PyExc_TypeError, "dict items "
2676 "iterator must return 2-tuples");
2677 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002678 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002679 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2680 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2681 goto error;
2682 Py_CLEAR(obj);
2683 n += 1;
2684
2685 if (n == BATCHSIZE)
2686 break;
2687
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002688 obj = PyIter_Next(iter);
2689 if (obj == NULL) {
2690 if (PyErr_Occurred())
2691 goto error;
2692 break;
2693 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002694 }
2695
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002696 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002697 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002698
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002699 } while (n == BATCHSIZE);
2700 return 0;
2701
2702 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002703 Py_XDECREF(firstitem);
2704 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002705 return -1;
2706}
2707
Collin Winter5c9b02d2009-05-25 05:43:30 +00002708/* This is a variant of batch_dict() above that specializes for dicts, with no
2709 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2710 * MARK key value ... key value SETITEMS
2711 * opcode sequences. Calling code should have arranged to first create an
2712 * empty dict, or dict-like object, for the SETITEMS to operate on.
2713 * Returns 0 on success, -1 on error.
2714 *
2715 * Note that this currently doesn't work for protocol 0.
2716 */
2717static int
2718batch_dict_exact(PicklerObject *self, PyObject *obj)
2719{
2720 PyObject *key = NULL, *value = NULL;
2721 int i;
2722 Py_ssize_t dict_size, ppos = 0;
2723
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002724 const char mark_op = MARK;
2725 const char setitem_op = SETITEM;
2726 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002727
2728 assert(obj != NULL);
2729 assert(self->proto > 0);
2730
2731 dict_size = PyDict_Size(obj);
2732
2733 /* Special-case len(d) == 1 to save space. */
2734 if (dict_size == 1) {
2735 PyDict_Next(obj, &ppos, &key, &value);
2736 if (save(self, key, 0) < 0)
2737 return -1;
2738 if (save(self, value, 0) < 0)
2739 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002740 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002741 return -1;
2742 return 0;
2743 }
2744
2745 /* Write in batches of BATCHSIZE. */
2746 do {
2747 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002748 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002749 return -1;
2750 while (PyDict_Next(obj, &ppos, &key, &value)) {
2751 if (save(self, key, 0) < 0)
2752 return -1;
2753 if (save(self, value, 0) < 0)
2754 return -1;
2755 if (++i == BATCHSIZE)
2756 break;
2757 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002758 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002759 return -1;
2760 if (PyDict_Size(obj) != dict_size) {
2761 PyErr_Format(
2762 PyExc_RuntimeError,
2763 "dictionary changed size during iteration");
2764 return -1;
2765 }
2766
2767 } while (i == BATCHSIZE);
2768 return 0;
2769}
2770
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002771static int
2772save_dict(PicklerObject *self, PyObject *obj)
2773{
2774 PyObject *items, *iter;
2775 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002776 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002777 int status = 0;
2778
2779 if (self->fast && !fast_save_enter(self, obj))
2780 goto error;
2781
2782 /* Create an empty dict. */
2783 if (self->bin) {
2784 header[0] = EMPTY_DICT;
2785 len = 1;
2786 }
2787 else {
2788 header[0] = MARK;
2789 header[1] = DICT;
2790 len = 2;
2791 }
2792
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002793 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002794 goto error;
2795
2796 /* Get dict size, and bow out early if empty. */
2797 if ((len = PyDict_Size(obj)) < 0)
2798 goto error;
2799
2800 if (memo_put(self, obj) < 0)
2801 goto error;
2802
2803 if (len != 0) {
2804 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002805 if (PyDict_CheckExact(obj) && self->proto > 0) {
2806 /* We can take certain shortcuts if we know this is a dict and
2807 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002808 if (Py_EnterRecursiveCall(" while pickling an object"))
2809 goto error;
2810 status = batch_dict_exact(self, obj);
2811 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002812 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002813 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002814
2815 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002816 if (items == NULL)
2817 goto error;
2818 iter = PyObject_GetIter(items);
2819 Py_DECREF(items);
2820 if (iter == NULL)
2821 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002822 if (Py_EnterRecursiveCall(" while pickling an object")) {
2823 Py_DECREF(iter);
2824 goto error;
2825 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002826 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002827 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002828 Py_DECREF(iter);
2829 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002830 }
2831
2832 if (0) {
2833 error:
2834 status = -1;
2835 }
2836
2837 if (self->fast && !fast_save_leave(self, obj))
2838 status = -1;
2839
2840 return status;
2841}
2842
2843static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002844save_set(PicklerObject *self, PyObject *obj)
2845{
2846 PyObject *item;
2847 int i;
2848 Py_ssize_t set_size, ppos = 0;
2849 Py_hash_t hash;
2850
2851 const char empty_set_op = EMPTY_SET;
2852 const char mark_op = MARK;
2853 const char additems_op = ADDITEMS;
2854
2855 if (self->proto < 4) {
2856 PyObject *items;
2857 PyObject *reduce_value;
2858 int status;
2859
2860 items = PySequence_List(obj);
2861 if (items == NULL) {
2862 return -1;
2863 }
2864 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2865 Py_DECREF(items);
2866 if (reduce_value == NULL) {
2867 return -1;
2868 }
2869 /* save_reduce() will memoize the object automatically. */
2870 status = save_reduce(self, reduce_value, obj);
2871 Py_DECREF(reduce_value);
2872 return status;
2873 }
2874
2875 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2876 return -1;
2877
2878 if (memo_put(self, obj) < 0)
2879 return -1;
2880
2881 set_size = PySet_GET_SIZE(obj);
2882 if (set_size == 0)
2883 return 0; /* nothing to do */
2884
2885 /* Write in batches of BATCHSIZE. */
2886 do {
2887 i = 0;
2888 if (_Pickler_Write(self, &mark_op, 1) < 0)
2889 return -1;
2890 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2891 if (save(self, item, 0) < 0)
2892 return -1;
2893 if (++i == BATCHSIZE)
2894 break;
2895 }
2896 if (_Pickler_Write(self, &additems_op, 1) < 0)
2897 return -1;
2898 if (PySet_GET_SIZE(obj) != set_size) {
2899 PyErr_Format(
2900 PyExc_RuntimeError,
2901 "set changed size during iteration");
2902 return -1;
2903 }
2904 } while (i == BATCHSIZE);
2905
2906 return 0;
2907}
2908
2909static int
2910save_frozenset(PicklerObject *self, PyObject *obj)
2911{
2912 PyObject *iter;
2913
2914 const char mark_op = MARK;
2915 const char frozenset_op = FROZENSET;
2916
2917 if (self->fast && !fast_save_enter(self, obj))
2918 return -1;
2919
2920 if (self->proto < 4) {
2921 PyObject *items;
2922 PyObject *reduce_value;
2923 int status;
2924
2925 items = PySequence_List(obj);
2926 if (items == NULL) {
2927 return -1;
2928 }
2929 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2930 items);
2931 Py_DECREF(items);
2932 if (reduce_value == NULL) {
2933 return -1;
2934 }
2935 /* save_reduce() will memoize the object automatically. */
2936 status = save_reduce(self, reduce_value, obj);
2937 Py_DECREF(reduce_value);
2938 return status;
2939 }
2940
2941 if (_Pickler_Write(self, &mark_op, 1) < 0)
2942 return -1;
2943
2944 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002945 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002946 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002947 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002948 for (;;) {
2949 PyObject *item;
2950
2951 item = PyIter_Next(iter);
2952 if (item == NULL) {
2953 if (PyErr_Occurred()) {
2954 Py_DECREF(iter);
2955 return -1;
2956 }
2957 break;
2958 }
2959 if (save(self, item, 0) < 0) {
2960 Py_DECREF(item);
2961 Py_DECREF(iter);
2962 return -1;
2963 }
2964 Py_DECREF(item);
2965 }
2966 Py_DECREF(iter);
2967
2968 /* If the object is already in the memo, this means it is
2969 recursive. In this case, throw away everything we put on the
2970 stack, and fetch the object back from the memo. */
2971 if (PyMemoTable_Get(self->memo, obj)) {
2972 const char pop_mark_op = POP_MARK;
2973
2974 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2975 return -1;
2976 if (memo_get(self, obj) < 0)
2977 return -1;
2978 return 0;
2979 }
2980
2981 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2982 return -1;
2983 if (memo_put(self, obj) < 0)
2984 return -1;
2985
2986 return 0;
2987}
2988
2989static int
2990fix_imports(PyObject **module_name, PyObject **global_name)
2991{
2992 PyObject *key;
2993 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002994 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002995
2996 key = PyTuple_Pack(2, *module_name, *global_name);
2997 if (key == NULL)
2998 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002999 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003000 Py_DECREF(key);
3001 if (item) {
3002 PyObject *fixed_module_name;
3003 PyObject *fixed_global_name;
3004
3005 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3006 PyErr_Format(PyExc_RuntimeError,
3007 "_compat_pickle.REVERSE_NAME_MAPPING values "
3008 "should be 2-tuples, not %.200s",
3009 Py_TYPE(item)->tp_name);
3010 return -1;
3011 }
3012 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3013 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3014 if (!PyUnicode_Check(fixed_module_name) ||
3015 !PyUnicode_Check(fixed_global_name)) {
3016 PyErr_Format(PyExc_RuntimeError,
3017 "_compat_pickle.REVERSE_NAME_MAPPING values "
3018 "should be pairs of str, not (%.200s, %.200s)",
3019 Py_TYPE(fixed_module_name)->tp_name,
3020 Py_TYPE(fixed_global_name)->tp_name);
3021 return -1;
3022 }
3023
3024 Py_CLEAR(*module_name);
3025 Py_CLEAR(*global_name);
3026 Py_INCREF(fixed_module_name);
3027 Py_INCREF(fixed_global_name);
3028 *module_name = fixed_module_name;
3029 *global_name = fixed_global_name;
3030 }
3031 else if (PyErr_Occurred()) {
3032 return -1;
3033 }
3034
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003035 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003036 if (item) {
3037 if (!PyUnicode_Check(item)) {
3038 PyErr_Format(PyExc_RuntimeError,
3039 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3040 "should be strings, not %.200s",
3041 Py_TYPE(item)->tp_name);
3042 return -1;
3043 }
3044 Py_CLEAR(*module_name);
3045 Py_INCREF(item);
3046 *module_name = item;
3047 }
3048 else if (PyErr_Occurred()) {
3049 return -1;
3050 }
3051
3052 return 0;
3053}
3054
3055static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003056save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3057{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003058 PyObject *global_name = NULL;
3059 PyObject *module_name = NULL;
3060 PyObject *module = NULL;
3061 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003062 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003063 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003064 _Py_IDENTIFIER(__name__);
3065 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003066
3067 const char global_op = GLOBAL;
3068
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003069 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003070 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003071 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003072 }
3073 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003074 if (self->proto >= 4) {
3075 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3076 if (global_name == NULL) {
3077 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3078 goto error;
3079 PyErr_Clear();
3080 }
3081 }
3082 if (global_name == NULL) {
3083 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3084 if (global_name == NULL)
3085 goto error;
3086 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003087 }
3088
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003089 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003090 if (module_name == NULL)
3091 goto error;
3092
3093 /* XXX: Change to use the import C API directly with level=0 to disallow
3094 relative imports.
3095
3096 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3097 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3098 custom import functions (IMHO, this would be a nice security
3099 feature). The import C API would need to be extended to support the
3100 extra parameters of __import__ to fix that. */
3101 module = PyImport_Import(module_name);
3102 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003103 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003104 "Can't pickle %R: import of module %R failed",
3105 obj, module_name);
3106 goto error;
3107 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003108 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003109 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003110 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003111 "Can't pickle %R: attribute lookup %S on %S failed",
3112 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003113 goto error;
3114 }
3115 if (cls != obj) {
3116 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003117 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003118 "Can't pickle %R: it's not the same object as %S.%S",
3119 obj, module_name, global_name);
3120 goto error;
3121 }
3122 Py_DECREF(cls);
3123
3124 if (self->proto >= 2) {
3125 /* See whether this is in the extension registry, and if
3126 * so generate an EXT opcode.
3127 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003128 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129 PyObject *code_obj; /* extension code as Python object */
3130 long code; /* extension code as C value */
3131 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003132 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003134 extension_key = PyTuple_Pack(2, module_name, global_name);
3135 if (extension_key == NULL) {
3136 goto error;
3137 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003138 code_obj = PyDict_GetItemWithError(st->extension_registry,
3139 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003140 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003141 /* The object is not registered in the extension registry.
3142 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003143 if (code_obj == NULL) {
3144 if (PyErr_Occurred()) {
3145 goto error;
3146 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003147 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149
3150 /* XXX: pickle.py doesn't check neither the type, nor the range
3151 of the value returned by the extension_registry. It should for
3152 consistency. */
3153
3154 /* Verify code_obj has the right type and value. */
3155 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003156 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003157 "Can't pickle %R: extension code %R isn't an integer",
3158 obj, code_obj);
3159 goto error;
3160 }
3161 code = PyLong_AS_LONG(code_obj);
3162 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003163 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003164 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3165 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003166 goto error;
3167 }
3168
3169 /* Generate an EXT opcode. */
3170 if (code <= 0xff) {
3171 pdata[0] = EXT1;
3172 pdata[1] = (unsigned char)code;
3173 n = 2;
3174 }
3175 else if (code <= 0xffff) {
3176 pdata[0] = EXT2;
3177 pdata[1] = (unsigned char)(code & 0xff);
3178 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3179 n = 3;
3180 }
3181 else {
3182 pdata[0] = EXT4;
3183 pdata[1] = (unsigned char)(code & 0xff);
3184 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3185 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3186 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3187 n = 5;
3188 }
3189
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003190 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003191 goto error;
3192 }
3193 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003194 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003195 if (self->proto >= 4) {
3196 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197
Christian Heimese8b1ba12013-11-23 21:13:39 +01003198 if (save(self, module_name, 0) < 0)
3199 goto error;
3200 if (save(self, global_name, 0) < 0)
3201 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003202
3203 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3204 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003205 }
3206 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003207 /* Generate a normal global opcode if we are using a pickle
3208 protocol < 4, or if the object is not registered in the
3209 extension registry. */
3210 PyObject *encoded;
3211 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003212
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003213 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003214 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003215
3216 /* For protocol < 3 and if the user didn't request against doing
3217 so, we convert module names to the old 2.x module names. */
3218 if (self->proto < 3 && self->fix_imports) {
3219 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003220 goto error;
3221 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003222 }
3223
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003224 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3225 both the module name and the global name using UTF-8. We do so
3226 only when we are using the pickle protocol newer than version
3227 3. This is to ensure compatibility with older Unpickler running
3228 on Python 2.x. */
3229 if (self->proto == 3) {
3230 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003231 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003232 else {
3233 unicode_encoder = PyUnicode_AsASCIIString;
3234 }
3235 encoded = unicode_encoder(module_name);
3236 if (encoded == NULL) {
3237 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003238 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003239 "can't pickle module identifier '%S' using "
3240 "pickle protocol %i",
3241 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003242 goto error;
3243 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003244 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3245 PyBytes_GET_SIZE(encoded)) < 0) {
3246 Py_DECREF(encoded);
3247 goto error;
3248 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003249 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003250 if(_Pickler_Write(self, "\n", 1) < 0)
3251 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003252
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003253 /* Save the name of the module. */
3254 encoded = unicode_encoder(global_name);
3255 if (encoded == NULL) {
3256 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003257 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003258 "can't pickle global identifier '%S' using "
3259 "pickle protocol %i",
3260 global_name, self->proto);
3261 goto error;
3262 }
3263 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3264 PyBytes_GET_SIZE(encoded)) < 0) {
3265 Py_DECREF(encoded);
3266 goto error;
3267 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003268 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003269 if (_Pickler_Write(self, "\n", 1) < 0)
3270 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003272 /* Memoize the object. */
3273 if (memo_put(self, obj) < 0)
3274 goto error;
3275 }
3276
3277 if (0) {
3278 error:
3279 status = -1;
3280 }
3281 Py_XDECREF(module_name);
3282 Py_XDECREF(global_name);
3283 Py_XDECREF(module);
3284
3285 return status;
3286}
3287
3288static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003289save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3290{
3291 PyObject *reduce_value;
3292 int status;
3293
3294 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3295 if (reduce_value == NULL) {
3296 return -1;
3297 }
3298 status = save_reduce(self, reduce_value, obj);
3299 Py_DECREF(reduce_value);
3300 return status;
3301}
3302
3303static int
3304save_type(PicklerObject *self, PyObject *obj)
3305{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003306 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003307 return save_singleton_type(self, obj, Py_None);
3308 }
3309 else if (obj == (PyObject *)&PyEllipsis_Type) {
3310 return save_singleton_type(self, obj, Py_Ellipsis);
3311 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003312 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003313 return save_singleton_type(self, obj, Py_NotImplemented);
3314 }
3315 return save_global(self, obj, NULL);
3316}
3317
3318static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003319save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3320{
3321 PyObject *pid = NULL;
3322 int status = 0;
3323
3324 const char persid_op = PERSID;
3325 const char binpersid_op = BINPERSID;
3326
3327 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003328 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003329 if (pid == NULL)
3330 return -1;
3331
3332 if (pid != Py_None) {
3333 if (self->bin) {
3334 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003335 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003336 goto error;
3337 }
3338 else {
3339 PyObject *pid_str = NULL;
3340 char *pid_ascii_bytes;
3341 Py_ssize_t size;
3342
3343 pid_str = PyObject_Str(pid);
3344 if (pid_str == NULL)
3345 goto error;
3346
3347 /* XXX: Should it check whether the persistent id only contains
3348 ASCII characters? And what if the pid contains embedded
3349 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003350 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003351 Py_DECREF(pid_str);
3352 if (pid_ascii_bytes == NULL)
3353 goto error;
3354
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003355 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3356 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3357 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 goto error;
3359 }
3360 status = 1;
3361 }
3362
3363 if (0) {
3364 error:
3365 status = -1;
3366 }
3367 Py_XDECREF(pid);
3368
3369 return status;
3370}
3371
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003372static PyObject *
3373get_class(PyObject *obj)
3374{
3375 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003376 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003377
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003378 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003379 if (cls == NULL) {
3380 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3381 PyErr_Clear();
3382 cls = (PyObject *) Py_TYPE(obj);
3383 Py_INCREF(cls);
3384 }
3385 }
3386 return cls;
3387}
3388
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003389/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3390 * appropriate __reduce__ method for obj.
3391 */
3392static int
3393save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3394{
3395 PyObject *callable;
3396 PyObject *argtup;
3397 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003398 PyObject *listitems = Py_None;
3399 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003400 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003401 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003402 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003403
3404 const char reduce_op = REDUCE;
3405 const char build_op = BUILD;
3406 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003407 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003409 size = PyTuple_Size(args);
3410 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003411 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003412 "__reduce__ must contain 2 through 5 elements");
3413 return -1;
3414 }
3415
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003416 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3417 &callable, &argtup, &state, &listitems, &dictitems))
3418 return -1;
3419
3420 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003421 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003422 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003423 return -1;
3424 }
3425 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003426 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003427 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003428 return -1;
3429 }
3430
3431 if (state == Py_None)
3432 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003433
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003434 if (listitems == Py_None)
3435 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003436 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003437 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003438 "returned by __reduce__ must be an iterator, not %s",
3439 Py_TYPE(listitems)->tp_name);
3440 return -1;
3441 }
3442
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443 if (dictitems == Py_None)
3444 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003445 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003446 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003447 "returned by __reduce__ must be an iterator, not %s",
3448 Py_TYPE(dictitems)->tp_name);
3449 return -1;
3450 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003451
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003452 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003453 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003454 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003455
Victor Stinner804e05e2013-11-14 01:26:17 +01003456 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003457 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003458 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003459 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003460 }
3461 PyErr_Clear();
3462 }
3463 else if (self->proto >= 4) {
3464 _Py_IDENTIFIER(__newobj_ex__);
3465 use_newobj_ex = PyUnicode_Check(name) &&
3466 PyUnicode_Compare(
3467 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3468 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003469 }
3470 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003471 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003472 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003473 PyUnicode_Compare(
3474 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003475 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003476 }
3477 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003478
3479 if (use_newobj_ex) {
3480 PyObject *cls;
3481 PyObject *args;
3482 PyObject *kwargs;
3483
3484 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003485 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003486 "length of the NEWOBJ_EX argument tuple must be "
3487 "exactly 3, not %zd", Py_SIZE(argtup));
3488 return -1;
3489 }
3490
3491 cls = PyTuple_GET_ITEM(argtup, 0);
3492 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003493 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003494 "first item from NEWOBJ_EX argument tuple must "
3495 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3496 return -1;
3497 }
3498 args = PyTuple_GET_ITEM(argtup, 1);
3499 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003500 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003501 "second item from NEWOBJ_EX argument tuple must "
3502 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3503 return -1;
3504 }
3505 kwargs = PyTuple_GET_ITEM(argtup, 2);
3506 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003507 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003508 "third item from NEWOBJ_EX argument tuple must "
3509 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3510 return -1;
3511 }
3512
3513 if (save(self, cls, 0) < 0 ||
3514 save(self, args, 0) < 0 ||
3515 save(self, kwargs, 0) < 0 ||
3516 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3517 return -1;
3518 }
3519 }
3520 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003521 PyObject *cls;
3522 PyObject *newargtup;
3523 PyObject *obj_class;
3524 int p;
3525
3526 /* Sanity checks. */
3527 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003528 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003529 return -1;
3530 }
3531
3532 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003533 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003534 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003535 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003536 return -1;
3537 }
3538
3539 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003540 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541 p = obj_class != cls; /* true iff a problem */
3542 Py_DECREF(obj_class);
3543 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003544 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545 "__newobj__ args has the wrong class");
3546 return -1;
3547 }
3548 }
3549 /* XXX: These calls save() are prone to infinite recursion. Imagine
3550 what happen if the value returned by the __reduce__() method of
3551 some extension type contains another object of the same type. Ouch!
3552
3553 Here is a quick example, that I ran into, to illustrate what I
3554 mean:
3555
3556 >>> import pickle, copyreg
3557 >>> copyreg.dispatch_table.pop(complex)
3558 >>> pickle.dumps(1+2j)
3559 Traceback (most recent call last):
3560 ...
3561 RuntimeError: maximum recursion depth exceeded
3562
3563 Removing the complex class from copyreg.dispatch_table made the
3564 __reduce_ex__() method emit another complex object:
3565
3566 >>> (1+1j).__reduce_ex__(2)
3567 (<function __newobj__ at 0xb7b71c3c>,
3568 (<class 'complex'>, (1+1j)), None, None, None)
3569
3570 Thus when save() was called on newargstup (the 2nd item) recursion
3571 ensued. Of course, the bug was in the complex class which had a
3572 broken __getnewargs__() that emitted another complex object. But,
3573 the point, here, is it is quite easy to end up with a broken reduce
3574 function. */
3575
3576 /* Save the class and its __new__ arguments. */
3577 if (save(self, cls, 0) < 0)
3578 return -1;
3579
3580 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3581 if (newargtup == NULL)
3582 return -1;
3583
3584 p = save(self, newargtup, 0);
3585 Py_DECREF(newargtup);
3586 if (p < 0)
3587 return -1;
3588
3589 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003590 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003591 return -1;
3592 }
3593 else { /* Not using NEWOBJ. */
3594 if (save(self, callable, 0) < 0 ||
3595 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003596 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 return -1;
3598 }
3599
3600 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3601 the caller do not want to memoize the object. Not particularly useful,
3602 but that is to mimic the behavior save_reduce() in pickle.py when
3603 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003604 if (obj != NULL) {
3605 /* If the object is already in the memo, this means it is
3606 recursive. In this case, throw away everything we put on the
3607 stack, and fetch the object back from the memo. */
3608 if (PyMemoTable_Get(self->memo, obj)) {
3609 const char pop_op = POP;
3610
3611 if (_Pickler_Write(self, &pop_op, 1) < 0)
3612 return -1;
3613 if (memo_get(self, obj) < 0)
3614 return -1;
3615
3616 return 0;
3617 }
3618 else if (memo_put(self, obj) < 0)
3619 return -1;
3620 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003621
3622 if (listitems && batch_list(self, listitems) < 0)
3623 return -1;
3624
3625 if (dictitems && batch_dict(self, dictitems) < 0)
3626 return -1;
3627
3628 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003629 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003630 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003631 return -1;
3632 }
3633
3634 return 0;
3635}
3636
3637static int
3638save(PicklerObject *self, PyObject *obj, int pers_save)
3639{
3640 PyTypeObject *type;
3641 PyObject *reduce_func = NULL;
3642 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003643 int status = 0;
3644
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003645 if (_Pickler_OpcodeBoundary(self) < 0)
3646 return -1;
3647
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003648 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003649 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003650
3651 /* The extra pers_save argument is necessary to avoid calling save_pers()
3652 on its returned object. */
3653 if (!pers_save && self->pers_func) {
3654 /* save_pers() returns:
3655 -1 to signal an error;
3656 0 if it did nothing successfully;
3657 1 if a persistent id was saved.
3658 */
3659 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3660 goto done;
3661 }
3662
3663 type = Py_TYPE(obj);
3664
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003665 /* The old cPickle had an optimization that used switch-case statement
3666 dispatching on the first letter of the type name. This has was removed
3667 since benchmarks shown that this optimization was actually slowing
3668 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669
3670 /* Atom types; these aren't memoized, so don't check the memo. */
3671
3672 if (obj == Py_None) {
3673 status = save_none(self, obj);
3674 goto done;
3675 }
3676 else if (obj == Py_False || obj == Py_True) {
3677 status = save_bool(self, obj);
3678 goto done;
3679 }
3680 else if (type == &PyLong_Type) {
3681 status = save_long(self, obj);
3682 goto done;
3683 }
3684 else if (type == &PyFloat_Type) {
3685 status = save_float(self, obj);
3686 goto done;
3687 }
3688
3689 /* Check the memo to see if it has the object. If so, generate
3690 a GET (or BINGET) opcode, instead of pickling the object
3691 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003692 if (PyMemoTable_Get(self->memo, obj)) {
3693 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003694 goto error;
3695 goto done;
3696 }
3697
3698 if (type == &PyBytes_Type) {
3699 status = save_bytes(self, obj);
3700 goto done;
3701 }
3702 else if (type == &PyUnicode_Type) {
3703 status = save_unicode(self, obj);
3704 goto done;
3705 }
3706 else if (type == &PyDict_Type) {
3707 status = save_dict(self, obj);
3708 goto done;
3709 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003710 else if (type == &PySet_Type) {
3711 status = save_set(self, obj);
3712 goto done;
3713 }
3714 else if (type == &PyFrozenSet_Type) {
3715 status = save_frozenset(self, obj);
3716 goto done;
3717 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003718 else if (type == &PyList_Type) {
3719 status = save_list(self, obj);
3720 goto done;
3721 }
3722 else if (type == &PyTuple_Type) {
3723 status = save_tuple(self, obj);
3724 goto done;
3725 }
3726 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003727 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003728 goto done;
3729 }
3730 else if (type == &PyFunction_Type) {
3731 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003732 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003733 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003734
3735 /* XXX: This part needs some unit tests. */
3736
3737 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003738 * self.dispatch_table, copyreg.dispatch_table, the object's
3739 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003740 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003741 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003742 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003743 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3744 (PyObject *)type);
3745 if (reduce_func == NULL) {
3746 if (PyErr_Occurred()) {
3747 goto error;
3748 }
3749 } else {
3750 /* PyDict_GetItemWithError() returns a borrowed reference.
3751 Increase the reference count to be consistent with
3752 PyObject_GetItem and _PyObject_GetAttrId used below. */
3753 Py_INCREF(reduce_func);
3754 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003755 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003756 reduce_func = PyObject_GetItem(self->dispatch_table,
3757 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003758 if (reduce_func == NULL) {
3759 if (PyErr_ExceptionMatches(PyExc_KeyError))
3760 PyErr_Clear();
3761 else
3762 goto error;
3763 }
3764 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003765 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003766 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003767 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003769 else if (PyType_IsSubtype(type, &PyType_Type)) {
3770 status = save_global(self, obj, NULL);
3771 goto done;
3772 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003773 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003774 _Py_IDENTIFIER(__reduce__);
3775 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003776
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003777
3778 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3779 automatically defined as __reduce__. While this is convenient, this
3780 make it impossible to know which method was actually called. Of
3781 course, this is not a big deal. But still, it would be nice to let
3782 the user know which method was called when something go
3783 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3784 don't actually have to check for a __reduce__ method. */
3785
3786 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003787 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003788 if (reduce_func != NULL) {
3789 PyObject *proto;
3790 proto = PyLong_FromLong(self->proto);
3791 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003792 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003793 }
3794 }
3795 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003796 PickleState *st = _Pickle_GetGlobalState();
3797
3798 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003799 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003800 }
3801 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003803 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003804 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003805 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003806 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003807 PyObject *empty_tuple = PyTuple_New(0);
3808 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003809 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003810 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003811 }
3812 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003813 PyErr_Format(st->PicklingError,
3814 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003815 type->tp_name, obj);
3816 goto error;
3817 }
3818 }
3819 }
3820
3821 if (reduce_value == NULL)
3822 goto error;
3823
3824 if (PyUnicode_Check(reduce_value)) {
3825 status = save_global(self, obj, reduce_value);
3826 goto done;
3827 }
3828
3829 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003830 PickleState *st = _Pickle_GetGlobalState();
3831 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003832 "__reduce__ must return a string or tuple");
3833 goto error;
3834 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835
3836 status = save_reduce(self, reduce_value, obj);
3837
3838 if (0) {
3839 error:
3840 status = -1;
3841 }
3842 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003843
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003844 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003845 Py_XDECREF(reduce_func);
3846 Py_XDECREF(reduce_value);
3847
3848 return status;
3849}
3850
3851static int
3852dump(PicklerObject *self, PyObject *obj)
3853{
3854 const char stop_op = STOP;
3855
3856 if (self->proto >= 2) {
3857 char header[2];
3858
3859 header[0] = PROTO;
3860 assert(self->proto >= 0 && self->proto < 256);
3861 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003862 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003863 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003864 if (self->proto >= 4)
3865 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 }
3867
3868 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003869 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003870 return -1;
3871
3872 return 0;
3873}
3874
Larry Hastings61272b72014-01-07 12:41:53 -08003875/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003876
3877_pickle.Pickler.clear_memo
3878
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003879Clears the pickler's "memo".
3880
3881The memo is the data structure that remembers which objects the
3882pickler has already seen, so that shared or recursive objects are
3883pickled by reference and not by value. This method is useful when
3884re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003885[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003886
Larry Hastings3cceb382014-01-04 11:09:09 -08003887static PyObject *
3888_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003889/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003890{
3891 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003892 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003893
3894 Py_RETURN_NONE;
3895}
3896
Larry Hastings61272b72014-01-07 12:41:53 -08003897/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003898
3899_pickle.Pickler.dump
3900
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003901 obj: object
3902 /
3903
3904Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003905[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003906
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003908_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003909/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003911 /* Check whether the Pickler was initialized correctly (issue3664).
3912 Developers often forget to call __init__() in their subclasses, which
3913 would trigger a segfault without this check. */
3914 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003915 PickleState *st = _Pickle_GetGlobalState();
3916 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003917 "Pickler.__init__() was not called by %s.__init__()",
3918 Py_TYPE(self)->tp_name);
3919 return NULL;
3920 }
3921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003922 if (_Pickler_ClearBuffer(self) < 0)
3923 return NULL;
3924
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003925 if (dump(self, obj) < 0)
3926 return NULL;
3927
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003928 if (_Pickler_FlushToFile(self) < 0)
3929 return NULL;
3930
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 Py_RETURN_NONE;
3932}
3933
3934static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003935 _PICKLE_PICKLER_DUMP_METHODDEF
3936 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 {NULL, NULL} /* sentinel */
3938};
3939
3940static void
3941Pickler_dealloc(PicklerObject *self)
3942{
3943 PyObject_GC_UnTrack(self);
3944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003945 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003946 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003948 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949 Py_XDECREF(self->fast_memo);
3950
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003951 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952
3953 Py_TYPE(self)->tp_free((PyObject *)self);
3954}
3955
3956static int
3957Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3958{
3959 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003961 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962 Py_VISIT(self->fast_memo);
3963 return 0;
3964}
3965
3966static int
3967Pickler_clear(PicklerObject *self)
3968{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003969 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003970 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003972 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 Py_CLEAR(self->fast_memo);
3974
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003975 if (self->memo != NULL) {
3976 PyMemoTable *memo = self->memo;
3977 self->memo = NULL;
3978 PyMemoTable_Del(memo);
3979 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003980 return 0;
3981}
3982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003983
Larry Hastings61272b72014-01-07 12:41:53 -08003984/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003985
3986_pickle.Pickler.__init__
3987
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003988 file: object
3989 protocol: object = NULL
3990 fix_imports: bool = True
3991
3992This takes a binary file for writing a pickle data stream.
3993
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08003994The optional *protocol* argument tells the pickler to use the given
3995protocol; supported protocols are 0, 1, 2, 3 and 4. The default
3996protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003997
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08003998Specifying a negative protocol version selects the highest protocol
3999version supported. The higher the protocol used, the more recent the
4000version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004001
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004002The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004003bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004004writing, a io.BytesIO instance, or any other custom object that meets
4005this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004006
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004007If *fix_imports* is True and protocol is less than 3, pickle will try
4008to map the new Python 3 names to the old module names used in Python
40092, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004010[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004011
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004012static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004013_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08004014/*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004015{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004016 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004017 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004018
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019 /* In case of multiple __init__() calls, clear previous content. */
4020 if (self->write != NULL)
4021 (void)Pickler_clear(self);
4022
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004023 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004024 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004025
4026 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004027 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004028
4029 /* memo and output_buffer may have already been created in _Pickler_New */
4030 if (self->memo == NULL) {
4031 self->memo = PyMemoTable_New();
4032 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004033 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004034 }
4035 self->output_len = 0;
4036 if (self->output_buffer == NULL) {
4037 self->max_output_len = WRITE_BUF_SIZE;
4038 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4039 self->max_output_len);
4040 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004041 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004042 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004044 self->fast = 0;
4045 self->fast_nesting = 0;
4046 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004047 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004048 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4049 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4050 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004051 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004052 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004053 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004054 self->dispatch_table = NULL;
4055 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4056 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4057 &PyId_dispatch_table);
4058 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004059 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004060 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004061
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 return 0;
4063}
4064
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004065
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004066/* Define a proxy object for the Pickler's internal memo object. This is to
4067 * avoid breaking code like:
4068 * pickler.memo.clear()
4069 * and
4070 * pickler.memo = saved_memo
4071 * Is this a good idea? Not really, but we don't want to break code that uses
4072 * it. Note that we don't implement the entire mapping API here. This is
4073 * intentional, as these should be treated as black-box implementation details.
4074 */
4075
Larry Hastings61272b72014-01-07 12:41:53 -08004076/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004077_pickle.PicklerMemoProxy.clear
4078
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004079Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004080[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004081
Larry Hastings3cceb382014-01-04 11:09:09 -08004082static PyObject *
4083_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004084/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004085{
4086 if (self->pickler->memo)
4087 PyMemoTable_Clear(self->pickler->memo);
4088 Py_RETURN_NONE;
4089}
4090
Larry Hastings61272b72014-01-07 12:41:53 -08004091/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004092_pickle.PicklerMemoProxy.copy
4093
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004094Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004095[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004096
Larry Hastings3cceb382014-01-04 11:09:09 -08004097static PyObject *
4098_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004099/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004100{
4101 Py_ssize_t i;
4102 PyMemoTable *memo;
4103 PyObject *new_memo = PyDict_New();
4104 if (new_memo == NULL)
4105 return NULL;
4106
4107 memo = self->pickler->memo;
4108 for (i = 0; i < memo->mt_allocated; ++i) {
4109 PyMemoEntry entry = memo->mt_table[i];
4110 if (entry.me_key != NULL) {
4111 int status;
4112 PyObject *key, *value;
4113
4114 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004115 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004116
4117 if (key == NULL || value == NULL) {
4118 Py_XDECREF(key);
4119 Py_XDECREF(value);
4120 goto error;
4121 }
4122 status = PyDict_SetItem(new_memo, key, value);
4123 Py_DECREF(key);
4124 Py_DECREF(value);
4125 if (status < 0)
4126 goto error;
4127 }
4128 }
4129 return new_memo;
4130
4131 error:
4132 Py_XDECREF(new_memo);
4133 return NULL;
4134}
4135
Larry Hastings61272b72014-01-07 12:41:53 -08004136/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004137_pickle.PicklerMemoProxy.__reduce__
4138
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004139Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004140[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004141
Larry Hastings3cceb382014-01-04 11:09:09 -08004142static PyObject *
4143_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004144/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004145{
4146 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004147 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004148 if (contents == NULL)
4149 return NULL;
4150
4151 reduce_value = PyTuple_New(2);
4152 if (reduce_value == NULL) {
4153 Py_DECREF(contents);
4154 return NULL;
4155 }
4156 dict_args = PyTuple_New(1);
4157 if (dict_args == NULL) {
4158 Py_DECREF(contents);
4159 Py_DECREF(reduce_value);
4160 return NULL;
4161 }
4162 PyTuple_SET_ITEM(dict_args, 0, contents);
4163 Py_INCREF((PyObject *)&PyDict_Type);
4164 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4165 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4166 return reduce_value;
4167}
4168
4169static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4171 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4172 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004173 {NULL, NULL} /* sentinel */
4174};
4175
4176static void
4177PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4178{
4179 PyObject_GC_UnTrack(self);
4180 Py_XDECREF(self->pickler);
4181 PyObject_GC_Del((PyObject *)self);
4182}
4183
4184static int
4185PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4186 visitproc visit, void *arg)
4187{
4188 Py_VISIT(self->pickler);
4189 return 0;
4190}
4191
4192static int
4193PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4194{
4195 Py_CLEAR(self->pickler);
4196 return 0;
4197}
4198
4199static PyTypeObject PicklerMemoProxyType = {
4200 PyVarObject_HEAD_INIT(NULL, 0)
4201 "_pickle.PicklerMemoProxy", /*tp_name*/
4202 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4203 0,
4204 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4205 0, /* tp_print */
4206 0, /* tp_getattr */
4207 0, /* tp_setattr */
4208 0, /* tp_compare */
4209 0, /* tp_repr */
4210 0, /* tp_as_number */
4211 0, /* tp_as_sequence */
4212 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004213 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004214 0, /* tp_call */
4215 0, /* tp_str */
4216 PyObject_GenericGetAttr, /* tp_getattro */
4217 PyObject_GenericSetAttr, /* tp_setattro */
4218 0, /* tp_as_buffer */
4219 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4220 0, /* tp_doc */
4221 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4222 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4223 0, /* tp_richcompare */
4224 0, /* tp_weaklistoffset */
4225 0, /* tp_iter */
4226 0, /* tp_iternext */
4227 picklerproxy_methods, /* tp_methods */
4228};
4229
4230static PyObject *
4231PicklerMemoProxy_New(PicklerObject *pickler)
4232{
4233 PicklerMemoProxyObject *self;
4234
4235 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4236 if (self == NULL)
4237 return NULL;
4238 Py_INCREF(pickler);
4239 self->pickler = pickler;
4240 PyObject_GC_Track(self);
4241 return (PyObject *)self;
4242}
4243
4244/*****************************************************************************/
4245
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004246static PyObject *
4247Pickler_get_memo(PicklerObject *self)
4248{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004249 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004250}
4251
4252static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004253Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004254{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004255 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004256
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004257 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004258 PyErr_SetString(PyExc_TypeError,
4259 "attribute deletion is not supported");
4260 return -1;
4261 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004262
4263 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4264 PicklerObject *pickler =
4265 ((PicklerMemoProxyObject *)obj)->pickler;
4266
4267 new_memo = PyMemoTable_Copy(pickler->memo);
4268 if (new_memo == NULL)
4269 return -1;
4270 }
4271 else if (PyDict_Check(obj)) {
4272 Py_ssize_t i = 0;
4273 PyObject *key, *value;
4274
4275 new_memo = PyMemoTable_New();
4276 if (new_memo == NULL)
4277 return -1;
4278
4279 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004280 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004281 PyObject *memo_obj;
4282
4283 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4284 PyErr_SetString(PyExc_TypeError,
4285 "'memo' values must be 2-item tuples");
4286 goto error;
4287 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004288 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004289 if (memo_id == -1 && PyErr_Occurred())
4290 goto error;
4291 memo_obj = PyTuple_GET_ITEM(value, 1);
4292 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4293 goto error;
4294 }
4295 }
4296 else {
4297 PyErr_Format(PyExc_TypeError,
4298 "'memo' attribute must be an PicklerMemoProxy object"
4299 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004300 return -1;
4301 }
4302
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004303 PyMemoTable_Del(self->memo);
4304 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004305
4306 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004307
4308 error:
4309 if (new_memo)
4310 PyMemoTable_Del(new_memo);
4311 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004312}
4313
4314static PyObject *
4315Pickler_get_persid(PicklerObject *self)
4316{
4317 if (self->pers_func == NULL)
4318 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4319 else
4320 Py_INCREF(self->pers_func);
4321 return self->pers_func;
4322}
4323
4324static int
4325Pickler_set_persid(PicklerObject *self, PyObject *value)
4326{
4327 PyObject *tmp;
4328
4329 if (value == NULL) {
4330 PyErr_SetString(PyExc_TypeError,
4331 "attribute deletion is not supported");
4332 return -1;
4333 }
4334 if (!PyCallable_Check(value)) {
4335 PyErr_SetString(PyExc_TypeError,
4336 "persistent_id must be a callable taking one argument");
4337 return -1;
4338 }
4339
4340 tmp = self->pers_func;
4341 Py_INCREF(value);
4342 self->pers_func = value;
4343 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4344
4345 return 0;
4346}
4347
4348static PyMemberDef Pickler_members[] = {
4349 {"bin", T_INT, offsetof(PicklerObject, bin)},
4350 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004351 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004352 {NULL}
4353};
4354
4355static PyGetSetDef Pickler_getsets[] = {
4356 {"memo", (getter)Pickler_get_memo,
4357 (setter)Pickler_set_memo},
4358 {"persistent_id", (getter)Pickler_get_persid,
4359 (setter)Pickler_set_persid},
4360 {NULL}
4361};
4362
4363static PyTypeObject Pickler_Type = {
4364 PyVarObject_HEAD_INIT(NULL, 0)
4365 "_pickle.Pickler" , /*tp_name*/
4366 sizeof(PicklerObject), /*tp_basicsize*/
4367 0, /*tp_itemsize*/
4368 (destructor)Pickler_dealloc, /*tp_dealloc*/
4369 0, /*tp_print*/
4370 0, /*tp_getattr*/
4371 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004372 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004373 0, /*tp_repr*/
4374 0, /*tp_as_number*/
4375 0, /*tp_as_sequence*/
4376 0, /*tp_as_mapping*/
4377 0, /*tp_hash*/
4378 0, /*tp_call*/
4379 0, /*tp_str*/
4380 0, /*tp_getattro*/
4381 0, /*tp_setattro*/
4382 0, /*tp_as_buffer*/
4383 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004384 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004385 (traverseproc)Pickler_traverse, /*tp_traverse*/
4386 (inquiry)Pickler_clear, /*tp_clear*/
4387 0, /*tp_richcompare*/
4388 0, /*tp_weaklistoffset*/
4389 0, /*tp_iter*/
4390 0, /*tp_iternext*/
4391 Pickler_methods, /*tp_methods*/
4392 Pickler_members, /*tp_members*/
4393 Pickler_getsets, /*tp_getset*/
4394 0, /*tp_base*/
4395 0, /*tp_dict*/
4396 0, /*tp_descr_get*/
4397 0, /*tp_descr_set*/
4398 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004399 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004400 PyType_GenericAlloc, /*tp_alloc*/
4401 PyType_GenericNew, /*tp_new*/
4402 PyObject_GC_Del, /*tp_free*/
4403 0, /*tp_is_gc*/
4404};
4405
Victor Stinner121aab42011-09-29 23:40:53 +02004406/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004407
4408 XXX: It would be nice to able to avoid Python function call overhead, by
4409 using directly the C version of find_class(), when find_class() is not
4410 overridden by a subclass. Although, this could become rather hackish. A
4411 simpler optimization would be to call the C function when self is not a
4412 subclass instance. */
4413static PyObject *
4414find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4415{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004416 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004417
4418 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4419 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004420}
4421
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004422static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004423marker(UnpicklerObject *self)
4424{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004425 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004426 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004427 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004428 return -1;
4429 }
4430
4431 return self->marks[--self->num_marks];
4432}
4433
4434static int
4435load_none(UnpicklerObject *self)
4436{
4437 PDATA_APPEND(self->stack, Py_None, -1);
4438 return 0;
4439}
4440
4441static int
4442bad_readline(void)
4443{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004444 PickleState *st = _Pickle_GetGlobalState();
4445 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004446 return -1;
4447}
4448
4449static int
4450load_int(UnpicklerObject *self)
4451{
4452 PyObject *value;
4453 char *endptr, *s;
4454 Py_ssize_t len;
4455 long x;
4456
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004457 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004458 return -1;
4459 if (len < 2)
4460 return bad_readline();
4461
4462 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004463 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004464 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004465 x = strtol(s, &endptr, 0);
4466
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004467 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004468 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004469 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 errno = 0;
4471 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004472 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004473 if (value == NULL) {
4474 PyErr_SetString(PyExc_ValueError,
4475 "could not convert string to int");
4476 return -1;
4477 }
4478 }
4479 else {
4480 if (len == 3 && (x == 0 || x == 1)) {
4481 if ((value = PyBool_FromLong(x)) == NULL)
4482 return -1;
4483 }
4484 else {
4485 if ((value = PyLong_FromLong(x)) == NULL)
4486 return -1;
4487 }
4488 }
4489
4490 PDATA_PUSH(self->stack, value, -1);
4491 return 0;
4492}
4493
4494static int
4495load_bool(UnpicklerObject *self, PyObject *boolean)
4496{
4497 assert(boolean == Py_True || boolean == Py_False);
4498 PDATA_APPEND(self->stack, boolean, -1);
4499 return 0;
4500}
4501
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004502/* s contains x bytes of an unsigned little-endian integer. Return its value
4503 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4504 */
4505static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004506calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004507{
4508 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004509 Py_ssize_t i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004510 size_t x = 0;
4511
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004512 for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004513 x |= (size_t) s[i] << (8 * i);
4514 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004515
4516 if (x > PY_SSIZE_T_MAX)
4517 return -1;
4518 else
4519 return (Py_ssize_t) x;
4520}
4521
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004522/* s contains x bytes of a little-endian integer. Return its value as a
4523 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4524 * int, but when x is 4 it's a signed one. This is an historical source
4525 * of x-platform bugs.
4526 */
4527static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004528calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004529{
4530 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004531 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004532 long x = 0;
4533
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004534 for (i = 0; i < nbytes; i++) {
4535 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004536 }
4537
4538 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4539 * is signed, so on a box with longs bigger than 4 bytes we need
4540 * to extend a BININT's sign bit to the full width.
4541 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004542 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004543 x |= -(x & (1L << 31));
4544 }
4545
4546 return x;
4547}
4548
4549static int
4550load_binintx(UnpicklerObject *self, char *s, int size)
4551{
4552 PyObject *value;
4553 long x;
4554
4555 x = calc_binint(s, size);
4556
4557 if ((value = PyLong_FromLong(x)) == NULL)
4558 return -1;
4559
4560 PDATA_PUSH(self->stack, value, -1);
4561 return 0;
4562}
4563
4564static int
4565load_binint(UnpicklerObject *self)
4566{
4567 char *s;
4568
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004569 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570 return -1;
4571
4572 return load_binintx(self, s, 4);
4573}
4574
4575static int
4576load_binint1(UnpicklerObject *self)
4577{
4578 char *s;
4579
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004580 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004581 return -1;
4582
4583 return load_binintx(self, s, 1);
4584}
4585
4586static int
4587load_binint2(UnpicklerObject *self)
4588{
4589 char *s;
4590
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004591 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004592 return -1;
4593
4594 return load_binintx(self, s, 2);
4595}
4596
4597static int
4598load_long(UnpicklerObject *self)
4599{
4600 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004601 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004602 Py_ssize_t len;
4603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004604 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004605 return -1;
4606 if (len < 2)
4607 return bad_readline();
4608
Mark Dickinson8dd05142009-01-20 20:43:58 +00004609 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4610 the 'L' before calling PyLong_FromString. In order to maintain
4611 compatibility with Python 3.0.0, we don't actually *require*
4612 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004613 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004614 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004615 /* XXX: Should the base argument explicitly set to 10? */
4616 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004617 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618 return -1;
4619
4620 PDATA_PUSH(self->stack, value, -1);
4621 return 0;
4622}
4623
4624/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4625 * data following.
4626 */
4627static int
4628load_counted_long(UnpicklerObject *self, int size)
4629{
4630 PyObject *value;
4631 char *nbytes;
4632 char *pdata;
4633
4634 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004635 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 return -1;
4637
4638 size = calc_binint(nbytes, size);
4639 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004640 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004642 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004643 "LONG pickle has negative byte count");
4644 return -1;
4645 }
4646
4647 if (size == 0)
4648 value = PyLong_FromLong(0L);
4649 else {
4650 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004651 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004652 return -1;
4653 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4654 1 /* little endian */ , 1 /* signed */ );
4655 }
4656 if (value == NULL)
4657 return -1;
4658 PDATA_PUSH(self->stack, value, -1);
4659 return 0;
4660}
4661
4662static int
4663load_float(UnpicklerObject *self)
4664{
4665 PyObject *value;
4666 char *endptr, *s;
4667 Py_ssize_t len;
4668 double d;
4669
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004670 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004671 return -1;
4672 if (len < 2)
4673 return bad_readline();
4674
4675 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004676 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4677 if (d == -1.0 && PyErr_Occurred())
4678 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004679 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004680 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4681 return -1;
4682 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004683 value = PyFloat_FromDouble(d);
4684 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004685 return -1;
4686
4687 PDATA_PUSH(self->stack, value, -1);
4688 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004689}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690
4691static int
4692load_binfloat(UnpicklerObject *self)
4693{
4694 PyObject *value;
4695 double x;
4696 char *s;
4697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004698 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699 return -1;
4700
4701 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4702 if (x == -1.0 && PyErr_Occurred())
4703 return -1;
4704
4705 if ((value = PyFloat_FromDouble(x)) == NULL)
4706 return -1;
4707
4708 PDATA_PUSH(self->stack, value, -1);
4709 return 0;
4710}
4711
4712static int
4713load_string(UnpicklerObject *self)
4714{
4715 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004716 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004717 Py_ssize_t len;
4718 char *s, *p;
4719
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004720 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004721 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004722 /* Strip the newline */
4723 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004724 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004725 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004726 p = s + 1;
4727 len -= 2;
4728 }
4729 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004730 PickleState *st = _Pickle_GetGlobalState();
4731 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004732 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 return -1;
4734 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004735 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736
4737 /* Use the PyBytes API to decode the string, since that is what is used
4738 to encode, and then coerce the result to Unicode. */
4739 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 if (bytes == NULL)
4741 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004742
4743 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4744 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4745 if (strcmp(self->encoding, "bytes") == 0) {
4746 obj = bytes;
4747 }
4748 else {
4749 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4750 Py_DECREF(bytes);
4751 if (obj == NULL) {
4752 return -1;
4753 }
4754 }
4755
4756 PDATA_PUSH(self->stack, obj, -1);
4757 return 0;
4758}
4759
4760static int
4761load_counted_binstring(UnpicklerObject *self, int nbytes)
4762{
4763 PyObject *obj;
4764 Py_ssize_t size;
4765 char *s;
4766
4767 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004768 return -1;
4769
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004770 size = calc_binsize(s, nbytes);
4771 if (size < 0) {
4772 PickleState *st = _Pickle_GetGlobalState();
4773 PyErr_Format(st->UnpicklingError,
4774 "BINSTRING exceeds system's maximum size of %zd bytes",
4775 PY_SSIZE_T_MAX);
4776 return -1;
4777 }
4778
4779 if (_Unpickler_Read(self, &s, size) < 0)
4780 return -1;
4781
4782 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4783 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4784 if (strcmp(self->encoding, "bytes") == 0) {
4785 obj = PyBytes_FromStringAndSize(s, size);
4786 }
4787 else {
4788 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4789 }
4790 if (obj == NULL) {
4791 return -1;
4792 }
4793
4794 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004795 return 0;
4796}
4797
4798static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004799load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004800{
4801 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004802 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004803 char *s;
4804
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004805 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004806 return -1;
4807
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004808 size = calc_binsize(s, nbytes);
4809 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004810 PyErr_Format(PyExc_OverflowError,
4811 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004812 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004813 return -1;
4814 }
4815
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004816 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004817 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004818
4819 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004820 if (bytes == NULL)
4821 return -1;
4822
4823 PDATA_PUSH(self->stack, bytes, -1);
4824 return 0;
4825}
4826
4827static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004828load_unicode(UnpicklerObject *self)
4829{
4830 PyObject *str;
4831 Py_ssize_t len;
4832 char *s;
4833
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004834 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004835 return -1;
4836 if (len < 1)
4837 return bad_readline();
4838
4839 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4840 if (str == NULL)
4841 return -1;
4842
4843 PDATA_PUSH(self->stack, str, -1);
4844 return 0;
4845}
4846
4847static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004848load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849{
4850 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004851 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852 char *s;
4853
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004854 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004855 return -1;
4856
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004857 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004859 PyErr_Format(PyExc_OverflowError,
4860 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004861 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 return -1;
4863 }
4864
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004865 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 return -1;
4867
Victor Stinner485fb562010-04-13 11:07:24 +00004868 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004869 if (str == NULL)
4870 return -1;
4871
4872 PDATA_PUSH(self->stack, str, -1);
4873 return 0;
4874}
4875
4876static int
4877load_tuple(UnpicklerObject *self)
4878{
4879 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004880 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881
4882 if ((i = marker(self)) < 0)
4883 return -1;
4884
4885 tuple = Pdata_poptuple(self->stack, i);
4886 if (tuple == NULL)
4887 return -1;
4888 PDATA_PUSH(self->stack, tuple, -1);
4889 return 0;
4890}
4891
4892static int
4893load_counted_tuple(UnpicklerObject *self, int len)
4894{
4895 PyObject *tuple;
4896
4897 tuple = PyTuple_New(len);
4898 if (tuple == NULL)
4899 return -1;
4900
4901 while (--len >= 0) {
4902 PyObject *item;
4903
4904 PDATA_POP(self->stack, item);
4905 if (item == NULL)
4906 return -1;
4907 PyTuple_SET_ITEM(tuple, len, item);
4908 }
4909 PDATA_PUSH(self->stack, tuple, -1);
4910 return 0;
4911}
4912
4913static int
4914load_empty_list(UnpicklerObject *self)
4915{
4916 PyObject *list;
4917
4918 if ((list = PyList_New(0)) == NULL)
4919 return -1;
4920 PDATA_PUSH(self->stack, list, -1);
4921 return 0;
4922}
4923
4924static int
4925load_empty_dict(UnpicklerObject *self)
4926{
4927 PyObject *dict;
4928
4929 if ((dict = PyDict_New()) == NULL)
4930 return -1;
4931 PDATA_PUSH(self->stack, dict, -1);
4932 return 0;
4933}
4934
4935static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004936load_empty_set(UnpicklerObject *self)
4937{
4938 PyObject *set;
4939
4940 if ((set = PySet_New(NULL)) == NULL)
4941 return -1;
4942 PDATA_PUSH(self->stack, set, -1);
4943 return 0;
4944}
4945
4946static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004947load_list(UnpicklerObject *self)
4948{
4949 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004950 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004951
4952 if ((i = marker(self)) < 0)
4953 return -1;
4954
4955 list = Pdata_poplist(self->stack, i);
4956 if (list == NULL)
4957 return -1;
4958 PDATA_PUSH(self->stack, list, -1);
4959 return 0;
4960}
4961
4962static int
4963load_dict(UnpicklerObject *self)
4964{
4965 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004966 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967
4968 if ((i = marker(self)) < 0)
4969 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004970 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004971
4972 if ((dict = PyDict_New()) == NULL)
4973 return -1;
4974
4975 for (k = i + 1; k < j; k += 2) {
4976 key = self->stack->data[k - 1];
4977 value = self->stack->data[k];
4978 if (PyDict_SetItem(dict, key, value) < 0) {
4979 Py_DECREF(dict);
4980 return -1;
4981 }
4982 }
4983 Pdata_clear(self->stack, i);
4984 PDATA_PUSH(self->stack, dict, -1);
4985 return 0;
4986}
4987
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004988static int
4989load_frozenset(UnpicklerObject *self)
4990{
4991 PyObject *items;
4992 PyObject *frozenset;
4993 Py_ssize_t i;
4994
4995 if ((i = marker(self)) < 0)
4996 return -1;
4997
4998 items = Pdata_poptuple(self->stack, i);
4999 if (items == NULL)
5000 return -1;
5001
5002 frozenset = PyFrozenSet_New(items);
5003 Py_DECREF(items);
5004 if (frozenset == NULL)
5005 return -1;
5006
5007 PDATA_PUSH(self->stack, frozenset, -1);
5008 return 0;
5009}
5010
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005011static PyObject *
5012instantiate(PyObject *cls, PyObject *args)
5013{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005014 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005015 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005016 /* Caller must assure args are a tuple. Normally, args come from
5017 Pdata_poptuple which packs objects from the top of the stack
5018 into a newly created tuple. */
5019 assert(PyTuple_Check(args));
5020 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005021 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005022 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005024 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005025 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005026
5027 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005028 }
5029 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030}
5031
5032static int
5033load_obj(UnpicklerObject *self)
5034{
5035 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005036 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037
5038 if ((i = marker(self)) < 0)
5039 return -1;
5040
5041 args = Pdata_poptuple(self->stack, i + 1);
5042 if (args == NULL)
5043 return -1;
5044
5045 PDATA_POP(self->stack, cls);
5046 if (cls) {
5047 obj = instantiate(cls, args);
5048 Py_DECREF(cls);
5049 }
5050 Py_DECREF(args);
5051 if (obj == NULL)
5052 return -1;
5053
5054 PDATA_PUSH(self->stack, obj, -1);
5055 return 0;
5056}
5057
5058static int
5059load_inst(UnpicklerObject *self)
5060{
5061 PyObject *cls = NULL;
5062 PyObject *args = NULL;
5063 PyObject *obj = NULL;
5064 PyObject *module_name;
5065 PyObject *class_name;
5066 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005067 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005068 char *s;
5069
5070 if ((i = marker(self)) < 0)
5071 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005072 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005073 return -1;
5074 if (len < 2)
5075 return bad_readline();
5076
5077 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5078 identifiers are permitted in Python 3.0, since the INST opcode is only
5079 supported by older protocols on Python 2.x. */
5080 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5081 if (module_name == NULL)
5082 return -1;
5083
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005084 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005085 if (len < 2)
5086 return bad_readline();
5087 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005088 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005089 cls = find_class(self, module_name, class_name);
5090 Py_DECREF(class_name);
5091 }
5092 }
5093 Py_DECREF(module_name);
5094
5095 if (cls == NULL)
5096 return -1;
5097
5098 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5099 obj = instantiate(cls, args);
5100 Py_DECREF(args);
5101 }
5102 Py_DECREF(cls);
5103
5104 if (obj == NULL)
5105 return -1;
5106
5107 PDATA_PUSH(self->stack, obj, -1);
5108 return 0;
5109}
5110
5111static int
5112load_newobj(UnpicklerObject *self)
5113{
5114 PyObject *args = NULL;
5115 PyObject *clsraw = NULL;
5116 PyTypeObject *cls; /* clsraw cast to its true type */
5117 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005118 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119
5120 /* Stack is ... cls argtuple, and we want to call
5121 * cls.__new__(cls, *argtuple).
5122 */
5123 PDATA_POP(self->stack, args);
5124 if (args == NULL)
5125 goto error;
5126 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005127 PyErr_SetString(st->UnpicklingError,
5128 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005129 goto error;
5130 }
5131
5132 PDATA_POP(self->stack, clsraw);
5133 cls = (PyTypeObject *)clsraw;
5134 if (cls == NULL)
5135 goto error;
5136 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005137 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138 "isn't a type object");
5139 goto error;
5140 }
5141 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005142 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005143 "has NULL tp_new");
5144 goto error;
5145 }
5146
5147 /* Call __new__. */
5148 obj = cls->tp_new(cls, args, NULL);
5149 if (obj == NULL)
5150 goto error;
5151
5152 Py_DECREF(args);
5153 Py_DECREF(clsraw);
5154 PDATA_PUSH(self->stack, obj, -1);
5155 return 0;
5156
5157 error:
5158 Py_XDECREF(args);
5159 Py_XDECREF(clsraw);
5160 return -1;
5161}
5162
5163static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005164load_newobj_ex(UnpicklerObject *self)
5165{
5166 PyObject *cls, *args, *kwargs;
5167 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005168 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005169
5170 PDATA_POP(self->stack, kwargs);
5171 if (kwargs == NULL) {
5172 return -1;
5173 }
5174 PDATA_POP(self->stack, args);
5175 if (args == NULL) {
5176 Py_DECREF(kwargs);
5177 return -1;
5178 }
5179 PDATA_POP(self->stack, cls);
5180 if (cls == NULL) {
5181 Py_DECREF(kwargs);
5182 Py_DECREF(args);
5183 return -1;
5184 }
Larry Hastings61272b72014-01-07 12:41:53 -08005185
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005186 if (!PyType_Check(cls)) {
5187 Py_DECREF(kwargs);
5188 Py_DECREF(args);
5189 Py_DECREF(cls);
Larry Hastings61272b72014-01-07 12:41:53 -08005190 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005191 "NEWOBJ_EX class argument must be a type, not %.200s",
5192 Py_TYPE(cls)->tp_name);
5193 return -1;
5194 }
5195
5196 if (((PyTypeObject *)cls)->tp_new == NULL) {
5197 Py_DECREF(kwargs);
5198 Py_DECREF(args);
5199 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005200 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005201 "NEWOBJ_EX class argument doesn't have __new__");
5202 return -1;
5203 }
5204 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5205 Py_DECREF(kwargs);
5206 Py_DECREF(args);
5207 Py_DECREF(cls);
5208 if (obj == NULL) {
5209 return -1;
5210 }
5211 PDATA_PUSH(self->stack, obj, -1);
5212 return 0;
5213}
5214
5215static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216load_global(UnpicklerObject *self)
5217{
5218 PyObject *global = NULL;
5219 PyObject *module_name;
5220 PyObject *global_name;
5221 Py_ssize_t len;
5222 char *s;
5223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005224 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005225 return -1;
5226 if (len < 2)
5227 return bad_readline();
5228 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5229 if (!module_name)
5230 return -1;
5231
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005232 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005233 if (len < 2) {
5234 Py_DECREF(module_name);
5235 return bad_readline();
5236 }
5237 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5238 if (global_name) {
5239 global = find_class(self, module_name, global_name);
5240 Py_DECREF(global_name);
5241 }
5242 }
5243 Py_DECREF(module_name);
5244
5245 if (global == NULL)
5246 return -1;
5247 PDATA_PUSH(self->stack, global, -1);
5248 return 0;
5249}
5250
5251static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005252load_stack_global(UnpicklerObject *self)
5253{
5254 PyObject *global;
5255 PyObject *module_name;
5256 PyObject *global_name;
5257
5258 PDATA_POP(self->stack, global_name);
5259 PDATA_POP(self->stack, module_name);
5260 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5261 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005262 PickleState *st = _Pickle_GetGlobalState();
5263 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005264 Py_XDECREF(global_name);
5265 Py_XDECREF(module_name);
5266 return -1;
5267 }
5268 global = find_class(self, module_name, global_name);
5269 Py_DECREF(global_name);
5270 Py_DECREF(module_name);
5271 if (global == NULL)
5272 return -1;
5273 PDATA_PUSH(self->stack, global, -1);
5274 return 0;
5275}
5276
5277static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005278load_persid(UnpicklerObject *self)
5279{
5280 PyObject *pid;
5281 Py_ssize_t len;
5282 char *s;
5283
5284 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005285 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005286 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005287 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005288 return bad_readline();
5289
5290 pid = PyBytes_FromStringAndSize(s, len - 1);
5291 if (pid == NULL)
5292 return -1;
5293
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005294 /* This does not leak since _Pickle_FastCall() steals the reference
5295 to pid first. */
5296 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005297 if (pid == NULL)
5298 return -1;
5299
5300 PDATA_PUSH(self->stack, pid, -1);
5301 return 0;
5302 }
5303 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005304 PickleState *st = _Pickle_GetGlobalState();
5305 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005306 "A load persistent id instruction was encountered,\n"
5307 "but no persistent_load function was specified.");
5308 return -1;
5309 }
5310}
5311
5312static int
5313load_binpersid(UnpicklerObject *self)
5314{
5315 PyObject *pid;
5316
5317 if (self->pers_func) {
5318 PDATA_POP(self->stack, pid);
5319 if (pid == NULL)
5320 return -1;
5321
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005322 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005323 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005324 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005325 if (pid == NULL)
5326 return -1;
5327
5328 PDATA_PUSH(self->stack, pid, -1);
5329 return 0;
5330 }
5331 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005332 PickleState *st = _Pickle_GetGlobalState();
5333 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005334 "A load persistent id instruction was encountered,\n"
5335 "but no persistent_load function was specified.");
5336 return -1;
5337 }
5338}
5339
5340static int
5341load_pop(UnpicklerObject *self)
5342{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005343 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005344
5345 /* Note that we split the (pickle.py) stack into two stacks,
5346 * an object stack and a mark stack. We have to be clever and
5347 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005348 * mark stack first, and only signalling a stack underflow if
5349 * the object stack is empty and the mark stack doesn't match
5350 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005351 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005352 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005353 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005354 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005355 len--;
5356 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005357 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005358 } else {
5359 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005360 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005361 return 0;
5362}
5363
5364static int
5365load_pop_mark(UnpicklerObject *self)
5366{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005367 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005368
5369 if ((i = marker(self)) < 0)
5370 return -1;
5371
5372 Pdata_clear(self->stack, i);
5373
5374 return 0;
5375}
5376
5377static int
5378load_dup(UnpicklerObject *self)
5379{
5380 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005381 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005383 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384 return stack_underflow();
5385 last = self->stack->data[len - 1];
5386 PDATA_APPEND(self->stack, last, -1);
5387 return 0;
5388}
5389
5390static int
5391load_get(UnpicklerObject *self)
5392{
5393 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005394 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005395 Py_ssize_t len;
5396 char *s;
5397
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005398 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399 return -1;
5400 if (len < 2)
5401 return bad_readline();
5402
5403 key = PyLong_FromString(s, NULL, 10);
5404 if (key == NULL)
5405 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005406 idx = PyLong_AsSsize_t(key);
5407 if (idx == -1 && PyErr_Occurred()) {
5408 Py_DECREF(key);
5409 return -1;
5410 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005411
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005412 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413 if (value == NULL) {
5414 if (!PyErr_Occurred())
5415 PyErr_SetObject(PyExc_KeyError, key);
5416 Py_DECREF(key);
5417 return -1;
5418 }
5419 Py_DECREF(key);
5420
5421 PDATA_APPEND(self->stack, value, -1);
5422 return 0;
5423}
5424
5425static int
5426load_binget(UnpicklerObject *self)
5427{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005428 PyObject *value;
5429 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005430 char *s;
5431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005432 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005433 return -1;
5434
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005435 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005437 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005438 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005439 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005440 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005442 Py_DECREF(key);
5443 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444 return -1;
5445 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446
5447 PDATA_APPEND(self->stack, value, -1);
5448 return 0;
5449}
5450
5451static int
5452load_long_binget(UnpicklerObject *self)
5453{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005454 PyObject *value;
5455 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005458 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 return -1;
5460
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005461 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005462
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005463 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005464 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005465 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005466 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005468 Py_DECREF(key);
5469 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005470 return -1;
5471 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005472
5473 PDATA_APPEND(self->stack, value, -1);
5474 return 0;
5475}
5476
5477/* Push an object from the extension registry (EXT[124]). nbytes is
5478 * the number of bytes following the opcode, holding the index (code) value.
5479 */
5480static int
5481load_extension(UnpicklerObject *self, int nbytes)
5482{
5483 char *codebytes; /* the nbytes bytes after the opcode */
5484 long code; /* calc_binint returns long */
5485 PyObject *py_code; /* code as a Python int */
5486 PyObject *obj; /* the object to push */
5487 PyObject *pair; /* (module_name, class_name) */
5488 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005489 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490
5491 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005492 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493 return -1;
5494 code = calc_binint(codebytes, nbytes);
5495 if (code <= 0) { /* note that 0 is forbidden */
5496 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005497 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005498 return -1;
5499 }
5500
5501 /* Look for the code in the cache. */
5502 py_code = PyLong_FromLong(code);
5503 if (py_code == NULL)
5504 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005505 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506 if (obj != NULL) {
5507 /* Bingo. */
5508 Py_DECREF(py_code);
5509 PDATA_APPEND(self->stack, obj, -1);
5510 return 0;
5511 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005512 if (PyErr_Occurred()) {
5513 Py_DECREF(py_code);
5514 return -1;
5515 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005516
5517 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005518 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519 if (pair == NULL) {
5520 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005521 if (!PyErr_Occurred()) {
5522 PyErr_Format(PyExc_ValueError, "unregistered extension "
5523 "code %ld", code);
5524 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525 return -1;
5526 }
5527 /* Since the extension registry is manipulable via Python code,
5528 * confirm that pair is really a 2-tuple of strings.
5529 */
5530 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5531 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5532 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5533 Py_DECREF(py_code);
5534 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5535 "isn't a 2-tuple of strings", code);
5536 return -1;
5537 }
5538 /* Load the object. */
5539 obj = find_class(self, module_name, class_name);
5540 if (obj == NULL) {
5541 Py_DECREF(py_code);
5542 return -1;
5543 }
5544 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005545 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005546 Py_DECREF(py_code);
5547 if (code < 0) {
5548 Py_DECREF(obj);
5549 return -1;
5550 }
5551 PDATA_PUSH(self->stack, obj, -1);
5552 return 0;
5553}
5554
5555static int
5556load_put(UnpicklerObject *self)
5557{
5558 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005559 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 Py_ssize_t len;
5561 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005563 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564 return -1;
5565 if (len < 2)
5566 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005567 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005569 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570
5571 key = PyLong_FromString(s, NULL, 10);
5572 if (key == NULL)
5573 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005574 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005575 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005576 if (idx < 0) {
5577 if (!PyErr_Occurred())
5578 PyErr_SetString(PyExc_ValueError,
5579 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005581 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005582
5583 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005584}
5585
5586static int
5587load_binput(UnpicklerObject *self)
5588{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005589 PyObject *value;
5590 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005593 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005594 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005595
5596 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005597 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603}
5604
5605static int
5606load_long_binput(UnpicklerObject *self)
5607{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 PyObject *value;
5609 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005612 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005614
5615 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005617 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005619 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005620 if (idx < 0) {
5621 PyErr_SetString(PyExc_ValueError,
5622 "negative LONG_BINPUT argument");
5623 return -1;
5624 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627}
5628
5629static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005630load_memoize(UnpicklerObject *self)
5631{
5632 PyObject *value;
5633
5634 if (Py_SIZE(self->stack) <= 0)
5635 return stack_underflow();
5636 value = self->stack->data[Py_SIZE(self->stack) - 1];
5637
5638 return _Unpickler_MemoPut(self, self->memo_len, value);
5639}
5640
5641static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005642do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643{
5644 PyObject *value;
5645 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005646 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005648 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649 if (x > len || x <= 0)
5650 return stack_underflow();
5651 if (len == x) /* nothing to do */
5652 return 0;
5653
5654 list = self->stack->data[x - 1];
5655
5656 if (PyList_Check(list)) {
5657 PyObject *slice;
5658 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005659 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660
5661 slice = Pdata_poplist(self->stack, x);
5662 if (!slice)
5663 return -1;
5664 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005665 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005667 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668 }
5669 else {
5670 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005671 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005673 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674 if (append_func == NULL)
5675 return -1;
5676 for (i = x; i < len; i++) {
5677 PyObject *result;
5678
5679 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005680 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 if (result == NULL) {
5682 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005683 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005684 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685 return -1;
5686 }
5687 Py_DECREF(result);
5688 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005689 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005690 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 }
5692
5693 return 0;
5694}
5695
5696static int
5697load_append(UnpicklerObject *self)
5698{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005699 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005700}
5701
5702static int
5703load_appends(UnpicklerObject *self)
5704{
5705 return do_append(self, marker(self));
5706}
5707
5708static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005709do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005710{
5711 PyObject *value, *key;
5712 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005713 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714 int status = 0;
5715
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005716 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717 if (x > len || x <= 0)
5718 return stack_underflow();
5719 if (len == x) /* nothing to do */
5720 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005721 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005722 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005723 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005724 PyErr_SetString(st->UnpicklingError,
5725 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005726 return -1;
5727 }
5728
5729 /* Here, dict does not actually need to be a PyDict; it could be anything
5730 that supports the __setitem__ attribute. */
5731 dict = self->stack->data[x - 1];
5732
5733 for (i = x + 1; i < len; i += 2) {
5734 key = self->stack->data[i - 1];
5735 value = self->stack->data[i];
5736 if (PyObject_SetItem(dict, key, value) < 0) {
5737 status = -1;
5738 break;
5739 }
5740 }
5741
5742 Pdata_clear(self->stack, x);
5743 return status;
5744}
5745
5746static int
5747load_setitem(UnpicklerObject *self)
5748{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005749 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750}
5751
5752static int
5753load_setitems(UnpicklerObject *self)
5754{
5755 return do_setitems(self, marker(self));
5756}
5757
5758static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005759load_additems(UnpicklerObject *self)
5760{
5761 PyObject *set;
5762 Py_ssize_t mark, len, i;
5763
5764 mark = marker(self);
5765 len = Py_SIZE(self->stack);
5766 if (mark > len || mark <= 0)
5767 return stack_underflow();
5768 if (len == mark) /* nothing to do */
5769 return 0;
5770
5771 set = self->stack->data[mark - 1];
5772
5773 if (PySet_Check(set)) {
5774 PyObject *items;
5775 int status;
5776
5777 items = Pdata_poptuple(self->stack, mark);
5778 if (items == NULL)
5779 return -1;
5780
5781 status = _PySet_Update(set, items);
5782 Py_DECREF(items);
5783 return status;
5784 }
5785 else {
5786 PyObject *add_func;
5787 _Py_IDENTIFIER(add);
5788
5789 add_func = _PyObject_GetAttrId(set, &PyId_add);
5790 if (add_func == NULL)
5791 return -1;
5792 for (i = mark; i < len; i++) {
5793 PyObject *result;
5794 PyObject *item;
5795
5796 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005797 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005798 if (result == NULL) {
5799 Pdata_clear(self->stack, i + 1);
5800 Py_SIZE(self->stack) = mark;
5801 return -1;
5802 }
5803 Py_DECREF(result);
5804 }
5805 Py_SIZE(self->stack) = mark;
5806 }
5807
5808 return 0;
5809}
5810
5811static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005812load_build(UnpicklerObject *self)
5813{
5814 PyObject *state, *inst, *slotstate;
5815 PyObject *setstate;
5816 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005817 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005818
5819 /* Stack is ... instance, state. We want to leave instance at
5820 * the stack top, possibly mutated via instance.__setstate__(state).
5821 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005822 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823 return stack_underflow();
5824
5825 PDATA_POP(self->stack, state);
5826 if (state == NULL)
5827 return -1;
5828
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005829 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005830
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005831 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005832 if (setstate == NULL) {
5833 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5834 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005835 else {
5836 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005837 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005838 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 }
5840 else {
5841 PyObject *result;
5842
5843 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005844 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845 Py_DECREF(setstate);
5846 if (result == NULL)
5847 return -1;
5848 Py_DECREF(result);
5849 return 0;
5850 }
5851
5852 /* A default __setstate__. First see whether state embeds a
5853 * slot state dict too (a proto 2 addition).
5854 */
5855 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5856 PyObject *tmp = state;
5857
5858 state = PyTuple_GET_ITEM(tmp, 0);
5859 slotstate = PyTuple_GET_ITEM(tmp, 1);
5860 Py_INCREF(state);
5861 Py_INCREF(slotstate);
5862 Py_DECREF(tmp);
5863 }
5864 else
5865 slotstate = NULL;
5866
5867 /* Set inst.__dict__ from the state dict (if any). */
5868 if (state != Py_None) {
5869 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005870 PyObject *d_key, *d_value;
5871 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005872 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873
5874 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005875 PickleState *st = _Pickle_GetGlobalState();
5876 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 goto error;
5878 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005879 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005880 if (dict == NULL)
5881 goto error;
5882
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005883 i = 0;
5884 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5885 /* normally the keys for instance attributes are
5886 interned. we should try to do that here. */
5887 Py_INCREF(d_key);
5888 if (PyUnicode_CheckExact(d_key))
5889 PyUnicode_InternInPlace(&d_key);
5890 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5891 Py_DECREF(d_key);
5892 goto error;
5893 }
5894 Py_DECREF(d_key);
5895 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 Py_DECREF(dict);
5897 }
5898
5899 /* Also set instance attributes from the slotstate dict (if any). */
5900 if (slotstate != NULL) {
5901 PyObject *d_key, *d_value;
5902 Py_ssize_t i;
5903
5904 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005905 PickleState *st = _Pickle_GetGlobalState();
5906 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005907 "slot state is not a dictionary");
5908 goto error;
5909 }
5910 i = 0;
5911 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5912 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5913 goto error;
5914 }
5915 }
5916
5917 if (0) {
5918 error:
5919 status = -1;
5920 }
5921
5922 Py_DECREF(state);
5923 Py_XDECREF(slotstate);
5924 return status;
5925}
5926
5927static int
5928load_mark(UnpicklerObject *self)
5929{
5930
5931 /* Note that we split the (pickle.py) stack into two stacks, an
5932 * object stack and a mark stack. Here we push a mark onto the
5933 * mark stack.
5934 */
5935
5936 if ((self->num_marks + 1) >= self->marks_size) {
5937 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005938 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005939
5940 /* Use the size_t type to check for overflow. */
5941 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005942 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00005943 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005944 PyErr_NoMemory();
5945 return -1;
5946 }
5947
5948 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005949 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005950 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005951 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
5952 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005953 if (marks == NULL) {
5954 PyErr_NoMemory();
5955 return -1;
5956 }
5957 self->marks = marks;
5958 self->marks_size = (Py_ssize_t)alloc;
5959 }
5960
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005961 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005962
5963 return 0;
5964}
5965
5966static int
5967load_reduce(UnpicklerObject *self)
5968{
5969 PyObject *callable = NULL;
5970 PyObject *argtup = NULL;
5971 PyObject *obj = NULL;
5972
5973 PDATA_POP(self->stack, argtup);
5974 if (argtup == NULL)
5975 return -1;
5976 PDATA_POP(self->stack, callable);
5977 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005978 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005979 Py_DECREF(callable);
5980 }
5981 Py_DECREF(argtup);
5982
5983 if (obj == NULL)
5984 return -1;
5985
5986 PDATA_PUSH(self->stack, obj, -1);
5987 return 0;
5988}
5989
5990/* Just raises an error if we don't know the protocol specified. PROTO
5991 * is the first opcode for protocols >= 2.
5992 */
5993static int
5994load_proto(UnpicklerObject *self)
5995{
5996 char *s;
5997 int i;
5998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005999 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000 return -1;
6001
6002 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006003 if (i <= HIGHEST_PROTOCOL) {
6004 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006005 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006006 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006007
6008 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6009 return -1;
6010}
6011
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006012static int
6013load_frame(UnpicklerObject *self)
6014{
6015 char *s;
6016 Py_ssize_t frame_len;
6017
6018 if (_Unpickler_Read(self, &s, 8) < 0)
6019 return -1;
6020
6021 frame_len = calc_binsize(s, 8);
6022 if (frame_len < 0) {
6023 PyErr_Format(PyExc_OverflowError,
6024 "FRAME length exceeds system's maximum of %zd bytes",
6025 PY_SSIZE_T_MAX);
6026 return -1;
6027 }
6028
6029 if (_Unpickler_Read(self, &s, frame_len) < 0)
6030 return -1;
6031
6032 /* Rewind to start of frame */
6033 self->next_read_idx -= frame_len;
6034 return 0;
6035}
6036
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006037static PyObject *
6038load(UnpicklerObject *self)
6039{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006040 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006041 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006042
6043 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006044 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006045 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046 Pdata_clear(self->stack, 0);
6047
6048 /* Convenient macros for the dispatch while-switch loop just below. */
6049#define OP(opcode, load_func) \
6050 case opcode: if (load_func(self) < 0) break; continue;
6051
6052#define OP_ARG(opcode, load_func, arg) \
6053 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6054
6055 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006056 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006057 break;
6058
6059 switch ((enum opcode)s[0]) {
6060 OP(NONE, load_none)
6061 OP(BININT, load_binint)
6062 OP(BININT1, load_binint1)
6063 OP(BININT2, load_binint2)
6064 OP(INT, load_int)
6065 OP(LONG, load_long)
6066 OP_ARG(LONG1, load_counted_long, 1)
6067 OP_ARG(LONG4, load_counted_long, 4)
6068 OP(FLOAT, load_float)
6069 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006070 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6071 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6072 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6073 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6074 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 OP(STRING, load_string)
6076 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006077 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6078 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6079 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6081 OP_ARG(TUPLE1, load_counted_tuple, 1)
6082 OP_ARG(TUPLE2, load_counted_tuple, 2)
6083 OP_ARG(TUPLE3, load_counted_tuple, 3)
6084 OP(TUPLE, load_tuple)
6085 OP(EMPTY_LIST, load_empty_list)
6086 OP(LIST, load_list)
6087 OP(EMPTY_DICT, load_empty_dict)
6088 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006089 OP(EMPTY_SET, load_empty_set)
6090 OP(ADDITEMS, load_additems)
6091 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006092 OP(OBJ, load_obj)
6093 OP(INST, load_inst)
6094 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006095 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006097 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098 OP(APPEND, load_append)
6099 OP(APPENDS, load_appends)
6100 OP(BUILD, load_build)
6101 OP(DUP, load_dup)
6102 OP(BINGET, load_binget)
6103 OP(LONG_BINGET, load_long_binget)
6104 OP(GET, load_get)
6105 OP(MARK, load_mark)
6106 OP(BINPUT, load_binput)
6107 OP(LONG_BINPUT, load_long_binput)
6108 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006109 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006110 OP(POP, load_pop)
6111 OP(POP_MARK, load_pop_mark)
6112 OP(SETITEM, load_setitem)
6113 OP(SETITEMS, load_setitems)
6114 OP(PERSID, load_persid)
6115 OP(BINPERSID, load_binpersid)
6116 OP(REDUCE, load_reduce)
6117 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006118 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119 OP_ARG(EXT1, load_extension, 1)
6120 OP_ARG(EXT2, load_extension, 2)
6121 OP_ARG(EXT4, load_extension, 4)
6122 OP_ARG(NEWTRUE, load_bool, Py_True)
6123 OP_ARG(NEWFALSE, load_bool, Py_False)
6124
6125 case STOP:
6126 break;
6127
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006129 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006130 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006131 }
6132 else {
6133 PickleState *st = _Pickle_GetGlobalState();
6134 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006135 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006136 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 return NULL;
6138 }
6139
6140 break; /* and we are done! */
6141 }
6142
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006143 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006144 return NULL;
6145 }
6146
Victor Stinner2ae57e32013-10-31 13:39:23 +01006147 if (_Unpickler_SkipConsumed(self) < 0)
6148 return NULL;
6149
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006150 PDATA_POP(self->stack, value);
6151 return value;
6152}
6153
Larry Hastings61272b72014-01-07 12:41:53 -08006154/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006155
6156_pickle.Unpickler.load
6157
6158Load a pickle.
6159
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006160Read a pickled object representation from the open file object given
6161in the constructor, and return the reconstituted object hierarchy
6162specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006163[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006164
Larry Hastings3cceb382014-01-04 11:09:09 -08006165static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006166_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006167/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006168{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006169 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006170
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 /* Check whether the Unpickler was initialized correctly. This prevents
6172 segfaulting if a subclass overridden __init__ with a function that does
6173 not call Unpickler.__init__(). Here, we simply ensure that self->read
6174 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006175 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006176 PickleState *st = _Pickle_GetGlobalState();
6177 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006178 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006179 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006180 return NULL;
6181 }
6182
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006183 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006184}
6185
6186/* The name of find_class() is misleading. In newer pickle protocols, this
6187 function is used for loading any global (i.e., functions), not just
6188 classes. The name is kept only for backward compatibility. */
6189
Larry Hastings61272b72014-01-07 12:41:53 -08006190/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006191
6192_pickle.Unpickler.find_class
6193
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006194 module_name: object
6195 global_name: object
6196 /
6197
6198Return an object from a specified module.
6199
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006200If necessary, the module will be imported. Subclasses may override
6201this method (e.g. to restrict unpickling of arbitrary classes and
6202functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006203
6204This method is called whenever a class or a function object is
6205needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006206[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006207
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006208static PyObject *
6209_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006210/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211{
6212 PyObject *global;
6213 PyObject *modules_dict;
6214 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006215 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006217 /* Try to map the old names used in Python 2.x to the new ones used in
6218 Python 3.x. We do this only with old pickle protocols and when the
6219 user has not disabled the feature. */
6220 if (self->proto < 3 && self->fix_imports) {
6221 PyObject *key;
6222 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006223 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006224
6225 /* Check if the global (i.e., a function or a class) was renamed
6226 or moved to another module. */
6227 key = PyTuple_Pack(2, module_name, global_name);
6228 if (key == NULL)
6229 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006230 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006231 Py_DECREF(key);
6232 if (item) {
6233 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6234 PyErr_Format(PyExc_RuntimeError,
6235 "_compat_pickle.NAME_MAPPING values should be "
6236 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6237 return NULL;
6238 }
6239 module_name = PyTuple_GET_ITEM(item, 0);
6240 global_name = PyTuple_GET_ITEM(item, 1);
6241 if (!PyUnicode_Check(module_name) ||
6242 !PyUnicode_Check(global_name)) {
6243 PyErr_Format(PyExc_RuntimeError,
6244 "_compat_pickle.NAME_MAPPING values should be "
6245 "pairs of str, not (%.200s, %.200s)",
6246 Py_TYPE(module_name)->tp_name,
6247 Py_TYPE(global_name)->tp_name);
6248 return NULL;
6249 }
6250 }
6251 else if (PyErr_Occurred()) {
6252 return NULL;
6253 }
6254
6255 /* Check if the module was renamed. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006256 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006257 if (item) {
6258 if (!PyUnicode_Check(item)) {
6259 PyErr_Format(PyExc_RuntimeError,
6260 "_compat_pickle.IMPORT_MAPPING values should be "
6261 "strings, not %.200s", Py_TYPE(item)->tp_name);
6262 return NULL;
6263 }
6264 module_name = item;
6265 }
6266 else if (PyErr_Occurred()) {
6267 return NULL;
6268 }
6269 }
6270
Victor Stinnerbb520202013-11-06 22:40:41 +01006271 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006272 if (modules_dict == NULL) {
6273 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006274 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006275 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006276
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006277 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006278 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006279 if (PyErr_Occurred())
6280 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 module = PyImport_Import(module_name);
6282 if (module == NULL)
6283 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006284 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285 Py_DECREF(module);
6286 }
Victor Stinner121aab42011-09-29 23:40:53 +02006287 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006288 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 }
6290 return global;
6291}
6292
6293static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006294 _PICKLE_UNPICKLER_LOAD_METHODDEF
6295 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006296 {NULL, NULL} /* sentinel */
6297};
6298
6299static void
6300Unpickler_dealloc(UnpicklerObject *self)
6301{
6302 PyObject_GC_UnTrack((PyObject *)self);
6303 Py_XDECREF(self->readline);
6304 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006305 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006306 Py_XDECREF(self->stack);
6307 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006308 if (self->buffer.buf != NULL) {
6309 PyBuffer_Release(&self->buffer);
6310 self->buffer.buf = NULL;
6311 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006313 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006315 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006316 PyMem_Free(self->encoding);
6317 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006318
6319 Py_TYPE(self)->tp_free((PyObject *)self);
6320}
6321
6322static int
6323Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6324{
6325 Py_VISIT(self->readline);
6326 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006327 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006328 Py_VISIT(self->stack);
6329 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006330 return 0;
6331}
6332
6333static int
6334Unpickler_clear(UnpicklerObject *self)
6335{
6336 Py_CLEAR(self->readline);
6337 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006338 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006339 Py_CLEAR(self->stack);
6340 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006341 if (self->buffer.buf != NULL) {
6342 PyBuffer_Release(&self->buffer);
6343 self->buffer.buf = NULL;
6344 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006345
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006346 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 PyMem_Free(self->marks);
6348 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006349 PyMem_Free(self->input_line);
6350 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006351 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006352 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006353 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006354 self->errors = NULL;
6355
6356 return 0;
6357}
6358
Larry Hastings61272b72014-01-07 12:41:53 -08006359/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006360
6361_pickle.Unpickler.__init__
6362
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006363 file: object
6364 *
6365 fix_imports: bool = True
6366 encoding: str = 'ASCII'
6367 errors: str = 'strict'
6368
6369This takes a binary file for reading a pickle data stream.
6370
6371The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006372protocol argument is needed. Bytes past the pickled object's
6373representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006374
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006375The argument *file* must have two methods, a read() method that takes
6376an integer argument, and a readline() method that requires no
6377arguments. Both methods should return bytes. Thus *file* can be a
6378binary file object opened for reading, a io.BytesIO object, or any
6379other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006380
6381Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6382which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006383generated by Python 2. If *fix_imports* is True, pickle will try to
6384map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006385*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006386instances pickled by Python 2; these default to 'ASCII' and 'strict',
6387respectively. The *encoding* can be 'bytes' to read these 8-bit
6388string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006389[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006390
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006391static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006392_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006393/*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006394{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006395 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006396
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397 /* In case of multiple __init__() calls, clear previous content. */
6398 if (self->read != NULL)
6399 (void)Unpickler_clear(self);
6400
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006401 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006402 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006403
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006404 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006405 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006406
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006407 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006408 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006409 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006410
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006411 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006412 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6413 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006414 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006415 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006416 }
6417 else {
6418 self->pers_func = NULL;
6419 }
6420
6421 self->stack = (Pdata *)Pdata_New();
6422 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006423 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006424
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006425 self->memo_size = 32;
6426 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006427 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006428 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006429
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006430 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006431
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006432 return 0;
6433}
6434
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006436/* Define a proxy object for the Unpickler's internal memo object. This is to
6437 * avoid breaking code like:
6438 * unpickler.memo.clear()
6439 * and
6440 * unpickler.memo = saved_memo
6441 * Is this a good idea? Not really, but we don't want to break code that uses
6442 * it. Note that we don't implement the entire mapping API here. This is
6443 * intentional, as these should be treated as black-box implementation details.
6444 *
6445 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006446 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006447 */
6448
Larry Hastings61272b72014-01-07 12:41:53 -08006449/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006450_pickle.UnpicklerMemoProxy.clear
6451
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006452Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006453[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006454
Larry Hastings3cceb382014-01-04 11:09:09 -08006455static PyObject *
6456_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006457/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006458{
6459 _Unpickler_MemoCleanup(self->unpickler);
6460 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6461 if (self->unpickler->memo == NULL)
6462 return NULL;
6463 Py_RETURN_NONE;
6464}
6465
Larry Hastings61272b72014-01-07 12:41:53 -08006466/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006467_pickle.UnpicklerMemoProxy.copy
6468
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006469Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006470[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006471
Larry Hastings3cceb382014-01-04 11:09:09 -08006472static PyObject *
6473_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006474/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006475{
6476 Py_ssize_t i;
6477 PyObject *new_memo = PyDict_New();
6478 if (new_memo == NULL)
6479 return NULL;
6480
6481 for (i = 0; i < self->unpickler->memo_size; i++) {
6482 int status;
6483 PyObject *key, *value;
6484
6485 value = self->unpickler->memo[i];
6486 if (value == NULL)
6487 continue;
6488
6489 key = PyLong_FromSsize_t(i);
6490 if (key == NULL)
6491 goto error;
6492 status = PyDict_SetItem(new_memo, key, value);
6493 Py_DECREF(key);
6494 if (status < 0)
6495 goto error;
6496 }
6497 return new_memo;
6498
6499error:
6500 Py_DECREF(new_memo);
6501 return NULL;
6502}
6503
Larry Hastings61272b72014-01-07 12:41:53 -08006504/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006505_pickle.UnpicklerMemoProxy.__reduce__
6506
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006507Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006508[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006509
Larry Hastings3cceb382014-01-04 11:09:09 -08006510static PyObject *
6511_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006512/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006513{
6514 PyObject *reduce_value;
6515 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006516 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006517 if (contents == NULL)
6518 return NULL;
6519
6520 reduce_value = PyTuple_New(2);
6521 if (reduce_value == NULL) {
6522 Py_DECREF(contents);
6523 return NULL;
6524 }
6525 constructor_args = PyTuple_New(1);
6526 if (constructor_args == NULL) {
6527 Py_DECREF(contents);
6528 Py_DECREF(reduce_value);
6529 return NULL;
6530 }
6531 PyTuple_SET_ITEM(constructor_args, 0, contents);
6532 Py_INCREF((PyObject *)&PyDict_Type);
6533 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6534 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6535 return reduce_value;
6536}
6537
6538static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006539 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6540 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6541 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006542 {NULL, NULL} /* sentinel */
6543};
6544
6545static void
6546UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6547{
6548 PyObject_GC_UnTrack(self);
6549 Py_XDECREF(self->unpickler);
6550 PyObject_GC_Del((PyObject *)self);
6551}
6552
6553static int
6554UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6555 visitproc visit, void *arg)
6556{
6557 Py_VISIT(self->unpickler);
6558 return 0;
6559}
6560
6561static int
6562UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6563{
6564 Py_CLEAR(self->unpickler);
6565 return 0;
6566}
6567
6568static PyTypeObject UnpicklerMemoProxyType = {
6569 PyVarObject_HEAD_INIT(NULL, 0)
6570 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6571 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6572 0,
6573 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6574 0, /* tp_print */
6575 0, /* tp_getattr */
6576 0, /* tp_setattr */
6577 0, /* tp_compare */
6578 0, /* tp_repr */
6579 0, /* tp_as_number */
6580 0, /* tp_as_sequence */
6581 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006582 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583 0, /* tp_call */
6584 0, /* tp_str */
6585 PyObject_GenericGetAttr, /* tp_getattro */
6586 PyObject_GenericSetAttr, /* tp_setattro */
6587 0, /* tp_as_buffer */
6588 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6589 0, /* tp_doc */
6590 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6591 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6592 0, /* tp_richcompare */
6593 0, /* tp_weaklistoffset */
6594 0, /* tp_iter */
6595 0, /* tp_iternext */
6596 unpicklerproxy_methods, /* tp_methods */
6597};
6598
6599static PyObject *
6600UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6601{
6602 UnpicklerMemoProxyObject *self;
6603
6604 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6605 &UnpicklerMemoProxyType);
6606 if (self == NULL)
6607 return NULL;
6608 Py_INCREF(unpickler);
6609 self->unpickler = unpickler;
6610 PyObject_GC_Track(self);
6611 return (PyObject *)self;
6612}
6613
6614/*****************************************************************************/
6615
6616
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006617static PyObject *
6618Unpickler_get_memo(UnpicklerObject *self)
6619{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006620 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006621}
6622
6623static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006624Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006625{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006626 PyObject **new_memo;
6627 Py_ssize_t new_memo_size = 0;
6628 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006630 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006631 PyErr_SetString(PyExc_TypeError,
6632 "attribute deletion is not supported");
6633 return -1;
6634 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006635
6636 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6637 UnpicklerObject *unpickler =
6638 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6639
6640 new_memo_size = unpickler->memo_size;
6641 new_memo = _Unpickler_NewMemo(new_memo_size);
6642 if (new_memo == NULL)
6643 return -1;
6644
6645 for (i = 0; i < new_memo_size; i++) {
6646 Py_XINCREF(unpickler->memo[i]);
6647 new_memo[i] = unpickler->memo[i];
6648 }
6649 }
6650 else if (PyDict_Check(obj)) {
6651 Py_ssize_t i = 0;
6652 PyObject *key, *value;
6653
6654 new_memo_size = PyDict_Size(obj);
6655 new_memo = _Unpickler_NewMemo(new_memo_size);
6656 if (new_memo == NULL)
6657 return -1;
6658
6659 while (PyDict_Next(obj, &i, &key, &value)) {
6660 Py_ssize_t idx;
6661 if (!PyLong_Check(key)) {
6662 PyErr_SetString(PyExc_TypeError,
6663 "memo key must be integers");
6664 goto error;
6665 }
6666 idx = PyLong_AsSsize_t(key);
6667 if (idx == -1 && PyErr_Occurred())
6668 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006669 if (idx < 0) {
6670 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006671 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006672 goto error;
6673 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006674 if (_Unpickler_MemoPut(self, idx, value) < 0)
6675 goto error;
6676 }
6677 }
6678 else {
6679 PyErr_Format(PyExc_TypeError,
6680 "'memo' attribute must be an UnpicklerMemoProxy object"
6681 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006682 return -1;
6683 }
6684
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006685 _Unpickler_MemoCleanup(self);
6686 self->memo_size = new_memo_size;
6687 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006688
6689 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006690
6691 error:
6692 if (new_memo_size) {
6693 i = new_memo_size;
6694 while (--i >= 0) {
6695 Py_XDECREF(new_memo[i]);
6696 }
6697 PyMem_FREE(new_memo);
6698 }
6699 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006700}
6701
6702static PyObject *
6703Unpickler_get_persload(UnpicklerObject *self)
6704{
6705 if (self->pers_func == NULL)
6706 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6707 else
6708 Py_INCREF(self->pers_func);
6709 return self->pers_func;
6710}
6711
6712static int
6713Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6714{
6715 PyObject *tmp;
6716
6717 if (value == NULL) {
6718 PyErr_SetString(PyExc_TypeError,
6719 "attribute deletion is not supported");
6720 return -1;
6721 }
6722 if (!PyCallable_Check(value)) {
6723 PyErr_SetString(PyExc_TypeError,
6724 "persistent_load must be a callable taking "
6725 "one argument");
6726 return -1;
6727 }
6728
6729 tmp = self->pers_func;
6730 Py_INCREF(value);
6731 self->pers_func = value;
6732 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6733
6734 return 0;
6735}
6736
6737static PyGetSetDef Unpickler_getsets[] = {
6738 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6739 {"persistent_load", (getter)Unpickler_get_persload,
6740 (setter)Unpickler_set_persload},
6741 {NULL}
6742};
6743
6744static PyTypeObject Unpickler_Type = {
6745 PyVarObject_HEAD_INIT(NULL, 0)
6746 "_pickle.Unpickler", /*tp_name*/
6747 sizeof(UnpicklerObject), /*tp_basicsize*/
6748 0, /*tp_itemsize*/
6749 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6750 0, /*tp_print*/
6751 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006752 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006753 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006754 0, /*tp_repr*/
6755 0, /*tp_as_number*/
6756 0, /*tp_as_sequence*/
6757 0, /*tp_as_mapping*/
6758 0, /*tp_hash*/
6759 0, /*tp_call*/
6760 0, /*tp_str*/
6761 0, /*tp_getattro*/
6762 0, /*tp_setattro*/
6763 0, /*tp_as_buffer*/
6764 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006765 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006766 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6767 (inquiry)Unpickler_clear, /*tp_clear*/
6768 0, /*tp_richcompare*/
6769 0, /*tp_weaklistoffset*/
6770 0, /*tp_iter*/
6771 0, /*tp_iternext*/
6772 Unpickler_methods, /*tp_methods*/
6773 0, /*tp_members*/
6774 Unpickler_getsets, /*tp_getset*/
6775 0, /*tp_base*/
6776 0, /*tp_dict*/
6777 0, /*tp_descr_get*/
6778 0, /*tp_descr_set*/
6779 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006780 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006781 PyType_GenericAlloc, /*tp_alloc*/
6782 PyType_GenericNew, /*tp_new*/
6783 PyObject_GC_Del, /*tp_free*/
6784 0, /*tp_is_gc*/
6785};
6786
Larry Hastings61272b72014-01-07 12:41:53 -08006787/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006788
6789_pickle.dump
6790
6791 obj: object
6792 file: object
6793 protocol: object = NULL
6794 *
6795 fix_imports: bool = True
6796
6797Write a pickled representation of obj to the open file object file.
6798
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006799This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6800be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006801
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006802The optional *protocol* argument tells the pickler to use the given
6803protocol supported protocols are 0, 1, 2, 3 and 4. The default
6804protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006805
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006806Specifying a negative protocol version selects the highest protocol
6807version supported. The higher the protocol used, the more recent the
6808version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006809
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006810The *file* argument must have a write() method that accepts a single
6811bytes argument. It can thus be a file object opened for binary
6812writing, a io.BytesIO instance, or any other custom object that meets
6813this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006814
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006815If *fix_imports* is True and protocol is less than 3, pickle will try
6816to map the new Python 3 names to the old module names used in Python
68172, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006818[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006819
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006820static PyObject *
6821_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006822/*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006823{
6824 PicklerObject *pickler = _Pickler_New();
6825
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006826 if (pickler == NULL)
6827 return NULL;
6828
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006829 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006830 goto error;
6831
6832 if (_Pickler_SetOutputStream(pickler, file) < 0)
6833 goto error;
6834
6835 if (dump(pickler, obj) < 0)
6836 goto error;
6837
6838 if (_Pickler_FlushToFile(pickler) < 0)
6839 goto error;
6840
6841 Py_DECREF(pickler);
6842 Py_RETURN_NONE;
6843
6844 error:
6845 Py_XDECREF(pickler);
6846 return NULL;
6847}
6848
Larry Hastings61272b72014-01-07 12:41:53 -08006849/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006850
6851_pickle.dumps
6852
6853 obj: object
6854 protocol: object = NULL
6855 *
6856 fix_imports: bool = True
6857
6858Return the pickled representation of the object as a bytes object.
6859
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006860The optional *protocol* argument tells the pickler to use the given
6861protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6862protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006863
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006864Specifying a negative protocol version selects the highest protocol
6865version supported. The higher the protocol used, the more recent the
6866version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006867
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006868If *fix_imports* is True and *protocol* is less than 3, pickle will
6869try to map the new Python 3 names to the old module names used in
6870Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006871[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006872
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006873static PyObject *
6874_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006875/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006876{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006878 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006879
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006880 if (pickler == NULL)
6881 return NULL;
6882
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006883 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006884 goto error;
6885
6886 if (dump(pickler, obj) < 0)
6887 goto error;
6888
6889 result = _Pickler_GetString(pickler);
6890 Py_DECREF(pickler);
6891 return result;
6892
6893 error:
6894 Py_XDECREF(pickler);
6895 return NULL;
6896}
6897
Larry Hastings61272b72014-01-07 12:41:53 -08006898/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006899
6900_pickle.load
6901
6902 file: object
6903 *
6904 fix_imports: bool = True
6905 encoding: str = 'ASCII'
6906 errors: str = 'strict'
6907
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006908Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006909
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006910This is equivalent to ``Unpickler(file).load()``, but may be more
6911efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006912
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006913The protocol version of the pickle is detected automatically, so no
6914protocol argument is needed. Bytes past the pickled object's
6915representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006916
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006917The argument *file* must have two methods, a read() method that takes
6918an integer argument, and a readline() method that requires no
6919arguments. Both methods should return bytes. Thus *file* can be a
6920binary file object opened for reading, a io.BytesIO object, or any
6921other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006922
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006923Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6924which are used to control compatiblity support for pickle stream
6925generated by Python 2. If *fix_imports* is True, pickle will try to
6926map the old Python 2 names to the new names used in Python 3. The
6927*encoding* and *errors* tell pickle how to decode 8-bit string
6928instances pickled by Python 2; these default to 'ASCII' and 'strict',
6929respectively. The *encoding* can be 'bytes' to read these 8-bit
6930string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006931[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006932
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006933static PyObject *
6934_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006935/*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006936{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006937 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006938 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006939
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006940 if (unpickler == NULL)
6941 return NULL;
6942
6943 if (_Unpickler_SetInputStream(unpickler, file) < 0)
6944 goto error;
6945
6946 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
6947 goto error;
6948
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006949 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006950
6951 result = load(unpickler);
6952 Py_DECREF(unpickler);
6953 return result;
6954
6955 error:
6956 Py_XDECREF(unpickler);
6957 return NULL;
6958}
6959
Larry Hastings61272b72014-01-07 12:41:53 -08006960/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006961
6962_pickle.loads
6963
6964 data: object
6965 *
6966 fix_imports: bool = True
6967 encoding: str = 'ASCII'
6968 errors: str = 'strict'
6969
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006970Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006971
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006972The protocol version of the pickle is detected automatically, so no
6973protocol argument is needed. Bytes past the pickled object's
6974representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006975
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006976Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6977which are used to control compatiblity support for pickle stream
6978generated by Python 2. If *fix_imports* is True, pickle will try to
6979map the old Python 2 names to the new names used in Python 3. The
6980*encoding* and *errors* tell pickle how to decode 8-bit string
6981instances pickled by Python 2; these default to 'ASCII' and 'strict',
6982respectively. The *encoding* can be 'bytes' to read these 8-bit
6983string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006984[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006985
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006986static PyObject *
6987_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006988/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006989{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006990 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006991 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006993 if (unpickler == NULL)
6994 return NULL;
6995
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006996 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006997 goto error;
6998
6999 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7000 goto error;
7001
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007002 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003
7004 result = load(unpickler);
7005 Py_DECREF(unpickler);
7006 return result;
7007
7008 error:
7009 Py_XDECREF(unpickler);
7010 return NULL;
7011}
7012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007013static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007014 _PICKLE_DUMP_METHODDEF
7015 _PICKLE_DUMPS_METHODDEF
7016 _PICKLE_LOAD_METHODDEF
7017 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007018 {NULL, NULL} /* sentinel */
7019};
7020
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007021static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007022pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007023{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007024 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007025 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007026}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007027
Stefan Krahf483b0f2013-12-14 13:43:10 +01007028static void
7029pickle_free(PyObject *m)
7030{
7031 _Pickle_ClearState(_Pickle_GetState(m));
7032}
7033
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007034static int
7035pickle_traverse(PyObject *m, visitproc visit, void *arg)
7036{
7037 PickleState *st = _Pickle_GetState(m);
7038 Py_VISIT(st->PickleError);
7039 Py_VISIT(st->PicklingError);
7040 Py_VISIT(st->UnpicklingError);
7041 Py_VISIT(st->dispatch_table);
7042 Py_VISIT(st->extension_registry);
7043 Py_VISIT(st->extension_cache);
7044 Py_VISIT(st->inverted_registry);
7045 Py_VISIT(st->name_mapping_2to3);
7046 Py_VISIT(st->import_mapping_2to3);
7047 Py_VISIT(st->name_mapping_3to2);
7048 Py_VISIT(st->import_mapping_3to2);
7049 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007050 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007051}
7052
7053static struct PyModuleDef _picklemodule = {
7054 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007055 "_pickle", /* m_name */
7056 pickle_module_doc, /* m_doc */
7057 sizeof(PickleState), /* m_size */
7058 pickle_methods, /* m_methods */
7059 NULL, /* m_reload */
7060 pickle_traverse, /* m_traverse */
7061 pickle_clear, /* m_clear */
7062 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007063};
7064
7065PyMODINIT_FUNC
7066PyInit__pickle(void)
7067{
7068 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007069 PickleState *st;
7070
7071 m = PyState_FindModule(&_picklemodule);
7072 if (m) {
7073 Py_INCREF(m);
7074 return m;
7075 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007076
7077 if (PyType_Ready(&Unpickler_Type) < 0)
7078 return NULL;
7079 if (PyType_Ready(&Pickler_Type) < 0)
7080 return NULL;
7081 if (PyType_Ready(&Pdata_Type) < 0)
7082 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007083 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7084 return NULL;
7085 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7086 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007087
7088 /* Create the module and add the functions. */
7089 m = PyModule_Create(&_picklemodule);
7090 if (m == NULL)
7091 return NULL;
7092
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007093 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007094 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7095 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007096 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007097 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7098 return NULL;
7099
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007100 st = _Pickle_GetState(m);
7101
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007102 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007103 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7104 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007105 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007106 st->PicklingError = \
7107 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7108 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007109 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007110 st->UnpicklingError = \
7111 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7112 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007113 return NULL;
7114
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007115 Py_INCREF(st->PickleError);
7116 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007117 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007118 Py_INCREF(st->PicklingError);
7119 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007120 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007121 Py_INCREF(st->UnpicklingError);
7122 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007123 return NULL;
7124
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007125 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007126 return NULL;
7127
7128 return m;
7129}