blob: 6271c4b33a41b663128e341fae8b895b019f22f7 [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
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007/*[clinic]
8module _pickle
9class _pickle.Pickler
10class _pickle.PicklerMemoProxy
11class _pickle.Unpickler
12class _pickle.UnpicklerMemoProxy
13[clinic]*/
14/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
15
16/*[python]
17class PicklerObject_converter(self_converter):
18 type = "PicklerObject *"
19
20class PicklerMemoProxyObject_converter(self_converter):
21 type = "PicklerMemoProxyObject *"
22
23class UnpicklerObject_converter(self_converter):
24 type = "UnpicklerObject *"
25
26class UnpicklerMemoProxyObject_converter(self_converter):
27 type = "UnpicklerMemoProxyObject *"
28[python]*/
29/*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
30
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000031/* Bump this when new opcodes are added to the pickle protocol. */
32enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010033 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000034 DEFAULT_PROTOCOL = 3
35};
36
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000037/* Pickle opcodes. These must be kept updated with pickle.py.
38 Extensive docs are in pickletools.py. */
39enum opcode {
40 MARK = '(',
41 STOP = '.',
42 POP = '0',
43 POP_MARK = '1',
44 DUP = '2',
45 FLOAT = 'F',
46 INT = 'I',
47 BININT = 'J',
48 BININT1 = 'K',
49 LONG = 'L',
50 BININT2 = 'M',
51 NONE = 'N',
52 PERSID = 'P',
53 BINPERSID = 'Q',
54 REDUCE = 'R',
55 STRING = 'S',
56 BINSTRING = 'T',
57 SHORT_BINSTRING = 'U',
58 UNICODE = 'V',
59 BINUNICODE = 'X',
60 APPEND = 'a',
61 BUILD = 'b',
62 GLOBAL = 'c',
63 DICT = 'd',
64 EMPTY_DICT = '}',
65 APPENDS = 'e',
66 GET = 'g',
67 BINGET = 'h',
68 INST = 'i',
69 LONG_BINGET = 'j',
70 LIST = 'l',
71 EMPTY_LIST = ']',
72 OBJ = 'o',
73 PUT = 'p',
74 BINPUT = 'q',
75 LONG_BINPUT = 'r',
76 SETITEM = 's',
77 TUPLE = 't',
78 EMPTY_TUPLE = ')',
79 SETITEMS = 'u',
80 BINFLOAT = 'G',
81
82 /* Protocol 2. */
83 PROTO = '\x80',
84 NEWOBJ = '\x81',
85 EXT1 = '\x82',
86 EXT2 = '\x83',
87 EXT4 = '\x84',
88 TUPLE1 = '\x85',
89 TUPLE2 = '\x86',
90 TUPLE3 = '\x87',
91 NEWTRUE = '\x88',
92 NEWFALSE = '\x89',
93 LONG1 = '\x8a',
94 LONG4 = '\x8b',
95
96 /* Protocol 3 (Python 3.x) */
97 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010098 SHORT_BINBYTES = 'C',
99
100 /* Protocol 4 */
101 SHORT_BINUNICODE = '\x8c',
102 BINUNICODE8 = '\x8d',
103 BINBYTES8 = '\x8e',
104 EMPTY_SET = '\x8f',
105 ADDITEMS = '\x90',
106 FROZENSET = '\x91',
107 NEWOBJ_EX = '\x92',
108 STACK_GLOBAL = '\x93',
109 MEMOIZE = '\x94',
110 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000111};
112
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000113enum {
114 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
115 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
116 break if this gets out of synch with pickle.py, but it's unclear that would
117 help anything either. */
118 BATCHSIZE = 1000,
119
120 /* Nesting limit until Pickler, when running in "fast mode", starts
121 checking for self-referential data-structures. */
122 FAST_NESTING_LIMIT = 50,
123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000124 /* Initial size of the write buffer of Pickler. */
125 WRITE_BUF_SIZE = 4096,
126
Antoine Pitrou04248a82010-10-12 20:51:21 +0000127 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100128 PREFETCH = 8192 * 16,
129
130 FRAME_SIZE_TARGET = 64 * 1024,
131
132 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133};
134
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800135/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800137/* State of the pickle module, per PEP 3121. */
138typedef struct {
139 /* Exception classes for pickle. */
140 PyObject *PickleError;
141 PyObject *PicklingError;
142 PyObject *UnpicklingError;
143
144 /* copyreg.dispatch_table, {type_object: pickling_function} */
145 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000146
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800147 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000148
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800149 /* copyreg._extension_registry, {(module_name, function_name): code} */
150 PyObject *extension_registry;
151 /* copyreg._extension_cache, {code: object} */
152 PyObject *extension_cache;
153 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
154 PyObject *inverted_registry;
155
156 /* Import mappings for compatibility with Python 2.x */
157
158 /* _compat_pickle.NAME_MAPPING,
159 {(oldmodule, oldname): (newmodule, newname)} */
160 PyObject *name_mapping_2to3;
161 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
162 PyObject *import_mapping_2to3;
163 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
164 PyObject *name_mapping_3to2;
165 PyObject *import_mapping_3to2;
166
167 /* codecs.encode, used for saving bytes in older protocols */
168 PyObject *codecs_encode;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800169} PickleState;
170
171/* Forward declaration of the _pickle module definition. */
172static struct PyModuleDef _picklemodule;
173
174/* Given a module object, get its per-module state. */
175static PickleState *
176_Pickle_GetState(PyObject *module)
177{
178 return (PickleState *)PyModule_GetState(module);
179}
180
181/* Find the module instance imported in the currently running sub-interpreter
182 and get its state. */
183static PickleState *
184_Pickle_GetGlobalState(void)
185{
186 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
187}
188
189/* Clear the given pickle module state. */
190static void
191_Pickle_ClearState(PickleState *st)
192{
193 Py_CLEAR(st->PickleError);
194 Py_CLEAR(st->PicklingError);
195 Py_CLEAR(st->UnpicklingError);
196 Py_CLEAR(st->dispatch_table);
197 Py_CLEAR(st->extension_registry);
198 Py_CLEAR(st->extension_cache);
199 Py_CLEAR(st->inverted_registry);
200 Py_CLEAR(st->name_mapping_2to3);
201 Py_CLEAR(st->import_mapping_2to3);
202 Py_CLEAR(st->name_mapping_3to2);
203 Py_CLEAR(st->import_mapping_3to2);
204 Py_CLEAR(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800205}
206
207/* Initialize the given pickle module state. */
208static int
209_Pickle_InitState(PickleState *st)
210{
211 PyObject *copyreg = NULL;
212 PyObject *compat_pickle = NULL;
213 PyObject *codecs = NULL;
214
215 copyreg = PyImport_ImportModule("copyreg");
216 if (!copyreg)
217 goto error;
218 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
219 if (!st->dispatch_table)
220 goto error;
221 if (!PyDict_CheckExact(st->dispatch_table)) {
222 PyErr_Format(PyExc_RuntimeError,
223 "copyreg.dispatch_table should be a dict, not %.200s",
224 Py_TYPE(st->dispatch_table)->tp_name);
225 goto error;
226 }
227 st->extension_registry = \
228 PyObject_GetAttrString(copyreg, "_extension_registry");
229 if (!st->extension_registry)
230 goto error;
231 if (!PyDict_CheckExact(st->extension_registry)) {
232 PyErr_Format(PyExc_RuntimeError,
233 "copyreg._extension_registry should be a dict, "
234 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
235 goto error;
236 }
237 st->inverted_registry = \
238 PyObject_GetAttrString(copyreg, "_inverted_registry");
239 if (!st->inverted_registry)
240 goto error;
241 if (!PyDict_CheckExact(st->inverted_registry)) {
242 PyErr_Format(PyExc_RuntimeError,
243 "copyreg._inverted_registry should be a dict, "
244 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
245 goto error;
246 }
247 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
248 if (!st->extension_cache)
249 goto error;
250 if (!PyDict_CheckExact(st->extension_cache)) {
251 PyErr_Format(PyExc_RuntimeError,
252 "copyreg._extension_cache should be a dict, "
253 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
254 goto error;
255 }
256 Py_CLEAR(copyreg);
257
258 /* Load the 2.x -> 3.x stdlib module mapping tables */
259 compat_pickle = PyImport_ImportModule("_compat_pickle");
260 if (!compat_pickle)
261 goto error;
262 st->name_mapping_2to3 = \
263 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
264 if (!st->name_mapping_2to3)
265 goto error;
266 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
267 PyErr_Format(PyExc_RuntimeError,
268 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
269 Py_TYPE(st->name_mapping_2to3)->tp_name);
270 goto error;
271 }
272 st->import_mapping_2to3 = \
273 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
274 if (!st->import_mapping_2to3)
275 goto error;
276 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
277 PyErr_Format(PyExc_RuntimeError,
278 "_compat_pickle.IMPORT_MAPPING should be a dict, "
279 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
280 goto error;
281 }
282 /* ... and the 3.x -> 2.x mapping tables */
283 st->name_mapping_3to2 = \
284 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
285 if (!st->name_mapping_3to2)
286 goto error;
287 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
288 PyErr_Format(PyExc_RuntimeError,
289 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
290 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
291 goto error;
292 }
293 st->import_mapping_3to2 = \
294 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
295 if (!st->import_mapping_3to2)
296 goto error;
297 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
298 PyErr_Format(PyExc_RuntimeError,
299 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
300 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
301 goto error;
302 }
303 Py_CLEAR(compat_pickle);
304
305 codecs = PyImport_ImportModule("codecs");
306 if (codecs == NULL)
307 goto error;
308 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
309 if (st->codecs_encode == NULL) {
310 goto error;
311 }
312 if (!PyCallable_Check(st->codecs_encode)) {
313 PyErr_Format(PyExc_RuntimeError,
314 "codecs.encode should be a callable, not %.200s",
315 Py_TYPE(st->codecs_encode)->tp_name);
316 goto error;
317 }
318 Py_CLEAR(codecs);
319
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800320 return 0;
321
322 error:
323 Py_CLEAR(copyreg);
324 Py_CLEAR(compat_pickle);
325 Py_CLEAR(codecs);
326 _Pickle_ClearState(st);
327 return -1;
328}
329
330/* Helper for calling a function with a single argument quickly.
331
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800332 This function steals the reference of the given argument. */
333static PyObject *
334_Pickle_FastCall(PyObject *func, PyObject *obj)
335{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800336 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800337 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800338
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800339 /* Note: this function used to reuse the argument tuple. This used to give
340 a slight performance boost with older pickle implementations where many
341 unbuffered reads occurred (thus needing many function calls).
342
343 However, this optimization was removed because it was too complicated
344 to get right. It abused the C API for tuples to mutate them which led
345 to subtle reference counting and concurrency bugs. Furthermore, the
346 introduction of protocol 4 and the prefetching optimization via peek()
347 significantly reduced the number of function calls we do. Thus, the
348 benefits became marginal at best. */
349
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800351 Py_DECREF(obj);
352 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800353 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 PyTuple_SET_ITEM(arg_tuple, 0, obj);
355 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800356 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800357 return result;
358}
359
360/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000361
362static int
363stack_underflow(void)
364{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PickleState *st = _Pickle_GetGlobalState();
366 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000367 return -1;
368}
369
370/* Internal data type used as the unpickling stack. */
371typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000372 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000374 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000375} Pdata;
376
377static void
378Pdata_dealloc(Pdata *self)
379{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200380 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000381 while (--i >= 0) {
382 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000383 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000384 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000385 PyObject_Del(self);
386}
387
388static PyTypeObject Pdata_Type = {
389 PyVarObject_HEAD_INIT(NULL, 0)
390 "_pickle.Pdata", /*tp_name*/
391 sizeof(Pdata), /*tp_basicsize*/
392 0, /*tp_itemsize*/
393 (destructor)Pdata_dealloc, /*tp_dealloc*/
394};
395
396static PyObject *
397Pdata_New(void)
398{
399 Pdata *self;
400
401 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
402 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000403 Py_SIZE(self) = 0;
404 self->allocated = 8;
405 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000406 if (self->data)
407 return (PyObject *)self;
408 Py_DECREF(self);
409 return PyErr_NoMemory();
410}
411
412
413/* Retain only the initial clearto items. If clearto >= the current
414 * number of items, this is a (non-erroneous) NOP.
415 */
416static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200417Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000418{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200419 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000420
421 if (clearto < 0)
422 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000424 return 0;
425
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 while (--i >= clearto) {
427 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431}
432
433static int
434Pdata_grow(Pdata *self)
435{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 PyObject **data = self->data;
437 Py_ssize_t allocated = self->allocated;
438 Py_ssize_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 new_allocated = (allocated >> 3) + 6;
441 /* check for integer overflow */
442 if (new_allocated > PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444 new_allocated += allocated;
445 if (new_allocated > (PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000447 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
448 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450
451 self->data = data;
452 self->allocated = new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 return 0;
454
455 nomemory:
456 PyErr_NoMemory();
457 return -1;
458}
459
460/* D is a Pdata*. Pop the topmost element and store it into V, which
461 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
462 * is raised and V is set to NULL.
463 */
464static PyObject *
465Pdata_pop(Pdata *self)
466{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800467 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000468 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800469 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000470 return NULL;
471 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000472 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000473}
474#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
475
476static int
477Pdata_push(Pdata *self, PyObject *obj)
478{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return -1;
481 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000482 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000483 return 0;
484}
485
486/* Push an object on stack, transferring its ownership to the stack. */
487#define PDATA_PUSH(D, O, ER) do { \
488 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
489
490/* Push an object on stack, adding a new reference to the object. */
491#define PDATA_APPEND(D, O, ER) do { \
492 Py_INCREF((O)); \
493 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
494
495static PyObject *
496Pdata_poptuple(Pdata *self, Py_ssize_t start)
497{
498 PyObject *tuple;
499 Py_ssize_t len, i, j;
500
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000501 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000502 tuple = PyTuple_New(len);
503 if (tuple == NULL)
504 return NULL;
505 for (i = start, j = 0; j < len; i++, j++)
506 PyTuple_SET_ITEM(tuple, j, self->data[i]);
507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000508 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000509 return tuple;
510}
511
512static PyObject *
513Pdata_poplist(Pdata *self, Py_ssize_t start)
514{
515 PyObject *list;
516 Py_ssize_t len, i, j;
517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000518 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000519 list = PyList_New(len);
520 if (list == NULL)
521 return NULL;
522 for (i = start, j = 0; j < len; i++, j++)
523 PyList_SET_ITEM(list, j, self->data[i]);
524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000525 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000526 return list;
527}
528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529typedef struct {
530 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200531 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000532} PyMemoEntry;
533
534typedef struct {
535 Py_ssize_t mt_mask;
536 Py_ssize_t mt_used;
537 Py_ssize_t mt_allocated;
538 PyMemoEntry *mt_table;
539} PyMemoTable;
540
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000541typedef struct PicklerObject {
542 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000545 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000546 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100547 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000548
549 PyObject *write; /* write() method of the output stream. */
550 PyObject *output_buffer; /* Write into a local bytearray buffer before
551 flushing to the stream. */
552 Py_ssize_t output_len; /* Length of output_buffer. */
553 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 int proto; /* Pickle protocol number, >= 0 */
555 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100556 int framing; /* True when framing is enabled, proto >= 4 */
557 Py_ssize_t frame_start; /* Position in output_buffer where the
558 where the current frame begins. -1 if there
559 is no frame currently open. */
560
561 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int fast; /* Enable fast mode if set to a true value.
563 The fast mode disable the usage of memo,
564 therefore speeding the pickling process by
565 not generating superfluous PUT opcodes. It
566 should not be used if with self-referential
567 objects. */
568 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000569 int fix_imports; /* Indicate whether Pickler should fix
570 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000571 PyObject *fast_memo;
572} PicklerObject;
573
574typedef struct UnpicklerObject {
575 PyObject_HEAD
576 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000577
578 /* The unpickler memo is just an array of PyObject *s. Using a dict
579 is unnecessary, since the keys are contiguous ints. */
580 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100581 Py_ssize_t memo_size; /* Capacity of the memo array */
582 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000583
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000584 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 Py_buffer buffer;
587 char *input_buffer;
588 char *input_line;
589 Py_ssize_t input_len;
590 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000591 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593 PyObject *read; /* read() method of the input stream. */
594 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000595 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000596
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000597 char *encoding; /* Name of the encoding to be used for
598 decoding strings pickled using Python
599 2.x. The default value is "ASCII" */
600 char *errors; /* Name of errors handling scheme to used when
601 decoding strings. The default value is
602 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500603 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000604 objects. */
605 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
606 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000607 int proto; /* Protocol of the pickle loaded. */
608 int fix_imports; /* Indicate whether Unpickler should fix
609 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000610} UnpicklerObject;
611
612/* Forward declarations */
613static int save(PicklerObject *, PyObject *, int);
614static int save_reduce(PicklerObject *, PyObject *, PyObject *);
615static PyTypeObject Pickler_Type;
616static PyTypeObject Unpickler_Type;
617
618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000619/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300620 A custom hashtable mapping void* to Python ints. This is used by the pickler
621 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000622 a bunch of unnecessary object creation. This makes a huge performance
623 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000625#define MT_MINSIZE 8
626#define PERTURB_SHIFT 5
627
628
629static PyMemoTable *
630PyMemoTable_New(void)
631{
632 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
633 if (memo == NULL) {
634 PyErr_NoMemory();
635 return NULL;
636 }
637
638 memo->mt_used = 0;
639 memo->mt_allocated = MT_MINSIZE;
640 memo->mt_mask = MT_MINSIZE - 1;
641 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
642 if (memo->mt_table == NULL) {
643 PyMem_FREE(memo);
644 PyErr_NoMemory();
645 return NULL;
646 }
647 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
648
649 return memo;
650}
651
652static PyMemoTable *
653PyMemoTable_Copy(PyMemoTable *self)
654{
655 Py_ssize_t i;
656 PyMemoTable *new = PyMemoTable_New();
657 if (new == NULL)
658 return NULL;
659
660 new->mt_used = self->mt_used;
661 new->mt_allocated = self->mt_allocated;
662 new->mt_mask = self->mt_mask;
663 /* The table we get from _New() is probably smaller than we wanted.
664 Free it and allocate one that's the right size. */
665 PyMem_FREE(new->mt_table);
666 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
667 if (new->mt_table == NULL) {
668 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200669 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000670 return NULL;
671 }
672 for (i = 0; i < self->mt_allocated; i++) {
673 Py_XINCREF(self->mt_table[i].me_key);
674 }
675 memcpy(new->mt_table, self->mt_table,
676 sizeof(PyMemoEntry) * self->mt_allocated);
677
678 return new;
679}
680
681static Py_ssize_t
682PyMemoTable_Size(PyMemoTable *self)
683{
684 return self->mt_used;
685}
686
687static int
688PyMemoTable_Clear(PyMemoTable *self)
689{
690 Py_ssize_t i = self->mt_allocated;
691
692 while (--i >= 0) {
693 Py_XDECREF(self->mt_table[i].me_key);
694 }
695 self->mt_used = 0;
696 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
697 return 0;
698}
699
700static void
701PyMemoTable_Del(PyMemoTable *self)
702{
703 if (self == NULL)
704 return;
705 PyMemoTable_Clear(self);
706
707 PyMem_FREE(self->mt_table);
708 PyMem_FREE(self);
709}
710
711/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
712 can be considerably simpler than dictobject.c's lookdict(). */
713static PyMemoEntry *
714_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
715{
716 size_t i;
717 size_t perturb;
718 size_t mask = (size_t)self->mt_mask;
719 PyMemoEntry *table = self->mt_table;
720 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000721 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000722
723 i = hash & mask;
724 entry = &table[i];
725 if (entry->me_key == NULL || entry->me_key == key)
726 return entry;
727
728 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
729 i = (i << 2) + i + perturb + 1;
730 entry = &table[i & mask];
731 if (entry->me_key == NULL || entry->me_key == key)
732 return entry;
733 }
734 assert(0); /* Never reached */
735 return NULL;
736}
737
738/* Returns -1 on failure, 0 on success. */
739static int
740_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
741{
742 PyMemoEntry *oldtable = NULL;
743 PyMemoEntry *oldentry, *newentry;
744 Py_ssize_t new_size = MT_MINSIZE;
745 Py_ssize_t to_process;
746
747 assert(min_size > 0);
748
749 /* Find the smallest valid table size >= min_size. */
750 while (new_size < min_size && new_size > 0)
751 new_size <<= 1;
752 if (new_size <= 0) {
753 PyErr_NoMemory();
754 return -1;
755 }
756 /* new_size needs to be a power of two. */
757 assert((new_size & (new_size - 1)) == 0);
758
759 /* Allocate new table. */
760 oldtable = self->mt_table;
761 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
762 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200763 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000764 PyErr_NoMemory();
765 return -1;
766 }
767 self->mt_allocated = new_size;
768 self->mt_mask = new_size - 1;
769 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
770
771 /* Copy entries from the old table. */
772 to_process = self->mt_used;
773 for (oldentry = oldtable; to_process > 0; oldentry++) {
774 if (oldentry->me_key != NULL) {
775 to_process--;
776 /* newentry is a pointer to a chunk of the new
777 mt_table, so we're setting the key:value pair
778 in-place. */
779 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
780 newentry->me_key = oldentry->me_key;
781 newentry->me_value = oldentry->me_value;
782 }
783 }
784
785 /* Deallocate the old table. */
786 PyMem_FREE(oldtable);
787 return 0;
788}
789
790/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200791static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000792PyMemoTable_Get(PyMemoTable *self, PyObject *key)
793{
794 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
795 if (entry->me_key == NULL)
796 return NULL;
797 return &entry->me_value;
798}
799
800/* Returns -1 on failure, 0 on success. */
801static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200802PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000803{
804 PyMemoEntry *entry;
805
806 assert(key != NULL);
807
808 entry = _PyMemoTable_Lookup(self, key);
809 if (entry->me_key != NULL) {
810 entry->me_value = value;
811 return 0;
812 }
813 Py_INCREF(key);
814 entry->me_key = key;
815 entry->me_value = value;
816 self->mt_used++;
817
818 /* If we added a key, we can safely resize. Otherwise just return!
819 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
820 *
821 * Quadrupling the size improves average table sparseness
822 * (reducing collisions) at the cost of some memory. It also halves
823 * the number of expensive resize operations in a growing memo table.
824 *
825 * Very large memo tables (over 50K items) use doubling instead.
826 * This may help applications with severe memory constraints.
827 */
828 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
829 return 0;
830 return _PyMemoTable_ResizeTable(self,
831 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
832}
833
834#undef MT_MINSIZE
835#undef PERTURB_SHIFT
836
837/*************************************************************************/
838
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000839
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000840static int
841_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000842{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000843 Py_CLEAR(self->output_buffer);
844 self->output_buffer =
845 PyBytes_FromStringAndSize(NULL, self->max_output_len);
846 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000847 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000848 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100849 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000850 return 0;
851}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000852
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100853static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100854_write_size64(char *out, size_t value)
855{
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800856 int i;
857
858 assert(sizeof(size_t) <= 8);
859
860 for (i = 0; i < sizeof(size_t); i++) {
861 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
862 }
863 for (i = sizeof(size_t); i < 8; i++) {
864 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800865 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100866}
867
868static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100869_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
870{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871 qdata[0] = FRAME;
872 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100873}
874
875static int
876_Pickler_CommitFrame(PicklerObject *self)
877{
878 size_t frame_len;
879 char *qdata;
880
881 if (!self->framing || self->frame_start == -1)
882 return 0;
883 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
884 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
885 _Pickler_WriteFrameHeader(self, qdata, frame_len);
886 self->frame_start = -1;
887 return 0;
888}
889
890static int
891_Pickler_OpcodeBoundary(PicklerObject *self)
892{
893 Py_ssize_t frame_len;
894
895 if (!self->framing || self->frame_start == -1)
896 return 0;
897 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
898 if (frame_len >= FRAME_SIZE_TARGET)
899 return _Pickler_CommitFrame(self);
900 else
901 return 0;
902}
903
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000904static PyObject *
905_Pickler_GetString(PicklerObject *self)
906{
907 PyObject *output_buffer = self->output_buffer;
908
909 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100910
911 if (_Pickler_CommitFrame(self))
912 return NULL;
913
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000914 self->output_buffer = NULL;
915 /* Resize down to exact size */
916 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
917 return NULL;
918 return output_buffer;
919}
920
921static int
922_Pickler_FlushToFile(PicklerObject *self)
923{
924 PyObject *output, *result;
925
926 assert(self->write != NULL);
927
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100928 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000929 output = _Pickler_GetString(self);
930 if (output == NULL)
931 return -1;
932
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800933 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 Py_XDECREF(result);
935 return (result == NULL) ? -1 : 0;
936}
937
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200938static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100939_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000940{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100941 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100943 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000944
945 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 need_new_frame = (self->framing && self->frame_start == -1);
947
948 if (need_new_frame)
949 n = data_len + FRAME_HEADER_SIZE;
950 else
951 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952
953 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954 if (required > self->max_output_len) {
955 /* Make place in buffer for the pickle chunk */
956 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
957 PyErr_NoMemory();
958 return -1;
959 }
960 self->max_output_len = (self->output_len + n) / 2 * 3;
961 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
962 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000963 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000964 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100965 if (need_new_frame) {
966 /* Setup new frame */
967 Py_ssize_t frame_start = self->output_len;
968 self->frame_start = frame_start;
969 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
970 /* Write an invalid value, for debugging */
971 buffer[frame_start + i] = 0xFE;
972 }
973 self->output_len += FRAME_HEADER_SIZE;
974 }
975 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000976 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100977 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000978 buffer[self->output_len + i] = s[i];
979 }
980 }
981 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100984 self->output_len += data_len;
985 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000986}
987
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988static PicklerObject *
989_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000990{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000991 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
994 if (self == NULL)
995 return NULL;
996
997 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100998 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 self->write = NULL;
1000 self->proto = 0;
1001 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001002 self->framing = 0;
1003 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 self->fast = 0;
1005 self->fast_nesting = 0;
1006 self->fix_imports = 0;
1007 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008 self->max_output_len = WRITE_BUF_SIZE;
1009 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001010
1011 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1013 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001014
1015 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001016 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001017 return NULL;
1018 }
1019 return self;
1020}
1021
1022static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001023_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001024{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001025 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001026
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001027 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001029 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001031 proto = PyLong_AsLong(protocol);
1032 if (proto < 0) {
1033 if (proto == -1 && PyErr_Occurred())
1034 return -1;
1035 proto = HIGHEST_PROTOCOL;
1036 }
1037 else if (proto > HIGHEST_PROTOCOL) {
1038 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1039 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001041 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001042 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001043 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044 self->bin = proto > 0;
1045 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 return 0;
1047}
1048
1049/* Returns -1 (with an exception set) on failure, 0 on success. This may
1050 be called once on a freshly created Pickler. */
1051static int
1052_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1053{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001054 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001055 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001056 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001057 if (self->write == NULL) {
1058 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1059 PyErr_SetString(PyExc_TypeError,
1060 "file must have a 'write' attribute");
1061 return -1;
1062 }
1063
1064 return 0;
1065}
1066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001067/* Returns the size of the input on success, -1 on failure. This takes its
1068 own reference to `input`. */
1069static Py_ssize_t
1070_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1071{
1072 if (self->buffer.buf != NULL)
1073 PyBuffer_Release(&self->buffer);
1074 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1075 return -1;
1076 self->input_buffer = self->buffer.buf;
1077 self->input_len = self->buffer.len;
1078 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001079 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001080 return self->input_len;
1081}
1082
Antoine Pitrou04248a82010-10-12 20:51:21 +00001083static int
1084_Unpickler_SkipConsumed(UnpicklerObject *self)
1085{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001086 Py_ssize_t consumed;
1087 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001088
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001089 consumed = self->next_read_idx - self->prefetched_idx;
1090 if (consumed <= 0)
1091 return 0;
1092
1093 assert(self->peek); /* otherwise we did something wrong */
1094 /* This makes an useless copy... */
1095 r = PyObject_CallFunction(self->read, "n", consumed);
1096 if (r == NULL)
1097 return -1;
1098 Py_DECREF(r);
1099
1100 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001101 return 0;
1102}
1103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001104static const Py_ssize_t READ_WHOLE_LINE = -1;
1105
1106/* If reading from a file, we need to only pull the bytes we need, since there
1107 may be multiple pickle objects arranged contiguously in the same input
1108 buffer.
1109
1110 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1111 bytes from the input stream/buffer.
1112
1113 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1114 failure; on success, returns the number of bytes read from the file.
1115
1116 On success, self->input_len will be 0; this is intentional so that when
1117 unpickling from a file, the "we've run out of data" code paths will trigger,
1118 causing the Unpickler to go back to the file for more data. Use the returned
1119 size to tell you how much data you can process. */
1120static Py_ssize_t
1121_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1122{
1123 PyObject *data;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001124 Py_ssize_t read_size, prefetched_size = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001125
1126 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001127
Antoine Pitrou04248a82010-10-12 20:51:21 +00001128 if (_Unpickler_SkipConsumed(self) < 0)
1129 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001131 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001132 PyObject *empty_tuple = PyTuple_New(0);
1133 data = PyObject_Call(self->readline, empty_tuple, NULL);
1134 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001135 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001136 else {
1137 PyObject *len = PyLong_FromSsize_t(n);
1138 if (len == NULL)
1139 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001140 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001141 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001142 if (data == NULL)
1143 return -1;
1144
Antoine Pitrou04248a82010-10-12 20:51:21 +00001145 /* Prefetch some data without advancing the file pointer, if possible */
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001146 if (self->peek) {
Antoine Pitrou04248a82010-10-12 20:51:21 +00001147 PyObject *len, *prefetched;
1148 len = PyLong_FromSsize_t(PREFETCH);
1149 if (len == NULL) {
1150 Py_DECREF(data);
1151 return -1;
1152 }
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001153 prefetched = _Pickle_FastCall(self->peek, len);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001154 if (prefetched == NULL) {
1155 if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) {
1156 /* peek() is probably not supported by the given file object */
1157 PyErr_Clear();
1158 Py_CLEAR(self->peek);
1159 }
1160 else {
1161 Py_DECREF(data);
1162 return -1;
1163 }
1164 }
1165 else {
1166 assert(PyBytes_Check(prefetched));
1167 prefetched_size = PyBytes_GET_SIZE(prefetched);
1168 PyBytes_ConcatAndDel(&data, prefetched);
1169 if (data == NULL)
1170 return -1;
1171 }
1172 }
1173
1174 read_size = _Unpickler_SetStringInput(self, data) - prefetched_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001175 Py_DECREF(data);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001176 self->prefetched_idx = read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001177 return read_size;
1178}
1179
1180/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1181
1182 This should be used for all data reads, rather than accessing the unpickler's
1183 input buffer directly. This method deals correctly with reading from input
1184 streams, which the input buffer doesn't deal with.
1185
1186 Note that when reading from a file-like object, self->next_read_idx won't
1187 be updated (it should remain at 0 for the entire unpickling process). You
1188 should use this function's return value to know how many bytes you can
1189 consume.
1190
1191 Returns -1 (with an exception set) on failure. On success, return the
1192 number of chars read. */
1193static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001194_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001195{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196 Py_ssize_t num_read;
1197
Antoine Pitrou04248a82010-10-12 20:51:21 +00001198 if (self->next_read_idx + n <= self->input_len) {
1199 *s = self->input_buffer + self->next_read_idx;
1200 self->next_read_idx += n;
1201 return n;
1202 }
1203 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001204 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001205 return -1;
1206 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001207 num_read = _Unpickler_ReadFromFile(self, n);
1208 if (num_read < 0)
1209 return -1;
1210 if (num_read < n) {
1211 PyErr_Format(PyExc_EOFError, "Ran out of input");
1212 return -1;
1213 }
1214 *s = self->input_buffer;
1215 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001216 return n;
1217}
1218
1219static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001220_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1221 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001224 if (input_line == NULL) {
1225 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001226 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001228
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001229 memcpy(input_line, line, len);
1230 input_line[len] = '\0';
1231 self->input_line = input_line;
1232 *result = self->input_line;
1233 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001234}
1235
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001236/* Read a line from the input stream/buffer. If we run off the end of the input
1237 before hitting \n, return the data we found.
1238
1239 Returns the number of chars read, or -1 on failure. */
1240static Py_ssize_t
1241_Unpickler_Readline(UnpicklerObject *self, char **result)
1242{
1243 Py_ssize_t i, num_read;
1244
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001245 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246 if (self->input_buffer[i] == '\n') {
1247 char *line_start = self->input_buffer + self->next_read_idx;
1248 num_read = i - self->next_read_idx + 1;
1249 self->next_read_idx = i + 1;
1250 return _Unpickler_CopyLine(self, line_start, num_read, result);
1251 }
1252 }
1253 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001254 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1255 if (num_read < 0)
1256 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001257 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001258 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001259 }
Victor Stinner121aab42011-09-29 23:40:53 +02001260
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001261 /* If we get here, we've run off the end of the input string. Return the
1262 remaining string and let the caller figure it out. */
1263 *result = self->input_buffer + self->next_read_idx;
1264 num_read = i - self->next_read_idx;
1265 self->next_read_idx = i;
1266 return num_read;
1267}
1268
1269/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1270 will be modified in place. */
1271static int
1272_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1273{
1274 Py_ssize_t i;
1275 PyObject **memo;
1276
1277 assert(new_size > self->memo_size);
1278
1279 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1280 if (memo == NULL) {
1281 PyErr_NoMemory();
1282 return -1;
1283 }
1284 self->memo = memo;
1285 for (i = self->memo_size; i < new_size; i++)
1286 self->memo[i] = NULL;
1287 self->memo_size = new_size;
1288 return 0;
1289}
1290
1291/* Returns NULL if idx is out of bounds. */
1292static PyObject *
1293_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1294{
1295 if (idx < 0 || idx >= self->memo_size)
1296 return NULL;
1297
1298 return self->memo[idx];
1299}
1300
1301/* Returns -1 (with an exception set) on failure, 0 on success.
1302 This takes its own reference to `value`. */
1303static int
1304_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1305{
1306 PyObject *old_item;
1307
1308 if (idx >= self->memo_size) {
1309 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1310 return -1;
1311 assert(idx < self->memo_size);
1312 }
1313 Py_INCREF(value);
1314 old_item = self->memo[idx];
1315 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001316 if (old_item != NULL) {
1317 Py_DECREF(old_item);
1318 }
1319 else {
1320 self->memo_len++;
1321 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001322 return 0;
1323}
1324
1325static PyObject **
1326_Unpickler_NewMemo(Py_ssize_t new_size)
1327{
1328 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
Victor Stinner42024562013-07-12 00:53:57 +02001329 if (memo == NULL) {
1330 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001332 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 memset(memo, 0, new_size * sizeof(PyObject *));
1334 return memo;
1335}
1336
1337/* Free the unpickler's memo, taking care to decref any items left in it. */
1338static void
1339_Unpickler_MemoCleanup(UnpicklerObject *self)
1340{
1341 Py_ssize_t i;
1342 PyObject **memo = self->memo;
1343
1344 if (self->memo == NULL)
1345 return;
1346 self->memo = NULL;
1347 i = self->memo_size;
1348 while (--i >= 0) {
1349 Py_XDECREF(memo[i]);
1350 }
1351 PyMem_FREE(memo);
1352}
1353
1354static UnpicklerObject *
1355_Unpickler_New(void)
1356{
1357 UnpicklerObject *self;
1358
1359 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1360 if (self == NULL)
1361 return NULL;
1362
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001363 self->pers_func = NULL;
1364 self->input_buffer = NULL;
1365 self->input_line = NULL;
1366 self->input_len = 0;
1367 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001368 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001369 self->read = NULL;
1370 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001371 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001372 self->encoding = NULL;
1373 self->errors = NULL;
1374 self->marks = NULL;
1375 self->num_marks = 0;
1376 self->marks_size = 0;
1377 self->proto = 0;
1378 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001379 memset(&self->buffer, 0, sizeof(Py_buffer));
1380 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001381 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001382 self->memo = _Unpickler_NewMemo(self->memo_size);
1383 self->stack = (Pdata *)Pdata_New();
1384
1385 if (self->memo == NULL || self->stack == NULL) {
1386 Py_DECREF(self);
1387 return NULL;
1388 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389
1390 return self;
1391}
1392
1393/* Returns -1 (with an exception set) on failure, 0 on success. This may
1394 be called once on a freshly created Pickler. */
1395static int
1396_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1397{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001398 _Py_IDENTIFIER(peek);
1399 _Py_IDENTIFIER(read);
1400 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001401
1402 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001403 if (self->peek == NULL) {
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1405 PyErr_Clear();
1406 else
1407 return -1;
1408 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001409 self->read = _PyObject_GetAttrId(file, &PyId_read);
1410 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001411 if (self->readline == NULL || self->read == NULL) {
1412 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1413 PyErr_SetString(PyExc_TypeError,
1414 "file must have 'read' and 'readline' attributes");
1415 Py_CLEAR(self->read);
1416 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001417 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001418 return -1;
1419 }
1420 return 0;
1421}
1422
1423/* Returns -1 (with an exception set) on failure, 0 on success. This may
1424 be called once on a freshly created Pickler. */
1425static int
1426_Unpickler_SetInputEncoding(UnpicklerObject *self,
1427 const char *encoding,
1428 const char *errors)
1429{
1430 if (encoding == NULL)
1431 encoding = "ASCII";
1432 if (errors == NULL)
1433 errors = "strict";
1434
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001435 self->encoding = _PyMem_Strdup(encoding);
1436 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 if (self->encoding == NULL || self->errors == NULL) {
1438 PyErr_NoMemory();
1439 return -1;
1440 }
1441 return 0;
1442}
1443
1444/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001445static int
1446memo_get(PicklerObject *self, PyObject *key)
1447{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001448 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001449 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001450 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001452 value = PyMemoTable_Get(self->memo, key);
1453 if (value == NULL) {
1454 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 return -1;
1456 }
1457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458 if (!self->bin) {
1459 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001460 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1461 "%" PY_FORMAT_SIZE_T "d\n", *value);
1462 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 }
1464 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001465 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001466 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001467 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 len = 2;
1469 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001470 else if (*value <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 pdata[1] = (unsigned char)(*value & 0xff);
1473 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1474 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1475 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001476 len = 5;
1477 }
1478 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001479 PickleState *st = _Pickle_GetGlobalState();
1480 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001481 "memo id too large for LONG_BINGET");
1482 return -1;
1483 }
1484 }
1485
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001486 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 return -1;
1488
1489 return 0;
1490}
1491
1492/* Store an object in the memo, assign it a new unique ID based on the number
1493 of objects currently stored in the memo and generate a PUT opcode. */
1494static int
1495memo_put(PicklerObject *self, PyObject *obj)
1496{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001498 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001499 Py_ssize_t idx;
1500
1501 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001502
1503 if (self->fast)
1504 return 0;
1505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001506 idx = PyMemoTable_Size(self->memo);
1507 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1508 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001509
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001510 if (self->proto >= 4) {
1511 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1512 return -1;
1513 return 0;
1514 }
1515 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001516 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001517 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001518 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519 len = strlen(pdata);
1520 }
1521 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001522 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001524 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 len = 2;
1526 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001527 else if (idx <= 0xffffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001528 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001529 pdata[1] = (unsigned char)(idx & 0xff);
1530 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1531 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1532 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533 len = 5;
1534 }
1535 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001536 PickleState *st = _Pickle_GetGlobalState();
1537 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538 "memo id too large for LONG_BINPUT");
1539 return -1;
1540 }
1541 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001542 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001544
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001545 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001546}
1547
1548static PyObject *
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001549getattribute(PyObject *obj, PyObject *name, int allow_qualname) {
1550 PyObject *dotted_path;
1551 Py_ssize_t i;
1552 _Py_static_string(PyId_dot, ".");
1553 _Py_static_string(PyId_locals, "<locals>");
1554
1555 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
1556 if (dotted_path == NULL) {
1557 return NULL;
1558 }
1559 assert(Py_SIZE(dotted_path) >= 1);
1560 if (!allow_qualname && Py_SIZE(dotted_path) > 1) {
1561 PyErr_Format(PyExc_AttributeError,
1562 "Can't get qualified attribute %R on %R;"
1563 "use protocols >= 4 to enable support",
1564 name, obj);
1565 Py_DECREF(dotted_path);
1566 return NULL;
1567 }
1568 Py_INCREF(obj);
1569 for (i = 0; i < Py_SIZE(dotted_path); i++) {
1570 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
1571 PyObject *tmp;
1572 PyObject *result = PyUnicode_RichCompare(
1573 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1574 int is_equal = (result == Py_True);
1575 assert(PyBool_Check(result));
1576 Py_DECREF(result);
1577 if (is_equal) {
1578 PyErr_Format(PyExc_AttributeError,
1579 "Can't get local attribute %R on %R", name, obj);
1580 Py_DECREF(dotted_path);
1581 Py_DECREF(obj);
1582 return NULL;
1583 }
1584 tmp = PyObject_GetAttr(obj, subpath);
1585 Py_DECREF(obj);
1586 if (tmp == NULL) {
1587 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1588 PyErr_Clear();
1589 PyErr_Format(PyExc_AttributeError,
1590 "Can't get attribute %R on %R", name, obj);
1591 }
1592 Py_DECREF(dotted_path);
1593 return NULL;
1594 }
1595 obj = tmp;
1596 }
1597 Py_DECREF(dotted_path);
1598 return obj;
1599}
1600
1601static PyObject *
1602whichmodule(PyObject *global, PyObject *global_name, int allow_qualname)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001603{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001604 PyObject *module_name;
1605 PyObject *modules_dict;
1606 PyObject *module;
1607 PyObject *obj;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001608 Py_ssize_t i, j;
1609 _Py_IDENTIFIER(__module__);
1610 _Py_IDENTIFIER(modules);
1611 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001612
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1614
1615 if (module_name == NULL) {
1616 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001617 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001618 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001619 }
1620 else {
1621 /* In some rare cases (e.g., bound methods of extension types),
1622 __module__ can be None. If it is so, then search sys.modules for
1623 the module of global. */
1624 if (module_name != Py_None)
1625 return module_name;
1626 Py_CLEAR(module_name);
1627 }
1628 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001629
Victor Stinnerbb520202013-11-06 22:40:41 +01001630 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001631 if (modules_dict == NULL) {
1632 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001633 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001634 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635
1636 i = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001637 while ((j = PyDict_Next(modules_dict, &i, &module_name, &module))) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001638 PyObject *result = PyUnicode_RichCompare(
1639 module_name, _PyUnicode_FromId(&PyId___main__), Py_EQ);
1640 int is_equal = (result == Py_True);
1641 assert(PyBool_Check(result));
1642 Py_DECREF(result);
1643 if (is_equal)
1644 continue;
1645 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001646 continue;
1647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 obj = getattribute(module, global_name, allow_qualname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 if (obj == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001651 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001652 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 continue;
1654 }
1655
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001656 if (obj == global) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001657 Py_DECREF(obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001658 Py_INCREF(module_name);
1659 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001660 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001661 Py_DECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001662 }
1663
1664 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001665 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001666 Py_INCREF(module_name);
1667 return module_name;
1668}
1669
1670/* fast_save_enter() and fast_save_leave() are guards against recursive
1671 objects when Pickler is used with the "fast mode" (i.e., with object
1672 memoization disabled). If the nesting of a list or dict object exceed
1673 FAST_NESTING_LIMIT, these guards will start keeping an internal
1674 reference to the seen list or dict objects and check whether these objects
1675 are recursive. These are not strictly necessary, since save() has a
1676 hard-coded recursion limit, but they give a nicer error message than the
1677 typical RuntimeError. */
1678static int
1679fast_save_enter(PicklerObject *self, PyObject *obj)
1680{
1681 /* if fast_nesting < 0, we're doing an error exit. */
1682 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1683 PyObject *key = NULL;
1684 if (self->fast_memo == NULL) {
1685 self->fast_memo = PyDict_New();
1686 if (self->fast_memo == NULL) {
1687 self->fast_nesting = -1;
1688 return 0;
1689 }
1690 }
1691 key = PyLong_FromVoidPtr(obj);
1692 if (key == NULL)
1693 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001694 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 Py_DECREF(key);
1696 PyErr_Format(PyExc_ValueError,
1697 "fast mode: can't pickle cyclic objects "
1698 "including object type %.200s at %p",
1699 obj->ob_type->tp_name, obj);
1700 self->fast_nesting = -1;
1701 return 0;
1702 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001703 if (PyErr_Occurred()) {
1704 return 0;
1705 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001706 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1707 Py_DECREF(key);
1708 self->fast_nesting = -1;
1709 return 0;
1710 }
1711 Py_DECREF(key);
1712 }
1713 return 1;
1714}
1715
1716static int
1717fast_save_leave(PicklerObject *self, PyObject *obj)
1718{
1719 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1720 PyObject *key = PyLong_FromVoidPtr(obj);
1721 if (key == NULL)
1722 return 0;
1723 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1724 Py_DECREF(key);
1725 return 0;
1726 }
1727 Py_DECREF(key);
1728 }
1729 return 1;
1730}
1731
1732static int
1733save_none(PicklerObject *self, PyObject *obj)
1734{
1735 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001736 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001737 return -1;
1738
1739 return 0;
1740}
1741
1742static int
1743save_bool(PicklerObject *self, PyObject *obj)
1744{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001745 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001746 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001747 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001748 return -1;
1749 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001750 else {
1751 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1752 * so that unpicklers written before bools were introduced unpickle them
1753 * as ints, but unpicklers after can recognize that bools were intended.
1754 * Note that protocol 2 added direct ways to pickle bools.
1755 */
1756 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1757 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1758 return -1;
1759 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001760 return 0;
1761}
1762
1763static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001764save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001765{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001766 PyObject *repr = NULL;
1767 Py_ssize_t size;
1768 long val;
1769 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001770
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001771 const char long_op = LONG;
1772
1773 val= PyLong_AsLong(obj);
1774 if (val == -1 && PyErr_Occurred()) {
1775 /* out of range for int pickling */
1776 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001777 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001778 else if (self->bin &&
1779 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001780 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
1781 /* result fits in a signed 4-byte integer.
1782
1783 Note: we can't use -0x80000000L in the above condition because some
1784 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1785 before applying the unary minus when sizeof(long) <= 4. The
1786 resulting value stays unsigned which is commonly not what we want,
1787 so MSVC happily warns us about it. However, that result would have
1788 been fine because we guard for sizeof(long) <= 4 which turns the
1789 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001790 char pdata[32];
1791 Py_ssize_t len = 0;
1792
1793 pdata[1] = (unsigned char)(val & 0xff);
1794 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1795 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1796 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797
1798 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1799 if (pdata[2] == 0) {
1800 pdata[0] = BININT1;
1801 len = 2;
1802 }
1803 else {
1804 pdata[0] = BININT2;
1805 len = 3;
1806 }
1807 }
1808 else {
1809 pdata[0] = BININT;
1810 len = 5;
1811 }
1812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001813 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001814 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001815
1816 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001817 }
1818
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819 if (self->proto >= 2) {
1820 /* Linear-time pickling. */
1821 size_t nbits;
1822 size_t nbytes;
1823 unsigned char *pdata;
1824 char header[5];
1825 int i;
1826 int sign = _PyLong_Sign(obj);
1827
1828 if (sign == 0) {
1829 header[0] = LONG1;
1830 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001831 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001832 goto error;
1833 return 0;
1834 }
1835 nbits = _PyLong_NumBits(obj);
1836 if (nbits == (size_t)-1 && PyErr_Occurred())
1837 goto error;
1838 /* How many bytes do we need? There are nbits >> 3 full
1839 * bytes of data, and nbits & 7 leftover bits. If there
1840 * are any leftover bits, then we clearly need another
1841 * byte. Wnat's not so obvious is that we *probably*
1842 * need another byte even if there aren't any leftovers:
1843 * the most-significant bit of the most-significant byte
1844 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001845 * opposite of the one we need. The exception is ints
1846 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 * its own 256's-complement, so has the right sign bit
1848 * even without the extra byte. That's a pain to check
1849 * for in advance, though, so we always grab an extra
1850 * byte at the start, and cut it back later if possible.
1851 */
1852 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001853 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001854 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001855 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001856 goto error;
1857 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001858 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001859 if (repr == NULL)
1860 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001861 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001862 i = _PyLong_AsByteArray((PyLongObject *)obj,
1863 pdata, nbytes,
1864 1 /* little endian */ , 1 /* signed */ );
1865 if (i < 0)
1866 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001867 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001868 * needed. This is so iff the MSB is all redundant sign
1869 * bits.
1870 */
1871 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001872 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001873 pdata[nbytes - 1] == 0xff &&
1874 (pdata[nbytes - 2] & 0x80) != 0) {
1875 nbytes--;
1876 }
1877
1878 if (nbytes < 256) {
1879 header[0] = LONG1;
1880 header[1] = (unsigned char)nbytes;
1881 size = 2;
1882 }
1883 else {
1884 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001885 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 for (i = 1; i < 5; i++) {
1887 header[i] = (unsigned char)(size & 0xff);
1888 size >>= 8;
1889 }
1890 size = 5;
1891 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001892 if (_Pickler_Write(self, header, size) < 0 ||
1893 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001894 goto error;
1895 }
1896 else {
1897 char *string;
1898
Mark Dickinson8dd05142009-01-20 20:43:58 +00001899 /* proto < 2: write the repr and newline. This is quadratic-time (in
1900 the number of digits), in both directions. We add a trailing 'L'
1901 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902
1903 repr = PyObject_Repr(obj);
1904 if (repr == NULL)
1905 goto error;
1906
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001907 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 if (string == NULL)
1909 goto error;
1910
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001911 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1912 _Pickler_Write(self, string, size) < 0 ||
1913 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001914 goto error;
1915 }
1916
1917 if (0) {
1918 error:
1919 status = -1;
1920 }
1921 Py_XDECREF(repr);
1922
1923 return status;
1924}
1925
1926static int
1927save_float(PicklerObject *self, PyObject *obj)
1928{
1929 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1930
1931 if (self->bin) {
1932 char pdata[9];
1933 pdata[0] = BINFLOAT;
1934 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1935 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001936 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001937 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001938 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001939 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001940 int result = -1;
1941 char *buf = NULL;
1942 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001944 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001945 goto done;
1946
Mark Dickinson3e09f432009-04-17 08:41:23 +00001947 buf = PyOS_double_to_string(x, 'g', 17, 0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001948 if (!buf) {
1949 PyErr_NoMemory();
1950 goto done;
1951 }
1952
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001953 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001954 goto done;
1955
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001956 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001957 goto done;
1958
1959 result = 0;
1960done:
1961 PyMem_Free(buf);
1962 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001963 }
1964
1965 return 0;
1966}
1967
1968static int
1969save_bytes(PicklerObject *self, PyObject *obj)
1970{
1971 if (self->proto < 3) {
1972 /* Older pickle protocols do not have an opcode for pickling bytes
1973 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001974 the __reduce__ method) to permit bytes object unpickling.
1975
1976 Here we use a hack to be compatible with Python 2. Since in Python
1977 2 'bytes' is just an alias for 'str' (which has different
1978 parameters than the actual bytes object), we use codecs.encode
1979 to create the appropriate 'str' object when unpickled using
1980 Python 2 *and* the appropriate 'bytes' object when unpickled
1981 using Python 3. Again this is a hack and we don't need to do this
1982 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001983 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001984 int status;
1985
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001986 if (PyBytes_GET_SIZE(obj) == 0) {
1987 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
1988 }
1989 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001990 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001991 PyObject *unicode_str =
1992 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
1993 PyBytes_GET_SIZE(obj),
1994 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001995 _Py_IDENTIFIER(latin1);
1996
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001997 if (unicode_str == NULL)
1998 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05001999 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002000 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002001 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002002 Py_DECREF(unicode_str);
2003 }
2004
2005 if (reduce_value == NULL)
2006 return -1;
2007
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002008 /* save_reduce() will memoize the object automatically. */
2009 status = save_reduce(self, reduce_value, obj);
2010 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002011 return status;
2012 }
2013 else {
2014 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002015 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002016 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002018 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002019 if (size < 0)
2020 return -1;
2021
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002022 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002023 header[0] = SHORT_BINBYTES;
2024 header[1] = (unsigned char)size;
2025 len = 2;
2026 }
2027 else if (size <= 0xffffffffL) {
2028 header[0] = BINBYTES;
2029 header[1] = (unsigned char)(size & 0xff);
2030 header[2] = (unsigned char)((size >> 8) & 0xff);
2031 header[3] = (unsigned char)((size >> 16) & 0xff);
2032 header[4] = (unsigned char)((size >> 24) & 0xff);
2033 len = 5;
2034 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002035 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002036 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002037 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002038 len = 8;
2039 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002040 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002041 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002042 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002043 return -1; /* string too large */
2044 }
2045
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002046 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002047 return -1;
2048
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002049 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050 return -1;
2051
2052 if (memo_put(self, obj) < 0)
2053 return -1;
2054
2055 return 0;
2056 }
2057}
2058
2059/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2060 backslash and newline characters to \uXXXX escapes. */
2061static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002062raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002063{
2064 PyObject *repr, *result;
2065 char *p;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002066 Py_ssize_t i, size, expandsize;
2067 void *data;
2068 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002070 if (PyUnicode_READY(obj))
2071 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002073 size = PyUnicode_GET_LENGTH(obj);
2074 data = PyUnicode_DATA(obj);
2075 kind = PyUnicode_KIND(obj);
2076 if (kind == PyUnicode_4BYTE_KIND)
2077 expandsize = 10;
2078 else
2079 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002080
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002081 if (size > PY_SSIZE_T_MAX / expandsize)
2082 return PyErr_NoMemory();
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002083 repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002084 if (repr == NULL)
2085 return NULL;
2086 if (size == 0)
2087 goto done;
2088
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002089 p = PyByteArray_AS_STRING(repr);
2090 for (i=0; i < size; i++) {
2091 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092 /* Map 32-bit characters to '\Uxxxxxxxx' */
2093 if (ch >= 0x10000) {
2094 *p++ = '\\';
2095 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002096 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2097 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2098 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2099 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2100 *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 Vassalottica2d6102008-06-12 18:26:05 +00002105 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002106 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002107 *p++ = '\\';
2108 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002109 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2110 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2111 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2112 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002114 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002115 else
2116 *p++ = (char) ch;
2117 }
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002118 size = p - PyByteArray_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002119
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002120done:
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002121 result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122 Py_DECREF(repr);
2123 return result;
2124}
2125
2126static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002127write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2128{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002129 char header[9];
2130 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002131
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002132 if (size <= 0xff && self->proto >= 4) {
2133 header[0] = SHORT_BINUNICODE;
2134 header[1] = (unsigned char)(size & 0xff);
2135 len = 2;
2136 }
2137 else if (size <= 0xffffffffUL) {
2138 header[0] = BINUNICODE;
2139 header[1] = (unsigned char)(size & 0xff);
2140 header[2] = (unsigned char)((size >> 8) & 0xff);
2141 header[3] = (unsigned char)((size >> 16) & 0xff);
2142 header[4] = (unsigned char)((size >> 24) & 0xff);
2143 len = 5;
2144 }
2145 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002146 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002147 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002148 len = 9;
2149 }
2150 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002151 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002152 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002153 return -1;
2154 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002155
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002156 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002157 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002158 if (_Pickler_Write(self, data, size) < 0)
2159 return -1;
2160
2161 return 0;
2162}
2163
2164static int
2165write_unicode_binary(PicklerObject *self, PyObject *obj)
2166{
2167 PyObject *encoded = NULL;
2168 Py_ssize_t size;
2169 char *data;
2170 int r;
2171
2172 if (PyUnicode_READY(obj))
2173 return -1;
2174
2175 data = PyUnicode_AsUTF8AndSize(obj, &size);
2176 if (data != NULL)
2177 return write_utf8(self, data, size);
2178
2179 /* Issue #8383: for strings with lone surrogates, fallback on the
2180 "surrogatepass" error handler. */
2181 PyErr_Clear();
2182 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2183 if (encoded == NULL)
2184 return -1;
2185
2186 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2187 PyBytes_GET_SIZE(encoded));
2188 Py_DECREF(encoded);
2189 return r;
2190}
2191
2192static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002193save_unicode(PicklerObject *self, PyObject *obj)
2194{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002195 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002196 if (write_unicode_binary(self, obj) < 0)
2197 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002198 }
2199 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002200 PyObject *encoded;
2201 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002202 const char unicode_op = UNICODE;
2203
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002204 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002205 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002207
Antoine Pitrou299978d2013-04-07 17:38:11 +02002208 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2209 Py_DECREF(encoded);
2210 return -1;
2211 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002212
2213 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002214 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2215 Py_DECREF(encoded);
2216 return -1;
2217 }
2218 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002219
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002220 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002221 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002222 }
2223 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002224 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002225
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002226 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002227}
2228
2229/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2230static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002231store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002232{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002233 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002234
2235 assert(PyTuple_Size(t) == len);
2236
2237 for (i = 0; i < len; i++) {
2238 PyObject *element = PyTuple_GET_ITEM(t, i);
2239
2240 if (element == NULL)
2241 return -1;
2242 if (save(self, element, 0) < 0)
2243 return -1;
2244 }
2245
2246 return 0;
2247}
2248
2249/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2250 * used across protocols to minimize the space needed to pickle them.
2251 * Tuples are also the only builtin immutable type that can be recursive
2252 * (a tuple can be reached from itself), and that requires some subtle
2253 * magic so that it works in all cases. IOW, this is a long routine.
2254 */
2255static int
2256save_tuple(PicklerObject *self, PyObject *obj)
2257{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002258 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259
2260 const char mark_op = MARK;
2261 const char tuple_op = TUPLE;
2262 const char pop_op = POP;
2263 const char pop_mark_op = POP_MARK;
2264 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2265
2266 if ((len = PyTuple_Size(obj)) < 0)
2267 return -1;
2268
2269 if (len == 0) {
2270 char pdata[2];
2271
2272 if (self->proto) {
2273 pdata[0] = EMPTY_TUPLE;
2274 len = 1;
2275 }
2276 else {
2277 pdata[0] = MARK;
2278 pdata[1] = TUPLE;
2279 len = 2;
2280 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002281 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282 return -1;
2283 return 0;
2284 }
2285
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002286 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287 * saving the tuple elements, the tuple must be recursive, in
2288 * which case we'll pop everything we put on the stack, and fetch
2289 * its value from the memo.
2290 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002291 if (len <= 3 && self->proto >= 2) {
2292 /* Use TUPLE{1,2,3} opcodes. */
2293 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002294 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002296 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297 /* pop the len elements */
2298 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002299 if (_Pickler_Write(self, &pop_op, 1) < 0)
2300 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002301 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002302 if (memo_get(self, obj) < 0)
2303 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002304
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002305 return 0;
2306 }
2307 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002308 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2309 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002310 }
2311 goto memoize;
2312 }
2313
2314 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2315 * Generate MARK e1 e2 ... TUPLE
2316 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002317 if (_Pickler_Write(self, &mark_op, 1) < 0)
2318 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002319
2320 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002321 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002323 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002324 /* pop the stack stuff we pushed */
2325 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002326 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2327 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 }
2329 else {
2330 /* Note that we pop one more than len, to remove
2331 * the MARK too.
2332 */
2333 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002334 if (_Pickler_Write(self, &pop_op, 1) < 0)
2335 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 }
2337 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002338 if (memo_get(self, obj) < 0)
2339 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002340
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 return 0;
2342 }
2343 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2345 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002346 }
2347
2348 memoize:
2349 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002350 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002352 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353}
2354
2355/* iter is an iterator giving items, and we batch up chunks of
2356 * MARK item item ... item APPENDS
2357 * opcode sequences. Calling code should have arranged to first create an
2358 * empty list, or list-like object, for the APPENDS to operate on.
2359 * Returns 0 on success, <0 on error.
2360 */
2361static int
2362batch_list(PicklerObject *self, PyObject *iter)
2363{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002364 PyObject *obj = NULL;
2365 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366 int i, n;
2367
2368 const char mark_op = MARK;
2369 const char append_op = APPEND;
2370 const char appends_op = APPENDS;
2371
2372 assert(iter != NULL);
2373
2374 /* XXX: I think this function could be made faster by avoiding the
2375 iterator interface and fetching objects directly from list using
2376 PyList_GET_ITEM.
2377 */
2378
2379 if (self->proto == 0) {
2380 /* APPENDS isn't available; do one at a time. */
2381 for (;;) {
2382 obj = PyIter_Next(iter);
2383 if (obj == NULL) {
2384 if (PyErr_Occurred())
2385 return -1;
2386 break;
2387 }
2388 i = save(self, obj, 0);
2389 Py_DECREF(obj);
2390 if (i < 0)
2391 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002392 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002393 return -1;
2394 }
2395 return 0;
2396 }
2397
2398 /* proto > 0: write in batches of BATCHSIZE. */
2399 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002400 /* Get first item */
2401 firstitem = PyIter_Next(iter);
2402 if (firstitem == NULL) {
2403 if (PyErr_Occurred())
2404 goto error;
2405
2406 /* nothing more to add */
2407 break;
2408 }
2409
2410 /* Try to get a second item */
2411 obj = PyIter_Next(iter);
2412 if (obj == NULL) {
2413 if (PyErr_Occurred())
2414 goto error;
2415
2416 /* Only one item to write */
2417 if (save(self, firstitem, 0) < 0)
2418 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002419 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002420 goto error;
2421 Py_CLEAR(firstitem);
2422 break;
2423 }
2424
2425 /* More than one item to write */
2426
2427 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002428 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002429 goto error;
2430
2431 if (save(self, firstitem, 0) < 0)
2432 goto error;
2433 Py_CLEAR(firstitem);
2434 n = 1;
2435
2436 /* Fetch and save up to BATCHSIZE items */
2437 while (obj) {
2438 if (save(self, obj, 0) < 0)
2439 goto error;
2440 Py_CLEAR(obj);
2441 n += 1;
2442
2443 if (n == BATCHSIZE)
2444 break;
2445
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002446 obj = PyIter_Next(iter);
2447 if (obj == NULL) {
2448 if (PyErr_Occurred())
2449 goto error;
2450 break;
2451 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002452 }
2453
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002454 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002455 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002457 } while (n == BATCHSIZE);
2458 return 0;
2459
2460 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002461 Py_XDECREF(firstitem);
2462 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002463 return -1;
2464}
2465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002466/* This is a variant of batch_list() above, specialized for lists (with no
2467 * support for list subclasses). Like batch_list(), we batch up chunks of
2468 * MARK item item ... item APPENDS
2469 * opcode sequences. Calling code should have arranged to first create an
2470 * empty list, or list-like object, for the APPENDS to operate on.
2471 * Returns 0 on success, -1 on error.
2472 *
2473 * This version is considerably faster than batch_list(), if less general.
2474 *
2475 * Note that this only works for protocols > 0.
2476 */
2477static int
2478batch_list_exact(PicklerObject *self, PyObject *obj)
2479{
2480 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002481 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002482
2483 const char append_op = APPEND;
2484 const char appends_op = APPENDS;
2485 const char mark_op = MARK;
2486
2487 assert(obj != NULL);
2488 assert(self->proto > 0);
2489 assert(PyList_CheckExact(obj));
2490
2491 if (PyList_GET_SIZE(obj) == 1) {
2492 item = PyList_GET_ITEM(obj, 0);
2493 if (save(self, item, 0) < 0)
2494 return -1;
2495 if (_Pickler_Write(self, &append_op, 1) < 0)
2496 return -1;
2497 return 0;
2498 }
2499
2500 /* Write in batches of BATCHSIZE. */
2501 total = 0;
2502 do {
2503 this_batch = 0;
2504 if (_Pickler_Write(self, &mark_op, 1) < 0)
2505 return -1;
2506 while (total < PyList_GET_SIZE(obj)) {
2507 item = PyList_GET_ITEM(obj, total);
2508 if (save(self, item, 0) < 0)
2509 return -1;
2510 total++;
2511 if (++this_batch == BATCHSIZE)
2512 break;
2513 }
2514 if (_Pickler_Write(self, &appends_op, 1) < 0)
2515 return -1;
2516
2517 } while (total < PyList_GET_SIZE(obj));
2518
2519 return 0;
2520}
2521
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002522static int
2523save_list(PicklerObject *self, PyObject *obj)
2524{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002525 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002526 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002527 int status = 0;
2528
2529 if (self->fast && !fast_save_enter(self, obj))
2530 goto error;
2531
2532 /* Create an empty list. */
2533 if (self->bin) {
2534 header[0] = EMPTY_LIST;
2535 len = 1;
2536 }
2537 else {
2538 header[0] = MARK;
2539 header[1] = LIST;
2540 len = 2;
2541 }
2542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002543 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002544 goto error;
2545
2546 /* Get list length, and bow out early if empty. */
2547 if ((len = PyList_Size(obj)) < 0)
2548 goto error;
2549
2550 if (memo_put(self, obj) < 0)
2551 goto error;
2552
2553 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002554 /* Materialize the list elements. */
2555 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002556 if (Py_EnterRecursiveCall(" while pickling an object"))
2557 goto error;
2558 status = batch_list_exact(self, obj);
2559 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002560 } else {
2561 PyObject *iter = PyObject_GetIter(obj);
2562 if (iter == NULL)
2563 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002564
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002565 if (Py_EnterRecursiveCall(" while pickling an object")) {
2566 Py_DECREF(iter);
2567 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002568 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002569 status = batch_list(self, iter);
2570 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002571 Py_DECREF(iter);
2572 }
2573 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 if (0) {
2575 error:
2576 status = -1;
2577 }
2578
2579 if (self->fast && !fast_save_leave(self, obj))
2580 status = -1;
2581
2582 return status;
2583}
2584
2585/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2586 * MARK key value ... key value SETITEMS
2587 * opcode sequences. Calling code should have arranged to first create an
2588 * empty dict, or dict-like object, for the SETITEMS to operate on.
2589 * Returns 0 on success, <0 on error.
2590 *
2591 * This is very much like batch_list(). The difference between saving
2592 * elements directly, and picking apart two-tuples, is so long-winded at
2593 * the C level, though, that attempts to combine these routines were too
2594 * ugly to bear.
2595 */
2596static int
2597batch_dict(PicklerObject *self, PyObject *iter)
2598{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002599 PyObject *obj = NULL;
2600 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002601 int i, n;
2602
2603 const char mark_op = MARK;
2604 const char setitem_op = SETITEM;
2605 const char setitems_op = SETITEMS;
2606
2607 assert(iter != NULL);
2608
2609 if (self->proto == 0) {
2610 /* SETITEMS isn't available; do one at a time. */
2611 for (;;) {
2612 obj = PyIter_Next(iter);
2613 if (obj == NULL) {
2614 if (PyErr_Occurred())
2615 return -1;
2616 break;
2617 }
2618 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2619 PyErr_SetString(PyExc_TypeError, "dict items "
2620 "iterator must return 2-tuples");
2621 return -1;
2622 }
2623 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2624 if (i >= 0)
2625 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2626 Py_DECREF(obj);
2627 if (i < 0)
2628 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002629 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002630 return -1;
2631 }
2632 return 0;
2633 }
2634
2635 /* proto > 0: write in batches of BATCHSIZE. */
2636 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002637 /* Get first item */
2638 firstitem = PyIter_Next(iter);
2639 if (firstitem == NULL) {
2640 if (PyErr_Occurred())
2641 goto error;
2642
2643 /* nothing more to add */
2644 break;
2645 }
2646 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2647 PyErr_SetString(PyExc_TypeError, "dict items "
2648 "iterator must return 2-tuples");
2649 goto error;
2650 }
2651
2652 /* Try to get a second item */
2653 obj = PyIter_Next(iter);
2654 if (obj == NULL) {
2655 if (PyErr_Occurred())
2656 goto error;
2657
2658 /* Only one item to write */
2659 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2660 goto error;
2661 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2662 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002663 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002664 goto error;
2665 Py_CLEAR(firstitem);
2666 break;
2667 }
2668
2669 /* More than one item to write */
2670
2671 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002672 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002673 goto error;
2674
2675 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2676 goto error;
2677 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2678 goto error;
2679 Py_CLEAR(firstitem);
2680 n = 1;
2681
2682 /* Fetch and save up to BATCHSIZE items */
2683 while (obj) {
2684 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2685 PyErr_SetString(PyExc_TypeError, "dict items "
2686 "iterator must return 2-tuples");
2687 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002688 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002689 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2690 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2691 goto error;
2692 Py_CLEAR(obj);
2693 n += 1;
2694
2695 if (n == BATCHSIZE)
2696 break;
2697
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002698 obj = PyIter_Next(iter);
2699 if (obj == NULL) {
2700 if (PyErr_Occurred())
2701 goto error;
2702 break;
2703 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002704 }
2705
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002706 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002707 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002708
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002709 } while (n == BATCHSIZE);
2710 return 0;
2711
2712 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002713 Py_XDECREF(firstitem);
2714 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002715 return -1;
2716}
2717
Collin Winter5c9b02d2009-05-25 05:43:30 +00002718/* This is a variant of batch_dict() above that specializes for dicts, with no
2719 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2720 * MARK key value ... key value SETITEMS
2721 * opcode sequences. Calling code should have arranged to first create an
2722 * empty dict, or dict-like object, for the SETITEMS to operate on.
2723 * Returns 0 on success, -1 on error.
2724 *
2725 * Note that this currently doesn't work for protocol 0.
2726 */
2727static int
2728batch_dict_exact(PicklerObject *self, PyObject *obj)
2729{
2730 PyObject *key = NULL, *value = NULL;
2731 int i;
2732 Py_ssize_t dict_size, ppos = 0;
2733
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002734 const char mark_op = MARK;
2735 const char setitem_op = SETITEM;
2736 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002737
2738 assert(obj != NULL);
2739 assert(self->proto > 0);
2740
2741 dict_size = PyDict_Size(obj);
2742
2743 /* Special-case len(d) == 1 to save space. */
2744 if (dict_size == 1) {
2745 PyDict_Next(obj, &ppos, &key, &value);
2746 if (save(self, key, 0) < 0)
2747 return -1;
2748 if (save(self, value, 0) < 0)
2749 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002750 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002751 return -1;
2752 return 0;
2753 }
2754
2755 /* Write in batches of BATCHSIZE. */
2756 do {
2757 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002758 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002759 return -1;
2760 while (PyDict_Next(obj, &ppos, &key, &value)) {
2761 if (save(self, key, 0) < 0)
2762 return -1;
2763 if (save(self, value, 0) < 0)
2764 return -1;
2765 if (++i == BATCHSIZE)
2766 break;
2767 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002768 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002769 return -1;
2770 if (PyDict_Size(obj) != dict_size) {
2771 PyErr_Format(
2772 PyExc_RuntimeError,
2773 "dictionary changed size during iteration");
2774 return -1;
2775 }
2776
2777 } while (i == BATCHSIZE);
2778 return 0;
2779}
2780
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002781static int
2782save_dict(PicklerObject *self, PyObject *obj)
2783{
2784 PyObject *items, *iter;
2785 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002786 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002787 int status = 0;
2788
2789 if (self->fast && !fast_save_enter(self, obj))
2790 goto error;
2791
2792 /* Create an empty dict. */
2793 if (self->bin) {
2794 header[0] = EMPTY_DICT;
2795 len = 1;
2796 }
2797 else {
2798 header[0] = MARK;
2799 header[1] = DICT;
2800 len = 2;
2801 }
2802
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002803 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002804 goto error;
2805
2806 /* Get dict size, and bow out early if empty. */
2807 if ((len = PyDict_Size(obj)) < 0)
2808 goto error;
2809
2810 if (memo_put(self, obj) < 0)
2811 goto error;
2812
2813 if (len != 0) {
2814 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002815 if (PyDict_CheckExact(obj) && self->proto > 0) {
2816 /* We can take certain shortcuts if we know this is a dict and
2817 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002818 if (Py_EnterRecursiveCall(" while pickling an object"))
2819 goto error;
2820 status = batch_dict_exact(self, obj);
2821 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002822 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002823 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002824
2825 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002826 if (items == NULL)
2827 goto error;
2828 iter = PyObject_GetIter(items);
2829 Py_DECREF(items);
2830 if (iter == NULL)
2831 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002832 if (Py_EnterRecursiveCall(" while pickling an object")) {
2833 Py_DECREF(iter);
2834 goto error;
2835 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002836 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002837 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002838 Py_DECREF(iter);
2839 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002840 }
2841
2842 if (0) {
2843 error:
2844 status = -1;
2845 }
2846
2847 if (self->fast && !fast_save_leave(self, obj))
2848 status = -1;
2849
2850 return status;
2851}
2852
2853static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002854save_set(PicklerObject *self, PyObject *obj)
2855{
2856 PyObject *item;
2857 int i;
2858 Py_ssize_t set_size, ppos = 0;
2859 Py_hash_t hash;
2860
2861 const char empty_set_op = EMPTY_SET;
2862 const char mark_op = MARK;
2863 const char additems_op = ADDITEMS;
2864
2865 if (self->proto < 4) {
2866 PyObject *items;
2867 PyObject *reduce_value;
2868 int status;
2869
2870 items = PySequence_List(obj);
2871 if (items == NULL) {
2872 return -1;
2873 }
2874 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2875 Py_DECREF(items);
2876 if (reduce_value == NULL) {
2877 return -1;
2878 }
2879 /* save_reduce() will memoize the object automatically. */
2880 status = save_reduce(self, reduce_value, obj);
2881 Py_DECREF(reduce_value);
2882 return status;
2883 }
2884
2885 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2886 return -1;
2887
2888 if (memo_put(self, obj) < 0)
2889 return -1;
2890
2891 set_size = PySet_GET_SIZE(obj);
2892 if (set_size == 0)
2893 return 0; /* nothing to do */
2894
2895 /* Write in batches of BATCHSIZE. */
2896 do {
2897 i = 0;
2898 if (_Pickler_Write(self, &mark_op, 1) < 0)
2899 return -1;
2900 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2901 if (save(self, item, 0) < 0)
2902 return -1;
2903 if (++i == BATCHSIZE)
2904 break;
2905 }
2906 if (_Pickler_Write(self, &additems_op, 1) < 0)
2907 return -1;
2908 if (PySet_GET_SIZE(obj) != set_size) {
2909 PyErr_Format(
2910 PyExc_RuntimeError,
2911 "set changed size during iteration");
2912 return -1;
2913 }
2914 } while (i == BATCHSIZE);
2915
2916 return 0;
2917}
2918
2919static int
2920save_frozenset(PicklerObject *self, PyObject *obj)
2921{
2922 PyObject *iter;
2923
2924 const char mark_op = MARK;
2925 const char frozenset_op = FROZENSET;
2926
2927 if (self->fast && !fast_save_enter(self, obj))
2928 return -1;
2929
2930 if (self->proto < 4) {
2931 PyObject *items;
2932 PyObject *reduce_value;
2933 int status;
2934
2935 items = PySequence_List(obj);
2936 if (items == NULL) {
2937 return -1;
2938 }
2939 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2940 items);
2941 Py_DECREF(items);
2942 if (reduce_value == NULL) {
2943 return -1;
2944 }
2945 /* save_reduce() will memoize the object automatically. */
2946 status = save_reduce(self, reduce_value, obj);
2947 Py_DECREF(reduce_value);
2948 return status;
2949 }
2950
2951 if (_Pickler_Write(self, &mark_op, 1) < 0)
2952 return -1;
2953
2954 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002955 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002956 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002957 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002958 for (;;) {
2959 PyObject *item;
2960
2961 item = PyIter_Next(iter);
2962 if (item == NULL) {
2963 if (PyErr_Occurred()) {
2964 Py_DECREF(iter);
2965 return -1;
2966 }
2967 break;
2968 }
2969 if (save(self, item, 0) < 0) {
2970 Py_DECREF(item);
2971 Py_DECREF(iter);
2972 return -1;
2973 }
2974 Py_DECREF(item);
2975 }
2976 Py_DECREF(iter);
2977
2978 /* If the object is already in the memo, this means it is
2979 recursive. In this case, throw away everything we put on the
2980 stack, and fetch the object back from the memo. */
2981 if (PyMemoTable_Get(self->memo, obj)) {
2982 const char pop_mark_op = POP_MARK;
2983
2984 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2985 return -1;
2986 if (memo_get(self, obj) < 0)
2987 return -1;
2988 return 0;
2989 }
2990
2991 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
2992 return -1;
2993 if (memo_put(self, obj) < 0)
2994 return -1;
2995
2996 return 0;
2997}
2998
2999static int
3000fix_imports(PyObject **module_name, PyObject **global_name)
3001{
3002 PyObject *key;
3003 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003004 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003005
3006 key = PyTuple_Pack(2, *module_name, *global_name);
3007 if (key == NULL)
3008 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003009 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003010 Py_DECREF(key);
3011 if (item) {
3012 PyObject *fixed_module_name;
3013 PyObject *fixed_global_name;
3014
3015 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3016 PyErr_Format(PyExc_RuntimeError,
3017 "_compat_pickle.REVERSE_NAME_MAPPING values "
3018 "should be 2-tuples, not %.200s",
3019 Py_TYPE(item)->tp_name);
3020 return -1;
3021 }
3022 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3023 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3024 if (!PyUnicode_Check(fixed_module_name) ||
3025 !PyUnicode_Check(fixed_global_name)) {
3026 PyErr_Format(PyExc_RuntimeError,
3027 "_compat_pickle.REVERSE_NAME_MAPPING values "
3028 "should be pairs of str, not (%.200s, %.200s)",
3029 Py_TYPE(fixed_module_name)->tp_name,
3030 Py_TYPE(fixed_global_name)->tp_name);
3031 return -1;
3032 }
3033
3034 Py_CLEAR(*module_name);
3035 Py_CLEAR(*global_name);
3036 Py_INCREF(fixed_module_name);
3037 Py_INCREF(fixed_global_name);
3038 *module_name = fixed_module_name;
3039 *global_name = fixed_global_name;
3040 }
3041 else if (PyErr_Occurred()) {
3042 return -1;
3043 }
3044
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003045 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003046 if (item) {
3047 if (!PyUnicode_Check(item)) {
3048 PyErr_Format(PyExc_RuntimeError,
3049 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3050 "should be strings, not %.200s",
3051 Py_TYPE(item)->tp_name);
3052 return -1;
3053 }
3054 Py_CLEAR(*module_name);
3055 Py_INCREF(item);
3056 *module_name = item;
3057 }
3058 else if (PyErr_Occurred()) {
3059 return -1;
3060 }
3061
3062 return 0;
3063}
3064
3065static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003066save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3067{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003068 PyObject *global_name = NULL;
3069 PyObject *module_name = NULL;
3070 PyObject *module = NULL;
3071 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003072 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003073 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003074 _Py_IDENTIFIER(__name__);
3075 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003076
3077 const char global_op = GLOBAL;
3078
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003079 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003080 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003081 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003082 }
3083 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003084 if (self->proto >= 4) {
3085 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3086 if (global_name == NULL) {
3087 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3088 goto error;
3089 PyErr_Clear();
3090 }
3091 }
3092 if (global_name == NULL) {
3093 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3094 if (global_name == NULL)
3095 goto error;
3096 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003097 }
3098
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003099 module_name = whichmodule(obj, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003100 if (module_name == NULL)
3101 goto error;
3102
3103 /* XXX: Change to use the import C API directly with level=0 to disallow
3104 relative imports.
3105
3106 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3107 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3108 custom import functions (IMHO, this would be a nice security
3109 feature). The import C API would need to be extended to support the
3110 extra parameters of __import__ to fix that. */
3111 module = PyImport_Import(module_name);
3112 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003113 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003114 "Can't pickle %R: import of module %R failed",
3115 obj, module_name);
3116 goto error;
3117 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003118 cls = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003120 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003121 "Can't pickle %R: attribute lookup %S on %S failed",
3122 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003123 goto error;
3124 }
3125 if (cls != obj) {
3126 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003127 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003128 "Can't pickle %R: it's not the same object as %S.%S",
3129 obj, module_name, global_name);
3130 goto error;
3131 }
3132 Py_DECREF(cls);
3133
3134 if (self->proto >= 2) {
3135 /* See whether this is in the extension registry, and if
3136 * so generate an EXT opcode.
3137 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003138 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 PyObject *code_obj; /* extension code as Python object */
3140 long code; /* extension code as C value */
3141 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003142 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003143
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003144 extension_key = PyTuple_Pack(2, module_name, global_name);
3145 if (extension_key == NULL) {
3146 goto error;
3147 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003148 code_obj = PyDict_GetItemWithError(st->extension_registry,
3149 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003150 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003151 /* The object is not registered in the extension registry.
3152 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003153 if (code_obj == NULL) {
3154 if (PyErr_Occurred()) {
3155 goto error;
3156 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003157 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003158 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003159
3160 /* XXX: pickle.py doesn't check neither the type, nor the range
3161 of the value returned by the extension_registry. It should for
3162 consistency. */
3163
3164 /* Verify code_obj has the right type and value. */
3165 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003166 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003167 "Can't pickle %R: extension code %R isn't an integer",
3168 obj, code_obj);
3169 goto error;
3170 }
3171 code = PyLong_AS_LONG(code_obj);
3172 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003173 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003174 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3175 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003176 goto error;
3177 }
3178
3179 /* Generate an EXT opcode. */
3180 if (code <= 0xff) {
3181 pdata[0] = EXT1;
3182 pdata[1] = (unsigned char)code;
3183 n = 2;
3184 }
3185 else if (code <= 0xffff) {
3186 pdata[0] = EXT2;
3187 pdata[1] = (unsigned char)(code & 0xff);
3188 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3189 n = 3;
3190 }
3191 else {
3192 pdata[0] = EXT4;
3193 pdata[1] = (unsigned char)(code & 0xff);
3194 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3195 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3196 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3197 n = 5;
3198 }
3199
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003200 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201 goto error;
3202 }
3203 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003204 gen_global:
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003205 if (self->proto >= 4) {
3206 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003207
Christian Heimese8b1ba12013-11-23 21:13:39 +01003208 if (save(self, module_name, 0) < 0)
3209 goto error;
3210 if (save(self, global_name, 0) < 0)
3211 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003212
3213 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3214 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215 }
3216 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003217 /* Generate a normal global opcode if we are using a pickle
3218 protocol < 4, or if the object is not registered in the
3219 extension registry. */
3220 PyObject *encoded;
3221 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003222
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003223 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003224 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003225
3226 /* For protocol < 3 and if the user didn't request against doing
3227 so, we convert module names to the old 2.x module names. */
3228 if (self->proto < 3 && self->fix_imports) {
3229 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003230 goto error;
3231 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003232 }
3233
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003234 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3235 both the module name and the global name using UTF-8. We do so
3236 only when we are using the pickle protocol newer than version
3237 3. This is to ensure compatibility with older Unpickler running
3238 on Python 2.x. */
3239 if (self->proto == 3) {
3240 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003241 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003242 else {
3243 unicode_encoder = PyUnicode_AsASCIIString;
3244 }
3245 encoded = unicode_encoder(module_name);
3246 if (encoded == NULL) {
3247 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003248 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003249 "can't pickle module identifier '%S' using "
3250 "pickle protocol %i",
3251 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003252 goto error;
3253 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003254 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3255 PyBytes_GET_SIZE(encoded)) < 0) {
3256 Py_DECREF(encoded);
3257 goto error;
3258 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003259 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003260 if(_Pickler_Write(self, "\n", 1) < 0)
3261 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003262
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003263 /* Save the name of the module. */
3264 encoded = unicode_encoder(global_name);
3265 if (encoded == NULL) {
3266 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003267 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268 "can't pickle global identifier '%S' using "
3269 "pickle protocol %i",
3270 global_name, self->proto);
3271 goto error;
3272 }
3273 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3274 PyBytes_GET_SIZE(encoded)) < 0) {
3275 Py_DECREF(encoded);
3276 goto error;
3277 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 if (_Pickler_Write(self, "\n", 1) < 0)
3280 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003281 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003282 /* Memoize the object. */
3283 if (memo_put(self, obj) < 0)
3284 goto error;
3285 }
3286
3287 if (0) {
3288 error:
3289 status = -1;
3290 }
3291 Py_XDECREF(module_name);
3292 Py_XDECREF(global_name);
3293 Py_XDECREF(module);
3294
3295 return status;
3296}
3297
3298static int
3299save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3300{
3301 PyObject *pid = NULL;
3302 int status = 0;
3303
3304 const char persid_op = PERSID;
3305 const char binpersid_op = BINPERSID;
3306
3307 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003308 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003309 if (pid == NULL)
3310 return -1;
3311
3312 if (pid != Py_None) {
3313 if (self->bin) {
3314 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003315 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003316 goto error;
3317 }
3318 else {
3319 PyObject *pid_str = NULL;
3320 char *pid_ascii_bytes;
3321 Py_ssize_t size;
3322
3323 pid_str = PyObject_Str(pid);
3324 if (pid_str == NULL)
3325 goto error;
3326
3327 /* XXX: Should it check whether the persistent id only contains
3328 ASCII characters? And what if the pid contains embedded
3329 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003330 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003331 Py_DECREF(pid_str);
3332 if (pid_ascii_bytes == NULL)
3333 goto error;
3334
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003335 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3336 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3337 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338 goto error;
3339 }
3340 status = 1;
3341 }
3342
3343 if (0) {
3344 error:
3345 status = -1;
3346 }
3347 Py_XDECREF(pid);
3348
3349 return status;
3350}
3351
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003352static PyObject *
3353get_class(PyObject *obj)
3354{
3355 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003356 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003357
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003358 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003359 if (cls == NULL) {
3360 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3361 PyErr_Clear();
3362 cls = (PyObject *) Py_TYPE(obj);
3363 Py_INCREF(cls);
3364 }
3365 }
3366 return cls;
3367}
3368
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3370 * appropriate __reduce__ method for obj.
3371 */
3372static int
3373save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3374{
3375 PyObject *callable;
3376 PyObject *argtup;
3377 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003378 PyObject *listitems = Py_None;
3379 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003380 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003381 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003382 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003383
3384 const char reduce_op = REDUCE;
3385 const char build_op = BUILD;
3386 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003387 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003389 size = PyTuple_Size(args);
3390 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003391 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003392 "__reduce__ must contain 2 through 5 elements");
3393 return -1;
3394 }
3395
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003396 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3397 &callable, &argtup, &state, &listitems, &dictitems))
3398 return -1;
3399
3400 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003401 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003402 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003403 return -1;
3404 }
3405 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003406 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003407 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408 return -1;
3409 }
3410
3411 if (state == Py_None)
3412 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003413
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003414 if (listitems == Py_None)
3415 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003416 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003417 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003418 "returned by __reduce__ must be an iterator, not %s",
3419 Py_TYPE(listitems)->tp_name);
3420 return -1;
3421 }
3422
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003423 if (dictitems == Py_None)
3424 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003425 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003426 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003427 "returned by __reduce__ must be an iterator, not %s",
3428 Py_TYPE(dictitems)->tp_name);
3429 return -1;
3430 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003431
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003432 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003433 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003434 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003435
Victor Stinner804e05e2013-11-14 01:26:17 +01003436 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003437 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003438 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003439 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003440 }
3441 PyErr_Clear();
3442 }
3443 else if (self->proto >= 4) {
3444 _Py_IDENTIFIER(__newobj_ex__);
3445 use_newobj_ex = PyUnicode_Check(name) &&
3446 PyUnicode_Compare(
3447 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3448 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003449 }
3450 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003451 _Py_IDENTIFIER(__newobj__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003452 use_newobj = PyUnicode_Check(name) &&
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003453 PyUnicode_Compare(
3454 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003455 Py_DECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003456 }
3457 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003458
3459 if (use_newobj_ex) {
3460 PyObject *cls;
3461 PyObject *args;
3462 PyObject *kwargs;
3463
3464 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003465 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 "length of the NEWOBJ_EX argument tuple must be "
3467 "exactly 3, not %zd", Py_SIZE(argtup));
3468 return -1;
3469 }
3470
3471 cls = PyTuple_GET_ITEM(argtup, 0);
3472 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003473 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003474 "first item from NEWOBJ_EX argument tuple must "
3475 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3476 return -1;
3477 }
3478 args = PyTuple_GET_ITEM(argtup, 1);
3479 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003480 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003481 "second item from NEWOBJ_EX argument tuple must "
3482 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3483 return -1;
3484 }
3485 kwargs = PyTuple_GET_ITEM(argtup, 2);
3486 if (!PyDict_Check(kwargs)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003487 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003488 "third item from NEWOBJ_EX argument tuple must "
3489 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3490 return -1;
3491 }
3492
3493 if (save(self, cls, 0) < 0 ||
3494 save(self, args, 0) < 0 ||
3495 save(self, kwargs, 0) < 0 ||
3496 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3497 return -1;
3498 }
3499 }
3500 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003501 PyObject *cls;
3502 PyObject *newargtup;
3503 PyObject *obj_class;
3504 int p;
3505
3506 /* Sanity checks. */
3507 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003508 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003509 return -1;
3510 }
3511
3512 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003513 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003514 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003515 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516 return -1;
3517 }
3518
3519 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003520 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003521 p = obj_class != cls; /* true iff a problem */
3522 Py_DECREF(obj_class);
3523 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003524 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 "__newobj__ args has the wrong class");
3526 return -1;
3527 }
3528 }
3529 /* XXX: These calls save() are prone to infinite recursion. Imagine
3530 what happen if the value returned by the __reduce__() method of
3531 some extension type contains another object of the same type. Ouch!
3532
3533 Here is a quick example, that I ran into, to illustrate what I
3534 mean:
3535
3536 >>> import pickle, copyreg
3537 >>> copyreg.dispatch_table.pop(complex)
3538 >>> pickle.dumps(1+2j)
3539 Traceback (most recent call last):
3540 ...
3541 RuntimeError: maximum recursion depth exceeded
3542
3543 Removing the complex class from copyreg.dispatch_table made the
3544 __reduce_ex__() method emit another complex object:
3545
3546 >>> (1+1j).__reduce_ex__(2)
3547 (<function __newobj__ at 0xb7b71c3c>,
3548 (<class 'complex'>, (1+1j)), None, None, None)
3549
3550 Thus when save() was called on newargstup (the 2nd item) recursion
3551 ensued. Of course, the bug was in the complex class which had a
3552 broken __getnewargs__() that emitted another complex object. But,
3553 the point, here, is it is quite easy to end up with a broken reduce
3554 function. */
3555
3556 /* Save the class and its __new__ arguments. */
3557 if (save(self, cls, 0) < 0)
3558 return -1;
3559
3560 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3561 if (newargtup == NULL)
3562 return -1;
3563
3564 p = save(self, newargtup, 0);
3565 Py_DECREF(newargtup);
3566 if (p < 0)
3567 return -1;
3568
3569 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003570 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003571 return -1;
3572 }
3573 else { /* Not using NEWOBJ. */
3574 if (save(self, callable, 0) < 0 ||
3575 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003576 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003577 return -1;
3578 }
3579
3580 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3581 the caller do not want to memoize the object. Not particularly useful,
3582 but that is to mimic the behavior save_reduce() in pickle.py when
3583 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003584 if (obj != NULL) {
3585 /* If the object is already in the memo, this means it is
3586 recursive. In this case, throw away everything we put on the
3587 stack, and fetch the object back from the memo. */
3588 if (PyMemoTable_Get(self->memo, obj)) {
3589 const char pop_op = POP;
3590
3591 if (_Pickler_Write(self, &pop_op, 1) < 0)
3592 return -1;
3593 if (memo_get(self, obj) < 0)
3594 return -1;
3595
3596 return 0;
3597 }
3598 else if (memo_put(self, obj) < 0)
3599 return -1;
3600 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003601
3602 if (listitems && batch_list(self, listitems) < 0)
3603 return -1;
3604
3605 if (dictitems && batch_dict(self, dictitems) < 0)
3606 return -1;
3607
3608 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003609 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003610 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003611 return -1;
3612 }
3613
3614 return 0;
3615}
3616
3617static int
3618save(PicklerObject *self, PyObject *obj, int pers_save)
3619{
3620 PyTypeObject *type;
3621 PyObject *reduce_func = NULL;
3622 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003623 int status = 0;
3624
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003625 if (_Pickler_OpcodeBoundary(self) < 0)
3626 return -1;
3627
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003628 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003629 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003630
3631 /* The extra pers_save argument is necessary to avoid calling save_pers()
3632 on its returned object. */
3633 if (!pers_save && self->pers_func) {
3634 /* save_pers() returns:
3635 -1 to signal an error;
3636 0 if it did nothing successfully;
3637 1 if a persistent id was saved.
3638 */
3639 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3640 goto done;
3641 }
3642
3643 type = Py_TYPE(obj);
3644
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003645 /* The old cPickle had an optimization that used switch-case statement
3646 dispatching on the first letter of the type name. This has was removed
3647 since benchmarks shown that this optimization was actually slowing
3648 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003649
3650 /* Atom types; these aren't memoized, so don't check the memo. */
3651
3652 if (obj == Py_None) {
3653 status = save_none(self, obj);
3654 goto done;
3655 }
3656 else if (obj == Py_False || obj == Py_True) {
3657 status = save_bool(self, obj);
3658 goto done;
3659 }
3660 else if (type == &PyLong_Type) {
3661 status = save_long(self, obj);
3662 goto done;
3663 }
3664 else if (type == &PyFloat_Type) {
3665 status = save_float(self, obj);
3666 goto done;
3667 }
3668
3669 /* Check the memo to see if it has the object. If so, generate
3670 a GET (or BINGET) opcode, instead of pickling the object
3671 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003672 if (PyMemoTable_Get(self->memo, obj)) {
3673 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003674 goto error;
3675 goto done;
3676 }
3677
3678 if (type == &PyBytes_Type) {
3679 status = save_bytes(self, obj);
3680 goto done;
3681 }
3682 else if (type == &PyUnicode_Type) {
3683 status = save_unicode(self, obj);
3684 goto done;
3685 }
3686 else if (type == &PyDict_Type) {
3687 status = save_dict(self, obj);
3688 goto done;
3689 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003690 else if (type == &PySet_Type) {
3691 status = save_set(self, obj);
3692 goto done;
3693 }
3694 else if (type == &PyFrozenSet_Type) {
3695 status = save_frozenset(self, obj);
3696 goto done;
3697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003698 else if (type == &PyList_Type) {
3699 status = save_list(self, obj);
3700 goto done;
3701 }
3702 else if (type == &PyTuple_Type) {
3703 status = save_tuple(self, obj);
3704 goto done;
3705 }
3706 else if (type == &PyType_Type) {
3707 status = save_global(self, obj, NULL);
3708 goto done;
3709 }
3710 else if (type == &PyFunction_Type) {
3711 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003712 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003713 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714
3715 /* XXX: This part needs some unit tests. */
3716
3717 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003718 * self.dispatch_table, copyreg.dispatch_table, the object's
3719 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003720 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003721 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003722 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003723 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3724 (PyObject *)type);
3725 if (reduce_func == NULL) {
3726 if (PyErr_Occurred()) {
3727 goto error;
3728 }
3729 } else {
3730 /* PyDict_GetItemWithError() returns a borrowed reference.
3731 Increase the reference count to be consistent with
3732 PyObject_GetItem and _PyObject_GetAttrId used below. */
3733 Py_INCREF(reduce_func);
3734 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003735 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003736 reduce_func = PyObject_GetItem(self->dispatch_table,
3737 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003738 if (reduce_func == NULL) {
3739 if (PyErr_ExceptionMatches(PyExc_KeyError))
3740 PyErr_Clear();
3741 else
3742 goto error;
3743 }
3744 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003745 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003746 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003747 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003748 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003749 else if (PyType_IsSubtype(type, &PyType_Type)) {
3750 status = save_global(self, obj, NULL);
3751 goto done;
3752 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003753 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003754 _Py_IDENTIFIER(__reduce__);
3755 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003756
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003757
3758 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3759 automatically defined as __reduce__. While this is convenient, this
3760 make it impossible to know which method was actually called. Of
3761 course, this is not a big deal. But still, it would be nice to let
3762 the user know which method was called when something go
3763 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3764 don't actually have to check for a __reduce__ method. */
3765
3766 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003767 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003768 if (reduce_func != NULL) {
3769 PyObject *proto;
3770 proto = PyLong_FromLong(self->proto);
3771 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003772 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003773 }
3774 }
3775 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003776 PickleState *st = _Pickle_GetGlobalState();
3777
3778 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003779 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003780 }
3781 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003782 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003783 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003784 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003785 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003786 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003787 PyObject *empty_tuple = PyTuple_New(0);
3788 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003789 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003790 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003791 }
3792 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003793 PyErr_Format(st->PicklingError,
3794 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003795 type->tp_name, obj);
3796 goto error;
3797 }
3798 }
3799 }
3800
3801 if (reduce_value == NULL)
3802 goto error;
3803
3804 if (PyUnicode_Check(reduce_value)) {
3805 status = save_global(self, obj, reduce_value);
3806 goto done;
3807 }
3808
3809 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003810 PickleState *st = _Pickle_GetGlobalState();
3811 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003812 "__reduce__ must return a string or tuple");
3813 goto error;
3814 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003815
3816 status = save_reduce(self, reduce_value, obj);
3817
3818 if (0) {
3819 error:
3820 status = -1;
3821 }
3822 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003823
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003824 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003825 Py_XDECREF(reduce_func);
3826 Py_XDECREF(reduce_value);
3827
3828 return status;
3829}
3830
3831static int
3832dump(PicklerObject *self, PyObject *obj)
3833{
3834 const char stop_op = STOP;
3835
3836 if (self->proto >= 2) {
3837 char header[2];
3838
3839 header[0] = PROTO;
3840 assert(self->proto >= 0 && self->proto < 256);
3841 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003842 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003843 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003844 if (self->proto >= 4)
3845 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003846 }
3847
3848 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003849 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003850 return -1;
3851
3852 return 0;
3853}
3854
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003855/*[clinic]
3856
3857_pickle.Pickler.clear_memo
3858
3859 self: PicklerObject
3860
3861Clears the pickler's "memo".
3862
3863The memo is the data structure that remembers which objects the
3864pickler has already seen, so that shared or recursive objects are
3865pickled by reference and not by value. This method is useful when
3866re-using picklers.
3867[clinic]*/
3868
3869PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__,
3870"clear_memo()\n"
3871"Clears the pickler\'s \"memo\".\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872"\n"
3873"The memo is the data structure that remembers which objects the\n"
3874"pickler has already seen, so that shared or recursive objects are\n"
3875"pickled by reference and not by value. This method is useful when\n"
3876"re-using picklers.");
3877
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003878#define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF \
3879 {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__},
3880
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003881static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003882_pickle_Pickler_clear_memo(PicklerObject *self)
3883/*[clinic checksum: 9c32be7e7a17ff82a81aae409d0d4f469033a5b2]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003884{
3885 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003886 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003887
3888 Py_RETURN_NONE;
3889}
3890
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003891/*[clinic]
3892
3893_pickle.Pickler.dump
3894
3895 self: PicklerObject
3896 obj: object
3897 /
3898
3899Write a pickled representation of the given object to the open file.
3900[clinic]*/
3901
3902PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
3903"dump(obj)\n"
3904"Write a pickled representation of the given object to the open file.");
3905
3906#define _PICKLE_PICKLER_DUMP_METHODDEF \
3907 {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003908
3909static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003910_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
3911/*[clinic checksum: b72a69ec98737fabf66dae7c5a3210178bdbd3e6]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003912{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003913 /* Check whether the Pickler was initialized correctly (issue3664).
3914 Developers often forget to call __init__() in their subclasses, which
3915 would trigger a segfault without this check. */
3916 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003917 PickleState *st = _Pickle_GetGlobalState();
3918 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003919 "Pickler.__init__() was not called by %s.__init__()",
3920 Py_TYPE(self)->tp_name);
3921 return NULL;
3922 }
3923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003924 if (_Pickler_ClearBuffer(self) < 0)
3925 return NULL;
3926
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 if (dump(self, obj) < 0)
3928 return NULL;
3929
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003930 if (_Pickler_FlushToFile(self) < 0)
3931 return NULL;
3932
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 Py_RETURN_NONE;
3934}
3935
3936static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003937 _PICKLE_PICKLER_DUMP_METHODDEF
3938 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 {NULL, NULL} /* sentinel */
3940};
3941
3942static void
3943Pickler_dealloc(PicklerObject *self)
3944{
3945 PyObject_GC_UnTrack(self);
3946
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003947 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003950 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003951 Py_XDECREF(self->fast_memo);
3952
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003953 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003954
3955 Py_TYPE(self)->tp_free((PyObject *)self);
3956}
3957
3958static int
3959Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
3960{
3961 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003962 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003963 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964 Py_VISIT(self->fast_memo);
3965 return 0;
3966}
3967
3968static int
3969Pickler_clear(PicklerObject *self)
3970{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003971 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003972 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003974 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975 Py_CLEAR(self->fast_memo);
3976
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003977 if (self->memo != NULL) {
3978 PyMemoTable *memo = self->memo;
3979 self->memo = NULL;
3980 PyMemoTable_Del(memo);
3981 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003982 return 0;
3983}
3984
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003985
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003986/*[clinic]
3987
3988_pickle.Pickler.__init__
3989
3990 self: PicklerObject
3991 file: object
3992 protocol: object = NULL
3993 fix_imports: bool = True
3994
3995This takes a binary file for writing a pickle data stream.
3996
3997The optional protocol argument tells the pickler to use the
3998given protocol; supported protocols are 0, 1, 2, 3 and 4. The
3999default protocol is 3; a backward-incompatible protocol designed for
4000Python 3.
4001
4002Specifying a negative protocol version selects the highest
4003protocol version supported. The higher the protocol used, the
4004more recent the version of Python needed to read the pickle
4005produced.
4006
4007The file argument must have a write() method that accepts a single
4008bytes argument. It can thus be a file object opened for binary
4009writing, a io.BytesIO instance, or any other custom object that
4010meets this interface.
4011
4012If fix_imports is True and protocol is less than 3, pickle will try to
4013map the new Python 3 names to the old module names used in Python 2,
4014so that the pickle data stream is readable with Python 2.
4015[clinic]*/
4016
4017PyDoc_STRVAR(_pickle_Pickler___init____doc__,
4018"__init__(file, protocol=None, fix_imports=True)\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019"This takes a binary file for writing a pickle data stream.\n"
4020"\n"
4021"The optional protocol argument tells the pickler to use the\n"
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004022"given protocol; supported protocols are 0, 1, 2, 3 and 4. The\n"
4023"default protocol is 3; a backward-incompatible protocol designed for\n"
4024"Python 3.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025"\n"
4026"Specifying a negative protocol version selects the highest\n"
4027"protocol version supported. The higher the protocol used, the\n"
4028"more recent the version of Python needed to read the pickle\n"
4029"produced.\n"
4030"\n"
4031"The file argument must have a write() method that accepts a single\n"
4032"bytes argument. It can thus be a file object opened for binary\n"
4033"writing, a io.BytesIO instance, or any other custom object that\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004034"meets this interface.\n"
4035"\n"
4036"If fix_imports is True and protocol is less than 3, pickle will try to\n"
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004037"map the new Python 3 names to the old module names used in Python 2,\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004038"so that the pickle data stream is readable with Python 2.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004039
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004040#define _PICKLE_PICKLER___INIT___METHODDEF \
4041 {"__init__", (PyCFunction)_pickle_Pickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Pickler___init____doc__},
4042
4043static PyObject *
4044_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports);
4045
4046static PyObject *
4047_pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004048{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004049 PyObject *return_value = NULL;
4050 static char *_keywords[] = {"file", "protocol", "fix_imports", NULL};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004051 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004052 PyObject *protocol = NULL;
4053 int fix_imports = 1;
4054
4055 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
4056 "O|Op:__init__", _keywords,
4057 &file, &protocol, &fix_imports))
4058 goto exit;
4059 return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
4060
4061exit:
4062 return return_value;
4063}
4064
4065static PyObject *
4066_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
4067/*[clinic checksum: c99ff417bd703a74affc4b708167e56e135e8969]*/
4068{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004069 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004070 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004072 /* In case of multiple __init__() calls, clear previous content. */
4073 if (self->write != NULL)
4074 (void)Pickler_clear(self);
4075
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004076 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
4077 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004078
4079 if (_Pickler_SetOutputStream(self, file) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004080 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004081
4082 /* memo and output_buffer may have already been created in _Pickler_New */
4083 if (self->memo == NULL) {
4084 self->memo = PyMemoTable_New();
4085 if (self->memo == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004086 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004087 }
4088 self->output_len = 0;
4089 if (self->output_buffer == NULL) {
4090 self->max_output_len = WRITE_BUF_SIZE;
4091 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4092 self->max_output_len);
4093 if (self->output_buffer == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004094 return NULL;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004095 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004096
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004097 self->fast = 0;
4098 self->fast_nesting = 0;
4099 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004100 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004101 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4102 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4103 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 if (self->pers_func == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004105 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004107 self->dispatch_table = NULL;
4108 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4109 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4110 &PyId_dispatch_table);
4111 if (self->dispatch_table == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004112 return NULL;
4113 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004114
4115 Py_RETURN_NONE;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004116}
4117
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004118/* Wrap the Clinic generated signature to slot it in tp_init. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004119static int
4120Pickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
4121{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004122 PyObject *result = _pickle_Pickler___init__(self, args, kwargs);
4123 if (result == NULL) {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004124 return -1;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004125 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004126 Py_DECREF(result);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 return 0;
4128}
4129
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004130/* Define a proxy object for the Pickler's internal memo object. This is to
4131 * avoid breaking code like:
4132 * pickler.memo.clear()
4133 * and
4134 * pickler.memo = saved_memo
4135 * Is this a good idea? Not really, but we don't want to break code that uses
4136 * it. Note that we don't implement the entire mapping API here. This is
4137 * intentional, as these should be treated as black-box implementation details.
4138 */
4139
4140typedef struct {
4141 PyObject_HEAD
4142 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
4143} PicklerMemoProxyObject;
4144
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004145/*[clinic]
4146_pickle.PicklerMemoProxy.clear
4147
4148 self: PicklerMemoProxyObject
4149
4150Remove all items from memo.
4151[clinic]*/
4152
4153PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
4154"clear()\n"
4155"Remove all items from memo.");
4156
4157#define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \
4158 {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004159
4160static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004161_pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4162/*[clinic checksum: 507f13938721992e175a3e58b5ad02620045a1cc]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004163{
4164 if (self->pickler->memo)
4165 PyMemoTable_Clear(self->pickler->memo);
4166 Py_RETURN_NONE;
4167}
4168
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004169/*[clinic]
4170_pickle.PicklerMemoProxy.copy
4171
4172 self: PicklerMemoProxyObject
4173
4174Copy the memo to a new object.
4175[clinic]*/
4176
4177PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
4178"copy()\n"
4179"Copy the memo to a new object.");
4180
4181#define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \
4182 {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183
4184static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185_pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self)
4186/*[clinic checksum: 73a5117ab354290ebdbe07bd0bf7232d0936a69d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004187{
4188 Py_ssize_t i;
4189 PyMemoTable *memo;
4190 PyObject *new_memo = PyDict_New();
4191 if (new_memo == NULL)
4192 return NULL;
4193
4194 memo = self->pickler->memo;
4195 for (i = 0; i < memo->mt_allocated; ++i) {
4196 PyMemoEntry entry = memo->mt_table[i];
4197 if (entry.me_key != NULL) {
4198 int status;
4199 PyObject *key, *value;
4200
4201 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004202 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004203
4204 if (key == NULL || value == NULL) {
4205 Py_XDECREF(key);
4206 Py_XDECREF(value);
4207 goto error;
4208 }
4209 status = PyDict_SetItem(new_memo, key, value);
4210 Py_DECREF(key);
4211 Py_DECREF(value);
4212 if (status < 0)
4213 goto error;
4214 }
4215 }
4216 return new_memo;
4217
4218 error:
4219 Py_XDECREF(new_memo);
4220 return NULL;
4221}
4222
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004223/*[clinic]
4224_pickle.PicklerMemoProxy.__reduce__
4225
4226 self: PicklerMemoProxyObject
4227
4228Implement pickle support.
4229[clinic]*/
4230
4231PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
4232"__reduce__()\n"
4233"Implement pickle support.");
4234
4235#define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \
4236 {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004237
4238static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004239_pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self)
4240/*[clinic checksum: 40f0bf7a9b161e77130674f0481bda0a0184dcce]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004241{
4242 PyObject *reduce_value, *dict_args;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004243 PyObject *contents = _pickle_PicklerMemoProxy_copy(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004244 if (contents == NULL)
4245 return NULL;
4246
4247 reduce_value = PyTuple_New(2);
4248 if (reduce_value == NULL) {
4249 Py_DECREF(contents);
4250 return NULL;
4251 }
4252 dict_args = PyTuple_New(1);
4253 if (dict_args == NULL) {
4254 Py_DECREF(contents);
4255 Py_DECREF(reduce_value);
4256 return NULL;
4257 }
4258 PyTuple_SET_ITEM(dict_args, 0, contents);
4259 Py_INCREF((PyObject *)&PyDict_Type);
4260 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4261 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4262 return reduce_value;
4263}
4264
4265static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004266 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4267 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4268 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004269 {NULL, NULL} /* sentinel */
4270};
4271
4272static void
4273PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4274{
4275 PyObject_GC_UnTrack(self);
4276 Py_XDECREF(self->pickler);
4277 PyObject_GC_Del((PyObject *)self);
4278}
4279
4280static int
4281PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4282 visitproc visit, void *arg)
4283{
4284 Py_VISIT(self->pickler);
4285 return 0;
4286}
4287
4288static int
4289PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4290{
4291 Py_CLEAR(self->pickler);
4292 return 0;
4293}
4294
4295static PyTypeObject PicklerMemoProxyType = {
4296 PyVarObject_HEAD_INIT(NULL, 0)
4297 "_pickle.PicklerMemoProxy", /*tp_name*/
4298 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4299 0,
4300 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4301 0, /* tp_print */
4302 0, /* tp_getattr */
4303 0, /* tp_setattr */
4304 0, /* tp_compare */
4305 0, /* tp_repr */
4306 0, /* tp_as_number */
4307 0, /* tp_as_sequence */
4308 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004309 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004310 0, /* tp_call */
4311 0, /* tp_str */
4312 PyObject_GenericGetAttr, /* tp_getattro */
4313 PyObject_GenericSetAttr, /* tp_setattro */
4314 0, /* tp_as_buffer */
4315 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4316 0, /* tp_doc */
4317 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4318 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4319 0, /* tp_richcompare */
4320 0, /* tp_weaklistoffset */
4321 0, /* tp_iter */
4322 0, /* tp_iternext */
4323 picklerproxy_methods, /* tp_methods */
4324};
4325
4326static PyObject *
4327PicklerMemoProxy_New(PicklerObject *pickler)
4328{
4329 PicklerMemoProxyObject *self;
4330
4331 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4332 if (self == NULL)
4333 return NULL;
4334 Py_INCREF(pickler);
4335 self->pickler = pickler;
4336 PyObject_GC_Track(self);
4337 return (PyObject *)self;
4338}
4339
4340/*****************************************************************************/
4341
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004342static PyObject *
4343Pickler_get_memo(PicklerObject *self)
4344{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004345 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004346}
4347
4348static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004349Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004350{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004351 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004352
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004353 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004354 PyErr_SetString(PyExc_TypeError,
4355 "attribute deletion is not supported");
4356 return -1;
4357 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004358
4359 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4360 PicklerObject *pickler =
4361 ((PicklerMemoProxyObject *)obj)->pickler;
4362
4363 new_memo = PyMemoTable_Copy(pickler->memo);
4364 if (new_memo == NULL)
4365 return -1;
4366 }
4367 else if (PyDict_Check(obj)) {
4368 Py_ssize_t i = 0;
4369 PyObject *key, *value;
4370
4371 new_memo = PyMemoTable_New();
4372 if (new_memo == NULL)
4373 return -1;
4374
4375 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004376 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004377 PyObject *memo_obj;
4378
4379 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4380 PyErr_SetString(PyExc_TypeError,
4381 "'memo' values must be 2-item tuples");
4382 goto error;
4383 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004384 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004385 if (memo_id == -1 && PyErr_Occurred())
4386 goto error;
4387 memo_obj = PyTuple_GET_ITEM(value, 1);
4388 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4389 goto error;
4390 }
4391 }
4392 else {
4393 PyErr_Format(PyExc_TypeError,
4394 "'memo' attribute must be an PicklerMemoProxy object"
4395 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004396 return -1;
4397 }
4398
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004399 PyMemoTable_Del(self->memo);
4400 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004401
4402 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004403
4404 error:
4405 if (new_memo)
4406 PyMemoTable_Del(new_memo);
4407 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004408}
4409
4410static PyObject *
4411Pickler_get_persid(PicklerObject *self)
4412{
4413 if (self->pers_func == NULL)
4414 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4415 else
4416 Py_INCREF(self->pers_func);
4417 return self->pers_func;
4418}
4419
4420static int
4421Pickler_set_persid(PicklerObject *self, PyObject *value)
4422{
4423 PyObject *tmp;
4424
4425 if (value == NULL) {
4426 PyErr_SetString(PyExc_TypeError,
4427 "attribute deletion is not supported");
4428 return -1;
4429 }
4430 if (!PyCallable_Check(value)) {
4431 PyErr_SetString(PyExc_TypeError,
4432 "persistent_id must be a callable taking one argument");
4433 return -1;
4434 }
4435
4436 tmp = self->pers_func;
4437 Py_INCREF(value);
4438 self->pers_func = value;
4439 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4440
4441 return 0;
4442}
4443
4444static PyMemberDef Pickler_members[] = {
4445 {"bin", T_INT, offsetof(PicklerObject, bin)},
4446 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004447 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004448 {NULL}
4449};
4450
4451static PyGetSetDef Pickler_getsets[] = {
4452 {"memo", (getter)Pickler_get_memo,
4453 (setter)Pickler_set_memo},
4454 {"persistent_id", (getter)Pickler_get_persid,
4455 (setter)Pickler_set_persid},
4456 {NULL}
4457};
4458
4459static PyTypeObject Pickler_Type = {
4460 PyVarObject_HEAD_INIT(NULL, 0)
4461 "_pickle.Pickler" , /*tp_name*/
4462 sizeof(PicklerObject), /*tp_basicsize*/
4463 0, /*tp_itemsize*/
4464 (destructor)Pickler_dealloc, /*tp_dealloc*/
4465 0, /*tp_print*/
4466 0, /*tp_getattr*/
4467 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004468 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004469 0, /*tp_repr*/
4470 0, /*tp_as_number*/
4471 0, /*tp_as_sequence*/
4472 0, /*tp_as_mapping*/
4473 0, /*tp_hash*/
4474 0, /*tp_call*/
4475 0, /*tp_str*/
4476 0, /*tp_getattro*/
4477 0, /*tp_setattro*/
4478 0, /*tp_as_buffer*/
4479 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004480 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004481 (traverseproc)Pickler_traverse, /*tp_traverse*/
4482 (inquiry)Pickler_clear, /*tp_clear*/
4483 0, /*tp_richcompare*/
4484 0, /*tp_weaklistoffset*/
4485 0, /*tp_iter*/
4486 0, /*tp_iternext*/
4487 Pickler_methods, /*tp_methods*/
4488 Pickler_members, /*tp_members*/
4489 Pickler_getsets, /*tp_getset*/
4490 0, /*tp_base*/
4491 0, /*tp_dict*/
4492 0, /*tp_descr_get*/
4493 0, /*tp_descr_set*/
4494 0, /*tp_dictoffset*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004495 Pickler_init, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004496 PyType_GenericAlloc, /*tp_alloc*/
4497 PyType_GenericNew, /*tp_new*/
4498 PyObject_GC_Del, /*tp_free*/
4499 0, /*tp_is_gc*/
4500};
4501
Victor Stinner121aab42011-09-29 23:40:53 +02004502/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004503
4504 XXX: It would be nice to able to avoid Python function call overhead, by
4505 using directly the C version of find_class(), when find_class() is not
4506 overridden by a subclass. Although, this could become rather hackish. A
4507 simpler optimization would be to call the C function when self is not a
4508 subclass instance. */
4509static PyObject *
4510find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4511{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004512 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004513
4514 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4515 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004516}
4517
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004518static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004519marker(UnpicklerObject *self)
4520{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004521 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004522 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004523 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004524 return -1;
4525 }
4526
4527 return self->marks[--self->num_marks];
4528}
4529
4530static int
4531load_none(UnpicklerObject *self)
4532{
4533 PDATA_APPEND(self->stack, Py_None, -1);
4534 return 0;
4535}
4536
4537static int
4538bad_readline(void)
4539{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004540 PickleState *st = _Pickle_GetGlobalState();
4541 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004542 return -1;
4543}
4544
4545static int
4546load_int(UnpicklerObject *self)
4547{
4548 PyObject *value;
4549 char *endptr, *s;
4550 Py_ssize_t len;
4551 long x;
4552
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004553 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004554 return -1;
4555 if (len < 2)
4556 return bad_readline();
4557
4558 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004559 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004560 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004561 x = strtol(s, &endptr, 0);
4562
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004563 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004564 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004565 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566 errno = 0;
4567 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004568 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569 if (value == NULL) {
4570 PyErr_SetString(PyExc_ValueError,
4571 "could not convert string to int");
4572 return -1;
4573 }
4574 }
4575 else {
4576 if (len == 3 && (x == 0 || x == 1)) {
4577 if ((value = PyBool_FromLong(x)) == NULL)
4578 return -1;
4579 }
4580 else {
4581 if ((value = PyLong_FromLong(x)) == NULL)
4582 return -1;
4583 }
4584 }
4585
4586 PDATA_PUSH(self->stack, value, -1);
4587 return 0;
4588}
4589
4590static int
4591load_bool(UnpicklerObject *self, PyObject *boolean)
4592{
4593 assert(boolean == Py_True || boolean == Py_False);
4594 PDATA_APPEND(self->stack, boolean, -1);
4595 return 0;
4596}
4597
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004598/* s contains x bytes of an unsigned little-endian integer. Return its value
4599 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4600 */
4601static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004602calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004603{
4604 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004605 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004606 size_t x = 0;
4607
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08004608 for (i = 0; i < nbytes && i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004609 x |= (size_t) s[i] << (8 * i);
4610 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004611
4612 if (x > PY_SSIZE_T_MAX)
4613 return -1;
4614 else
4615 return (Py_ssize_t) x;
4616}
4617
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618/* s contains x bytes of a little-endian integer. Return its value as a
4619 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4620 * int, but when x is 4 it's a signed one. This is an historical source
4621 * of x-platform bugs.
4622 */
4623static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004624calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004625{
4626 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004627 int i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628 long x = 0;
4629
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004630 for (i = 0; i < nbytes; i++) {
4631 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004632 }
4633
4634 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4635 * is signed, so on a box with longs bigger than 4 bytes we need
4636 * to extend a BININT's sign bit to the full width.
4637 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004638 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004639 x |= -(x & (1L << 31));
4640 }
4641
4642 return x;
4643}
4644
4645static int
4646load_binintx(UnpicklerObject *self, char *s, int size)
4647{
4648 PyObject *value;
4649 long x;
4650
4651 x = calc_binint(s, size);
4652
4653 if ((value = PyLong_FromLong(x)) == NULL)
4654 return -1;
4655
4656 PDATA_PUSH(self->stack, value, -1);
4657 return 0;
4658}
4659
4660static int
4661load_binint(UnpicklerObject *self)
4662{
4663 char *s;
4664
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004665 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004666 return -1;
4667
4668 return load_binintx(self, s, 4);
4669}
4670
4671static int
4672load_binint1(UnpicklerObject *self)
4673{
4674 char *s;
4675
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004676 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004677 return -1;
4678
4679 return load_binintx(self, s, 1);
4680}
4681
4682static int
4683load_binint2(UnpicklerObject *self)
4684{
4685 char *s;
4686
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004687 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004688 return -1;
4689
4690 return load_binintx(self, s, 2);
4691}
4692
4693static int
4694load_long(UnpicklerObject *self)
4695{
4696 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004697 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 Py_ssize_t len;
4699
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004700 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004701 return -1;
4702 if (len < 2)
4703 return bad_readline();
4704
Mark Dickinson8dd05142009-01-20 20:43:58 +00004705 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4706 the 'L' before calling PyLong_FromString. In order to maintain
4707 compatibility with Python 3.0.0, we don't actually *require*
4708 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004709 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004710 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004711 /* XXX: Should the base argument explicitly set to 10? */
4712 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004713 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004714 return -1;
4715
4716 PDATA_PUSH(self->stack, value, -1);
4717 return 0;
4718}
4719
4720/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4721 * data following.
4722 */
4723static int
4724load_counted_long(UnpicklerObject *self, int size)
4725{
4726 PyObject *value;
4727 char *nbytes;
4728 char *pdata;
4729
4730 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004731 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004732 return -1;
4733
4734 size = calc_binint(nbytes, size);
4735 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004736 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004737 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004738 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004739 "LONG pickle has negative byte count");
4740 return -1;
4741 }
4742
4743 if (size == 0)
4744 value = PyLong_FromLong(0L);
4745 else {
4746 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004747 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004748 return -1;
4749 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4750 1 /* little endian */ , 1 /* signed */ );
4751 }
4752 if (value == NULL)
4753 return -1;
4754 PDATA_PUSH(self->stack, value, -1);
4755 return 0;
4756}
4757
4758static int
4759load_float(UnpicklerObject *self)
4760{
4761 PyObject *value;
4762 char *endptr, *s;
4763 Py_ssize_t len;
4764 double d;
4765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004766 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767 return -1;
4768 if (len < 2)
4769 return bad_readline();
4770
4771 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004772 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4773 if (d == -1.0 && PyErr_Occurred())
4774 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004775 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004776 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4777 return -1;
4778 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004779 value = PyFloat_FromDouble(d);
4780 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004781 return -1;
4782
4783 PDATA_PUSH(self->stack, value, -1);
4784 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004785}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786
4787static int
4788load_binfloat(UnpicklerObject *self)
4789{
4790 PyObject *value;
4791 double x;
4792 char *s;
4793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004794 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004795 return -1;
4796
4797 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4798 if (x == -1.0 && PyErr_Occurred())
4799 return -1;
4800
4801 if ((value = PyFloat_FromDouble(x)) == NULL)
4802 return -1;
4803
4804 PDATA_PUSH(self->stack, value, -1);
4805 return 0;
4806}
4807
4808static int
4809load_string(UnpicklerObject *self)
4810{
4811 PyObject *bytes;
4812 PyObject *str = NULL;
4813 Py_ssize_t len;
4814 char *s, *p;
4815
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004816 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004817 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004818 /* Strip the newline */
4819 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004820 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004821 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004822 p = s + 1;
4823 len -= 2;
4824 }
4825 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004826 PickleState *st = _Pickle_GetGlobalState();
4827 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004828 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829 return -1;
4830 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004831 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004832
4833 /* Use the PyBytes API to decode the string, since that is what is used
4834 to encode, and then coerce the result to Unicode. */
4835 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004836 if (bytes == NULL)
4837 return -1;
4838 str = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4839 Py_DECREF(bytes);
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_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004849{
4850 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004851 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);
4858 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004859 PyErr_Format(PyExc_OverflowError,
4860 "BINBYTES 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 Pitrouc9dc4a22013-11-23 18:59:12 +01004865 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004867
4868 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004869 if (bytes == NULL)
4870 return -1;
4871
4872 PDATA_PUSH(self->stack, bytes, -1);
4873 return 0;
4874}
4875
4876static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004877load_counted_binstring(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004878{
4879 PyObject *str;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004880 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 char *s;
4882
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004883 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004884 return -1;
4885
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004886 size = calc_binsize(s, nbytes);
4887 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004888 PickleState *st = _Pickle_GetGlobalState();
4889 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004890 "BINSTRING exceeds system's maximum size of %zd bytes",
4891 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892 return -1;
4893 }
4894
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004895 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 /* Convert Python 2.x strings to unicode. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004898 str = PyUnicode_Decode(s, size, self->encoding, self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 if (str == NULL)
4900 return -1;
4901
4902 PDATA_PUSH(self->stack, str, -1);
4903 return 0;
4904}
4905
4906static int
4907load_unicode(UnpicklerObject *self)
4908{
4909 PyObject *str;
4910 Py_ssize_t len;
4911 char *s;
4912
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004913 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 return -1;
4915 if (len < 1)
4916 return bad_readline();
4917
4918 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4919 if (str == NULL)
4920 return -1;
4921
4922 PDATA_PUSH(self->stack, str, -1);
4923 return 0;
4924}
4925
4926static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004927load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928{
4929 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004930 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 char *s;
4932
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004933 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004934 return -1;
4935
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004936 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004937 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004938 PyErr_Format(PyExc_OverflowError,
4939 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004940 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941 return -1;
4942 }
4943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004944 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945 return -1;
4946
Victor Stinner485fb562010-04-13 11:07:24 +00004947 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004948 if (str == NULL)
4949 return -1;
4950
4951 PDATA_PUSH(self->stack, str, -1);
4952 return 0;
4953}
4954
4955static int
4956load_tuple(UnpicklerObject *self)
4957{
4958 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004959 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004960
4961 if ((i = marker(self)) < 0)
4962 return -1;
4963
4964 tuple = Pdata_poptuple(self->stack, i);
4965 if (tuple == NULL)
4966 return -1;
4967 PDATA_PUSH(self->stack, tuple, -1);
4968 return 0;
4969}
4970
4971static int
4972load_counted_tuple(UnpicklerObject *self, int len)
4973{
4974 PyObject *tuple;
4975
4976 tuple = PyTuple_New(len);
4977 if (tuple == NULL)
4978 return -1;
4979
4980 while (--len >= 0) {
4981 PyObject *item;
4982
4983 PDATA_POP(self->stack, item);
4984 if (item == NULL)
4985 return -1;
4986 PyTuple_SET_ITEM(tuple, len, item);
4987 }
4988 PDATA_PUSH(self->stack, tuple, -1);
4989 return 0;
4990}
4991
4992static int
4993load_empty_list(UnpicklerObject *self)
4994{
4995 PyObject *list;
4996
4997 if ((list = PyList_New(0)) == NULL)
4998 return -1;
4999 PDATA_PUSH(self->stack, list, -1);
5000 return 0;
5001}
5002
5003static int
5004load_empty_dict(UnpicklerObject *self)
5005{
5006 PyObject *dict;
5007
5008 if ((dict = PyDict_New()) == NULL)
5009 return -1;
5010 PDATA_PUSH(self->stack, dict, -1);
5011 return 0;
5012}
5013
5014static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005015load_empty_set(UnpicklerObject *self)
5016{
5017 PyObject *set;
5018
5019 if ((set = PySet_New(NULL)) == NULL)
5020 return -1;
5021 PDATA_PUSH(self->stack, set, -1);
5022 return 0;
5023}
5024
5025static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026load_list(UnpicklerObject *self)
5027{
5028 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005029 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030
5031 if ((i = marker(self)) < 0)
5032 return -1;
5033
5034 list = Pdata_poplist(self->stack, i);
5035 if (list == NULL)
5036 return -1;
5037 PDATA_PUSH(self->stack, list, -1);
5038 return 0;
5039}
5040
5041static int
5042load_dict(UnpicklerObject *self)
5043{
5044 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005045 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046
5047 if ((i = marker(self)) < 0)
5048 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005049 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005050
5051 if ((dict = PyDict_New()) == NULL)
5052 return -1;
5053
5054 for (k = i + 1; k < j; k += 2) {
5055 key = self->stack->data[k - 1];
5056 value = self->stack->data[k];
5057 if (PyDict_SetItem(dict, key, value) < 0) {
5058 Py_DECREF(dict);
5059 return -1;
5060 }
5061 }
5062 Pdata_clear(self->stack, i);
5063 PDATA_PUSH(self->stack, dict, -1);
5064 return 0;
5065}
5066
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005067static int
5068load_frozenset(UnpicklerObject *self)
5069{
5070 PyObject *items;
5071 PyObject *frozenset;
5072 Py_ssize_t i;
5073
5074 if ((i = marker(self)) < 0)
5075 return -1;
5076
5077 items = Pdata_poptuple(self->stack, i);
5078 if (items == NULL)
5079 return -1;
5080
5081 frozenset = PyFrozenSet_New(items);
5082 Py_DECREF(items);
5083 if (frozenset == NULL)
5084 return -1;
5085
5086 PDATA_PUSH(self->stack, frozenset, -1);
5087 return 0;
5088}
5089
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005090static PyObject *
5091instantiate(PyObject *cls, PyObject *args)
5092{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005093 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005094 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005095 /* Caller must assure args are a tuple. Normally, args come from
5096 Pdata_poptuple which packs objects from the top of the stack
5097 into a newly created tuple. */
5098 assert(PyTuple_Check(args));
5099 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005100 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005101 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005102 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005103 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005104 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005105
5106 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005107 }
5108 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005109}
5110
5111static int
5112load_obj(UnpicklerObject *self)
5113{
5114 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005115 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116
5117 if ((i = marker(self)) < 0)
5118 return -1;
5119
5120 args = Pdata_poptuple(self->stack, i + 1);
5121 if (args == NULL)
5122 return -1;
5123
5124 PDATA_POP(self->stack, cls);
5125 if (cls) {
5126 obj = instantiate(cls, args);
5127 Py_DECREF(cls);
5128 }
5129 Py_DECREF(args);
5130 if (obj == NULL)
5131 return -1;
5132
5133 PDATA_PUSH(self->stack, obj, -1);
5134 return 0;
5135}
5136
5137static int
5138load_inst(UnpicklerObject *self)
5139{
5140 PyObject *cls = NULL;
5141 PyObject *args = NULL;
5142 PyObject *obj = NULL;
5143 PyObject *module_name;
5144 PyObject *class_name;
5145 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005146 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005147 char *s;
5148
5149 if ((i = marker(self)) < 0)
5150 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005151 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005152 return -1;
5153 if (len < 2)
5154 return bad_readline();
5155
5156 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5157 identifiers are permitted in Python 3.0, since the INST opcode is only
5158 supported by older protocols on Python 2.x. */
5159 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5160 if (module_name == NULL)
5161 return -1;
5162
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005163 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005164 if (len < 2)
5165 return bad_readline();
5166 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005167 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005168 cls = find_class(self, module_name, class_name);
5169 Py_DECREF(class_name);
5170 }
5171 }
5172 Py_DECREF(module_name);
5173
5174 if (cls == NULL)
5175 return -1;
5176
5177 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5178 obj = instantiate(cls, args);
5179 Py_DECREF(args);
5180 }
5181 Py_DECREF(cls);
5182
5183 if (obj == NULL)
5184 return -1;
5185
5186 PDATA_PUSH(self->stack, obj, -1);
5187 return 0;
5188}
5189
5190static int
5191load_newobj(UnpicklerObject *self)
5192{
5193 PyObject *args = NULL;
5194 PyObject *clsraw = NULL;
5195 PyTypeObject *cls; /* clsraw cast to its true type */
5196 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005197 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005198
5199 /* Stack is ... cls argtuple, and we want to call
5200 * cls.__new__(cls, *argtuple).
5201 */
5202 PDATA_POP(self->stack, args);
5203 if (args == NULL)
5204 goto error;
5205 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005206 PyErr_SetString(st->UnpicklingError,
5207 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208 goto error;
5209 }
5210
5211 PDATA_POP(self->stack, clsraw);
5212 cls = (PyTypeObject *)clsraw;
5213 if (cls == NULL)
5214 goto error;
5215 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005216 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005217 "isn't a type object");
5218 goto error;
5219 }
5220 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005221 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 "has NULL tp_new");
5223 goto error;
5224 }
5225
5226 /* Call __new__. */
5227 obj = cls->tp_new(cls, args, NULL);
5228 if (obj == NULL)
5229 goto error;
5230
5231 Py_DECREF(args);
5232 Py_DECREF(clsraw);
5233 PDATA_PUSH(self->stack, obj, -1);
5234 return 0;
5235
5236 error:
5237 Py_XDECREF(args);
5238 Py_XDECREF(clsraw);
5239 return -1;
5240}
5241
5242static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005243load_newobj_ex(UnpicklerObject *self)
5244{
5245 PyObject *cls, *args, *kwargs;
5246 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005247 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005248
5249 PDATA_POP(self->stack, kwargs);
5250 if (kwargs == NULL) {
5251 return -1;
5252 }
5253 PDATA_POP(self->stack, args);
5254 if (args == NULL) {
5255 Py_DECREF(kwargs);
5256 return -1;
5257 }
5258 PDATA_POP(self->stack, cls);
5259 if (cls == NULL) {
5260 Py_DECREF(kwargs);
5261 Py_DECREF(args);
5262 return -1;
5263 }
5264
5265 if (!PyType_Check(cls)) {
5266 Py_DECREF(kwargs);
5267 Py_DECREF(args);
5268 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005269 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005270 "NEWOBJ_EX class argument must be a type, not %.200s",
5271 Py_TYPE(cls)->tp_name);
5272 return -1;
5273 }
5274
5275 if (((PyTypeObject *)cls)->tp_new == NULL) {
5276 Py_DECREF(kwargs);
5277 Py_DECREF(args);
5278 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005279 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005280 "NEWOBJ_EX class argument doesn't have __new__");
5281 return -1;
5282 }
5283 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5284 Py_DECREF(kwargs);
5285 Py_DECREF(args);
5286 Py_DECREF(cls);
5287 if (obj == NULL) {
5288 return -1;
5289 }
5290 PDATA_PUSH(self->stack, obj, -1);
5291 return 0;
5292}
5293
5294static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005295load_global(UnpicklerObject *self)
5296{
5297 PyObject *global = NULL;
5298 PyObject *module_name;
5299 PyObject *global_name;
5300 Py_ssize_t len;
5301 char *s;
5302
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005303 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005304 return -1;
5305 if (len < 2)
5306 return bad_readline();
5307 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5308 if (!module_name)
5309 return -1;
5310
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005311 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005312 if (len < 2) {
5313 Py_DECREF(module_name);
5314 return bad_readline();
5315 }
5316 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5317 if (global_name) {
5318 global = find_class(self, module_name, global_name);
5319 Py_DECREF(global_name);
5320 }
5321 }
5322 Py_DECREF(module_name);
5323
5324 if (global == NULL)
5325 return -1;
5326 PDATA_PUSH(self->stack, global, -1);
5327 return 0;
5328}
5329
5330static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005331load_stack_global(UnpicklerObject *self)
5332{
5333 PyObject *global;
5334 PyObject *module_name;
5335 PyObject *global_name;
5336
5337 PDATA_POP(self->stack, global_name);
5338 PDATA_POP(self->stack, module_name);
5339 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5340 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005341 PickleState *st = _Pickle_GetGlobalState();
5342 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005343 Py_XDECREF(global_name);
5344 Py_XDECREF(module_name);
5345 return -1;
5346 }
5347 global = find_class(self, module_name, global_name);
5348 Py_DECREF(global_name);
5349 Py_DECREF(module_name);
5350 if (global == NULL)
5351 return -1;
5352 PDATA_PUSH(self->stack, global, -1);
5353 return 0;
5354}
5355
5356static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005357load_persid(UnpicklerObject *self)
5358{
5359 PyObject *pid;
5360 Py_ssize_t len;
5361 char *s;
5362
5363 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005364 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005365 return -1;
5366 if (len < 2)
5367 return bad_readline();
5368
5369 pid = PyBytes_FromStringAndSize(s, len - 1);
5370 if (pid == NULL)
5371 return -1;
5372
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005373 /* This does not leak since _Pickle_FastCall() steals the reference
5374 to pid first. */
5375 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005376 if (pid == NULL)
5377 return -1;
5378
5379 PDATA_PUSH(self->stack, pid, -1);
5380 return 0;
5381 }
5382 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005383 PickleState *st = _Pickle_GetGlobalState();
5384 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005385 "A load persistent id instruction was encountered,\n"
5386 "but no persistent_load function was specified.");
5387 return -1;
5388 }
5389}
5390
5391static int
5392load_binpersid(UnpicklerObject *self)
5393{
5394 PyObject *pid;
5395
5396 if (self->pers_func) {
5397 PDATA_POP(self->stack, pid);
5398 if (pid == NULL)
5399 return -1;
5400
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005401 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005402 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005403 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005404 if (pid == NULL)
5405 return -1;
5406
5407 PDATA_PUSH(self->stack, pid, -1);
5408 return 0;
5409 }
5410 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005411 PickleState *st = _Pickle_GetGlobalState();
5412 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005413 "A load persistent id instruction was encountered,\n"
5414 "but no persistent_load function was specified.");
5415 return -1;
5416 }
5417}
5418
5419static int
5420load_pop(UnpicklerObject *self)
5421{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005422 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005423
5424 /* Note that we split the (pickle.py) stack into two stacks,
5425 * an object stack and a mark stack. We have to be clever and
5426 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005427 * mark stack first, and only signalling a stack underflow if
5428 * the object stack is empty and the mark stack doesn't match
5429 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005430 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005431 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005432 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005433 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005434 len--;
5435 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005436 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005437 } else {
5438 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005439 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440 return 0;
5441}
5442
5443static int
5444load_pop_mark(UnpicklerObject *self)
5445{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005446 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005447
5448 if ((i = marker(self)) < 0)
5449 return -1;
5450
5451 Pdata_clear(self->stack, i);
5452
5453 return 0;
5454}
5455
5456static int
5457load_dup(UnpicklerObject *self)
5458{
5459 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005460 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005462 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005463 return stack_underflow();
5464 last = self->stack->data[len - 1];
5465 PDATA_APPEND(self->stack, last, -1);
5466 return 0;
5467}
5468
5469static int
5470load_get(UnpicklerObject *self)
5471{
5472 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005473 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474 Py_ssize_t len;
5475 char *s;
5476
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005477 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005478 return -1;
5479 if (len < 2)
5480 return bad_readline();
5481
5482 key = PyLong_FromString(s, NULL, 10);
5483 if (key == NULL)
5484 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005485 idx = PyLong_AsSsize_t(key);
5486 if (idx == -1 && PyErr_Occurred()) {
5487 Py_DECREF(key);
5488 return -1;
5489 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005491 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492 if (value == NULL) {
5493 if (!PyErr_Occurred())
5494 PyErr_SetObject(PyExc_KeyError, key);
5495 Py_DECREF(key);
5496 return -1;
5497 }
5498 Py_DECREF(key);
5499
5500 PDATA_APPEND(self->stack, value, -1);
5501 return 0;
5502}
5503
5504static int
5505load_binget(UnpicklerObject *self)
5506{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005507 PyObject *value;
5508 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005509 char *s;
5510
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005511 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512 return -1;
5513
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005514 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005516 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005517 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005518 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005519 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005520 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005521 Py_DECREF(key);
5522 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 return -1;
5524 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525
5526 PDATA_APPEND(self->stack, value, -1);
5527 return 0;
5528}
5529
5530static int
5531load_long_binget(UnpicklerObject *self)
5532{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005533 PyObject *value;
5534 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005535 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005537 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005538 return -1;
5539
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005540 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005542 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005544 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005545 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005546 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005547 Py_DECREF(key);
5548 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 return -1;
5550 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551
5552 PDATA_APPEND(self->stack, value, -1);
5553 return 0;
5554}
5555
5556/* Push an object from the extension registry (EXT[124]). nbytes is
5557 * the number of bytes following the opcode, holding the index (code) value.
5558 */
5559static int
5560load_extension(UnpicklerObject *self, int nbytes)
5561{
5562 char *codebytes; /* the nbytes bytes after the opcode */
5563 long code; /* calc_binint returns long */
5564 PyObject *py_code; /* code as a Python int */
5565 PyObject *obj; /* the object to push */
5566 PyObject *pair; /* (module_name, class_name) */
5567 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005568 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005569
5570 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005571 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005572 return -1;
5573 code = calc_binint(codebytes, nbytes);
5574 if (code <= 0) { /* note that 0 is forbidden */
5575 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005576 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005577 return -1;
5578 }
5579
5580 /* Look for the code in the cache. */
5581 py_code = PyLong_FromLong(code);
5582 if (py_code == NULL)
5583 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005584 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 if (obj != NULL) {
5586 /* Bingo. */
5587 Py_DECREF(py_code);
5588 PDATA_APPEND(self->stack, obj, -1);
5589 return 0;
5590 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005591 if (PyErr_Occurred()) {
5592 Py_DECREF(py_code);
5593 return -1;
5594 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005595
5596 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005597 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005598 if (pair == NULL) {
5599 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005600 if (!PyErr_Occurred()) {
5601 PyErr_Format(PyExc_ValueError, "unregistered extension "
5602 "code %ld", code);
5603 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 return -1;
5605 }
5606 /* Since the extension registry is manipulable via Python code,
5607 * confirm that pair is really a 2-tuple of strings.
5608 */
5609 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5610 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5611 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5612 Py_DECREF(py_code);
5613 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5614 "isn't a 2-tuple of strings", code);
5615 return -1;
5616 }
5617 /* Load the object. */
5618 obj = find_class(self, module_name, class_name);
5619 if (obj == NULL) {
5620 Py_DECREF(py_code);
5621 return -1;
5622 }
5623 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005624 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 Py_DECREF(py_code);
5626 if (code < 0) {
5627 Py_DECREF(obj);
5628 return -1;
5629 }
5630 PDATA_PUSH(self->stack, obj, -1);
5631 return 0;
5632}
5633
5634static int
5635load_put(UnpicklerObject *self)
5636{
5637 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005638 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639 Py_ssize_t len;
5640 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005641
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005642 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643 return -1;
5644 if (len < 2)
5645 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005646 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005648 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649
5650 key = PyLong_FromString(s, NULL, 10);
5651 if (key == NULL)
5652 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005653 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005655 if (idx < 0) {
5656 if (!PyErr_Occurred())
5657 PyErr_SetString(PyExc_ValueError,
5658 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005659 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005660 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005661
5662 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663}
5664
5665static int
5666load_binput(UnpicklerObject *self)
5667{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005668 PyObject *value;
5669 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005672 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005673 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005674
5675 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005676 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005677 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005679 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005681 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005682}
5683
5684static int
5685load_long_binput(UnpicklerObject *self)
5686{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005687 PyObject *value;
5688 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005691 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005693
5694 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005696 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005698 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005699 if (idx < 0) {
5700 PyErr_SetString(PyExc_ValueError,
5701 "negative LONG_BINPUT argument");
5702 return -1;
5703 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005705 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005706}
5707
5708static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005709load_memoize(UnpicklerObject *self)
5710{
5711 PyObject *value;
5712
5713 if (Py_SIZE(self->stack) <= 0)
5714 return stack_underflow();
5715 value = self->stack->data[Py_SIZE(self->stack) - 1];
5716
5717 return _Unpickler_MemoPut(self, self->memo_len, value);
5718}
5719
5720static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005721do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722{
5723 PyObject *value;
5724 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005725 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005726
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005727 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005728 if (x > len || x <= 0)
5729 return stack_underflow();
5730 if (len == x) /* nothing to do */
5731 return 0;
5732
5733 list = self->stack->data[x - 1];
5734
5735 if (PyList_Check(list)) {
5736 PyObject *slice;
5737 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005738 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005739
5740 slice = Pdata_poplist(self->stack, x);
5741 if (!slice)
5742 return -1;
5743 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005744 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005746 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 }
5748 else {
5749 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005750 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005751
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005752 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005753 if (append_func == NULL)
5754 return -1;
5755 for (i = x; i < len; i++) {
5756 PyObject *result;
5757
5758 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005759 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760 if (result == NULL) {
5761 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005763 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 return -1;
5765 }
5766 Py_DECREF(result);
5767 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005769 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770 }
5771
5772 return 0;
5773}
5774
5775static int
5776load_append(UnpicklerObject *self)
5777{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005778 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779}
5780
5781static int
5782load_appends(UnpicklerObject *self)
5783{
5784 return do_append(self, marker(self));
5785}
5786
5787static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005788do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789{
5790 PyObject *value, *key;
5791 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005792 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793 int status = 0;
5794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796 if (x > len || x <= 0)
5797 return stack_underflow();
5798 if (len == x) /* nothing to do */
5799 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005800 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005801 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005802 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005803 PyErr_SetString(st->UnpicklingError,
5804 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005805 return -1;
5806 }
5807
5808 /* Here, dict does not actually need to be a PyDict; it could be anything
5809 that supports the __setitem__ attribute. */
5810 dict = self->stack->data[x - 1];
5811
5812 for (i = x + 1; i < len; i += 2) {
5813 key = self->stack->data[i - 1];
5814 value = self->stack->data[i];
5815 if (PyObject_SetItem(dict, key, value) < 0) {
5816 status = -1;
5817 break;
5818 }
5819 }
5820
5821 Pdata_clear(self->stack, x);
5822 return status;
5823}
5824
5825static int
5826load_setitem(UnpicklerObject *self)
5827{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005828 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829}
5830
5831static int
5832load_setitems(UnpicklerObject *self)
5833{
5834 return do_setitems(self, marker(self));
5835}
5836
5837static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005838load_additems(UnpicklerObject *self)
5839{
5840 PyObject *set;
5841 Py_ssize_t mark, len, i;
5842
5843 mark = marker(self);
5844 len = Py_SIZE(self->stack);
5845 if (mark > len || mark <= 0)
5846 return stack_underflow();
5847 if (len == mark) /* nothing to do */
5848 return 0;
5849
5850 set = self->stack->data[mark - 1];
5851
5852 if (PySet_Check(set)) {
5853 PyObject *items;
5854 int status;
5855
5856 items = Pdata_poptuple(self->stack, mark);
5857 if (items == NULL)
5858 return -1;
5859
5860 status = _PySet_Update(set, items);
5861 Py_DECREF(items);
5862 return status;
5863 }
5864 else {
5865 PyObject *add_func;
5866 _Py_IDENTIFIER(add);
5867
5868 add_func = _PyObject_GetAttrId(set, &PyId_add);
5869 if (add_func == NULL)
5870 return -1;
5871 for (i = mark; i < len; i++) {
5872 PyObject *result;
5873 PyObject *item;
5874
5875 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005876 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005877 if (result == NULL) {
5878 Pdata_clear(self->stack, i + 1);
5879 Py_SIZE(self->stack) = mark;
5880 return -1;
5881 }
5882 Py_DECREF(result);
5883 }
5884 Py_SIZE(self->stack) = mark;
5885 }
5886
5887 return 0;
5888}
5889
5890static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005891load_build(UnpicklerObject *self)
5892{
5893 PyObject *state, *inst, *slotstate;
5894 PyObject *setstate;
5895 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005896 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897
5898 /* Stack is ... instance, state. We want to leave instance at
5899 * the stack top, possibly mutated via instance.__setstate__(state).
5900 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005901 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005902 return stack_underflow();
5903
5904 PDATA_POP(self->stack, state);
5905 if (state == NULL)
5906 return -1;
5907
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005908 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005909
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005910 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005911 if (setstate == NULL) {
5912 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5913 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005914 else {
5915 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005916 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005917 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918 }
5919 else {
5920 PyObject *result;
5921
5922 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005923 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005924 Py_DECREF(setstate);
5925 if (result == NULL)
5926 return -1;
5927 Py_DECREF(result);
5928 return 0;
5929 }
5930
5931 /* A default __setstate__. First see whether state embeds a
5932 * slot state dict too (a proto 2 addition).
5933 */
5934 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5935 PyObject *tmp = state;
5936
5937 state = PyTuple_GET_ITEM(tmp, 0);
5938 slotstate = PyTuple_GET_ITEM(tmp, 1);
5939 Py_INCREF(state);
5940 Py_INCREF(slotstate);
5941 Py_DECREF(tmp);
5942 }
5943 else
5944 slotstate = NULL;
5945
5946 /* Set inst.__dict__ from the state dict (if any). */
5947 if (state != Py_None) {
5948 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005949 PyObject *d_key, *d_value;
5950 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005951 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005952
5953 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005954 PickleState *st = _Pickle_GetGlobalState();
5955 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005956 goto error;
5957 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005958 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005959 if (dict == NULL)
5960 goto error;
5961
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005962 i = 0;
5963 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5964 /* normally the keys for instance attributes are
5965 interned. we should try to do that here. */
5966 Py_INCREF(d_key);
5967 if (PyUnicode_CheckExact(d_key))
5968 PyUnicode_InternInPlace(&d_key);
5969 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5970 Py_DECREF(d_key);
5971 goto error;
5972 }
5973 Py_DECREF(d_key);
5974 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005975 Py_DECREF(dict);
5976 }
5977
5978 /* Also set instance attributes from the slotstate dict (if any). */
5979 if (slotstate != NULL) {
5980 PyObject *d_key, *d_value;
5981 Py_ssize_t i;
5982
5983 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005984 PickleState *st = _Pickle_GetGlobalState();
5985 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986 "slot state is not a dictionary");
5987 goto error;
5988 }
5989 i = 0;
5990 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
5991 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
5992 goto error;
5993 }
5994 }
5995
5996 if (0) {
5997 error:
5998 status = -1;
5999 }
6000
6001 Py_DECREF(state);
6002 Py_XDECREF(slotstate);
6003 return status;
6004}
6005
6006static int
6007load_mark(UnpicklerObject *self)
6008{
6009
6010 /* Note that we split the (pickle.py) stack into two stacks, an
6011 * object stack and a mark stack. Here we push a mark onto the
6012 * mark stack.
6013 */
6014
6015 if ((self->num_marks + 1) >= self->marks_size) {
6016 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006017 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006018
6019 /* Use the size_t type to check for overflow. */
6020 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006021 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006022 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023 PyErr_NoMemory();
6024 return -1;
6025 }
6026
6027 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006028 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006029 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006030 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
6031 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006032 if (marks == NULL) {
6033 PyErr_NoMemory();
6034 return -1;
6035 }
6036 self->marks = marks;
6037 self->marks_size = (Py_ssize_t)alloc;
6038 }
6039
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006040 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041
6042 return 0;
6043}
6044
6045static int
6046load_reduce(UnpicklerObject *self)
6047{
6048 PyObject *callable = NULL;
6049 PyObject *argtup = NULL;
6050 PyObject *obj = NULL;
6051
6052 PDATA_POP(self->stack, argtup);
6053 if (argtup == NULL)
6054 return -1;
6055 PDATA_POP(self->stack, callable);
6056 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006057 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058 Py_DECREF(callable);
6059 }
6060 Py_DECREF(argtup);
6061
6062 if (obj == NULL)
6063 return -1;
6064
6065 PDATA_PUSH(self->stack, obj, -1);
6066 return 0;
6067}
6068
6069/* Just raises an error if we don't know the protocol specified. PROTO
6070 * is the first opcode for protocols >= 2.
6071 */
6072static int
6073load_proto(UnpicklerObject *self)
6074{
6075 char *s;
6076 int i;
6077
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006078 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 return -1;
6080
6081 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006082 if (i <= HIGHEST_PROTOCOL) {
6083 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006084 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006085 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006086
6087 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6088 return -1;
6089}
6090
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006091static int
6092load_frame(UnpicklerObject *self)
6093{
6094 char *s;
6095 Py_ssize_t frame_len;
6096
6097 if (_Unpickler_Read(self, &s, 8) < 0)
6098 return -1;
6099
6100 frame_len = calc_binsize(s, 8);
6101 if (frame_len < 0) {
6102 PyErr_Format(PyExc_OverflowError,
6103 "FRAME length exceeds system's maximum of %zd bytes",
6104 PY_SSIZE_T_MAX);
6105 return -1;
6106 }
6107
6108 if (_Unpickler_Read(self, &s, frame_len) < 0)
6109 return -1;
6110
6111 /* Rewind to start of frame */
6112 self->next_read_idx -= frame_len;
6113 return 0;
6114}
6115
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006116static PyObject *
6117load(UnpicklerObject *self)
6118{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119 PyObject *value = NULL;
6120 char *s;
6121
6122 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006123 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006124 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125 Pdata_clear(self->stack, 0);
6126
6127 /* Convenient macros for the dispatch while-switch loop just below. */
6128#define OP(opcode, load_func) \
6129 case opcode: if (load_func(self) < 0) break; continue;
6130
6131#define OP_ARG(opcode, load_func, arg) \
6132 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6133
6134 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006135 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006136 break;
6137
6138 switch ((enum opcode)s[0]) {
6139 OP(NONE, load_none)
6140 OP(BININT, load_binint)
6141 OP(BININT1, load_binint1)
6142 OP(BININT2, load_binint2)
6143 OP(INT, load_int)
6144 OP(LONG, load_long)
6145 OP_ARG(LONG1, load_counted_long, 1)
6146 OP_ARG(LONG4, load_counted_long, 4)
6147 OP(FLOAT, load_float)
6148 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006149 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6150 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6151 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6152 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6153 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006154 OP(STRING, load_string)
6155 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006156 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6157 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6158 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006159 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6160 OP_ARG(TUPLE1, load_counted_tuple, 1)
6161 OP_ARG(TUPLE2, load_counted_tuple, 2)
6162 OP_ARG(TUPLE3, load_counted_tuple, 3)
6163 OP(TUPLE, load_tuple)
6164 OP(EMPTY_LIST, load_empty_list)
6165 OP(LIST, load_list)
6166 OP(EMPTY_DICT, load_empty_dict)
6167 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006168 OP(EMPTY_SET, load_empty_set)
6169 OP(ADDITEMS, load_additems)
6170 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 OP(OBJ, load_obj)
6172 OP(INST, load_inst)
6173 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006174 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006175 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006176 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006177 OP(APPEND, load_append)
6178 OP(APPENDS, load_appends)
6179 OP(BUILD, load_build)
6180 OP(DUP, load_dup)
6181 OP(BINGET, load_binget)
6182 OP(LONG_BINGET, load_long_binget)
6183 OP(GET, load_get)
6184 OP(MARK, load_mark)
6185 OP(BINPUT, load_binput)
6186 OP(LONG_BINPUT, load_long_binput)
6187 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006188 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189 OP(POP, load_pop)
6190 OP(POP_MARK, load_pop_mark)
6191 OP(SETITEM, load_setitem)
6192 OP(SETITEMS, load_setitems)
6193 OP(PERSID, load_persid)
6194 OP(BINPERSID, load_binpersid)
6195 OP(REDUCE, load_reduce)
6196 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006197 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006198 OP_ARG(EXT1, load_extension, 1)
6199 OP_ARG(EXT2, load_extension, 2)
6200 OP_ARG(EXT4, load_extension, 4)
6201 OP_ARG(NEWTRUE, load_bool, Py_True)
6202 OP_ARG(NEWFALSE, load_bool, Py_False)
6203
6204 case STOP:
6205 break;
6206
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006208 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006209 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006210 }
6211 else {
6212 PickleState *st = _Pickle_GetGlobalState();
6213 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006214 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006215 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216 return NULL;
6217 }
6218
6219 break; /* and we are done! */
6220 }
6221
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006222 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006223 return NULL;
6224 }
6225
Victor Stinner2ae57e32013-10-31 13:39:23 +01006226 if (_Unpickler_SkipConsumed(self) < 0)
6227 return NULL;
6228
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006229 PDATA_POP(self->stack, value);
6230 return value;
6231}
6232
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006233/*[clinic]
6234
6235_pickle.Unpickler.load
6236
6237Load a pickle.
6238
6239Read a pickled object representation from the open file object given in
6240the constructor, and return the reconstituted object hierarchy specified
6241therein.
6242[clinic]*/
6243
6244PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
6245"load()\n"
6246"Load a pickle.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247"\n"
6248"Read a pickled object representation from the open file object given in\n"
6249"the constructor, and return the reconstituted object hierarchy specified\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006250"therein.");
6251
6252#define _PICKLE_UNPICKLER_LOAD_METHODDEF \
6253 {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254
6255static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006256_pickle_Unpickler_load(PyObject *self)
6257/*[clinic checksum: 9a30ba4e4d9221d4dcd705e1471ab11b2c9e3ac6]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006259 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006260
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006261 /* Check whether the Unpickler was initialized correctly. This prevents
6262 segfaulting if a subclass overridden __init__ with a function that does
6263 not call Unpickler.__init__(). Here, we simply ensure that self->read
6264 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006265 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006266 PickleState *st = _Pickle_GetGlobalState();
6267 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006269 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006270 return NULL;
6271 }
6272
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006273 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006274}
6275
6276/* The name of find_class() is misleading. In newer pickle protocols, this
6277 function is used for loading any global (i.e., functions), not just
6278 classes. The name is kept only for backward compatibility. */
6279
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006280/*[clinic]
6281
6282_pickle.Unpickler.find_class
6283
6284 self: UnpicklerObject
6285 module_name: object
6286 global_name: object
6287 /
6288
6289Return an object from a specified module.
6290
6291If necessary, the module will be imported. Subclasses may override this
6292method (e.g. to restrict unpickling of arbitrary classes and functions).
6293
6294This method is called whenever a class or a function object is
6295needed. Both arguments passed are str objects.
6296[clinic]*/
6297
6298PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
6299"find_class(module_name, global_name)\n"
6300"Return an object from a specified module.\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006301"\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006302"If necessary, the module will be imported. Subclasses may override this\n"
6303"method (e.g. to restrict unpickling of arbitrary classes and functions).\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304"\n"
6305"This method is called whenever a class or a function object is\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006306"needed. Both arguments passed are str objects.");
6307
6308#define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
6309 {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_VARARGS, _pickle_Unpickler_find_class__doc__},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310
6311static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006312_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name);
6313
6314static PyObject *
6315_pickle_Unpickler_find_class(PyObject *self, PyObject *args)
6316{
6317 PyObject *return_value = NULL;
6318 PyObject *module_name;
6319 PyObject *global_name;
6320
6321 if (!PyArg_ParseTuple(args,
6322 "OO:find_class",
6323 &module_name, &global_name))
6324 goto exit;
6325 return_value = _pickle_Unpickler_find_class_impl((UnpicklerObject *)self, module_name, global_name);
6326
6327exit:
6328 return return_value;
6329}
6330
6331static PyObject *
6332_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
6333/*[clinic checksum: b7d05d4dd8adc698e5780c1ac2be0f5062d33915]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006334{
6335 PyObject *global;
6336 PyObject *modules_dict;
6337 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006338 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006339
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006340 /* Try to map the old names used in Python 2.x to the new ones used in
6341 Python 3.x. We do this only with old pickle protocols and when the
6342 user has not disabled the feature. */
6343 if (self->proto < 3 && self->fix_imports) {
6344 PyObject *key;
6345 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006346 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006347
6348 /* Check if the global (i.e., a function or a class) was renamed
6349 or moved to another module. */
6350 key = PyTuple_Pack(2, module_name, global_name);
6351 if (key == NULL)
6352 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006353 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006354 Py_DECREF(key);
6355 if (item) {
6356 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6357 PyErr_Format(PyExc_RuntimeError,
6358 "_compat_pickle.NAME_MAPPING values should be "
6359 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6360 return NULL;
6361 }
6362 module_name = PyTuple_GET_ITEM(item, 0);
6363 global_name = PyTuple_GET_ITEM(item, 1);
6364 if (!PyUnicode_Check(module_name) ||
6365 !PyUnicode_Check(global_name)) {
6366 PyErr_Format(PyExc_RuntimeError,
6367 "_compat_pickle.NAME_MAPPING values should be "
6368 "pairs of str, not (%.200s, %.200s)",
6369 Py_TYPE(module_name)->tp_name,
6370 Py_TYPE(global_name)->tp_name);
6371 return NULL;
6372 }
6373 }
6374 else if (PyErr_Occurred()) {
6375 return NULL;
6376 }
6377
6378 /* Check if the module was renamed. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006379 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006380 if (item) {
6381 if (!PyUnicode_Check(item)) {
6382 PyErr_Format(PyExc_RuntimeError,
6383 "_compat_pickle.IMPORT_MAPPING values should be "
6384 "strings, not %.200s", Py_TYPE(item)->tp_name);
6385 return NULL;
6386 }
6387 module_name = item;
6388 }
6389 else if (PyErr_Occurred()) {
6390 return NULL;
6391 }
6392 }
6393
Victor Stinnerbb520202013-11-06 22:40:41 +01006394 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006395 if (modules_dict == NULL) {
6396 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006397 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006398 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006399
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006400 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006401 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006402 if (PyErr_Occurred())
6403 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006404 module = PyImport_Import(module_name);
6405 if (module == NULL)
6406 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006407 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006408 Py_DECREF(module);
6409 }
Victor Stinner121aab42011-09-29 23:40:53 +02006410 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006411 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006412 }
6413 return global;
6414}
6415
6416static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006417 _PICKLE_UNPICKLER_LOAD_METHODDEF
6418 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006419 {NULL, NULL} /* sentinel */
6420};
6421
6422static void
6423Unpickler_dealloc(UnpicklerObject *self)
6424{
6425 PyObject_GC_UnTrack((PyObject *)self);
6426 Py_XDECREF(self->readline);
6427 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006428 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006429 Py_XDECREF(self->stack);
6430 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006431 if (self->buffer.buf != NULL) {
6432 PyBuffer_Release(&self->buffer);
6433 self->buffer.buf = NULL;
6434 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006436 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006437 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006438 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006439 PyMem_Free(self->encoding);
6440 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006441
6442 Py_TYPE(self)->tp_free((PyObject *)self);
6443}
6444
6445static int
6446Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6447{
6448 Py_VISIT(self->readline);
6449 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006450 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006451 Py_VISIT(self->stack);
6452 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006453 return 0;
6454}
6455
6456static int
6457Unpickler_clear(UnpicklerObject *self)
6458{
6459 Py_CLEAR(self->readline);
6460 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006461 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 Py_CLEAR(self->stack);
6463 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006464 if (self->buffer.buf != NULL) {
6465 PyBuffer_Release(&self->buffer);
6466 self->buffer.buf = NULL;
6467 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006468
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006469 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006470 PyMem_Free(self->marks);
6471 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006472 PyMem_Free(self->input_line);
6473 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006474 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006476 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 self->errors = NULL;
6478
6479 return 0;
6480}
6481
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006482/*[clinic]
6483
6484_pickle.Unpickler.__init__
6485
6486 self: UnpicklerObject
6487 file: object
6488 *
6489 fix_imports: bool = True
6490 encoding: str = 'ASCII'
6491 errors: str = 'strict'
6492
6493This takes a binary file for reading a pickle data stream.
6494
6495The protocol version of the pickle is detected automatically, so no
6496proto argument is needed.
6497
6498The file-like object must have two methods, a read() method
6499that takes an integer argument, and a readline() method that
6500requires no arguments. Both methods should return bytes.
6501Thus file-like object can be a binary file object opened for
6502reading, a BytesIO object, or any other custom object that
6503meets this interface.
6504
6505Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6506which are used to control compatiblity support for pickle stream
6507generated by Python 2.x. If *fix_imports* is True, pickle will try to
6508map the old Python 2.x names to the new names used in Python 3.x. The
6509*encoding* and *errors* tell pickle how to decode 8-bit string
6510instances pickled by Python 2.x; these default to 'ASCII' and
6511'strict', respectively.
6512
6513[clinic]*/
6514
6515PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
6516"__init__(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006517"This takes a binary file for reading a pickle data stream.\n"
6518"\n"
6519"The protocol version of the pickle is detected automatically, so no\n"
6520"proto argument is needed.\n"
6521"\n"
6522"The file-like object must have two methods, a read() method\n"
6523"that takes an integer argument, and a readline() method that\n"
6524"requires no arguments. Both methods should return bytes.\n"
6525"Thus file-like object can be a binary file object opened for\n"
6526"reading, a BytesIO object, or any other custom object that\n"
6527"meets this interface.\n"
6528"\n"
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006529"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
6530"which are used to control compatiblity support for pickle stream\n"
6531"generated by Python 2.x. If *fix_imports* is True, pickle will try to\n"
6532"map the old Python 2.x names to the new names used in Python 3.x. The\n"
6533"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006534"instances pickled by Python 2.x; these default to \'ASCII\' and\n"
6535"\'strict\', respectively.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537#define _PICKLE_UNPICKLER___INIT___METHODDEF \
6538 {"__init__", (PyCFunction)_pickle_Unpickler___init__, METH_VARARGS|METH_KEYWORDS, _pickle_Unpickler___init____doc__},
6539
6540static PyObject *
6541_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors);
6542
6543static PyObject *
6544_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006545{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006546 PyObject *return_value = NULL;
6547 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006549 int fix_imports = 1;
6550 const char *encoding = "ASCII";
6551 const char *errors = "strict";
6552
6553 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
6554 "O|$pss:__init__", _keywords,
6555 &file, &fix_imports, &encoding, &errors))
6556 goto exit;
6557 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
6558
6559exit:
6560 return return_value;
6561}
6562
6563static PyObject *
6564_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
6565/*[clinic checksum: bed0d8bbe1c647960ccc6f997b33bf33935fa56f]*/
6566{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006567 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006568
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006569 /* In case of multiple __init__() calls, clear previous content. */
6570 if (self->read != NULL)
6571 (void)Unpickler_clear(self);
6572
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006573 if (_Unpickler_SetInputStream(self, file) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006575
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006576 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006578
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006580 if (self->fix_imports == -1)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006581 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006583 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006584 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6585 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 if (self->pers_func == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006587 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006588 }
6589 else {
6590 self->pers_func = NULL;
6591 }
6592
6593 self->stack = (Pdata *)Pdata_New();
6594 if (self->stack == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006595 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006597 self->memo_size = 32;
6598 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006599 if (self->memo == NULL)
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006600 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006601
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006602 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006603
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006604 Py_RETURN_NONE;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006605}
6606
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006607/* Wrap the Clinic generated signature to slot it in tp_init. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608static int
6609Unpickler_init(PyObject *self, PyObject *args, PyObject *kwargs)
6610{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006611 PyObject *result = _pickle_Unpickler___init__(self, args, kwargs);
6612 if (result == NULL) {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006613 return -1;
6614 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006615 Py_DECREF(result);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006616 return 0;
6617}
6618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006619/* Define a proxy object for the Unpickler's internal memo object. This is to
6620 * avoid breaking code like:
6621 * unpickler.memo.clear()
6622 * and
6623 * unpickler.memo = saved_memo
6624 * Is this a good idea? Not really, but we don't want to break code that uses
6625 * it. Note that we don't implement the entire mapping API here. This is
6626 * intentional, as these should be treated as black-box implementation details.
6627 *
6628 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006629 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006630 */
6631
6632typedef struct {
6633 PyObject_HEAD
6634 UnpicklerObject *unpickler;
6635} UnpicklerMemoProxyObject;
6636
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006637/*[clinic]
6638_pickle.UnpicklerMemoProxy.clear
6639
6640 self: UnpicklerMemoProxyObject
6641
6642Remove all items from memo.
6643[clinic]*/
6644
6645PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
6646"clear()\n"
6647"Remove all items from memo.");
6648
6649#define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
6650 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006651
6652static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006653_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6654/*[clinic checksum: 46fecf4e33c0c873124f845edf6cc3a2e9864bd5]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006655{
6656 _Unpickler_MemoCleanup(self->unpickler);
6657 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6658 if (self->unpickler->memo == NULL)
6659 return NULL;
6660 Py_RETURN_NONE;
6661}
6662
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006663/*[clinic]
6664_pickle.UnpicklerMemoProxy.copy
6665
6666 self: UnpicklerMemoProxyObject
6667
6668Copy the memo to a new object.
6669[clinic]*/
6670
6671PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
6672"copy()\n"
6673"Copy the memo to a new object.");
6674
6675#define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
6676 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006677
6678static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006679_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self)
6680/*[clinic checksum: f8856c4e8a33540886dfbb245f286af3008fa0ad]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006681{
6682 Py_ssize_t i;
6683 PyObject *new_memo = PyDict_New();
6684 if (new_memo == NULL)
6685 return NULL;
6686
6687 for (i = 0; i < self->unpickler->memo_size; i++) {
6688 int status;
6689 PyObject *key, *value;
6690
6691 value = self->unpickler->memo[i];
6692 if (value == NULL)
6693 continue;
6694
6695 key = PyLong_FromSsize_t(i);
6696 if (key == NULL)
6697 goto error;
6698 status = PyDict_SetItem(new_memo, key, value);
6699 Py_DECREF(key);
6700 if (status < 0)
6701 goto error;
6702 }
6703 return new_memo;
6704
6705error:
6706 Py_DECREF(new_memo);
6707 return NULL;
6708}
6709
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006710/*[clinic]
6711_pickle.UnpicklerMemoProxy.__reduce__
6712
6713 self: UnpicklerMemoProxyObject
6714
6715Implement pickling support.
6716[clinic]*/
6717
6718PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
6719"__reduce__()\n"
6720"Implement pickling support.");
6721
6722#define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
6723 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006724
6725static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006726_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self)
6727/*[clinic checksum: ab5516a77659144e1191c7dd70a0c6c7455660bc]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006728{
6729 PyObject *reduce_value;
6730 PyObject *constructor_args;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006731 PyObject *contents = _pickle_UnpicklerMemoProxy_copy(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006732 if (contents == NULL)
6733 return NULL;
6734
6735 reduce_value = PyTuple_New(2);
6736 if (reduce_value == NULL) {
6737 Py_DECREF(contents);
6738 return NULL;
6739 }
6740 constructor_args = PyTuple_New(1);
6741 if (constructor_args == NULL) {
6742 Py_DECREF(contents);
6743 Py_DECREF(reduce_value);
6744 return NULL;
6745 }
6746 PyTuple_SET_ITEM(constructor_args, 0, contents);
6747 Py_INCREF((PyObject *)&PyDict_Type);
6748 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6749 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6750 return reduce_value;
6751}
6752
6753static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006754 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6755 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6756 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006757 {NULL, NULL} /* sentinel */
6758};
6759
6760static void
6761UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6762{
6763 PyObject_GC_UnTrack(self);
6764 Py_XDECREF(self->unpickler);
6765 PyObject_GC_Del((PyObject *)self);
6766}
6767
6768static int
6769UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6770 visitproc visit, void *arg)
6771{
6772 Py_VISIT(self->unpickler);
6773 return 0;
6774}
6775
6776static int
6777UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6778{
6779 Py_CLEAR(self->unpickler);
6780 return 0;
6781}
6782
6783static PyTypeObject UnpicklerMemoProxyType = {
6784 PyVarObject_HEAD_INIT(NULL, 0)
6785 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6786 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6787 0,
6788 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6789 0, /* tp_print */
6790 0, /* tp_getattr */
6791 0, /* tp_setattr */
6792 0, /* tp_compare */
6793 0, /* tp_repr */
6794 0, /* tp_as_number */
6795 0, /* tp_as_sequence */
6796 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006797 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006798 0, /* tp_call */
6799 0, /* tp_str */
6800 PyObject_GenericGetAttr, /* tp_getattro */
6801 PyObject_GenericSetAttr, /* tp_setattro */
6802 0, /* tp_as_buffer */
6803 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6804 0, /* tp_doc */
6805 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6806 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6807 0, /* tp_richcompare */
6808 0, /* tp_weaklistoffset */
6809 0, /* tp_iter */
6810 0, /* tp_iternext */
6811 unpicklerproxy_methods, /* tp_methods */
6812};
6813
6814static PyObject *
6815UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6816{
6817 UnpicklerMemoProxyObject *self;
6818
6819 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6820 &UnpicklerMemoProxyType);
6821 if (self == NULL)
6822 return NULL;
6823 Py_INCREF(unpickler);
6824 self->unpickler = unpickler;
6825 PyObject_GC_Track(self);
6826 return (PyObject *)self;
6827}
6828
6829/*****************************************************************************/
6830
6831
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006832static PyObject *
6833Unpickler_get_memo(UnpicklerObject *self)
6834{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006835 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006836}
6837
6838static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006839Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006840{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006841 PyObject **new_memo;
6842 Py_ssize_t new_memo_size = 0;
6843 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006844
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006845 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006846 PyErr_SetString(PyExc_TypeError,
6847 "attribute deletion is not supported");
6848 return -1;
6849 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006850
6851 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6852 UnpicklerObject *unpickler =
6853 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6854
6855 new_memo_size = unpickler->memo_size;
6856 new_memo = _Unpickler_NewMemo(new_memo_size);
6857 if (new_memo == NULL)
6858 return -1;
6859
6860 for (i = 0; i < new_memo_size; i++) {
6861 Py_XINCREF(unpickler->memo[i]);
6862 new_memo[i] = unpickler->memo[i];
6863 }
6864 }
6865 else if (PyDict_Check(obj)) {
6866 Py_ssize_t i = 0;
6867 PyObject *key, *value;
6868
6869 new_memo_size = PyDict_Size(obj);
6870 new_memo = _Unpickler_NewMemo(new_memo_size);
6871 if (new_memo == NULL)
6872 return -1;
6873
6874 while (PyDict_Next(obj, &i, &key, &value)) {
6875 Py_ssize_t idx;
6876 if (!PyLong_Check(key)) {
6877 PyErr_SetString(PyExc_TypeError,
6878 "memo key must be integers");
6879 goto error;
6880 }
6881 idx = PyLong_AsSsize_t(key);
6882 if (idx == -1 && PyErr_Occurred())
6883 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006884 if (idx < 0) {
6885 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006886 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006887 goto error;
6888 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006889 if (_Unpickler_MemoPut(self, idx, value) < 0)
6890 goto error;
6891 }
6892 }
6893 else {
6894 PyErr_Format(PyExc_TypeError,
6895 "'memo' attribute must be an UnpicklerMemoProxy object"
6896 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006897 return -1;
6898 }
6899
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006900 _Unpickler_MemoCleanup(self);
6901 self->memo_size = new_memo_size;
6902 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006903
6904 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006905
6906 error:
6907 if (new_memo_size) {
6908 i = new_memo_size;
6909 while (--i >= 0) {
6910 Py_XDECREF(new_memo[i]);
6911 }
6912 PyMem_FREE(new_memo);
6913 }
6914 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006915}
6916
6917static PyObject *
6918Unpickler_get_persload(UnpicklerObject *self)
6919{
6920 if (self->pers_func == NULL)
6921 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6922 else
6923 Py_INCREF(self->pers_func);
6924 return self->pers_func;
6925}
6926
6927static int
6928Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6929{
6930 PyObject *tmp;
6931
6932 if (value == NULL) {
6933 PyErr_SetString(PyExc_TypeError,
6934 "attribute deletion is not supported");
6935 return -1;
6936 }
6937 if (!PyCallable_Check(value)) {
6938 PyErr_SetString(PyExc_TypeError,
6939 "persistent_load must be a callable taking "
6940 "one argument");
6941 return -1;
6942 }
6943
6944 tmp = self->pers_func;
6945 Py_INCREF(value);
6946 self->pers_func = value;
6947 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6948
6949 return 0;
6950}
6951
6952static PyGetSetDef Unpickler_getsets[] = {
6953 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6954 {"persistent_load", (getter)Unpickler_get_persload,
6955 (setter)Unpickler_set_persload},
6956 {NULL}
6957};
6958
6959static PyTypeObject Unpickler_Type = {
6960 PyVarObject_HEAD_INIT(NULL, 0)
6961 "_pickle.Unpickler", /*tp_name*/
6962 sizeof(UnpicklerObject), /*tp_basicsize*/
6963 0, /*tp_itemsize*/
6964 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6965 0, /*tp_print*/
6966 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006967 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006968 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006969 0, /*tp_repr*/
6970 0, /*tp_as_number*/
6971 0, /*tp_as_sequence*/
6972 0, /*tp_as_mapping*/
6973 0, /*tp_hash*/
6974 0, /*tp_call*/
6975 0, /*tp_str*/
6976 0, /*tp_getattro*/
6977 0, /*tp_setattro*/
6978 0, /*tp_as_buffer*/
6979 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006980 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006981 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6982 (inquiry)Unpickler_clear, /*tp_clear*/
6983 0, /*tp_richcompare*/
6984 0, /*tp_weaklistoffset*/
6985 0, /*tp_iter*/
6986 0, /*tp_iternext*/
6987 Unpickler_methods, /*tp_methods*/
6988 0, /*tp_members*/
6989 Unpickler_getsets, /*tp_getset*/
6990 0, /*tp_base*/
6991 0, /*tp_dict*/
6992 0, /*tp_descr_get*/
6993 0, /*tp_descr_set*/
6994 0, /*tp_dictoffset*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006995 Unpickler_init, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006996 PyType_GenericAlloc, /*tp_alloc*/
6997 PyType_GenericNew, /*tp_new*/
6998 PyObject_GC_Del, /*tp_free*/
6999 0, /*tp_is_gc*/
7000};
7001
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007002/*[clinic]
7003
7004_pickle.dump
7005
7006 obj: object
7007 file: object
7008 protocol: object = NULL
7009 *
7010 fix_imports: bool = True
7011
7012Write a pickled representation of obj to the open file object file.
7013
7014This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more
7015efficient.
7016
7017The optional protocol argument tells the pickler to use the given protocol
7018supported protocols are 0, 1, 2, 3. The default protocol is 3; a
7019backward-incompatible protocol designed for Python 3.0.
7020
7021Specifying a negative protocol version selects the highest protocol version
7022supported. The higher the protocol used, the more recent the version of
7023Python needed to read the pickle produced.
7024
7025The file argument must have a write() method that accepts a single bytes
7026argument. It can thus be a file object opened for binary writing, a
7027io.BytesIO instance, or any other custom object that meets this interface.
7028
7029If fix_imports is True and protocol is less than 3, pickle will try to
7030map the new Python 3.x names to the old module names used in Python 2.x,
7031so that the pickle data stream is readable with Python 2.x.
7032[clinic]*/
7033
7034PyDoc_STRVAR(_pickle_dump__doc__,
7035"dump(obj, file, protocol=None, *, fix_imports=True)\n"
7036"Write a pickled representation of obj to the open file object file.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007037"\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007038"This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may be more\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007039"efficient.\n"
7040"\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007041"The optional protocol argument tells the pickler to use the given protocol\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007042"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
7043"backward-incompatible protocol designed for Python 3.0.\n"
7044"\n"
7045"Specifying a negative protocol version selects the highest protocol version\n"
7046"supported. The higher the protocol used, the more recent the version of\n"
7047"Python needed to read the pickle produced.\n"
7048"\n"
7049"The file argument must have a write() method that accepts a single bytes\n"
7050"argument. It can thus be a file object opened for binary writing, a\n"
7051"io.BytesIO instance, or any other custom object that meets this interface.\n"
7052"\n"
7053"If fix_imports is True and protocol is less than 3, pickle will try to\n"
7054"map the new Python 3.x names to the old module names used in Python 2.x,\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007055"so that the pickle data stream is readable with Python 2.x.");
7056
7057#define _PICKLE_DUMP_METHODDEF \
7058 {"dump", (PyCFunction)_pickle_dump, METH_VARARGS|METH_KEYWORDS, _pickle_dump__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007059
7060static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports);
7062
7063static PyObject *
7064_pickle_dump(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007066 PyObject *return_value = NULL;
7067 static char *_keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007068 PyObject *obj;
7069 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007070 PyObject *protocol = NULL;
7071 int fix_imports = 1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007073 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7074 "OO|O$p:dump", _keywords,
7075 &obj, &file, &protocol, &fix_imports))
7076 goto exit;
7077 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007078
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007079exit:
7080 return return_value;
7081}
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007082
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007083static PyObject *
7084_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
7085/*[clinic checksum: e442721b16052d921b5e3fbd146d0a62e94a459e]*/
7086{
7087 PicklerObject *pickler = _Pickler_New();
7088
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007089 if (pickler == NULL)
7090 return NULL;
7091
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007092 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007093 goto error;
7094
7095 if (_Pickler_SetOutputStream(pickler, file) < 0)
7096 goto error;
7097
7098 if (dump(pickler, obj) < 0)
7099 goto error;
7100
7101 if (_Pickler_FlushToFile(pickler) < 0)
7102 goto error;
7103
7104 Py_DECREF(pickler);
7105 Py_RETURN_NONE;
7106
7107 error:
7108 Py_XDECREF(pickler);
7109 return NULL;
7110}
7111
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007112/*[clinic]
7113
7114_pickle.dumps
7115
7116 obj: object
7117 protocol: object = NULL
7118 *
7119 fix_imports: bool = True
7120
7121Return the pickled representation of the object as a bytes object.
7122
7123The optional protocol argument tells the pickler to use the given protocol;
7124supported protocols are 0, 1, 2, 3. The default protocol is 3; a
7125backward-incompatible protocol designed for Python 3.0.
7126
7127Specifying a negative protocol version selects the highest protocol version
7128supported. The higher the protocol used, the more recent the version of
7129Python needed to read the pickle produced.
7130
7131If fix_imports is True and *protocol* is less than 3, pickle will try to
7132map the new Python 3.x names to the old module names used in Python 2.x,
7133so that the pickle data stream is readable with Python 2.x.
7134[clinic]*/
7135
7136PyDoc_STRVAR(_pickle_dumps__doc__,
7137"dumps(obj, protocol=None, *, fix_imports=True)\n"
7138"Return the pickled representation of the object as a bytes object.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007139"\n"
7140"The optional protocol argument tells the pickler to use the given protocol;\n"
7141"supported protocols are 0, 1, 2, 3. The default protocol is 3; a\n"
7142"backward-incompatible protocol designed for Python 3.0.\n"
7143"\n"
7144"Specifying a negative protocol version selects the highest protocol version\n"
7145"supported. The higher the protocol used, the more recent the version of\n"
7146"Python needed to read the pickle produced.\n"
7147"\n"
7148"If fix_imports is True and *protocol* is less than 3, pickle will try to\n"
7149"map the new Python 3.x names to the old module names used in Python 2.x,\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150"so that the pickle data stream is readable with Python 2.x.");
7151
7152#define _PICKLE_DUMPS_METHODDEF \
7153 {"dumps", (PyCFunction)_pickle_dumps, METH_VARARGS|METH_KEYWORDS, _pickle_dumps__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007154
7155static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports);
7157
7158static PyObject *
7159_pickle_dumps(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007160{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007161 PyObject *return_value = NULL;
7162 static char *_keywords[] = {"obj", "protocol", "fix_imports", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007163 PyObject *obj;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007164 PyObject *protocol = NULL;
7165 int fix_imports = 1;
7166
7167 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7168 "O|O$p:dumps", _keywords,
7169 &obj, &protocol, &fix_imports))
7170 goto exit;
7171 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
7172
7173exit:
7174 return return_value;
7175}
7176
7177static PyObject *
7178_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
7179/*[clinic checksum: df6262c4c487f537f47aec8a1709318204c1e174]*/
7180{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007181 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007182 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007183
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007184 if (pickler == NULL)
7185 return NULL;
7186
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007188 goto error;
7189
7190 if (dump(pickler, obj) < 0)
7191 goto error;
7192
7193 result = _Pickler_GetString(pickler);
7194 Py_DECREF(pickler);
7195 return result;
7196
7197 error:
7198 Py_XDECREF(pickler);
7199 return NULL;
7200}
7201
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007202/*[clinic]
7203
7204_pickle.load
7205
7206 file: object
7207 *
7208 fix_imports: bool = True
7209 encoding: str = 'ASCII'
7210 errors: str = 'strict'
7211
7212Return a reconstituted object from the pickle data stored in a file.
7213
7214This is equivalent to ``Unpickler(file).load()``, but may be more efficient.
7215
7216The protocol version of the pickle is detected automatically, so no protocol
7217argument is needed. Bytes past the pickled object's representation are
7218ignored.
7219
7220The argument file must have two methods, a read() method that takes an
7221integer argument, and a readline() method that requires no arguments. Both
7222methods should return bytes. Thus *file* can be a binary file object opened
7223for reading, a BytesIO object, or any other custom object that meets this
7224interface.
7225
7226Optional keyword arguments are fix_imports, encoding and errors,
7227which are used to control compatiblity support for pickle stream generated
7228by Python 2.x. If fix_imports is True, pickle will try to map the old
7229Python 2.x names to the new names used in Python 3.x. The encoding and
7230errors tell pickle how to decode 8-bit string instances pickled by Python
72312.x; these default to 'ASCII' and 'strict', respectively.
7232[clinic]*/
7233
7234PyDoc_STRVAR(_pickle_load__doc__,
7235"load(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
7236"Return a reconstituted object from the pickle data stored in a file.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007237"\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007238"This is equivalent to ``Unpickler(file).load()``, but may be more efficient.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007239"\n"
7240"The protocol version of the pickle is detected automatically, so no protocol\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007241"argument is needed. Bytes past the pickled object\'s representation are\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007242"ignored.\n"
7243"\n"
7244"The argument file must have two methods, a read() method that takes an\n"
7245"integer argument, and a readline() method that requires no arguments. Both\n"
7246"methods should return bytes. Thus *file* can be a binary file object opened\n"
7247"for reading, a BytesIO object, or any other custom object that meets this\n"
7248"interface.\n"
7249"\n"
7250"Optional keyword arguments are fix_imports, encoding and errors,\n"
7251"which are used to control compatiblity support for pickle stream generated\n"
7252"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
7253"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
7254"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007255"2.x; these default to \'ASCII\' and \'strict\', respectively.");
7256
7257#define _PICKLE_LOAD_METHODDEF \
7258 {"load", (PyCFunction)_pickle_load, METH_VARARGS|METH_KEYWORDS, _pickle_load__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007259
7260static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007261_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors);
7262
7263static PyObject *
7264_pickle_load(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007265{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007266 PyObject *return_value = NULL;
7267 static char *_keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007268 PyObject *file;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007269 int fix_imports = 1;
7270 const char *encoding = "ASCII";
7271 const char *errors = "strict";
7272
7273 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7274 "O|$pss:load", _keywords,
7275 &file, &fix_imports, &encoding, &errors))
7276 goto exit;
7277 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
7278
7279exit:
7280 return return_value;
7281}
7282
7283static PyObject *
7284_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
7285/*[clinic checksum: e10796f6765b22ce48dca6940f11b3933853ca35]*/
7286{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007287 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007288 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007289
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007290 if (unpickler == NULL)
7291 return NULL;
7292
7293 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7294 goto error;
7295
7296 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7297 goto error;
7298
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007299 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007300
7301 result = load(unpickler);
7302 Py_DECREF(unpickler);
7303 return result;
7304
7305 error:
7306 Py_XDECREF(unpickler);
7307 return NULL;
7308}
7309
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007310/*[clinic]
7311
7312_pickle.loads
7313
7314 data: object
7315 *
7316 fix_imports: bool = True
7317 encoding: str = 'ASCII'
7318 errors: str = 'strict'
7319
7320Return a reconstituted object from the given pickle data.
7321
7322The protocol version of the pickle is detected automatically, so no protocol
7323argument is needed. Bytes past the pickled object's representation are
7324ignored.
7325
7326Optional keyword arguments are fix_imports, encoding and errors, which
7327are used to control compatiblity support for pickle stream generated
7328by Python 2.x. If fix_imports is True, pickle will try to map the old
7329Python 2.x names to the new names used in Python 3.x. The encoding and
7330errors tell pickle how to decode 8-bit string instances pickled by Python
73312.x; these default to 'ASCII' and 'strict', respectively.
7332[clinic]*/
7333
7334PyDoc_STRVAR(_pickle_loads__doc__,
7335"loads(data, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
7336"Return a reconstituted object from the given pickle data.\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007337"\n"
7338"The protocol version of the pickle is detected automatically, so no protocol\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007339"argument is needed. Bytes past the pickled object\'s representation are\n"
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007340"ignored.\n"
7341"\n"
7342"Optional keyword arguments are fix_imports, encoding and errors, which\n"
7343"are used to control compatiblity support for pickle stream generated\n"
7344"by Python 2.x. If fix_imports is True, pickle will try to map the old\n"
7345"Python 2.x names to the new names used in Python 3.x. The encoding and\n"
7346"errors tell pickle how to decode 8-bit string instances pickled by Python\n"
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007347"2.x; these default to \'ASCII\' and \'strict\', respectively.");
7348
7349#define _PICKLE_LOADS_METHODDEF \
7350 {"loads", (PyCFunction)_pickle_loads, METH_VARARGS|METH_KEYWORDS, _pickle_loads__doc__},
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007351
7352static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007353_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors);
7354
7355static PyObject *
7356_pickle_loads(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007357{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007358 PyObject *return_value = NULL;
7359 static char *_keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
7360 PyObject *data;
7361 int fix_imports = 1;
7362 const char *encoding = "ASCII";
7363 const char *errors = "strict";
7364
7365 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
7366 "O|$pss:loads", _keywords,
7367 &data, &fix_imports, &encoding, &errors))
7368 goto exit;
7369 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
7370
7371exit:
7372 return return_value;
7373}
7374
7375static PyObject *
7376_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
7377/*[clinic checksum: 29ee725efcbf51a3533c19cb8261a8e267b7080a]*/
7378{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007379 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007380 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007381
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007382 if (unpickler == NULL)
7383 return NULL;
7384
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007385 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007386 goto error;
7387
7388 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7389 goto error;
7390
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007391 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007392
7393 result = load(unpickler);
7394 Py_DECREF(unpickler);
7395 return result;
7396
7397 error:
7398 Py_XDECREF(unpickler);
7399 return NULL;
7400}
7401
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007402static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007403 _PICKLE_DUMP_METHODDEF
7404 _PICKLE_DUMPS_METHODDEF
7405 _PICKLE_LOAD_METHODDEF
7406 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007407 {NULL, NULL} /* sentinel */
7408};
7409
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007410static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007411pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007412{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007413 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007414 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007415}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007416
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007417static int
7418pickle_traverse(PyObject *m, visitproc visit, void *arg)
7419{
7420 PickleState *st = _Pickle_GetState(m);
7421 Py_VISIT(st->PickleError);
7422 Py_VISIT(st->PicklingError);
7423 Py_VISIT(st->UnpicklingError);
7424 Py_VISIT(st->dispatch_table);
7425 Py_VISIT(st->extension_registry);
7426 Py_VISIT(st->extension_cache);
7427 Py_VISIT(st->inverted_registry);
7428 Py_VISIT(st->name_mapping_2to3);
7429 Py_VISIT(st->import_mapping_2to3);
7430 Py_VISIT(st->name_mapping_3to2);
7431 Py_VISIT(st->import_mapping_3to2);
7432 Py_VISIT(st->codecs_encode);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007433 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007434}
7435
7436static struct PyModuleDef _picklemodule = {
7437 PyModuleDef_HEAD_INIT,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007438 "_pickle", /* m_name */
7439 pickle_module_doc, /* m_doc */
7440 sizeof(PickleState), /* m_size */
7441 pickle_methods, /* m_methods */
7442 NULL, /* m_reload */
7443 pickle_traverse, /* m_traverse */
7444 pickle_clear, /* m_clear */
7445 NULL /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007446};
7447
7448PyMODINIT_FUNC
7449PyInit__pickle(void)
7450{
7451 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007452 PickleState *st;
7453
7454 m = PyState_FindModule(&_picklemodule);
7455 if (m) {
7456 Py_INCREF(m);
7457 return m;
7458 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007459
7460 if (PyType_Ready(&Unpickler_Type) < 0)
7461 return NULL;
7462 if (PyType_Ready(&Pickler_Type) < 0)
7463 return NULL;
7464 if (PyType_Ready(&Pdata_Type) < 0)
7465 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007466 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7467 return NULL;
7468 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7469 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007470
7471 /* Create the module and add the functions. */
7472 m = PyModule_Create(&_picklemodule);
7473 if (m == NULL)
7474 return NULL;
7475
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007476 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007477 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7478 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007479 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007480 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7481 return NULL;
7482
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007483 st = _Pickle_GetState(m);
7484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007485 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007486 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7487 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007488 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007489 st->PicklingError = \
7490 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7491 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007492 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007493 st->UnpicklingError = \
7494 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7495 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007496 return NULL;
7497
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007498 Py_INCREF(st->PickleError);
7499 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007500 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007501 Py_INCREF(st->PicklingError);
7502 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007503 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007504 Py_INCREF(st->UnpicklingError);
7505 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007506 return NULL;
7507
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007508 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007509 return NULL;
7510
7511 return m;
7512}