blob: d4aff5e19e441ee118ce046e25b75fc00ae513db [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800156} PickleState;
157
158/* Forward declaration of the _pickle module definition. */
159static struct PyModuleDef _picklemodule;
160
161/* Given a module object, get its per-module state. */
162static PickleState *
163_Pickle_GetState(PyObject *module)
164{
165 return (PickleState *)PyModule_GetState(module);
166}
167
168/* Find the module instance imported in the currently running sub-interpreter
169 and get its state. */
170static PickleState *
171_Pickle_GetGlobalState(void)
172{
173 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
174}
175
176/* Clear the given pickle module state. */
177static void
178_Pickle_ClearState(PickleState *st)
179{
180 Py_CLEAR(st->PickleError);
181 Py_CLEAR(st->PicklingError);
182 Py_CLEAR(st->UnpicklingError);
183 Py_CLEAR(st->dispatch_table);
184 Py_CLEAR(st->extension_registry);
185 Py_CLEAR(st->extension_cache);
186 Py_CLEAR(st->inverted_registry);
187 Py_CLEAR(st->name_mapping_2to3);
188 Py_CLEAR(st->import_mapping_2to3);
189 Py_CLEAR(st->name_mapping_3to2);
190 Py_CLEAR(st->import_mapping_3to2);
191 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300192 Py_CLEAR(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800193}
194
195/* Initialize the given pickle module state. */
196static int
197_Pickle_InitState(PickleState *st)
198{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300199 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800200 PyObject *copyreg = NULL;
201 PyObject *compat_pickle = NULL;
202 PyObject *codecs = NULL;
203
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300204 builtins = PyEval_GetBuiltins();
205 if (builtins == NULL)
206 goto error;
207 st->getattr = PyDict_GetItemString(builtins, "getattr");
208 if (st->getattr == NULL)
209 goto error;
210 Py_INCREF(st->getattr);
211
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800212 copyreg = PyImport_ImportModule("copyreg");
213 if (!copyreg)
214 goto error;
215 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
216 if (!st->dispatch_table)
217 goto error;
218 if (!PyDict_CheckExact(st->dispatch_table)) {
219 PyErr_Format(PyExc_RuntimeError,
220 "copyreg.dispatch_table should be a dict, not %.200s",
221 Py_TYPE(st->dispatch_table)->tp_name);
222 goto error;
223 }
224 st->extension_registry = \
225 PyObject_GetAttrString(copyreg, "_extension_registry");
226 if (!st->extension_registry)
227 goto error;
228 if (!PyDict_CheckExact(st->extension_registry)) {
229 PyErr_Format(PyExc_RuntimeError,
230 "copyreg._extension_registry should be a dict, "
231 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
232 goto error;
233 }
234 st->inverted_registry = \
235 PyObject_GetAttrString(copyreg, "_inverted_registry");
236 if (!st->inverted_registry)
237 goto error;
238 if (!PyDict_CheckExact(st->inverted_registry)) {
239 PyErr_Format(PyExc_RuntimeError,
240 "copyreg._inverted_registry should be a dict, "
241 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
242 goto error;
243 }
244 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
245 if (!st->extension_cache)
246 goto error;
247 if (!PyDict_CheckExact(st->extension_cache)) {
248 PyErr_Format(PyExc_RuntimeError,
249 "copyreg._extension_cache should be a dict, "
250 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
251 goto error;
252 }
253 Py_CLEAR(copyreg);
254
255 /* Load the 2.x -> 3.x stdlib module mapping tables */
256 compat_pickle = PyImport_ImportModule("_compat_pickle");
257 if (!compat_pickle)
258 goto error;
259 st->name_mapping_2to3 = \
260 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
261 if (!st->name_mapping_2to3)
262 goto error;
263 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
264 PyErr_Format(PyExc_RuntimeError,
265 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
266 Py_TYPE(st->name_mapping_2to3)->tp_name);
267 goto error;
268 }
269 st->import_mapping_2to3 = \
270 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
271 if (!st->import_mapping_2to3)
272 goto error;
273 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
274 PyErr_Format(PyExc_RuntimeError,
275 "_compat_pickle.IMPORT_MAPPING should be a dict, "
276 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
277 goto error;
278 }
279 /* ... and the 3.x -> 2.x mapping tables */
280 st->name_mapping_3to2 = \
281 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
282 if (!st->name_mapping_3to2)
283 goto error;
284 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
285 PyErr_Format(PyExc_RuntimeError,
286 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
287 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
288 goto error;
289 }
290 st->import_mapping_3to2 = \
291 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
292 if (!st->import_mapping_3to2)
293 goto error;
294 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
295 PyErr_Format(PyExc_RuntimeError,
296 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
297 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
298 goto error;
299 }
300 Py_CLEAR(compat_pickle);
301
302 codecs = PyImport_ImportModule("codecs");
303 if (codecs == NULL)
304 goto error;
305 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
306 if (st->codecs_encode == NULL) {
307 goto error;
308 }
309 if (!PyCallable_Check(st->codecs_encode)) {
310 PyErr_Format(PyExc_RuntimeError,
311 "codecs.encode should be a callable, not %.200s",
312 Py_TYPE(st->codecs_encode)->tp_name);
313 goto error;
314 }
315 Py_CLEAR(codecs);
316
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800317 return 0;
318
319 error:
320 Py_CLEAR(copyreg);
321 Py_CLEAR(compat_pickle);
322 Py_CLEAR(codecs);
323 _Pickle_ClearState(st);
324 return -1;
325}
326
327/* Helper for calling a function with a single argument quickly.
328
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800329 This function steals the reference of the given argument. */
330static PyObject *
331_Pickle_FastCall(PyObject *func, PyObject *obj)
332{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800333 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800334 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800335
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800336 /* Note: this function used to reuse the argument tuple. This used to give
337 a slight performance boost with older pickle implementations where many
338 unbuffered reads occurred (thus needing many function calls).
339
340 However, this optimization was removed because it was too complicated
341 to get right. It abused the C API for tuples to mutate them which led
342 to subtle reference counting and concurrency bugs. Furthermore, the
343 introduction of protocol 4 and the prefetching optimization via peek()
344 significantly reduced the number of function calls we do. Thus, the
345 benefits became marginal at best. */
346
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 Py_DECREF(obj);
349 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 PyTuple_SET_ITEM(arg_tuple, 0, obj);
352 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800353 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 return result;
355}
356
357/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000358
359static int
360stack_underflow(void)
361{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800362 PickleState *st = _Pickle_GetGlobalState();
363 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000364 return -1;
365}
366
367/* Internal data type used as the unpickling stack. */
368typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000371 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372} Pdata;
373
374static void
375Pdata_dealloc(Pdata *self)
376{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200377 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000378 while (--i >= 0) {
379 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000381 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000382 PyObject_Del(self);
383}
384
385static PyTypeObject Pdata_Type = {
386 PyVarObject_HEAD_INIT(NULL, 0)
387 "_pickle.Pdata", /*tp_name*/
388 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200389 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000390 (destructor)Pdata_dealloc, /*tp_dealloc*/
391};
392
393static PyObject *
394Pdata_New(void)
395{
396 Pdata *self;
397
398 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
399 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000400 Py_SIZE(self) = 0;
401 self->allocated = 8;
402 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 if (self->data)
404 return (PyObject *)self;
405 Py_DECREF(self);
406 return PyErr_NoMemory();
407}
408
409
410/* Retain only the initial clearto items. If clearto >= the current
411 * number of items, this is a (non-erroneous) NOP.
412 */
413static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200414Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000415{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200416 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417
418 if (clearto < 0)
419 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000420 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000421 return 0;
422
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 while (--i >= clearto) {
424 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427 return 0;
428}
429
430static int
431Pdata_grow(Pdata *self)
432{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000433 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200434 size_t allocated = (size_t)self->allocated;
435 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437 new_allocated = (allocated >> 3) + 6;
438 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200439 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000441 new_allocated += allocated;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200442 if (new_allocated > ((size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
445 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000447
448 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200449 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000450 return 0;
451
452 nomemory:
453 PyErr_NoMemory();
454 return -1;
455}
456
457/* D is a Pdata*. Pop the topmost element and store it into V, which
458 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
459 * is raised and V is set to NULL.
460 */
461static PyObject *
462Pdata_pop(Pdata *self)
463{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800464 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000465 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800466 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000467 return NULL;
468 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000469 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000470}
471#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
472
473static int
474Pdata_push(Pdata *self, PyObject *obj)
475{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477 return -1;
478 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return 0;
481}
482
483/* Push an object on stack, transferring its ownership to the stack. */
484#define PDATA_PUSH(D, O, ER) do { \
485 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
486
487/* Push an object on stack, adding a new reference to the object. */
488#define PDATA_APPEND(D, O, ER) do { \
489 Py_INCREF((O)); \
490 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
491
492static PyObject *
493Pdata_poptuple(Pdata *self, Py_ssize_t start)
494{
495 PyObject *tuple;
496 Py_ssize_t len, i, j;
497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 tuple = PyTuple_New(len);
500 if (tuple == NULL)
501 return NULL;
502 for (i = start, j = 0; j < len; i++, j++)
503 PyTuple_SET_ITEM(tuple, j, self->data[i]);
504
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000505 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000506 return tuple;
507}
508
509static PyObject *
510Pdata_poplist(Pdata *self, Py_ssize_t start)
511{
512 PyObject *list;
513 Py_ssize_t len, i, j;
514
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000515 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000516 list = PyList_New(len);
517 if (list == NULL)
518 return NULL;
519 for (i = start, j = 0; j < len; i++, j++)
520 PyList_SET_ITEM(list, j, self->data[i]);
521
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000522 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000523 return list;
524}
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526typedef struct {
527 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200528 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529} PyMemoEntry;
530
531typedef struct {
532 Py_ssize_t mt_mask;
533 Py_ssize_t mt_used;
534 Py_ssize_t mt_allocated;
535 PyMemoEntry *mt_table;
536} PyMemoTable;
537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538typedef struct PicklerObject {
539 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000541 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000542 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000543 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100544 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000545
546 PyObject *write; /* write() method of the output stream. */
547 PyObject *output_buffer; /* Write into a local bytearray buffer before
548 flushing to the stream. */
549 Py_ssize_t output_len; /* Length of output_buffer. */
550 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000551 int proto; /* Pickle protocol number, >= 0 */
552 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100553 int framing; /* True when framing is enabled, proto >= 4 */
554 Py_ssize_t frame_start; /* Position in output_buffer where the
555 where the current frame begins. -1 if there
556 is no frame currently open. */
557
558 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000559 int fast; /* Enable fast mode if set to a true value.
560 The fast mode disable the usage of memo,
561 therefore speeding the pickling process by
562 not generating superfluous PUT opcodes. It
563 should not be used if with self-referential
564 objects. */
565 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000566 int fix_imports; /* Indicate whether Pickler should fix
567 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000568 PyObject *fast_memo;
569} PicklerObject;
570
571typedef struct UnpicklerObject {
572 PyObject_HEAD
573 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000574
575 /* The unpickler memo is just an array of PyObject *s. Using a dict
576 is unnecessary, since the keys are contiguous ints. */
577 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100578 Py_ssize_t memo_size; /* Capacity of the memo array */
579 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000580
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000581 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582
583 Py_buffer buffer;
584 char *input_buffer;
585 char *input_line;
586 Py_ssize_t input_len;
587 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000588 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000590 PyObject *read; /* read() method of the input stream. */
591 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000592 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594 char *encoding; /* Name of the encoding to be used for
595 decoding strings pickled using Python
596 2.x. The default value is "ASCII" */
597 char *errors; /* Name of errors handling scheme to used when
598 decoding strings. The default value is
599 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500600 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000601 objects. */
602 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
603 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000604 int proto; /* Protocol of the pickle loaded. */
605 int fix_imports; /* Indicate whether Unpickler should fix
606 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000607} UnpicklerObject;
608
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200609typedef struct {
610 PyObject_HEAD
611 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
612} PicklerMemoProxyObject;
613
614typedef struct {
615 PyObject_HEAD
616 UnpicklerObject *unpickler;
617} UnpicklerMemoProxyObject;
618
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000619/* Forward declarations */
620static int save(PicklerObject *, PyObject *, int);
621static int save_reduce(PicklerObject *, PyObject *, PyObject *);
622static PyTypeObject Pickler_Type;
623static PyTypeObject Unpickler_Type;
624
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200625#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000626
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000627/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300628 A custom hashtable mapping void* to Python ints. This is used by the pickler
629 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000630 a bunch of unnecessary object creation. This makes a huge performance
631 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000633#define MT_MINSIZE 8
634#define PERTURB_SHIFT 5
635
636
637static PyMemoTable *
638PyMemoTable_New(void)
639{
640 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
641 if (memo == NULL) {
642 PyErr_NoMemory();
643 return NULL;
644 }
645
646 memo->mt_used = 0;
647 memo->mt_allocated = MT_MINSIZE;
648 memo->mt_mask = MT_MINSIZE - 1;
649 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
650 if (memo->mt_table == NULL) {
651 PyMem_FREE(memo);
652 PyErr_NoMemory();
653 return NULL;
654 }
655 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
656
657 return memo;
658}
659
660static PyMemoTable *
661PyMemoTable_Copy(PyMemoTable *self)
662{
663 Py_ssize_t i;
664 PyMemoTable *new = PyMemoTable_New();
665 if (new == NULL)
666 return NULL;
667
668 new->mt_used = self->mt_used;
669 new->mt_allocated = self->mt_allocated;
670 new->mt_mask = self->mt_mask;
671 /* The table we get from _New() is probably smaller than we wanted.
672 Free it and allocate one that's the right size. */
673 PyMem_FREE(new->mt_table);
674 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
675 if (new->mt_table == NULL) {
676 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200677 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000678 return NULL;
679 }
680 for (i = 0; i < self->mt_allocated; i++) {
681 Py_XINCREF(self->mt_table[i].me_key);
682 }
683 memcpy(new->mt_table, self->mt_table,
684 sizeof(PyMemoEntry) * self->mt_allocated);
685
686 return new;
687}
688
689static Py_ssize_t
690PyMemoTable_Size(PyMemoTable *self)
691{
692 return self->mt_used;
693}
694
695static int
696PyMemoTable_Clear(PyMemoTable *self)
697{
698 Py_ssize_t i = self->mt_allocated;
699
700 while (--i >= 0) {
701 Py_XDECREF(self->mt_table[i].me_key);
702 }
703 self->mt_used = 0;
704 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
705 return 0;
706}
707
708static void
709PyMemoTable_Del(PyMemoTable *self)
710{
711 if (self == NULL)
712 return;
713 PyMemoTable_Clear(self);
714
715 PyMem_FREE(self->mt_table);
716 PyMem_FREE(self);
717}
718
719/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
720 can be considerably simpler than dictobject.c's lookdict(). */
721static PyMemoEntry *
722_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
723{
724 size_t i;
725 size_t perturb;
726 size_t mask = (size_t)self->mt_mask;
727 PyMemoEntry *table = self->mt_table;
728 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000729 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000730
731 i = hash & mask;
732 entry = &table[i];
733 if (entry->me_key == NULL || entry->me_key == key)
734 return entry;
735
736 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
737 i = (i << 2) + i + perturb + 1;
738 entry = &table[i & mask];
739 if (entry->me_key == NULL || entry->me_key == key)
740 return entry;
741 }
742 assert(0); /* Never reached */
743 return NULL;
744}
745
746/* Returns -1 on failure, 0 on success. */
747static int
748_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
749{
750 PyMemoEntry *oldtable = NULL;
751 PyMemoEntry *oldentry, *newentry;
752 Py_ssize_t new_size = MT_MINSIZE;
753 Py_ssize_t to_process;
754
755 assert(min_size > 0);
756
757 /* Find the smallest valid table size >= min_size. */
758 while (new_size < min_size && new_size > 0)
759 new_size <<= 1;
760 if (new_size <= 0) {
761 PyErr_NoMemory();
762 return -1;
763 }
764 /* new_size needs to be a power of two. */
765 assert((new_size & (new_size - 1)) == 0);
766
767 /* Allocate new table. */
768 oldtable = self->mt_table;
769 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
770 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200771 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000772 PyErr_NoMemory();
773 return -1;
774 }
775 self->mt_allocated = new_size;
776 self->mt_mask = new_size - 1;
777 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
778
779 /* Copy entries from the old table. */
780 to_process = self->mt_used;
781 for (oldentry = oldtable; to_process > 0; oldentry++) {
782 if (oldentry->me_key != NULL) {
783 to_process--;
784 /* newentry is a pointer to a chunk of the new
785 mt_table, so we're setting the key:value pair
786 in-place. */
787 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
788 newentry->me_key = oldentry->me_key;
789 newentry->me_value = oldentry->me_value;
790 }
791 }
792
793 /* Deallocate the old table. */
794 PyMem_FREE(oldtable);
795 return 0;
796}
797
798/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200799static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000800PyMemoTable_Get(PyMemoTable *self, PyObject *key)
801{
802 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
803 if (entry->me_key == NULL)
804 return NULL;
805 return &entry->me_value;
806}
807
808/* Returns -1 on failure, 0 on success. */
809static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200810PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000811{
812 PyMemoEntry *entry;
813
814 assert(key != NULL);
815
816 entry = _PyMemoTable_Lookup(self, key);
817 if (entry->me_key != NULL) {
818 entry->me_value = value;
819 return 0;
820 }
821 Py_INCREF(key);
822 entry->me_key = key;
823 entry->me_value = value;
824 self->mt_used++;
825
826 /* If we added a key, we can safely resize. Otherwise just return!
827 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
828 *
829 * Quadrupling the size improves average table sparseness
830 * (reducing collisions) at the cost of some memory. It also halves
831 * the number of expensive resize operations in a growing memo table.
832 *
833 * Very large memo tables (over 50K items) use doubling instead.
834 * This may help applications with severe memory constraints.
835 */
836 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
837 return 0;
838 return _PyMemoTable_ResizeTable(self,
839 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
840}
841
842#undef MT_MINSIZE
843#undef PERTURB_SHIFT
844
845/*************************************************************************/
846
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000847
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000848static int
849_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000850{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000851 Py_CLEAR(self->output_buffer);
852 self->output_buffer =
853 PyBytes_FromStringAndSize(NULL, self->max_output_len);
854 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000855 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000856 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100857 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000858 return 0;
859}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000860
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100861static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100862_write_size64(char *out, size_t value)
863{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200864 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800865
866 assert(sizeof(size_t) <= 8);
867
868 for (i = 0; i < sizeof(size_t); i++) {
869 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
870 }
871 for (i = sizeof(size_t); i < 8; i++) {
872 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800873 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100874}
875
876static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100877_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
878{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100879 qdata[0] = FRAME;
880 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100881}
882
883static int
884_Pickler_CommitFrame(PicklerObject *self)
885{
886 size_t frame_len;
887 char *qdata;
888
889 if (!self->framing || self->frame_start == -1)
890 return 0;
891 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
892 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
893 _Pickler_WriteFrameHeader(self, qdata, frame_len);
894 self->frame_start = -1;
895 return 0;
896}
897
898static int
899_Pickler_OpcodeBoundary(PicklerObject *self)
900{
901 Py_ssize_t frame_len;
902
903 if (!self->framing || self->frame_start == -1)
904 return 0;
905 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
906 if (frame_len >= FRAME_SIZE_TARGET)
907 return _Pickler_CommitFrame(self);
908 else
909 return 0;
910}
911
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000912static PyObject *
913_Pickler_GetString(PicklerObject *self)
914{
915 PyObject *output_buffer = self->output_buffer;
916
917 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100918
919 if (_Pickler_CommitFrame(self))
920 return NULL;
921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000922 self->output_buffer = NULL;
923 /* Resize down to exact size */
924 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
925 return NULL;
926 return output_buffer;
927}
928
929static int
930_Pickler_FlushToFile(PicklerObject *self)
931{
932 PyObject *output, *result;
933
934 assert(self->write != NULL);
935
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937 output = _Pickler_GetString(self);
938 if (output == NULL)
939 return -1;
940
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800941 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 Py_XDECREF(result);
943 return (result == NULL) ? -1 : 0;
944}
945
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200946static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100947_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000948{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000950 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952
953 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954 need_new_frame = (self->framing && self->frame_start == -1);
955
956 if (need_new_frame)
957 n = data_len + FRAME_HEADER_SIZE;
958 else
959 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000960
961 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100962 if (required > self->max_output_len) {
963 /* Make place in buffer for the pickle chunk */
964 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
965 PyErr_NoMemory();
966 return -1;
967 }
968 self->max_output_len = (self->output_len + n) / 2 * 3;
969 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
970 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000971 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100973 if (need_new_frame) {
974 /* Setup new frame */
975 Py_ssize_t frame_start = self->output_len;
976 self->frame_start = frame_start;
977 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
978 /* Write an invalid value, for debugging */
979 buffer[frame_start + i] = 0xFE;
980 }
981 self->output_len += FRAME_HEADER_SIZE;
982 }
983 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100985 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 buffer[self->output_len + i] = s[i];
987 }
988 }
989 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100990 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000991 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100992 self->output_len += data_len;
993 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000994}
995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996static PicklerObject *
997_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000998{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001000
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1002 if (self == NULL)
1003 return NULL;
1004
1005 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001006 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007 self->write = NULL;
1008 self->proto = 0;
1009 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001010 self->framing = 0;
1011 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 self->fast = 0;
1013 self->fast_nesting = 0;
1014 self->fix_imports = 0;
1015 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 self->max_output_len = WRITE_BUF_SIZE;
1017 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001018
1019 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001020 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1021 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001022
1023 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001024 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 return NULL;
1026 }
1027 return self;
1028}
1029
1030static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001031_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001033 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001035 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001037 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001038 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001039 proto = PyLong_AsLong(protocol);
1040 if (proto < 0) {
1041 if (proto == -1 && PyErr_Occurred())
1042 return -1;
1043 proto = HIGHEST_PROTOCOL;
1044 }
1045 else if (proto > HIGHEST_PROTOCOL) {
1046 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1047 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 self->bin = proto > 0;
1053 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054 return 0;
1055}
1056
1057/* Returns -1 (with an exception set) on failure, 0 on success. This may
1058 be called once on a freshly created Pickler. */
1059static int
1060_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1061{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001062 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001064 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001065 if (self->write == NULL) {
1066 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1067 PyErr_SetString(PyExc_TypeError,
1068 "file must have a 'write' attribute");
1069 return -1;
1070 }
1071
1072 return 0;
1073}
1074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075/* Returns the size of the input on success, -1 on failure. This takes its
1076 own reference to `input`. */
1077static Py_ssize_t
1078_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1079{
1080 if (self->buffer.buf != NULL)
1081 PyBuffer_Release(&self->buffer);
1082 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1083 return -1;
1084 self->input_buffer = self->buffer.buf;
1085 self->input_len = self->buffer.len;
1086 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001087 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001088 return self->input_len;
1089}
1090
Antoine Pitrou04248a82010-10-12 20:51:21 +00001091static int
1092_Unpickler_SkipConsumed(UnpicklerObject *self)
1093{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001094 Py_ssize_t consumed;
1095 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001097 consumed = self->next_read_idx - self->prefetched_idx;
1098 if (consumed <= 0)
1099 return 0;
1100
1101 assert(self->peek); /* otherwise we did something wrong */
1102 /* This makes an useless copy... */
1103 r = PyObject_CallFunction(self->read, "n", consumed);
1104 if (r == NULL)
1105 return -1;
1106 Py_DECREF(r);
1107
1108 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001109 return 0;
1110}
1111
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001112static const Py_ssize_t READ_WHOLE_LINE = -1;
1113
1114/* If reading from a file, we need to only pull the bytes we need, since there
1115 may be multiple pickle objects arranged contiguously in the same input
1116 buffer.
1117
1118 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1119 bytes from the input stream/buffer.
1120
1121 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1122 failure; on success, returns the number of bytes read from the file.
1123
1124 On success, self->input_len will be 0; this is intentional so that when
1125 unpickling from a file, the "we've run out of data" code paths will trigger,
1126 causing the Unpickler to go back to the file for more data. Use the returned
1127 size to tell you how much data you can process. */
1128static Py_ssize_t
1129_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1130{
1131 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001132 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133
1134 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001135
Antoine Pitrou04248a82010-10-12 20:51:21 +00001136 if (_Unpickler_SkipConsumed(self) < 0)
1137 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001139 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001140 PyObject *empty_tuple = PyTuple_New(0);
1141 data = PyObject_Call(self->readline, empty_tuple, NULL);
1142 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001143 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001145 PyObject *len;
1146 /* Prefetch some data without advancing the file pointer, if possible */
1147 if (self->peek && n < PREFETCH) {
1148 len = PyLong_FromSsize_t(PREFETCH);
1149 if (len == NULL)
1150 return -1;
1151 data = _Pickle_FastCall(self->peek, len);
1152 if (data == NULL) {
1153 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1154 return -1;
1155 /* peek() is probably not supported by the given file object */
1156 PyErr_Clear();
1157 Py_CLEAR(self->peek);
1158 }
1159 else {
1160 read_size = _Unpickler_SetStringInput(self, data);
1161 Py_DECREF(data);
1162 self->prefetched_idx = 0;
1163 if (n <= read_size)
1164 return n;
1165 }
1166 }
1167 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168 if (len == NULL)
1169 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001170 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001171 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001172 if (data == NULL)
1173 return -1;
1174
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001175 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001176 Py_DECREF(data);
1177 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 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001470 else if ((size_t)*value <= 0xffffffffUL) {
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 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001527 else if ((size_t)idx <= 0xffffffffUL) {
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 *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001549get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550 _Py_static_string(PyId_dot, ".");
1551 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001552 PyObject *dotted_path;
1553 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001554
1555 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001556 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001557 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001558 n = PyList_GET_SIZE(dotted_path);
1559 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001560 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001561 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001562 PyObject *result = PyUnicode_RichCompare(
1563 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1564 int is_equal = (result == Py_True);
1565 assert(PyBool_Check(result));
1566 Py_DECREF(result);
1567 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001568 if (obj == NULL)
1569 PyErr_Format(PyExc_AttributeError,
1570 "Can't pickle local object %R", name);
1571 else
1572 PyErr_Format(PyExc_AttributeError,
1573 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575 return NULL;
1576 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001577 }
1578 return dotted_path;
1579}
1580
1581static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001582get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001583{
1584 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001585 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001586
1587 assert(PyList_CheckExact(names));
1588 Py_INCREF(obj);
1589 n = PyList_GET_SIZE(names);
1590 for (i = 0; i < n; i++) {
1591 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001592 Py_XDECREF(parent);
1593 parent = obj;
1594 obj = PyObject_GetAttr(parent, name);
1595 if (obj == NULL) {
1596 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001597 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001598 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001599 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001600 if (pparent != NULL)
1601 *pparent = parent;
1602 else
1603 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 return obj;
1605}
1606
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001607static void
1608reformat_attribute_error(PyObject *obj, PyObject *name)
1609{
1610 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1611 PyErr_Clear();
1612 PyErr_Format(PyExc_AttributeError,
1613 "Can't get attribute %R on %R", name, obj);
1614 }
1615}
1616
1617
1618static PyObject *
1619getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1620{
1621 PyObject *dotted_path, *attr;
1622
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001623 if (allow_qualname) {
1624 dotted_path = get_dotted_path(obj, name);
1625 if (dotted_path == NULL)
1626 return NULL;
1627 attr = get_deep_attribute(obj, dotted_path, NULL);
1628 Py_DECREF(dotted_path);
1629 }
1630 else
1631 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001632 if (attr == NULL)
1633 reformat_attribute_error(obj, name);
1634 return attr;
1635}
1636
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001637static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001638whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001639{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 PyObject *module_name;
1641 PyObject *modules_dict;
1642 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001643 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 _Py_IDENTIFIER(__module__);
1645 _Py_IDENTIFIER(modules);
1646 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1649
1650 if (module_name == NULL) {
1651 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001652 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654 }
1655 else {
1656 /* In some rare cases (e.g., bound methods of extension types),
1657 __module__ can be None. If it is so, then search sys.modules for
1658 the module of global. */
1659 if (module_name != Py_None)
1660 return module_name;
1661 Py_CLEAR(module_name);
1662 }
1663 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001664
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001666 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001667 if (modules_dict == NULL) {
1668 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001669 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001670 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001671
1672 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001673 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1674 PyObject *candidate;
1675 if (PyUnicode_Check(module_name) &&
1676 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001677 continue;
1678 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001679 continue;
1680
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001681 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001683 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001684 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001685 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001686 continue;
1687 }
1688
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001690 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001692 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001693 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001694 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 }
1696
1697 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001698 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001699 Py_INCREF(module_name);
1700 return module_name;
1701}
1702
1703/* fast_save_enter() and fast_save_leave() are guards against recursive
1704 objects when Pickler is used with the "fast mode" (i.e., with object
1705 memoization disabled). If the nesting of a list or dict object exceed
1706 FAST_NESTING_LIMIT, these guards will start keeping an internal
1707 reference to the seen list or dict objects and check whether these objects
1708 are recursive. These are not strictly necessary, since save() has a
1709 hard-coded recursion limit, but they give a nicer error message than the
1710 typical RuntimeError. */
1711static int
1712fast_save_enter(PicklerObject *self, PyObject *obj)
1713{
1714 /* if fast_nesting < 0, we're doing an error exit. */
1715 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1716 PyObject *key = NULL;
1717 if (self->fast_memo == NULL) {
1718 self->fast_memo = PyDict_New();
1719 if (self->fast_memo == NULL) {
1720 self->fast_nesting = -1;
1721 return 0;
1722 }
1723 }
1724 key = PyLong_FromVoidPtr(obj);
1725 if (key == NULL)
1726 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001727 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001728 Py_DECREF(key);
1729 PyErr_Format(PyExc_ValueError,
1730 "fast mode: can't pickle cyclic objects "
1731 "including object type %.200s at %p",
1732 obj->ob_type->tp_name, obj);
1733 self->fast_nesting = -1;
1734 return 0;
1735 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001736 if (PyErr_Occurred()) {
1737 return 0;
1738 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001739 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1740 Py_DECREF(key);
1741 self->fast_nesting = -1;
1742 return 0;
1743 }
1744 Py_DECREF(key);
1745 }
1746 return 1;
1747}
1748
1749static int
1750fast_save_leave(PicklerObject *self, PyObject *obj)
1751{
1752 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1753 PyObject *key = PyLong_FromVoidPtr(obj);
1754 if (key == NULL)
1755 return 0;
1756 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1757 Py_DECREF(key);
1758 return 0;
1759 }
1760 Py_DECREF(key);
1761 }
1762 return 1;
1763}
1764
1765static int
1766save_none(PicklerObject *self, PyObject *obj)
1767{
1768 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001769 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001770 return -1;
1771
1772 return 0;
1773}
1774
1775static int
1776save_bool(PicklerObject *self, PyObject *obj)
1777{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001779 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001780 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001781 return -1;
1782 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001783 else {
1784 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1785 * so that unpicklers written before bools were introduced unpickle them
1786 * as ints, but unpicklers after can recognize that bools were intended.
1787 * Note that protocol 2 added direct ways to pickle bools.
1788 */
1789 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1790 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1791 return -1;
1792 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001793 return 0;
1794}
1795
1796static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001797save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001798{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001799 PyObject *repr = NULL;
1800 Py_ssize_t size;
1801 long val;
1802 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001803
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001804 const char long_op = LONG;
1805
1806 val= PyLong_AsLong(obj);
1807 if (val == -1 && PyErr_Occurred()) {
1808 /* out of range for int pickling */
1809 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001810 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001811 else if (self->bin &&
1812 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001813 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001814 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001815
1816 Note: we can't use -0x80000000L in the above condition because some
1817 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1818 before applying the unary minus when sizeof(long) <= 4. The
1819 resulting value stays unsigned which is commonly not what we want,
1820 so MSVC happily warns us about it. However, that result would have
1821 been fine because we guard for sizeof(long) <= 4 which turns the
1822 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001823 char pdata[32];
1824 Py_ssize_t len = 0;
1825
1826 pdata[1] = (unsigned char)(val & 0xff);
1827 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1828 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1829 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001830
1831 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1832 if (pdata[2] == 0) {
1833 pdata[0] = BININT1;
1834 len = 2;
1835 }
1836 else {
1837 pdata[0] = BININT2;
1838 len = 3;
1839 }
1840 }
1841 else {
1842 pdata[0] = BININT;
1843 len = 5;
1844 }
1845
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001846 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001848
1849 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001850 }
1851
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 if (self->proto >= 2) {
1853 /* Linear-time pickling. */
1854 size_t nbits;
1855 size_t nbytes;
1856 unsigned char *pdata;
1857 char header[5];
1858 int i;
1859 int sign = _PyLong_Sign(obj);
1860
1861 if (sign == 0) {
1862 header[0] = LONG1;
1863 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001864 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 goto error;
1866 return 0;
1867 }
1868 nbits = _PyLong_NumBits(obj);
1869 if (nbits == (size_t)-1 && PyErr_Occurred())
1870 goto error;
1871 /* How many bytes do we need? There are nbits >> 3 full
1872 * bytes of data, and nbits & 7 leftover bits. If there
1873 * are any leftover bits, then we clearly need another
1874 * byte. Wnat's not so obvious is that we *probably*
1875 * need another byte even if there aren't any leftovers:
1876 * the most-significant bit of the most-significant byte
1877 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001878 * opposite of the one we need. The exception is ints
1879 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001880 * its own 256's-complement, so has the right sign bit
1881 * even without the extra byte. That's a pain to check
1882 * for in advance, though, so we always grab an extra
1883 * byte at the start, and cut it back later if possible.
1884 */
1885 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001886 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001887 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001888 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 goto error;
1890 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001891 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001892 if (repr == NULL)
1893 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001894 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001895 i = _PyLong_AsByteArray((PyLongObject *)obj,
1896 pdata, nbytes,
1897 1 /* little endian */ , 1 /* signed */ );
1898 if (i < 0)
1899 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001900 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001901 * needed. This is so iff the MSB is all redundant sign
1902 * bits.
1903 */
1904 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001905 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 pdata[nbytes - 1] == 0xff &&
1907 (pdata[nbytes - 2] & 0x80) != 0) {
1908 nbytes--;
1909 }
1910
1911 if (nbytes < 256) {
1912 header[0] = LONG1;
1913 header[1] = (unsigned char)nbytes;
1914 size = 2;
1915 }
1916 else {
1917 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001918 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 for (i = 1; i < 5; i++) {
1920 header[i] = (unsigned char)(size & 0xff);
1921 size >>= 8;
1922 }
1923 size = 5;
1924 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001925 if (_Pickler_Write(self, header, size) < 0 ||
1926 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927 goto error;
1928 }
1929 else {
1930 char *string;
1931
Mark Dickinson8dd05142009-01-20 20:43:58 +00001932 /* proto < 2: write the repr and newline. This is quadratic-time (in
1933 the number of digits), in both directions. We add a trailing 'L'
1934 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935
1936 repr = PyObject_Repr(obj);
1937 if (repr == NULL)
1938 goto error;
1939
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001940 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001941 if (string == NULL)
1942 goto error;
1943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001944 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1945 _Pickler_Write(self, string, size) < 0 ||
1946 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947 goto error;
1948 }
1949
1950 if (0) {
1951 error:
1952 status = -1;
1953 }
1954 Py_XDECREF(repr);
1955
1956 return status;
1957}
1958
1959static int
1960save_float(PicklerObject *self, PyObject *obj)
1961{
1962 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1963
1964 if (self->bin) {
1965 char pdata[9];
1966 pdata[0] = BINFLOAT;
1967 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1968 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001969 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001971 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001973 int result = -1;
1974 char *buf = NULL;
1975 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001977 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001978 goto done;
1979
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001980 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001981 if (!buf) {
1982 PyErr_NoMemory();
1983 goto done;
1984 }
1985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001986 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001987 goto done;
1988
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001989 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001990 goto done;
1991
1992 result = 0;
1993done:
1994 PyMem_Free(buf);
1995 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 }
1997
1998 return 0;
1999}
2000
2001static int
2002save_bytes(PicklerObject *self, PyObject *obj)
2003{
2004 if (self->proto < 3) {
2005 /* Older pickle protocols do not have an opcode for pickling bytes
2006 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002007 the __reduce__ method) to permit bytes object unpickling.
2008
2009 Here we use a hack to be compatible with Python 2. Since in Python
2010 2 'bytes' is just an alias for 'str' (which has different
2011 parameters than the actual bytes object), we use codecs.encode
2012 to create the appropriate 'str' object when unpickled using
2013 Python 2 *and* the appropriate 'bytes' object when unpickled
2014 using Python 3. Again this is a hack and we don't need to do this
2015 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 int status;
2018
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002019 if (PyBytes_GET_SIZE(obj) == 0) {
2020 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2021 }
2022 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002023 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002024 PyObject *unicode_str =
2025 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2026 PyBytes_GET_SIZE(obj),
2027 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 _Py_IDENTIFIER(latin1);
2029
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002030 if (unicode_str == NULL)
2031 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002032 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002033 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002034 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002035 Py_DECREF(unicode_str);
2036 }
2037
2038 if (reduce_value == NULL)
2039 return -1;
2040
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 /* save_reduce() will memoize the object automatically. */
2042 status = save_reduce(self, reduce_value, obj);
2043 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 return status;
2045 }
2046 else {
2047 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002048 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002049 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002051 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052 if (size < 0)
2053 return -1;
2054
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002055 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 header[0] = SHORT_BINBYTES;
2057 header[1] = (unsigned char)size;
2058 len = 2;
2059 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002060 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061 header[0] = BINBYTES;
2062 header[1] = (unsigned char)(size & 0xff);
2063 header[2] = (unsigned char)((size >> 8) & 0xff);
2064 header[3] = (unsigned char)((size >> 16) & 0xff);
2065 header[4] = (unsigned char)((size >> 24) & 0xff);
2066 len = 5;
2067 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002068 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002069 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002070 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002071 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002072 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002074 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002075 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 return -1; /* string too large */
2077 }
2078
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002079 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002080 return -1;
2081
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002082 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002083 return -1;
2084
2085 if (memo_put(self, obj) < 0)
2086 return -1;
2087
2088 return 0;
2089 }
2090}
2091
2092/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2093 backslash and newline characters to \uXXXX escapes. */
2094static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002095raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002097 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002099 Py_ssize_t i, size;
2100 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002101 void *data;
2102 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002104 if (PyUnicode_READY(obj))
2105 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002107 size = PyUnicode_GET_LENGTH(obj);
2108 data = PyUnicode_DATA(obj);
2109 kind = PyUnicode_KIND(obj);
2110 if (kind == PyUnicode_4BYTE_KIND)
2111 expandsize = 10;
2112 else
2113 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002114
Victor Stinner049e5092014-08-17 22:20:00 +02002115 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002116 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002117 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002118 if (repr == NULL)
2119 return NULL;
2120 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002121 return repr;
2122 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123
Victor Stinner7270b7f2014-08-17 21:14:46 +02002124 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002125 for (i=0; i < size; i++) {
2126 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002127 /* Map 32-bit characters to '\Uxxxxxxxx' */
2128 if (ch >= 0x10000) {
2129 *p++ = '\\';
2130 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002131 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2132 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2133 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2134 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2135 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2136 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2137 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2138 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002139 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002140 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002141 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 *p++ = '\\';
2143 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002144 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2147 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002148 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002149 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002150 else
2151 *p++ = (char) ch;
2152 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002153 size = p - PyBytes_AS_STRING(repr);
2154 if (_PyBytes_Resize(&repr, size) < 0)
2155 return NULL;
2156 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002157}
2158
2159static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002160write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2161{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002162 char header[9];
2163 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002164
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002165 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002166 if (size <= 0xff && self->proto >= 4) {
2167 header[0] = SHORT_BINUNICODE;
2168 header[1] = (unsigned char)(size & 0xff);
2169 len = 2;
2170 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002171 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002172 header[0] = BINUNICODE;
2173 header[1] = (unsigned char)(size & 0xff);
2174 header[2] = (unsigned char)((size >> 8) & 0xff);
2175 header[3] = (unsigned char)((size >> 16) & 0xff);
2176 header[4] = (unsigned char)((size >> 24) & 0xff);
2177 len = 5;
2178 }
2179 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002180 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002181 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002182 len = 9;
2183 }
2184 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002185 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002186 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002187 return -1;
2188 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002189
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002190 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002191 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192 if (_Pickler_Write(self, data, size) < 0)
2193 return -1;
2194
2195 return 0;
2196}
2197
2198static int
2199write_unicode_binary(PicklerObject *self, PyObject *obj)
2200{
2201 PyObject *encoded = NULL;
2202 Py_ssize_t size;
2203 char *data;
2204 int r;
2205
2206 if (PyUnicode_READY(obj))
2207 return -1;
2208
2209 data = PyUnicode_AsUTF8AndSize(obj, &size);
2210 if (data != NULL)
2211 return write_utf8(self, data, size);
2212
2213 /* Issue #8383: for strings with lone surrogates, fallback on the
2214 "surrogatepass" error handler. */
2215 PyErr_Clear();
2216 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2217 if (encoded == NULL)
2218 return -1;
2219
2220 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2221 PyBytes_GET_SIZE(encoded));
2222 Py_DECREF(encoded);
2223 return r;
2224}
2225
2226static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002227save_unicode(PicklerObject *self, PyObject *obj)
2228{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002229 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002230 if (write_unicode_binary(self, obj) < 0)
2231 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002232 }
2233 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002234 PyObject *encoded;
2235 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002236 const char unicode_op = UNICODE;
2237
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002238 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002239 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002240 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002241
Antoine Pitrou299978d2013-04-07 17:38:11 +02002242 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2243 Py_DECREF(encoded);
2244 return -1;
2245 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002246
2247 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2249 Py_DECREF(encoded);
2250 return -1;
2251 }
2252 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002254 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 }
2257 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002258 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261}
2262
2263/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2264static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002265store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002267 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002268
2269 assert(PyTuple_Size(t) == len);
2270
2271 for (i = 0; i < len; i++) {
2272 PyObject *element = PyTuple_GET_ITEM(t, i);
2273
2274 if (element == NULL)
2275 return -1;
2276 if (save(self, element, 0) < 0)
2277 return -1;
2278 }
2279
2280 return 0;
2281}
2282
2283/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2284 * used across protocols to minimize the space needed to pickle them.
2285 * Tuples are also the only builtin immutable type that can be recursive
2286 * (a tuple can be reached from itself), and that requires some subtle
2287 * magic so that it works in all cases. IOW, this is a long routine.
2288 */
2289static int
2290save_tuple(PicklerObject *self, PyObject *obj)
2291{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002292 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293
2294 const char mark_op = MARK;
2295 const char tuple_op = TUPLE;
2296 const char pop_op = POP;
2297 const char pop_mark_op = POP_MARK;
2298 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2299
2300 if ((len = PyTuple_Size(obj)) < 0)
2301 return -1;
2302
2303 if (len == 0) {
2304 char pdata[2];
2305
2306 if (self->proto) {
2307 pdata[0] = EMPTY_TUPLE;
2308 len = 1;
2309 }
2310 else {
2311 pdata[0] = MARK;
2312 pdata[1] = TUPLE;
2313 len = 2;
2314 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316 return -1;
2317 return 0;
2318 }
2319
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002320 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 * saving the tuple elements, the tuple must be recursive, in
2322 * which case we'll pop everything we put on the stack, and fetch
2323 * its value from the memo.
2324 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325 if (len <= 3 && self->proto >= 2) {
2326 /* Use TUPLE{1,2,3} opcodes. */
2327 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002328 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 /* pop the len elements */
2332 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002333 if (_Pickler_Write(self, &pop_op, 1) < 0)
2334 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (memo_get(self, obj) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 return 0;
2340 }
2341 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2343 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 }
2345 goto memoize;
2346 }
2347
2348 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2349 * Generate MARK e1 e2 ... TUPLE
2350 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 if (_Pickler_Write(self, &mark_op, 1) < 0)
2352 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353
2354 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002355 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002357 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 /* pop the stack stuff we pushed */
2359 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002360 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2361 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002362 }
2363 else {
2364 /* Note that we pop one more than len, to remove
2365 * the MARK too.
2366 */
2367 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002368 if (_Pickler_Write(self, &pop_op, 1) < 0)
2369 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002370 }
2371 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002372 if (memo_get(self, obj) < 0)
2373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 return 0;
2376 }
2377 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2379 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 }
2381
2382 memoize:
2383 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002386 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002387}
2388
2389/* iter is an iterator giving items, and we batch up chunks of
2390 * MARK item item ... item APPENDS
2391 * opcode sequences. Calling code should have arranged to first create an
2392 * empty list, or list-like object, for the APPENDS to operate on.
2393 * Returns 0 on success, <0 on error.
2394 */
2395static int
2396batch_list(PicklerObject *self, PyObject *iter)
2397{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002398 PyObject *obj = NULL;
2399 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 int i, n;
2401
2402 const char mark_op = MARK;
2403 const char append_op = APPEND;
2404 const char appends_op = APPENDS;
2405
2406 assert(iter != NULL);
2407
2408 /* XXX: I think this function could be made faster by avoiding the
2409 iterator interface and fetching objects directly from list using
2410 PyList_GET_ITEM.
2411 */
2412
2413 if (self->proto == 0) {
2414 /* APPENDS isn't available; do one at a time. */
2415 for (;;) {
2416 obj = PyIter_Next(iter);
2417 if (obj == NULL) {
2418 if (PyErr_Occurred())
2419 return -1;
2420 break;
2421 }
2422 i = save(self, obj, 0);
2423 Py_DECREF(obj);
2424 if (i < 0)
2425 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002426 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002427 return -1;
2428 }
2429 return 0;
2430 }
2431
2432 /* proto > 0: write in batches of BATCHSIZE. */
2433 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002434 /* Get first item */
2435 firstitem = PyIter_Next(iter);
2436 if (firstitem == NULL) {
2437 if (PyErr_Occurred())
2438 goto error;
2439
2440 /* nothing more to add */
2441 break;
2442 }
2443
2444 /* Try to get a second item */
2445 obj = PyIter_Next(iter);
2446 if (obj == NULL) {
2447 if (PyErr_Occurred())
2448 goto error;
2449
2450 /* Only one item to write */
2451 if (save(self, firstitem, 0) < 0)
2452 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002453 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002454 goto error;
2455 Py_CLEAR(firstitem);
2456 break;
2457 }
2458
2459 /* More than one item to write */
2460
2461 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002462 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002463 goto error;
2464
2465 if (save(self, firstitem, 0) < 0)
2466 goto error;
2467 Py_CLEAR(firstitem);
2468 n = 1;
2469
2470 /* Fetch and save up to BATCHSIZE items */
2471 while (obj) {
2472 if (save(self, obj, 0) < 0)
2473 goto error;
2474 Py_CLEAR(obj);
2475 n += 1;
2476
2477 if (n == BATCHSIZE)
2478 break;
2479
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002480 obj = PyIter_Next(iter);
2481 if (obj == NULL) {
2482 if (PyErr_Occurred())
2483 goto error;
2484 break;
2485 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486 }
2487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002488 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002489 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002490
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002491 } while (n == BATCHSIZE);
2492 return 0;
2493
2494 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002495 Py_XDECREF(firstitem);
2496 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002497 return -1;
2498}
2499
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002500/* This is a variant of batch_list() above, specialized for lists (with no
2501 * support for list subclasses). Like batch_list(), we batch up chunks of
2502 * MARK item item ... item APPENDS
2503 * opcode sequences. Calling code should have arranged to first create an
2504 * empty list, or list-like object, for the APPENDS to operate on.
2505 * Returns 0 on success, -1 on error.
2506 *
2507 * This version is considerably faster than batch_list(), if less general.
2508 *
2509 * Note that this only works for protocols > 0.
2510 */
2511static int
2512batch_list_exact(PicklerObject *self, PyObject *obj)
2513{
2514 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002515 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516
2517 const char append_op = APPEND;
2518 const char appends_op = APPENDS;
2519 const char mark_op = MARK;
2520
2521 assert(obj != NULL);
2522 assert(self->proto > 0);
2523 assert(PyList_CheckExact(obj));
2524
2525 if (PyList_GET_SIZE(obj) == 1) {
2526 item = PyList_GET_ITEM(obj, 0);
2527 if (save(self, item, 0) < 0)
2528 return -1;
2529 if (_Pickler_Write(self, &append_op, 1) < 0)
2530 return -1;
2531 return 0;
2532 }
2533
2534 /* Write in batches of BATCHSIZE. */
2535 total = 0;
2536 do {
2537 this_batch = 0;
2538 if (_Pickler_Write(self, &mark_op, 1) < 0)
2539 return -1;
2540 while (total < PyList_GET_SIZE(obj)) {
2541 item = PyList_GET_ITEM(obj, total);
2542 if (save(self, item, 0) < 0)
2543 return -1;
2544 total++;
2545 if (++this_batch == BATCHSIZE)
2546 break;
2547 }
2548 if (_Pickler_Write(self, &appends_op, 1) < 0)
2549 return -1;
2550
2551 } while (total < PyList_GET_SIZE(obj));
2552
2553 return 0;
2554}
2555
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556static int
2557save_list(PicklerObject *self, PyObject *obj)
2558{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002560 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002561 int status = 0;
2562
2563 if (self->fast && !fast_save_enter(self, obj))
2564 goto error;
2565
2566 /* Create an empty list. */
2567 if (self->bin) {
2568 header[0] = EMPTY_LIST;
2569 len = 1;
2570 }
2571 else {
2572 header[0] = MARK;
2573 header[1] = LIST;
2574 len = 2;
2575 }
2576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002577 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002578 goto error;
2579
2580 /* Get list length, and bow out early if empty. */
2581 if ((len = PyList_Size(obj)) < 0)
2582 goto error;
2583
2584 if (memo_put(self, obj) < 0)
2585 goto error;
2586
2587 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002588 /* Materialize the list elements. */
2589 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002590 if (Py_EnterRecursiveCall(" while pickling an object"))
2591 goto error;
2592 status = batch_list_exact(self, obj);
2593 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002594 } else {
2595 PyObject *iter = PyObject_GetIter(obj);
2596 if (iter == NULL)
2597 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002598
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002599 if (Py_EnterRecursiveCall(" while pickling an object")) {
2600 Py_DECREF(iter);
2601 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002602 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002603 status = batch_list(self, iter);
2604 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002605 Py_DECREF(iter);
2606 }
2607 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002608 if (0) {
2609 error:
2610 status = -1;
2611 }
2612
2613 if (self->fast && !fast_save_leave(self, obj))
2614 status = -1;
2615
2616 return status;
2617}
2618
2619/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2620 * MARK key value ... key value SETITEMS
2621 * opcode sequences. Calling code should have arranged to first create an
2622 * empty dict, or dict-like object, for the SETITEMS to operate on.
2623 * Returns 0 on success, <0 on error.
2624 *
2625 * This is very much like batch_list(). The difference between saving
2626 * elements directly, and picking apart two-tuples, is so long-winded at
2627 * the C level, though, that attempts to combine these routines were too
2628 * ugly to bear.
2629 */
2630static int
2631batch_dict(PicklerObject *self, PyObject *iter)
2632{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002633 PyObject *obj = NULL;
2634 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002635 int i, n;
2636
2637 const char mark_op = MARK;
2638 const char setitem_op = SETITEM;
2639 const char setitems_op = SETITEMS;
2640
2641 assert(iter != NULL);
2642
2643 if (self->proto == 0) {
2644 /* SETITEMS isn't available; do one at a time. */
2645 for (;;) {
2646 obj = PyIter_Next(iter);
2647 if (obj == NULL) {
2648 if (PyErr_Occurred())
2649 return -1;
2650 break;
2651 }
2652 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2653 PyErr_SetString(PyExc_TypeError, "dict items "
2654 "iterator must return 2-tuples");
2655 return -1;
2656 }
2657 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2658 if (i >= 0)
2659 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2660 Py_DECREF(obj);
2661 if (i < 0)
2662 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002663 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002664 return -1;
2665 }
2666 return 0;
2667 }
2668
2669 /* proto > 0: write in batches of BATCHSIZE. */
2670 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002671 /* Get first item */
2672 firstitem = PyIter_Next(iter);
2673 if (firstitem == NULL) {
2674 if (PyErr_Occurred())
2675 goto error;
2676
2677 /* nothing more to add */
2678 break;
2679 }
2680 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2681 PyErr_SetString(PyExc_TypeError, "dict items "
2682 "iterator must return 2-tuples");
2683 goto error;
2684 }
2685
2686 /* Try to get a second item */
2687 obj = PyIter_Next(iter);
2688 if (obj == NULL) {
2689 if (PyErr_Occurred())
2690 goto error;
2691
2692 /* Only one item to write */
2693 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2694 goto error;
2695 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2696 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002697 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002698 goto error;
2699 Py_CLEAR(firstitem);
2700 break;
2701 }
2702
2703 /* More than one item to write */
2704
2705 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002706 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002707 goto error;
2708
2709 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2710 goto error;
2711 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2712 goto error;
2713 Py_CLEAR(firstitem);
2714 n = 1;
2715
2716 /* Fetch and save up to BATCHSIZE items */
2717 while (obj) {
2718 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2719 PyErr_SetString(PyExc_TypeError, "dict items "
2720 "iterator must return 2-tuples");
2721 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002722 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002723 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2724 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2725 goto error;
2726 Py_CLEAR(obj);
2727 n += 1;
2728
2729 if (n == BATCHSIZE)
2730 break;
2731
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002732 obj = PyIter_Next(iter);
2733 if (obj == NULL) {
2734 if (PyErr_Occurred())
2735 goto error;
2736 break;
2737 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002738 }
2739
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002740 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002741 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002742
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002743 } while (n == BATCHSIZE);
2744 return 0;
2745
2746 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002747 Py_XDECREF(firstitem);
2748 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002749 return -1;
2750}
2751
Collin Winter5c9b02d2009-05-25 05:43:30 +00002752/* This is a variant of batch_dict() above that specializes for dicts, with no
2753 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2754 * MARK key value ... key value SETITEMS
2755 * opcode sequences. Calling code should have arranged to first create an
2756 * empty dict, or dict-like object, for the SETITEMS to operate on.
2757 * Returns 0 on success, -1 on error.
2758 *
2759 * Note that this currently doesn't work for protocol 0.
2760 */
2761static int
2762batch_dict_exact(PicklerObject *self, PyObject *obj)
2763{
2764 PyObject *key = NULL, *value = NULL;
2765 int i;
2766 Py_ssize_t dict_size, ppos = 0;
2767
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002768 const char mark_op = MARK;
2769 const char setitem_op = SETITEM;
2770 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002771
2772 assert(obj != NULL);
2773 assert(self->proto > 0);
2774
2775 dict_size = PyDict_Size(obj);
2776
2777 /* Special-case len(d) == 1 to save space. */
2778 if (dict_size == 1) {
2779 PyDict_Next(obj, &ppos, &key, &value);
2780 if (save(self, key, 0) < 0)
2781 return -1;
2782 if (save(self, value, 0) < 0)
2783 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002784 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002785 return -1;
2786 return 0;
2787 }
2788
2789 /* Write in batches of BATCHSIZE. */
2790 do {
2791 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002792 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002793 return -1;
2794 while (PyDict_Next(obj, &ppos, &key, &value)) {
2795 if (save(self, key, 0) < 0)
2796 return -1;
2797 if (save(self, value, 0) < 0)
2798 return -1;
2799 if (++i == BATCHSIZE)
2800 break;
2801 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002802 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002803 return -1;
2804 if (PyDict_Size(obj) != dict_size) {
2805 PyErr_Format(
2806 PyExc_RuntimeError,
2807 "dictionary changed size during iteration");
2808 return -1;
2809 }
2810
2811 } while (i == BATCHSIZE);
2812 return 0;
2813}
2814
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002815static int
2816save_dict(PicklerObject *self, PyObject *obj)
2817{
2818 PyObject *items, *iter;
2819 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002820 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002821 int status = 0;
2822
2823 if (self->fast && !fast_save_enter(self, obj))
2824 goto error;
2825
2826 /* Create an empty dict. */
2827 if (self->bin) {
2828 header[0] = EMPTY_DICT;
2829 len = 1;
2830 }
2831 else {
2832 header[0] = MARK;
2833 header[1] = DICT;
2834 len = 2;
2835 }
2836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002837 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002838 goto error;
2839
2840 /* Get dict size, and bow out early if empty. */
2841 if ((len = PyDict_Size(obj)) < 0)
2842 goto error;
2843
2844 if (memo_put(self, obj) < 0)
2845 goto error;
2846
2847 if (len != 0) {
2848 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002849 if (PyDict_CheckExact(obj) && self->proto > 0) {
2850 /* We can take certain shortcuts if we know this is a dict and
2851 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002852 if (Py_EnterRecursiveCall(" while pickling an object"))
2853 goto error;
2854 status = batch_dict_exact(self, obj);
2855 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002856 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002857 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002858
2859 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002860 if (items == NULL)
2861 goto error;
2862 iter = PyObject_GetIter(items);
2863 Py_DECREF(items);
2864 if (iter == NULL)
2865 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002866 if (Py_EnterRecursiveCall(" while pickling an object")) {
2867 Py_DECREF(iter);
2868 goto error;
2869 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002870 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002871 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002872 Py_DECREF(iter);
2873 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002874 }
2875
2876 if (0) {
2877 error:
2878 status = -1;
2879 }
2880
2881 if (self->fast && !fast_save_leave(self, obj))
2882 status = -1;
2883
2884 return status;
2885}
2886
2887static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002888save_set(PicklerObject *self, PyObject *obj)
2889{
2890 PyObject *item;
2891 int i;
2892 Py_ssize_t set_size, ppos = 0;
2893 Py_hash_t hash;
2894
2895 const char empty_set_op = EMPTY_SET;
2896 const char mark_op = MARK;
2897 const char additems_op = ADDITEMS;
2898
2899 if (self->proto < 4) {
2900 PyObject *items;
2901 PyObject *reduce_value;
2902 int status;
2903
2904 items = PySequence_List(obj);
2905 if (items == NULL) {
2906 return -1;
2907 }
2908 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2909 Py_DECREF(items);
2910 if (reduce_value == NULL) {
2911 return -1;
2912 }
2913 /* save_reduce() will memoize the object automatically. */
2914 status = save_reduce(self, reduce_value, obj);
2915 Py_DECREF(reduce_value);
2916 return status;
2917 }
2918
2919 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2920 return -1;
2921
2922 if (memo_put(self, obj) < 0)
2923 return -1;
2924
2925 set_size = PySet_GET_SIZE(obj);
2926 if (set_size == 0)
2927 return 0; /* nothing to do */
2928
2929 /* Write in batches of BATCHSIZE. */
2930 do {
2931 i = 0;
2932 if (_Pickler_Write(self, &mark_op, 1) < 0)
2933 return -1;
2934 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2935 if (save(self, item, 0) < 0)
2936 return -1;
2937 if (++i == BATCHSIZE)
2938 break;
2939 }
2940 if (_Pickler_Write(self, &additems_op, 1) < 0)
2941 return -1;
2942 if (PySet_GET_SIZE(obj) != set_size) {
2943 PyErr_Format(
2944 PyExc_RuntimeError,
2945 "set changed size during iteration");
2946 return -1;
2947 }
2948 } while (i == BATCHSIZE);
2949
2950 return 0;
2951}
2952
2953static int
2954save_frozenset(PicklerObject *self, PyObject *obj)
2955{
2956 PyObject *iter;
2957
2958 const char mark_op = MARK;
2959 const char frozenset_op = FROZENSET;
2960
2961 if (self->fast && !fast_save_enter(self, obj))
2962 return -1;
2963
2964 if (self->proto < 4) {
2965 PyObject *items;
2966 PyObject *reduce_value;
2967 int status;
2968
2969 items = PySequence_List(obj);
2970 if (items == NULL) {
2971 return -1;
2972 }
2973 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2974 items);
2975 Py_DECREF(items);
2976 if (reduce_value == NULL) {
2977 return -1;
2978 }
2979 /* save_reduce() will memoize the object automatically. */
2980 status = save_reduce(self, reduce_value, obj);
2981 Py_DECREF(reduce_value);
2982 return status;
2983 }
2984
2985 if (_Pickler_Write(self, &mark_op, 1) < 0)
2986 return -1;
2987
2988 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002989 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002990 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002991 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002992 for (;;) {
2993 PyObject *item;
2994
2995 item = PyIter_Next(iter);
2996 if (item == NULL) {
2997 if (PyErr_Occurred()) {
2998 Py_DECREF(iter);
2999 return -1;
3000 }
3001 break;
3002 }
3003 if (save(self, item, 0) < 0) {
3004 Py_DECREF(item);
3005 Py_DECREF(iter);
3006 return -1;
3007 }
3008 Py_DECREF(item);
3009 }
3010 Py_DECREF(iter);
3011
3012 /* If the object is already in the memo, this means it is
3013 recursive. In this case, throw away everything we put on the
3014 stack, and fetch the object back from the memo. */
3015 if (PyMemoTable_Get(self->memo, obj)) {
3016 const char pop_mark_op = POP_MARK;
3017
3018 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3019 return -1;
3020 if (memo_get(self, obj) < 0)
3021 return -1;
3022 return 0;
3023 }
3024
3025 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3026 return -1;
3027 if (memo_put(self, obj) < 0)
3028 return -1;
3029
3030 return 0;
3031}
3032
3033static int
3034fix_imports(PyObject **module_name, PyObject **global_name)
3035{
3036 PyObject *key;
3037 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003038 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003039
3040 key = PyTuple_Pack(2, *module_name, *global_name);
3041 if (key == NULL)
3042 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003043 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003044 Py_DECREF(key);
3045 if (item) {
3046 PyObject *fixed_module_name;
3047 PyObject *fixed_global_name;
3048
3049 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3050 PyErr_Format(PyExc_RuntimeError,
3051 "_compat_pickle.REVERSE_NAME_MAPPING values "
3052 "should be 2-tuples, not %.200s",
3053 Py_TYPE(item)->tp_name);
3054 return -1;
3055 }
3056 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3057 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3058 if (!PyUnicode_Check(fixed_module_name) ||
3059 !PyUnicode_Check(fixed_global_name)) {
3060 PyErr_Format(PyExc_RuntimeError,
3061 "_compat_pickle.REVERSE_NAME_MAPPING values "
3062 "should be pairs of str, not (%.200s, %.200s)",
3063 Py_TYPE(fixed_module_name)->tp_name,
3064 Py_TYPE(fixed_global_name)->tp_name);
3065 return -1;
3066 }
3067
3068 Py_CLEAR(*module_name);
3069 Py_CLEAR(*global_name);
3070 Py_INCREF(fixed_module_name);
3071 Py_INCREF(fixed_global_name);
3072 *module_name = fixed_module_name;
3073 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003074 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003075 }
3076 else if (PyErr_Occurred()) {
3077 return -1;
3078 }
3079
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003080 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003081 if (item) {
3082 if (!PyUnicode_Check(item)) {
3083 PyErr_Format(PyExc_RuntimeError,
3084 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3085 "should be strings, not %.200s",
3086 Py_TYPE(item)->tp_name);
3087 return -1;
3088 }
3089 Py_CLEAR(*module_name);
3090 Py_INCREF(item);
3091 *module_name = item;
3092 }
3093 else if (PyErr_Occurred()) {
3094 return -1;
3095 }
3096
3097 return 0;
3098}
3099
3100static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003101save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3102{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003103 PyObject *global_name = NULL;
3104 PyObject *module_name = NULL;
3105 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003106 PyObject *parent = NULL;
3107 PyObject *dotted_path = NULL;
3108 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003109 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003110 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003111 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003112 _Py_IDENTIFIER(__name__);
3113 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003114
3115 const char global_op = GLOBAL;
3116
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003117 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003118 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003120 }
3121 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003122 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3123 if (global_name == NULL) {
3124 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3125 goto error;
3126 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003127 }
3128 if (global_name == NULL) {
3129 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3130 if (global_name == NULL)
3131 goto error;
3132 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 }
3134
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003135 dotted_path = get_dotted_path(module, global_name);
3136 if (dotted_path == NULL)
3137 goto error;
3138 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 if (module_name == NULL)
3140 goto error;
3141
3142 /* XXX: Change to use the import C API directly with level=0 to disallow
3143 relative imports.
3144
3145 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3146 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3147 custom import functions (IMHO, this would be a nice security
3148 feature). The import C API would need to be extended to support the
3149 extra parameters of __import__ to fix that. */
3150 module = PyImport_Import(module_name);
3151 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003152 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003153 "Can't pickle %R: import of module %R failed",
3154 obj, module_name);
3155 goto error;
3156 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003157 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3158 Py_INCREF(lastname);
3159 cls = get_deep_attribute(module, dotted_path, &parent);
3160 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003161 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003162 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003163 "Can't pickle %R: attribute lookup %S on %S failed",
3164 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003165 goto error;
3166 }
3167 if (cls != obj) {
3168 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003169 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003170 "Can't pickle %R: it's not the same object as %S.%S",
3171 obj, module_name, global_name);
3172 goto error;
3173 }
3174 Py_DECREF(cls);
3175
3176 if (self->proto >= 2) {
3177 /* See whether this is in the extension registry, and if
3178 * so generate an EXT opcode.
3179 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003180 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 PyObject *code_obj; /* extension code as Python object */
3182 long code; /* extension code as C value */
3183 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003184 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003185
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003186 extension_key = PyTuple_Pack(2, module_name, global_name);
3187 if (extension_key == NULL) {
3188 goto error;
3189 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003190 code_obj = PyDict_GetItemWithError(st->extension_registry,
3191 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003192 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193 /* The object is not registered in the extension registry.
3194 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003195 if (code_obj == NULL) {
3196 if (PyErr_Occurred()) {
3197 goto error;
3198 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003199 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003200 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201
3202 /* XXX: pickle.py doesn't check neither the type, nor the range
3203 of the value returned by the extension_registry. It should for
3204 consistency. */
3205
3206 /* Verify code_obj has the right type and value. */
3207 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003208 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003209 "Can't pickle %R: extension code %R isn't an integer",
3210 obj, code_obj);
3211 goto error;
3212 }
3213 code = PyLong_AS_LONG(code_obj);
3214 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003215 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003216 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3217 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003218 goto error;
3219 }
3220
3221 /* Generate an EXT opcode. */
3222 if (code <= 0xff) {
3223 pdata[0] = EXT1;
3224 pdata[1] = (unsigned char)code;
3225 n = 2;
3226 }
3227 else if (code <= 0xffff) {
3228 pdata[0] = EXT2;
3229 pdata[1] = (unsigned char)(code & 0xff);
3230 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3231 n = 3;
3232 }
3233 else {
3234 pdata[0] = EXT4;
3235 pdata[1] = (unsigned char)(code & 0xff);
3236 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3237 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3238 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3239 n = 5;
3240 }
3241
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003242 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003243 goto error;
3244 }
3245 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003246 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003247 if (parent == module) {
3248 Py_INCREF(lastname);
3249 Py_DECREF(global_name);
3250 global_name = lastname;
3251 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003252 if (self->proto >= 4) {
3253 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003254
Christian Heimese8b1ba12013-11-23 21:13:39 +01003255 if (save(self, module_name, 0) < 0)
3256 goto error;
3257 if (save(self, global_name, 0) < 0)
3258 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003259
3260 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3261 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003262 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003263 else if (parent != module) {
3264 PickleState *st = _Pickle_GetGlobalState();
3265 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3266 st->getattr, parent, lastname);
3267 status = save_reduce(self, reduce_value, NULL);
3268 Py_DECREF(reduce_value);
3269 if (status < 0)
3270 goto error;
3271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003272 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273 /* Generate a normal global opcode if we are using a pickle
3274 protocol < 4, or if the object is not registered in the
3275 extension registry. */
3276 PyObject *encoded;
3277 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003280 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003281
3282 /* For protocol < 3 and if the user didn't request against doing
3283 so, we convert module names to the old 2.x module names. */
3284 if (self->proto < 3 && self->fix_imports) {
3285 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003286 goto error;
3287 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003288 }
3289
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003290 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3291 both the module name and the global name using UTF-8. We do so
3292 only when we are using the pickle protocol newer than version
3293 3. This is to ensure compatibility with older Unpickler running
3294 on Python 2.x. */
3295 if (self->proto == 3) {
3296 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003297 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003298 else {
3299 unicode_encoder = PyUnicode_AsASCIIString;
3300 }
3301 encoded = unicode_encoder(module_name);
3302 if (encoded == NULL) {
3303 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003304 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003305 "can't pickle module identifier '%S' using "
3306 "pickle protocol %i",
3307 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003308 goto error;
3309 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003310 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3311 PyBytes_GET_SIZE(encoded)) < 0) {
3312 Py_DECREF(encoded);
3313 goto error;
3314 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003315 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003316 if(_Pickler_Write(self, "\n", 1) < 0)
3317 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003318
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 /* Save the name of the module. */
3320 encoded = unicode_encoder(global_name);
3321 if (encoded == NULL) {
3322 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003323 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003324 "can't pickle global identifier '%S' using "
3325 "pickle protocol %i",
3326 global_name, self->proto);
3327 goto error;
3328 }
3329 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3330 PyBytes_GET_SIZE(encoded)) < 0) {
3331 Py_DECREF(encoded);
3332 goto error;
3333 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003334 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003335 if (_Pickler_Write(self, "\n", 1) < 0)
3336 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003337 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338 /* Memoize the object. */
3339 if (memo_put(self, obj) < 0)
3340 goto error;
3341 }
3342
3343 if (0) {
3344 error:
3345 status = -1;
3346 }
3347 Py_XDECREF(module_name);
3348 Py_XDECREF(global_name);
3349 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003350 Py_XDECREF(parent);
3351 Py_XDECREF(dotted_path);
3352 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353
3354 return status;
3355}
3356
3357static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003358save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3359{
3360 PyObject *reduce_value;
3361 int status;
3362
3363 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3364 if (reduce_value == NULL) {
3365 return -1;
3366 }
3367 status = save_reduce(self, reduce_value, obj);
3368 Py_DECREF(reduce_value);
3369 return status;
3370}
3371
3372static int
3373save_type(PicklerObject *self, PyObject *obj)
3374{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003375 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003376 return save_singleton_type(self, obj, Py_None);
3377 }
3378 else if (obj == (PyObject *)&PyEllipsis_Type) {
3379 return save_singleton_type(self, obj, Py_Ellipsis);
3380 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003381 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003382 return save_singleton_type(self, obj, Py_NotImplemented);
3383 }
3384 return save_global(self, obj, NULL);
3385}
3386
3387static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3389{
3390 PyObject *pid = NULL;
3391 int status = 0;
3392
3393 const char persid_op = PERSID;
3394 const char binpersid_op = BINPERSID;
3395
3396 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003397 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003398 if (pid == NULL)
3399 return -1;
3400
3401 if (pid != Py_None) {
3402 if (self->bin) {
3403 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003404 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405 goto error;
3406 }
3407 else {
3408 PyObject *pid_str = NULL;
3409 char *pid_ascii_bytes;
3410 Py_ssize_t size;
3411
3412 pid_str = PyObject_Str(pid);
3413 if (pid_str == NULL)
3414 goto error;
3415
3416 /* XXX: Should it check whether the persistent id only contains
3417 ASCII characters? And what if the pid contains embedded
3418 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003419 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003420 Py_DECREF(pid_str);
3421 if (pid_ascii_bytes == NULL)
3422 goto error;
3423
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003424 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3425 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3426 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003427 goto error;
3428 }
3429 status = 1;
3430 }
3431
3432 if (0) {
3433 error:
3434 status = -1;
3435 }
3436 Py_XDECREF(pid);
3437
3438 return status;
3439}
3440
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003441static PyObject *
3442get_class(PyObject *obj)
3443{
3444 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003446
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003447 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003448 if (cls == NULL) {
3449 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3450 PyErr_Clear();
3451 cls = (PyObject *) Py_TYPE(obj);
3452 Py_INCREF(cls);
3453 }
3454 }
3455 return cls;
3456}
3457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3459 * appropriate __reduce__ method for obj.
3460 */
3461static int
3462save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3463{
3464 PyObject *callable;
3465 PyObject *argtup;
3466 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003467 PyObject *listitems = Py_None;
3468 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003469 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003470 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003471 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003472
3473 const char reduce_op = REDUCE;
3474 const char build_op = BUILD;
3475 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003477
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003478 size = PyTuple_Size(args);
3479 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003480 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003481 "__reduce__ must contain 2 through 5 elements");
3482 return -1;
3483 }
3484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003485 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3486 &callable, &argtup, &state, &listitems, &dictitems))
3487 return -1;
3488
3489 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003490 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003491 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003492 return -1;
3493 }
3494 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003495 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003496 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003497 return -1;
3498 }
3499
3500 if (state == Py_None)
3501 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003502
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003503 if (listitems == Py_None)
3504 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003505 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003506 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003507 "returned by __reduce__ must be an iterator, not %s",
3508 Py_TYPE(listitems)->tp_name);
3509 return -1;
3510 }
3511
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003512 if (dictitems == Py_None)
3513 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003514 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003515 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003516 "returned by __reduce__ must be an iterator, not %s",
3517 Py_TYPE(dictitems)->tp_name);
3518 return -1;
3519 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003520
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003521 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003522 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003523 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524
Victor Stinner804e05e2013-11-14 01:26:17 +01003525 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003526 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003527 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003529 }
3530 PyErr_Clear();
3531 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003532 else if (PyUnicode_Check(name)) {
3533 if (self->proto >= 4) {
3534 _Py_IDENTIFIER(__newobj_ex__);
3535 use_newobj_ex = PyUnicode_Compare(
3536 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3537 }
3538 if (!use_newobj_ex) {
3539 _Py_IDENTIFIER(__newobj__);
3540 use_newobj = PyUnicode_Compare(
3541 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3542 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003544 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003546
3547 if (use_newobj_ex) {
3548 PyObject *cls;
3549 PyObject *args;
3550 PyObject *kwargs;
3551
3552 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003553 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003554 "length of the NEWOBJ_EX argument tuple must be "
3555 "exactly 3, not %zd", Py_SIZE(argtup));
3556 return -1;
3557 }
3558
3559 cls = PyTuple_GET_ITEM(argtup, 0);
3560 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003561 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562 "first item from NEWOBJ_EX argument tuple must "
3563 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3564 return -1;
3565 }
3566 args = PyTuple_GET_ITEM(argtup, 1);
3567 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003568 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003569 "second item from NEWOBJ_EX argument tuple must "
3570 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3571 return -1;
3572 }
3573 kwargs = PyTuple_GET_ITEM(argtup, 2);
3574 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003575 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003576 "third item from NEWOBJ_EX argument tuple must "
3577 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3578 return -1;
3579 }
3580
3581 if (save(self, cls, 0) < 0 ||
3582 save(self, args, 0) < 0 ||
3583 save(self, kwargs, 0) < 0 ||
3584 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3585 return -1;
3586 }
3587 }
3588 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003589 PyObject *cls;
3590 PyObject *newargtup;
3591 PyObject *obj_class;
3592 int p;
3593
3594 /* Sanity checks. */
3595 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003596 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 return -1;
3598 }
3599
3600 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003601 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003602 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003603 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003604 return -1;
3605 }
3606
3607 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003608 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 p = obj_class != cls; /* true iff a problem */
3610 Py_DECREF(obj_class);
3611 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003612 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613 "__newobj__ args has the wrong class");
3614 return -1;
3615 }
3616 }
3617 /* XXX: These calls save() are prone to infinite recursion. Imagine
3618 what happen if the value returned by the __reduce__() method of
3619 some extension type contains another object of the same type. Ouch!
3620
3621 Here is a quick example, that I ran into, to illustrate what I
3622 mean:
3623
3624 >>> import pickle, copyreg
3625 >>> copyreg.dispatch_table.pop(complex)
3626 >>> pickle.dumps(1+2j)
3627 Traceback (most recent call last):
3628 ...
3629 RuntimeError: maximum recursion depth exceeded
3630
3631 Removing the complex class from copyreg.dispatch_table made the
3632 __reduce_ex__() method emit another complex object:
3633
3634 >>> (1+1j).__reduce_ex__(2)
3635 (<function __newobj__ at 0xb7b71c3c>,
3636 (<class 'complex'>, (1+1j)), None, None, None)
3637
3638 Thus when save() was called on newargstup (the 2nd item) recursion
3639 ensued. Of course, the bug was in the complex class which had a
3640 broken __getnewargs__() that emitted another complex object. But,
3641 the point, here, is it is quite easy to end up with a broken reduce
3642 function. */
3643
3644 /* Save the class and its __new__ arguments. */
3645 if (save(self, cls, 0) < 0)
3646 return -1;
3647
3648 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3649 if (newargtup == NULL)
3650 return -1;
3651
3652 p = save(self, newargtup, 0);
3653 Py_DECREF(newargtup);
3654 if (p < 0)
3655 return -1;
3656
3657 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003658 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003659 return -1;
3660 }
3661 else { /* Not using NEWOBJ. */
3662 if (save(self, callable, 0) < 0 ||
3663 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003664 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665 return -1;
3666 }
3667
3668 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3669 the caller do not want to memoize the object. Not particularly useful,
3670 but that is to mimic the behavior save_reduce() in pickle.py when
3671 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003672 if (obj != NULL) {
3673 /* If the object is already in the memo, this means it is
3674 recursive. In this case, throw away everything we put on the
3675 stack, and fetch the object back from the memo. */
3676 if (PyMemoTable_Get(self->memo, obj)) {
3677 const char pop_op = POP;
3678
3679 if (_Pickler_Write(self, &pop_op, 1) < 0)
3680 return -1;
3681 if (memo_get(self, obj) < 0)
3682 return -1;
3683
3684 return 0;
3685 }
3686 else if (memo_put(self, obj) < 0)
3687 return -1;
3688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689
3690 if (listitems && batch_list(self, listitems) < 0)
3691 return -1;
3692
3693 if (dictitems && batch_dict(self, dictitems) < 0)
3694 return -1;
3695
3696 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003697 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003698 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003699 return -1;
3700 }
3701
3702 return 0;
3703}
3704
3705static int
3706save(PicklerObject *self, PyObject *obj, int pers_save)
3707{
3708 PyTypeObject *type;
3709 PyObject *reduce_func = NULL;
3710 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711 int status = 0;
3712
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003713 if (_Pickler_OpcodeBoundary(self) < 0)
3714 return -1;
3715
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003716 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003717 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003718
3719 /* The extra pers_save argument is necessary to avoid calling save_pers()
3720 on its returned object. */
3721 if (!pers_save && self->pers_func) {
3722 /* save_pers() returns:
3723 -1 to signal an error;
3724 0 if it did nothing successfully;
3725 1 if a persistent id was saved.
3726 */
3727 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3728 goto done;
3729 }
3730
3731 type = Py_TYPE(obj);
3732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003733 /* The old cPickle had an optimization that used switch-case statement
3734 dispatching on the first letter of the type name. This has was removed
3735 since benchmarks shown that this optimization was actually slowing
3736 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003737
3738 /* Atom types; these aren't memoized, so don't check the memo. */
3739
3740 if (obj == Py_None) {
3741 status = save_none(self, obj);
3742 goto done;
3743 }
3744 else if (obj == Py_False || obj == Py_True) {
3745 status = save_bool(self, obj);
3746 goto done;
3747 }
3748 else if (type == &PyLong_Type) {
3749 status = save_long(self, obj);
3750 goto done;
3751 }
3752 else if (type == &PyFloat_Type) {
3753 status = save_float(self, obj);
3754 goto done;
3755 }
3756
3757 /* Check the memo to see if it has the object. If so, generate
3758 a GET (or BINGET) opcode, instead of pickling the object
3759 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003760 if (PyMemoTable_Get(self->memo, obj)) {
3761 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 goto error;
3763 goto done;
3764 }
3765
3766 if (type == &PyBytes_Type) {
3767 status = save_bytes(self, obj);
3768 goto done;
3769 }
3770 else if (type == &PyUnicode_Type) {
3771 status = save_unicode(self, obj);
3772 goto done;
3773 }
3774 else if (type == &PyDict_Type) {
3775 status = save_dict(self, obj);
3776 goto done;
3777 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003778 else if (type == &PySet_Type) {
3779 status = save_set(self, obj);
3780 goto done;
3781 }
3782 else if (type == &PyFrozenSet_Type) {
3783 status = save_frozenset(self, obj);
3784 goto done;
3785 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003786 else if (type == &PyList_Type) {
3787 status = save_list(self, obj);
3788 goto done;
3789 }
3790 else if (type == &PyTuple_Type) {
3791 status = save_tuple(self, obj);
3792 goto done;
3793 }
3794 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003795 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796 goto done;
3797 }
3798 else if (type == &PyFunction_Type) {
3799 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003800 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802
3803 /* XXX: This part needs some unit tests. */
3804
3805 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003806 * self.dispatch_table, copyreg.dispatch_table, the object's
3807 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003808 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003809 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003810 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003811 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3812 (PyObject *)type);
3813 if (reduce_func == NULL) {
3814 if (PyErr_Occurred()) {
3815 goto error;
3816 }
3817 } else {
3818 /* PyDict_GetItemWithError() returns a borrowed reference.
3819 Increase the reference count to be consistent with
3820 PyObject_GetItem and _PyObject_GetAttrId used below. */
3821 Py_INCREF(reduce_func);
3822 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003823 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003824 reduce_func = PyObject_GetItem(self->dispatch_table,
3825 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003826 if (reduce_func == NULL) {
3827 if (PyErr_ExceptionMatches(PyExc_KeyError))
3828 PyErr_Clear();
3829 else
3830 goto error;
3831 }
3832 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003833 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003834 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003835 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003836 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003837 else if (PyType_IsSubtype(type, &PyType_Type)) {
3838 status = save_global(self, obj, NULL);
3839 goto done;
3840 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003842 _Py_IDENTIFIER(__reduce__);
3843 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003845
3846 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3847 automatically defined as __reduce__. While this is convenient, this
3848 make it impossible to know which method was actually called. Of
3849 course, this is not a big deal. But still, it would be nice to let
3850 the user know which method was called when something go
3851 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3852 don't actually have to check for a __reduce__ method. */
3853
3854 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003855 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003856 if (reduce_func != NULL) {
3857 PyObject *proto;
3858 proto = PyLong_FromLong(self->proto);
3859 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003860 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861 }
3862 }
3863 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003864 PickleState *st = _Pickle_GetGlobalState();
3865
3866 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003868 }
3869 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003870 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003871 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003873 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003875 PyObject *empty_tuple = PyTuple_New(0);
3876 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003877 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003878 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003879 }
3880 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003881 PyErr_Format(st->PicklingError,
3882 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003883 type->tp_name, obj);
3884 goto error;
3885 }
3886 }
3887 }
3888
3889 if (reduce_value == NULL)
3890 goto error;
3891
3892 if (PyUnicode_Check(reduce_value)) {
3893 status = save_global(self, obj, reduce_value);
3894 goto done;
3895 }
3896
3897 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003898 PickleState *st = _Pickle_GetGlobalState();
3899 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 "__reduce__ must return a string or tuple");
3901 goto error;
3902 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903
3904 status = save_reduce(self, reduce_value, obj);
3905
3906 if (0) {
3907 error:
3908 status = -1;
3909 }
3910 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003911
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003912 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913 Py_XDECREF(reduce_func);
3914 Py_XDECREF(reduce_value);
3915
3916 return status;
3917}
3918
3919static int
3920dump(PicklerObject *self, PyObject *obj)
3921{
3922 const char stop_op = STOP;
3923
3924 if (self->proto >= 2) {
3925 char header[2];
3926
3927 header[0] = PROTO;
3928 assert(self->proto >= 0 && self->proto < 256);
3929 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003930 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003932 if (self->proto >= 4)
3933 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 }
3935
3936 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003937 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 return -1;
3939
3940 return 0;
3941}
3942
Larry Hastings61272b72014-01-07 12:41:53 -08003943/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003944
3945_pickle.Pickler.clear_memo
3946
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003947Clears the pickler's "memo".
3948
3949The memo is the data structure that remembers which objects the
3950pickler has already seen, so that shared or recursive objects are
3951pickled by reference and not by value. This method is useful when
3952re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003953[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003954
Larry Hastings3cceb382014-01-04 11:09:09 -08003955static PyObject *
3956_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003957/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003958{
3959 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003960 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961
3962 Py_RETURN_NONE;
3963}
3964
Larry Hastings61272b72014-01-07 12:41:53 -08003965/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003966
3967_pickle.Pickler.dump
3968
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003969 obj: object
3970 /
3971
3972Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003973[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003974
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003976_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003977/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003979 /* Check whether the Pickler was initialized correctly (issue3664).
3980 Developers often forget to call __init__() in their subclasses, which
3981 would trigger a segfault without this check. */
3982 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003983 PickleState *st = _Pickle_GetGlobalState();
3984 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003985 "Pickler.__init__() was not called by %s.__init__()",
3986 Py_TYPE(self)->tp_name);
3987 return NULL;
3988 }
3989
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003990 if (_Pickler_ClearBuffer(self) < 0)
3991 return NULL;
3992
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 if (dump(self, obj) < 0)
3994 return NULL;
3995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003996 if (_Pickler_FlushToFile(self) < 0)
3997 return NULL;
3998
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 Py_RETURN_NONE;
4000}
4001
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004002/*[clinic input]
4003
4004_pickle.Pickler.__sizeof__ -> Py_ssize_t
4005
4006Returns size in memory, in bytes.
4007[clinic start generated code]*/
4008
4009static Py_ssize_t
4010_pickle_Pickler___sizeof___impl(PicklerObject *self)
4011/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4012{
4013 Py_ssize_t res, s;
4014
4015 res = sizeof(PicklerObject);
4016 if (self->memo != NULL) {
4017 res += sizeof(PyMemoTable);
4018 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4019 }
4020 if (self->output_buffer != NULL) {
4021 s = _PySys_GetSizeOf(self->output_buffer);
4022 if (s == -1)
4023 return -1;
4024 res += s;
4025 }
4026 return res;
4027}
4028
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030 _PICKLE_PICKLER_DUMP_METHODDEF
4031 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004032 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033 {NULL, NULL} /* sentinel */
4034};
4035
4036static void
4037Pickler_dealloc(PicklerObject *self)
4038{
4039 PyObject_GC_UnTrack(self);
4040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004041 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004044 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004045 Py_XDECREF(self->fast_memo);
4046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004047 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004048
4049 Py_TYPE(self)->tp_free((PyObject *)self);
4050}
4051
4052static int
4053Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4054{
4055 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004056 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004057 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058 Py_VISIT(self->fast_memo);
4059 return 0;
4060}
4061
4062static int
4063Pickler_clear(PicklerObject *self)
4064{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004065 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004068 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004069 Py_CLEAR(self->fast_memo);
4070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004071 if (self->memo != NULL) {
4072 PyMemoTable *memo = self->memo;
4073 self->memo = NULL;
4074 PyMemoTable_Del(memo);
4075 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076 return 0;
4077}
4078
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004079
Larry Hastings61272b72014-01-07 12:41:53 -08004080/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004081
4082_pickle.Pickler.__init__
4083
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004084 file: object
4085 protocol: object = NULL
4086 fix_imports: bool = True
4087
4088This takes a binary file for writing a pickle data stream.
4089
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004090The optional *protocol* argument tells the pickler to use the given
4091protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4092protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004093
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004094Specifying a negative protocol version selects the highest protocol
4095version supported. The higher the protocol used, the more recent the
4096version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004097
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004098The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004099bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004100writing, a io.BytesIO instance, or any other custom object that meets
4101this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004103If *fix_imports* is True and protocol is less than 3, pickle will try
4104to map the new Python 3 names to the old module names used in Python
41052, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004106[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004107
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004108static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004109_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08004110/*[clinic end generated code: output=56e229f3b1f4332f input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004111{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004112 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004113 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004114
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004115 /* In case of multiple __init__() calls, clear previous content. */
4116 if (self->write != NULL)
4117 (void)Pickler_clear(self);
4118
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004119 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004120 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004121
4122 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004123 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004124
4125 /* memo and output_buffer may have already been created in _Pickler_New */
4126 if (self->memo == NULL) {
4127 self->memo = PyMemoTable_New();
4128 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004129 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004130 }
4131 self->output_len = 0;
4132 if (self->output_buffer == NULL) {
4133 self->max_output_len = WRITE_BUF_SIZE;
4134 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4135 self->max_output_len);
4136 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004137 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004138 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004140 self->fast = 0;
4141 self->fast_nesting = 0;
4142 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004143 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004144 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4145 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4146 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004147 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004148 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004149 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004150 self->dispatch_table = NULL;
4151 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4152 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4153 &PyId_dispatch_table);
4154 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004155 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004156 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004157
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004158 return 0;
4159}
4160
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004161
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004162/* Define a proxy object for the Pickler's internal memo object. This is to
4163 * avoid breaking code like:
4164 * pickler.memo.clear()
4165 * and
4166 * pickler.memo = saved_memo
4167 * Is this a good idea? Not really, but we don't want to break code that uses
4168 * it. Note that we don't implement the entire mapping API here. This is
4169 * intentional, as these should be treated as black-box implementation details.
4170 */
4171
Larry Hastings61272b72014-01-07 12:41:53 -08004172/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173_pickle.PicklerMemoProxy.clear
4174
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004175Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004176[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177
Larry Hastings3cceb382014-01-04 11:09:09 -08004178static PyObject *
4179_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004180/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004181{
4182 if (self->pickler->memo)
4183 PyMemoTable_Clear(self->pickler->memo);
4184 Py_RETURN_NONE;
4185}
4186
Larry Hastings61272b72014-01-07 12:41:53 -08004187/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004188_pickle.PicklerMemoProxy.copy
4189
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004190Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004191[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004192
Larry Hastings3cceb382014-01-04 11:09:09 -08004193static PyObject *
4194_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004195/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004196{
4197 Py_ssize_t i;
4198 PyMemoTable *memo;
4199 PyObject *new_memo = PyDict_New();
4200 if (new_memo == NULL)
4201 return NULL;
4202
4203 memo = self->pickler->memo;
4204 for (i = 0; i < memo->mt_allocated; ++i) {
4205 PyMemoEntry entry = memo->mt_table[i];
4206 if (entry.me_key != NULL) {
4207 int status;
4208 PyObject *key, *value;
4209
4210 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004211 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004212
4213 if (key == NULL || value == NULL) {
4214 Py_XDECREF(key);
4215 Py_XDECREF(value);
4216 goto error;
4217 }
4218 status = PyDict_SetItem(new_memo, key, value);
4219 Py_DECREF(key);
4220 Py_DECREF(value);
4221 if (status < 0)
4222 goto error;
4223 }
4224 }
4225 return new_memo;
4226
4227 error:
4228 Py_XDECREF(new_memo);
4229 return NULL;
4230}
4231
Larry Hastings61272b72014-01-07 12:41:53 -08004232/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004233_pickle.PicklerMemoProxy.__reduce__
4234
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004235Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004236[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004237
Larry Hastings3cceb382014-01-04 11:09:09 -08004238static PyObject *
4239_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004240/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004241{
4242 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004243 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(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*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004495 _pickle_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;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004605 Py_ssize_t i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004606 size_t x = 0;
4607
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004608 for (i = 0; i < nbytes && (size_t)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;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004627 Py_ssize_t 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;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004812 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004813 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;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004838
4839 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4840 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4841 if (strcmp(self->encoding, "bytes") == 0) {
4842 obj = bytes;
4843 }
4844 else {
4845 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4846 Py_DECREF(bytes);
4847 if (obj == NULL) {
4848 return -1;
4849 }
4850 }
4851
4852 PDATA_PUSH(self->stack, obj, -1);
4853 return 0;
4854}
4855
4856static int
4857load_counted_binstring(UnpicklerObject *self, int nbytes)
4858{
4859 PyObject *obj;
4860 Py_ssize_t size;
4861 char *s;
4862
4863 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004864 return -1;
4865
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004866 size = calc_binsize(s, nbytes);
4867 if (size < 0) {
4868 PickleState *st = _Pickle_GetGlobalState();
4869 PyErr_Format(st->UnpicklingError,
4870 "BINSTRING exceeds system's maximum size of %zd bytes",
4871 PY_SSIZE_T_MAX);
4872 return -1;
4873 }
4874
4875 if (_Unpickler_Read(self, &s, size) < 0)
4876 return -1;
4877
4878 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4879 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4880 if (strcmp(self->encoding, "bytes") == 0) {
4881 obj = PyBytes_FromStringAndSize(s, size);
4882 }
4883 else {
4884 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4885 }
4886 if (obj == NULL) {
4887 return -1;
4888 }
4889
4890 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004891 return 0;
4892}
4893
4894static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004895load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896{
4897 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004898 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 char *s;
4900
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004901 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004902 return -1;
4903
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004904 size = calc_binsize(s, nbytes);
4905 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004906 PyErr_Format(PyExc_OverflowError,
4907 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004908 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004909 return -1;
4910 }
4911
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004912 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004913 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004914
4915 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004916 if (bytes == NULL)
4917 return -1;
4918
4919 PDATA_PUSH(self->stack, bytes, -1);
4920 return 0;
4921}
4922
4923static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004924load_unicode(UnpicklerObject *self)
4925{
4926 PyObject *str;
4927 Py_ssize_t len;
4928 char *s;
4929
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004930 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004931 return -1;
4932 if (len < 1)
4933 return bad_readline();
4934
4935 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4936 if (str == NULL)
4937 return -1;
4938
4939 PDATA_PUSH(self->stack, str, -1);
4940 return 0;
4941}
4942
4943static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004944load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945{
4946 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004947 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004948 char *s;
4949
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004950 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004951 return -1;
4952
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004953 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004954 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004955 PyErr_Format(PyExc_OverflowError,
4956 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004957 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 return -1;
4959 }
4960
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004961 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962 return -1;
4963
Victor Stinner485fb562010-04-13 11:07:24 +00004964 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004965 if (str == NULL)
4966 return -1;
4967
4968 PDATA_PUSH(self->stack, str, -1);
4969 return 0;
4970}
4971
4972static int
4973load_tuple(UnpicklerObject *self)
4974{
4975 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004976 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977
4978 if ((i = marker(self)) < 0)
4979 return -1;
4980
4981 tuple = Pdata_poptuple(self->stack, i);
4982 if (tuple == NULL)
4983 return -1;
4984 PDATA_PUSH(self->stack, tuple, -1);
4985 return 0;
4986}
4987
4988static int
4989load_counted_tuple(UnpicklerObject *self, int len)
4990{
4991 PyObject *tuple;
4992
4993 tuple = PyTuple_New(len);
4994 if (tuple == NULL)
4995 return -1;
4996
4997 while (--len >= 0) {
4998 PyObject *item;
4999
5000 PDATA_POP(self->stack, item);
5001 if (item == NULL)
5002 return -1;
5003 PyTuple_SET_ITEM(tuple, len, item);
5004 }
5005 PDATA_PUSH(self->stack, tuple, -1);
5006 return 0;
5007}
5008
5009static int
5010load_empty_list(UnpicklerObject *self)
5011{
5012 PyObject *list;
5013
5014 if ((list = PyList_New(0)) == NULL)
5015 return -1;
5016 PDATA_PUSH(self->stack, list, -1);
5017 return 0;
5018}
5019
5020static int
5021load_empty_dict(UnpicklerObject *self)
5022{
5023 PyObject *dict;
5024
5025 if ((dict = PyDict_New()) == NULL)
5026 return -1;
5027 PDATA_PUSH(self->stack, dict, -1);
5028 return 0;
5029}
5030
5031static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005032load_empty_set(UnpicklerObject *self)
5033{
5034 PyObject *set;
5035
5036 if ((set = PySet_New(NULL)) == NULL)
5037 return -1;
5038 PDATA_PUSH(self->stack, set, -1);
5039 return 0;
5040}
5041
5042static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005043load_list(UnpicklerObject *self)
5044{
5045 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005046 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005047
5048 if ((i = marker(self)) < 0)
5049 return -1;
5050
5051 list = Pdata_poplist(self->stack, i);
5052 if (list == NULL)
5053 return -1;
5054 PDATA_PUSH(self->stack, list, -1);
5055 return 0;
5056}
5057
5058static int
5059load_dict(UnpicklerObject *self)
5060{
5061 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005062 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005063
5064 if ((i = marker(self)) < 0)
5065 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005066 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067
5068 if ((dict = PyDict_New()) == NULL)
5069 return -1;
5070
5071 for (k = i + 1; k < j; k += 2) {
5072 key = self->stack->data[k - 1];
5073 value = self->stack->data[k];
5074 if (PyDict_SetItem(dict, key, value) < 0) {
5075 Py_DECREF(dict);
5076 return -1;
5077 }
5078 }
5079 Pdata_clear(self->stack, i);
5080 PDATA_PUSH(self->stack, dict, -1);
5081 return 0;
5082}
5083
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005084static int
5085load_frozenset(UnpicklerObject *self)
5086{
5087 PyObject *items;
5088 PyObject *frozenset;
5089 Py_ssize_t i;
5090
5091 if ((i = marker(self)) < 0)
5092 return -1;
5093
5094 items = Pdata_poptuple(self->stack, i);
5095 if (items == NULL)
5096 return -1;
5097
5098 frozenset = PyFrozenSet_New(items);
5099 Py_DECREF(items);
5100 if (frozenset == NULL)
5101 return -1;
5102
5103 PDATA_PUSH(self->stack, frozenset, -1);
5104 return 0;
5105}
5106
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005107static PyObject *
5108instantiate(PyObject *cls, PyObject *args)
5109{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005110 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005111 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005112 /* Caller must assure args are a tuple. Normally, args come from
5113 Pdata_poptuple which packs objects from the top of the stack
5114 into a newly created tuple. */
5115 assert(PyTuple_Check(args));
5116 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005117 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005118 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005120 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005121 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005122
5123 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005124 }
5125 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126}
5127
5128static int
5129load_obj(UnpicklerObject *self)
5130{
5131 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005132 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005133
5134 if ((i = marker(self)) < 0)
5135 return -1;
5136
5137 args = Pdata_poptuple(self->stack, i + 1);
5138 if (args == NULL)
5139 return -1;
5140
5141 PDATA_POP(self->stack, cls);
5142 if (cls) {
5143 obj = instantiate(cls, args);
5144 Py_DECREF(cls);
5145 }
5146 Py_DECREF(args);
5147 if (obj == NULL)
5148 return -1;
5149
5150 PDATA_PUSH(self->stack, obj, -1);
5151 return 0;
5152}
5153
5154static int
5155load_inst(UnpicklerObject *self)
5156{
5157 PyObject *cls = NULL;
5158 PyObject *args = NULL;
5159 PyObject *obj = NULL;
5160 PyObject *module_name;
5161 PyObject *class_name;
5162 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005163 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005164 char *s;
5165
5166 if ((i = marker(self)) < 0)
5167 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005168 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005169 return -1;
5170 if (len < 2)
5171 return bad_readline();
5172
5173 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5174 identifiers are permitted in Python 3.0, since the INST opcode is only
5175 supported by older protocols on Python 2.x. */
5176 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5177 if (module_name == NULL)
5178 return -1;
5179
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005180 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005181 if (len < 2)
5182 return bad_readline();
5183 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005184 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005185 cls = find_class(self, module_name, class_name);
5186 Py_DECREF(class_name);
5187 }
5188 }
5189 Py_DECREF(module_name);
5190
5191 if (cls == NULL)
5192 return -1;
5193
5194 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5195 obj = instantiate(cls, args);
5196 Py_DECREF(args);
5197 }
5198 Py_DECREF(cls);
5199
5200 if (obj == NULL)
5201 return -1;
5202
5203 PDATA_PUSH(self->stack, obj, -1);
5204 return 0;
5205}
5206
5207static int
5208load_newobj(UnpicklerObject *self)
5209{
5210 PyObject *args = NULL;
5211 PyObject *clsraw = NULL;
5212 PyTypeObject *cls; /* clsraw cast to its true type */
5213 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005214 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215
5216 /* Stack is ... cls argtuple, and we want to call
5217 * cls.__new__(cls, *argtuple).
5218 */
5219 PDATA_POP(self->stack, args);
5220 if (args == NULL)
5221 goto error;
5222 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005223 PyErr_SetString(st->UnpicklingError,
5224 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005225 goto error;
5226 }
5227
5228 PDATA_POP(self->stack, clsraw);
5229 cls = (PyTypeObject *)clsraw;
5230 if (cls == NULL)
5231 goto error;
5232 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005233 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005234 "isn't a type object");
5235 goto error;
5236 }
5237 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005238 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005239 "has NULL tp_new");
5240 goto error;
5241 }
5242
5243 /* Call __new__. */
5244 obj = cls->tp_new(cls, args, NULL);
5245 if (obj == NULL)
5246 goto error;
5247
5248 Py_DECREF(args);
5249 Py_DECREF(clsraw);
5250 PDATA_PUSH(self->stack, obj, -1);
5251 return 0;
5252
5253 error:
5254 Py_XDECREF(args);
5255 Py_XDECREF(clsraw);
5256 return -1;
5257}
5258
5259static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005260load_newobj_ex(UnpicklerObject *self)
5261{
5262 PyObject *cls, *args, *kwargs;
5263 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005264 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005265
5266 PDATA_POP(self->stack, kwargs);
5267 if (kwargs == NULL) {
5268 return -1;
5269 }
5270 PDATA_POP(self->stack, args);
5271 if (args == NULL) {
5272 Py_DECREF(kwargs);
5273 return -1;
5274 }
5275 PDATA_POP(self->stack, cls);
5276 if (cls == NULL) {
5277 Py_DECREF(kwargs);
5278 Py_DECREF(args);
5279 return -1;
5280 }
Larry Hastings61272b72014-01-07 12:41:53 -08005281
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005282 if (!PyType_Check(cls)) {
5283 Py_DECREF(kwargs);
5284 Py_DECREF(args);
5285 Py_DECREF(cls);
Larry Hastings61272b72014-01-07 12:41:53 -08005286 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005287 "NEWOBJ_EX class argument must be a type, not %.200s",
5288 Py_TYPE(cls)->tp_name);
5289 return -1;
5290 }
5291
5292 if (((PyTypeObject *)cls)->tp_new == NULL) {
5293 Py_DECREF(kwargs);
5294 Py_DECREF(args);
5295 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005296 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005297 "NEWOBJ_EX class argument doesn't have __new__");
5298 return -1;
5299 }
5300 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5301 Py_DECREF(kwargs);
5302 Py_DECREF(args);
5303 Py_DECREF(cls);
5304 if (obj == NULL) {
5305 return -1;
5306 }
5307 PDATA_PUSH(self->stack, obj, -1);
5308 return 0;
5309}
5310
5311static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005312load_global(UnpicklerObject *self)
5313{
5314 PyObject *global = NULL;
5315 PyObject *module_name;
5316 PyObject *global_name;
5317 Py_ssize_t len;
5318 char *s;
5319
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005320 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 return -1;
5322 if (len < 2)
5323 return bad_readline();
5324 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5325 if (!module_name)
5326 return -1;
5327
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005328 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005329 if (len < 2) {
5330 Py_DECREF(module_name);
5331 return bad_readline();
5332 }
5333 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5334 if (global_name) {
5335 global = find_class(self, module_name, global_name);
5336 Py_DECREF(global_name);
5337 }
5338 }
5339 Py_DECREF(module_name);
5340
5341 if (global == NULL)
5342 return -1;
5343 PDATA_PUSH(self->stack, global, -1);
5344 return 0;
5345}
5346
5347static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005348load_stack_global(UnpicklerObject *self)
5349{
5350 PyObject *global;
5351 PyObject *module_name;
5352 PyObject *global_name;
5353
5354 PDATA_POP(self->stack, global_name);
5355 PDATA_POP(self->stack, module_name);
5356 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5357 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005358 PickleState *st = _Pickle_GetGlobalState();
5359 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005360 Py_XDECREF(global_name);
5361 Py_XDECREF(module_name);
5362 return -1;
5363 }
5364 global = find_class(self, module_name, global_name);
5365 Py_DECREF(global_name);
5366 Py_DECREF(module_name);
5367 if (global == NULL)
5368 return -1;
5369 PDATA_PUSH(self->stack, global, -1);
5370 return 0;
5371}
5372
5373static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005374load_persid(UnpicklerObject *self)
5375{
5376 PyObject *pid;
5377 Py_ssize_t len;
5378 char *s;
5379
5380 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005381 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005382 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005383 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384 return bad_readline();
5385
5386 pid = PyBytes_FromStringAndSize(s, len - 1);
5387 if (pid == NULL)
5388 return -1;
5389
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005390 /* This does not leak since _Pickle_FastCall() steals the reference
5391 to pid first. */
5392 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005393 if (pid == NULL)
5394 return -1;
5395
5396 PDATA_PUSH(self->stack, pid, -1);
5397 return 0;
5398 }
5399 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005400 PickleState *st = _Pickle_GetGlobalState();
5401 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005402 "A load persistent id instruction was encountered,\n"
5403 "but no persistent_load function was specified.");
5404 return -1;
5405 }
5406}
5407
5408static int
5409load_binpersid(UnpicklerObject *self)
5410{
5411 PyObject *pid;
5412
5413 if (self->pers_func) {
5414 PDATA_POP(self->stack, pid);
5415 if (pid == NULL)
5416 return -1;
5417
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005418 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005419 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005420 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005421 if (pid == NULL)
5422 return -1;
5423
5424 PDATA_PUSH(self->stack, pid, -1);
5425 return 0;
5426 }
5427 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005428 PickleState *st = _Pickle_GetGlobalState();
5429 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005430 "A load persistent id instruction was encountered,\n"
5431 "but no persistent_load function was specified.");
5432 return -1;
5433 }
5434}
5435
5436static int
5437load_pop(UnpicklerObject *self)
5438{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005439 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440
5441 /* Note that we split the (pickle.py) stack into two stacks,
5442 * an object stack and a mark stack. We have to be clever and
5443 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005444 * mark stack first, and only signalling a stack underflow if
5445 * the object stack is empty and the mark stack doesn't match
5446 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005447 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005448 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005449 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005450 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451 len--;
5452 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005453 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005454 } else {
5455 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457 return 0;
5458}
5459
5460static int
5461load_pop_mark(UnpicklerObject *self)
5462{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005463 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005464
5465 if ((i = marker(self)) < 0)
5466 return -1;
5467
5468 Pdata_clear(self->stack, i);
5469
5470 return 0;
5471}
5472
5473static int
5474load_dup(UnpicklerObject *self)
5475{
5476 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005477 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005478
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005479 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480 return stack_underflow();
5481 last = self->stack->data[len - 1];
5482 PDATA_APPEND(self->stack, last, -1);
5483 return 0;
5484}
5485
5486static int
5487load_get(UnpicklerObject *self)
5488{
5489 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005490 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 Py_ssize_t len;
5492 char *s;
5493
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005494 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 return -1;
5496 if (len < 2)
5497 return bad_readline();
5498
5499 key = PyLong_FromString(s, NULL, 10);
5500 if (key == NULL)
5501 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005502 idx = PyLong_AsSsize_t(key);
5503 if (idx == -1 && PyErr_Occurred()) {
5504 Py_DECREF(key);
5505 return -1;
5506 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005508 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005509 if (value == NULL) {
5510 if (!PyErr_Occurred())
5511 PyErr_SetObject(PyExc_KeyError, key);
5512 Py_DECREF(key);
5513 return -1;
5514 }
5515 Py_DECREF(key);
5516
5517 PDATA_APPEND(self->stack, value, -1);
5518 return 0;
5519}
5520
5521static int
5522load_binget(UnpicklerObject *self)
5523{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005524 PyObject *value;
5525 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005526 char *s;
5527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005528 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005529 return -1;
5530
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005531 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005533 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005535 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005536 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005537 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005538 Py_DECREF(key);
5539 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540 return -1;
5541 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542
5543 PDATA_APPEND(self->stack, value, -1);
5544 return 0;
5545}
5546
5547static int
5548load_long_binget(UnpicklerObject *self)
5549{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005550 PyObject *value;
5551 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005554 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005555 return -1;
5556
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005557 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005558
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005559 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005561 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005562 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005564 Py_DECREF(key);
5565 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005566 return -1;
5567 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568
5569 PDATA_APPEND(self->stack, value, -1);
5570 return 0;
5571}
5572
5573/* Push an object from the extension registry (EXT[124]). nbytes is
5574 * the number of bytes following the opcode, holding the index (code) value.
5575 */
5576static int
5577load_extension(UnpicklerObject *self, int nbytes)
5578{
5579 char *codebytes; /* the nbytes bytes after the opcode */
5580 long code; /* calc_binint returns long */
5581 PyObject *py_code; /* code as a Python int */
5582 PyObject *obj; /* the object to push */
5583 PyObject *pair; /* (module_name, class_name) */
5584 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005585 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005586
5587 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005588 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005589 return -1;
5590 code = calc_binint(codebytes, nbytes);
5591 if (code <= 0) { /* note that 0 is forbidden */
5592 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005593 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005594 return -1;
5595 }
5596
5597 /* Look for the code in the cache. */
5598 py_code = PyLong_FromLong(code);
5599 if (py_code == NULL)
5600 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005601 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602 if (obj != NULL) {
5603 /* Bingo. */
5604 Py_DECREF(py_code);
5605 PDATA_APPEND(self->stack, obj, -1);
5606 return 0;
5607 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005608 if (PyErr_Occurred()) {
5609 Py_DECREF(py_code);
5610 return -1;
5611 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612
5613 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005614 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005615 if (pair == NULL) {
5616 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005617 if (!PyErr_Occurred()) {
5618 PyErr_Format(PyExc_ValueError, "unregistered extension "
5619 "code %ld", code);
5620 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005621 return -1;
5622 }
5623 /* Since the extension registry is manipulable via Python code,
5624 * confirm that pair is really a 2-tuple of strings.
5625 */
5626 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5627 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5628 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5629 Py_DECREF(py_code);
5630 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5631 "isn't a 2-tuple of strings", code);
5632 return -1;
5633 }
5634 /* Load the object. */
5635 obj = find_class(self, module_name, class_name);
5636 if (obj == NULL) {
5637 Py_DECREF(py_code);
5638 return -1;
5639 }
5640 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005641 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642 Py_DECREF(py_code);
5643 if (code < 0) {
5644 Py_DECREF(obj);
5645 return -1;
5646 }
5647 PDATA_PUSH(self->stack, obj, -1);
5648 return 0;
5649}
5650
5651static int
5652load_put(UnpicklerObject *self)
5653{
5654 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 Py_ssize_t len;
5657 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005659 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660 return -1;
5661 if (len < 2)
5662 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005663 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005664 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005665 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666
5667 key = PyLong_FromString(s, NULL, 10);
5668 if (key == NULL)
5669 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005670 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005672 if (idx < 0) {
5673 if (!PyErr_Occurred())
5674 PyErr_SetString(PyExc_ValueError,
5675 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005676 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005677 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005678
5679 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680}
5681
5682static int
5683load_binput(UnpicklerObject *self)
5684{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005685 PyObject *value;
5686 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005689 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005691
5692 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005694 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005695
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005696 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005698 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005699}
5700
5701static int
5702load_long_binput(UnpicklerObject *self)
5703{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005704 PyObject *value;
5705 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005706 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005708 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005710
5711 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005712 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005713 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005715 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005716 if (idx < 0) {
5717 PyErr_SetString(PyExc_ValueError,
5718 "negative LONG_BINPUT argument");
5719 return -1;
5720 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005721
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005722 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005723}
5724
5725static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005726load_memoize(UnpicklerObject *self)
5727{
5728 PyObject *value;
5729
5730 if (Py_SIZE(self->stack) <= 0)
5731 return stack_underflow();
5732 value = self->stack->data[Py_SIZE(self->stack) - 1];
5733
5734 return _Unpickler_MemoPut(self, self->memo_len, value);
5735}
5736
5737static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005738do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005739{
5740 PyObject *value;
5741 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005742 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005743
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005744 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005745 if (x > len || x <= 0)
5746 return stack_underflow();
5747 if (len == x) /* nothing to do */
5748 return 0;
5749
5750 list = self->stack->data[x - 1];
5751
5752 if (PyList_Check(list)) {
5753 PyObject *slice;
5754 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005755 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756
5757 slice = Pdata_poplist(self->stack, x);
5758 if (!slice)
5759 return -1;
5760 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005761 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005763 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 }
5765 else {
5766 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005767 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005768
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005769 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770 if (append_func == NULL)
5771 return -1;
5772 for (i = x; i < len; i++) {
5773 PyObject *result;
5774
5775 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005776 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777 if (result == NULL) {
5778 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005780 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 return -1;
5782 }
5783 Py_DECREF(result);
5784 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005786 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787 }
5788
5789 return 0;
5790}
5791
5792static int
5793load_append(UnpicklerObject *self)
5794{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796}
5797
5798static int
5799load_appends(UnpicklerObject *self)
5800{
5801 return do_append(self, marker(self));
5802}
5803
5804static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005805do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005806{
5807 PyObject *value, *key;
5808 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005809 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810 int status = 0;
5811
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005812 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005813 if (x > len || x <= 0)
5814 return stack_underflow();
5815 if (len == x) /* nothing to do */
5816 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005817 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005818 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005820 PyErr_SetString(st->UnpicklingError,
5821 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822 return -1;
5823 }
5824
5825 /* Here, dict does not actually need to be a PyDict; it could be anything
5826 that supports the __setitem__ attribute. */
5827 dict = self->stack->data[x - 1];
5828
5829 for (i = x + 1; i < len; i += 2) {
5830 key = self->stack->data[i - 1];
5831 value = self->stack->data[i];
5832 if (PyObject_SetItem(dict, key, value) < 0) {
5833 status = -1;
5834 break;
5835 }
5836 }
5837
5838 Pdata_clear(self->stack, x);
5839 return status;
5840}
5841
5842static int
5843load_setitem(UnpicklerObject *self)
5844{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005845 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846}
5847
5848static int
5849load_setitems(UnpicklerObject *self)
5850{
5851 return do_setitems(self, marker(self));
5852}
5853
5854static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005855load_additems(UnpicklerObject *self)
5856{
5857 PyObject *set;
5858 Py_ssize_t mark, len, i;
5859
5860 mark = marker(self);
5861 len = Py_SIZE(self->stack);
5862 if (mark > len || mark <= 0)
5863 return stack_underflow();
5864 if (len == mark) /* nothing to do */
5865 return 0;
5866
5867 set = self->stack->data[mark - 1];
5868
5869 if (PySet_Check(set)) {
5870 PyObject *items;
5871 int status;
5872
5873 items = Pdata_poptuple(self->stack, mark);
5874 if (items == NULL)
5875 return -1;
5876
5877 status = _PySet_Update(set, items);
5878 Py_DECREF(items);
5879 return status;
5880 }
5881 else {
5882 PyObject *add_func;
5883 _Py_IDENTIFIER(add);
5884
5885 add_func = _PyObject_GetAttrId(set, &PyId_add);
5886 if (add_func == NULL)
5887 return -1;
5888 for (i = mark; i < len; i++) {
5889 PyObject *result;
5890 PyObject *item;
5891
5892 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005893 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005894 if (result == NULL) {
5895 Pdata_clear(self->stack, i + 1);
5896 Py_SIZE(self->stack) = mark;
5897 return -1;
5898 }
5899 Py_DECREF(result);
5900 }
5901 Py_SIZE(self->stack) = mark;
5902 }
5903
5904 return 0;
5905}
5906
5907static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005908load_build(UnpicklerObject *self)
5909{
5910 PyObject *state, *inst, *slotstate;
5911 PyObject *setstate;
5912 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005913 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914
5915 /* Stack is ... instance, state. We want to leave instance at
5916 * the stack top, possibly mutated via instance.__setstate__(state).
5917 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005918 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005919 return stack_underflow();
5920
5921 PDATA_POP(self->stack, state);
5922 if (state == NULL)
5923 return -1;
5924
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005925 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005926
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005927 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005928 if (setstate == NULL) {
5929 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5930 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005931 else {
5932 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005933 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005934 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005935 }
5936 else {
5937 PyObject *result;
5938
5939 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005940 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005941 Py_DECREF(setstate);
5942 if (result == NULL)
5943 return -1;
5944 Py_DECREF(result);
5945 return 0;
5946 }
5947
5948 /* A default __setstate__. First see whether state embeds a
5949 * slot state dict too (a proto 2 addition).
5950 */
5951 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5952 PyObject *tmp = state;
5953
5954 state = PyTuple_GET_ITEM(tmp, 0);
5955 slotstate = PyTuple_GET_ITEM(tmp, 1);
5956 Py_INCREF(state);
5957 Py_INCREF(slotstate);
5958 Py_DECREF(tmp);
5959 }
5960 else
5961 slotstate = NULL;
5962
5963 /* Set inst.__dict__ from the state dict (if any). */
5964 if (state != Py_None) {
5965 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005966 PyObject *d_key, *d_value;
5967 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005968 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005969
5970 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005971 PickleState *st = _Pickle_GetGlobalState();
5972 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973 goto error;
5974 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005975 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005976 if (dict == NULL)
5977 goto error;
5978
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005979 i = 0;
5980 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5981 /* normally the keys for instance attributes are
5982 interned. we should try to do that here. */
5983 Py_INCREF(d_key);
5984 if (PyUnicode_CheckExact(d_key))
5985 PyUnicode_InternInPlace(&d_key);
5986 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5987 Py_DECREF(d_key);
5988 goto error;
5989 }
5990 Py_DECREF(d_key);
5991 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005992 Py_DECREF(dict);
5993 }
5994
5995 /* Also set instance attributes from the slotstate dict (if any). */
5996 if (slotstate != NULL) {
5997 PyObject *d_key, *d_value;
5998 Py_ssize_t i;
5999
6000 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006001 PickleState *st = _Pickle_GetGlobalState();
6002 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006003 "slot state is not a dictionary");
6004 goto error;
6005 }
6006 i = 0;
6007 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6008 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6009 goto error;
6010 }
6011 }
6012
6013 if (0) {
6014 error:
6015 status = -1;
6016 }
6017
6018 Py_DECREF(state);
6019 Py_XDECREF(slotstate);
6020 return status;
6021}
6022
6023static int
6024load_mark(UnpicklerObject *self)
6025{
6026
6027 /* Note that we split the (pickle.py) stack into two stacks, an
6028 * object stack and a mark stack. Here we push a mark onto the
6029 * mark stack.
6030 */
6031
6032 if ((self->num_marks + 1) >= self->marks_size) {
6033 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006034 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006035
6036 /* Use the size_t type to check for overflow. */
6037 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006038 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006039 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006040 PyErr_NoMemory();
6041 return -1;
6042 }
6043
6044 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006045 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006047 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
6048 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006049 if (marks == NULL) {
6050 PyErr_NoMemory();
6051 return -1;
6052 }
6053 self->marks = marks;
6054 self->marks_size = (Py_ssize_t)alloc;
6055 }
6056
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006057 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006058
6059 return 0;
6060}
6061
6062static int
6063load_reduce(UnpicklerObject *self)
6064{
6065 PyObject *callable = NULL;
6066 PyObject *argtup = NULL;
6067 PyObject *obj = NULL;
6068
6069 PDATA_POP(self->stack, argtup);
6070 if (argtup == NULL)
6071 return -1;
6072 PDATA_POP(self->stack, callable);
6073 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006074 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 Py_DECREF(callable);
6076 }
6077 Py_DECREF(argtup);
6078
6079 if (obj == NULL)
6080 return -1;
6081
6082 PDATA_PUSH(self->stack, obj, -1);
6083 return 0;
6084}
6085
6086/* Just raises an error if we don't know the protocol specified. PROTO
6087 * is the first opcode for protocols >= 2.
6088 */
6089static int
6090load_proto(UnpicklerObject *self)
6091{
6092 char *s;
6093 int i;
6094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006095 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096 return -1;
6097
6098 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006099 if (i <= HIGHEST_PROTOCOL) {
6100 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006101 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006102 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006103
6104 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6105 return -1;
6106}
6107
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006108static int
6109load_frame(UnpicklerObject *self)
6110{
6111 char *s;
6112 Py_ssize_t frame_len;
6113
6114 if (_Unpickler_Read(self, &s, 8) < 0)
6115 return -1;
6116
6117 frame_len = calc_binsize(s, 8);
6118 if (frame_len < 0) {
6119 PyErr_Format(PyExc_OverflowError,
6120 "FRAME length exceeds system's maximum of %zd bytes",
6121 PY_SSIZE_T_MAX);
6122 return -1;
6123 }
6124
6125 if (_Unpickler_Read(self, &s, frame_len) < 0)
6126 return -1;
6127
6128 /* Rewind to start of frame */
6129 self->next_read_idx -= frame_len;
6130 return 0;
6131}
6132
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006133static PyObject *
6134load(UnpicklerObject *self)
6135{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006136 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006137 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006138
6139 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006140 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006141 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006142 Pdata_clear(self->stack, 0);
6143
6144 /* Convenient macros for the dispatch while-switch loop just below. */
6145#define OP(opcode, load_func) \
6146 case opcode: if (load_func(self) < 0) break; continue;
6147
6148#define OP_ARG(opcode, load_func, arg) \
6149 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6150
6151 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006152 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006153 break;
6154
6155 switch ((enum opcode)s[0]) {
6156 OP(NONE, load_none)
6157 OP(BININT, load_binint)
6158 OP(BININT1, load_binint1)
6159 OP(BININT2, load_binint2)
6160 OP(INT, load_int)
6161 OP(LONG, load_long)
6162 OP_ARG(LONG1, load_counted_long, 1)
6163 OP_ARG(LONG4, load_counted_long, 4)
6164 OP(FLOAT, load_float)
6165 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006166 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6167 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6168 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6169 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6170 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 OP(STRING, load_string)
6172 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006173 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6174 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6175 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006176 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6177 OP_ARG(TUPLE1, load_counted_tuple, 1)
6178 OP_ARG(TUPLE2, load_counted_tuple, 2)
6179 OP_ARG(TUPLE3, load_counted_tuple, 3)
6180 OP(TUPLE, load_tuple)
6181 OP(EMPTY_LIST, load_empty_list)
6182 OP(LIST, load_list)
6183 OP(EMPTY_DICT, load_empty_dict)
6184 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006185 OP(EMPTY_SET, load_empty_set)
6186 OP(ADDITEMS, load_additems)
6187 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006188 OP(OBJ, load_obj)
6189 OP(INST, load_inst)
6190 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006191 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006192 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006193 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194 OP(APPEND, load_append)
6195 OP(APPENDS, load_appends)
6196 OP(BUILD, load_build)
6197 OP(DUP, load_dup)
6198 OP(BINGET, load_binget)
6199 OP(LONG_BINGET, load_long_binget)
6200 OP(GET, load_get)
6201 OP(MARK, load_mark)
6202 OP(BINPUT, load_binput)
6203 OP(LONG_BINPUT, load_long_binput)
6204 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006205 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 OP(POP, load_pop)
6207 OP(POP_MARK, load_pop_mark)
6208 OP(SETITEM, load_setitem)
6209 OP(SETITEMS, load_setitems)
6210 OP(PERSID, load_persid)
6211 OP(BINPERSID, load_binpersid)
6212 OP(REDUCE, load_reduce)
6213 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006214 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006215 OP_ARG(EXT1, load_extension, 1)
6216 OP_ARG(EXT2, load_extension, 2)
6217 OP_ARG(EXT4, load_extension, 4)
6218 OP_ARG(NEWTRUE, load_bool, Py_True)
6219 OP_ARG(NEWFALSE, load_bool, Py_False)
6220
6221 case STOP:
6222 break;
6223
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006225 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006226 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006227 }
6228 else {
6229 PickleState *st = _Pickle_GetGlobalState();
6230 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006231 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006232 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006233 return NULL;
6234 }
6235
6236 break; /* and we are done! */
6237 }
6238
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006239 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006240 return NULL;
6241 }
6242
Victor Stinner2ae57e32013-10-31 13:39:23 +01006243 if (_Unpickler_SkipConsumed(self) < 0)
6244 return NULL;
6245
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006246 PDATA_POP(self->stack, value);
6247 return value;
6248}
6249
Larry Hastings61272b72014-01-07 12:41:53 -08006250/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006251
6252_pickle.Unpickler.load
6253
6254Load a pickle.
6255
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006256Read a pickled object representation from the open file object given
6257in the constructor, and return the reconstituted object hierarchy
6258specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006259[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006260
Larry Hastings3cceb382014-01-04 11:09:09 -08006261static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006262_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006263/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006264{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006265 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006266
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267 /* Check whether the Unpickler was initialized correctly. This prevents
6268 segfaulting if a subclass overridden __init__ with a function that does
6269 not call Unpickler.__init__(). Here, we simply ensure that self->read
6270 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006271 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006272 PickleState *st = _Pickle_GetGlobalState();
6273 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006274 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006275 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006276 return NULL;
6277 }
6278
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006279 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006280}
6281
6282/* The name of find_class() is misleading. In newer pickle protocols, this
6283 function is used for loading any global (i.e., functions), not just
6284 classes. The name is kept only for backward compatibility. */
6285
Larry Hastings61272b72014-01-07 12:41:53 -08006286/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006287
6288_pickle.Unpickler.find_class
6289
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006290 module_name: object
6291 global_name: object
6292 /
6293
6294Return an object from a specified module.
6295
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006296If necessary, the module will be imported. Subclasses may override
6297this method (e.g. to restrict unpickling of arbitrary classes and
6298functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006299
6300This method is called whenever a class or a function object is
6301needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006302[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006303
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006304static PyObject *
6305_pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
Larry Hastings581ee362014-01-28 05:00:08 -08006306/*[clinic end generated code: output=64c77437e088e188 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307{
6308 PyObject *global;
6309 PyObject *modules_dict;
6310 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006311 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006312
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006313 /* Try to map the old names used in Python 2.x to the new ones used in
6314 Python 3.x. We do this only with old pickle protocols and when the
6315 user has not disabled the feature. */
6316 if (self->proto < 3 && self->fix_imports) {
6317 PyObject *key;
6318 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006319 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006320
6321 /* Check if the global (i.e., a function or a class) was renamed
6322 or moved to another module. */
6323 key = PyTuple_Pack(2, module_name, global_name);
6324 if (key == NULL)
6325 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006326 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006327 Py_DECREF(key);
6328 if (item) {
6329 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6330 PyErr_Format(PyExc_RuntimeError,
6331 "_compat_pickle.NAME_MAPPING values should be "
6332 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6333 return NULL;
6334 }
6335 module_name = PyTuple_GET_ITEM(item, 0);
6336 global_name = PyTuple_GET_ITEM(item, 1);
6337 if (!PyUnicode_Check(module_name) ||
6338 !PyUnicode_Check(global_name)) {
6339 PyErr_Format(PyExc_RuntimeError,
6340 "_compat_pickle.NAME_MAPPING values should be "
6341 "pairs of str, not (%.200s, %.200s)",
6342 Py_TYPE(module_name)->tp_name,
6343 Py_TYPE(global_name)->tp_name);
6344 return NULL;
6345 }
6346 }
6347 else if (PyErr_Occurred()) {
6348 return NULL;
6349 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006350 else {
6351 /* Check if the module was renamed. */
6352 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6353 if (item) {
6354 if (!PyUnicode_Check(item)) {
6355 PyErr_Format(PyExc_RuntimeError,
6356 "_compat_pickle.IMPORT_MAPPING values should be "
6357 "strings, not %.200s", Py_TYPE(item)->tp_name);
6358 return NULL;
6359 }
6360 module_name = item;
6361 }
6362 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006363 return NULL;
6364 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006365 }
6366 }
6367
Victor Stinnerbb520202013-11-06 22:40:41 +01006368 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006369 if (modules_dict == NULL) {
6370 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006371 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006372 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006373
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006374 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006375 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006376 if (PyErr_Occurred())
6377 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006378 module = PyImport_Import(module_name);
6379 if (module == NULL)
6380 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006381 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006382 Py_DECREF(module);
6383 }
Victor Stinner121aab42011-09-29 23:40:53 +02006384 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006385 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006386 }
6387 return global;
6388}
6389
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006390/*[clinic input]
6391
6392_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6393
6394Returns size in memory, in bytes.
6395[clinic start generated code]*/
6396
6397static Py_ssize_t
6398_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6399/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6400{
6401 Py_ssize_t res;
6402
6403 res = sizeof(UnpicklerObject);
6404 if (self->memo != NULL)
6405 res += self->memo_size * sizeof(PyObject *);
6406 if (self->marks != NULL)
6407 res += self->marks_size * sizeof(Py_ssize_t);
6408 if (self->input_line != NULL)
6409 res += strlen(self->input_line) + 1;
6410 if (self->encoding != NULL)
6411 res += strlen(self->encoding) + 1;
6412 if (self->errors != NULL)
6413 res += strlen(self->errors) + 1;
6414 return res;
6415}
6416
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006417static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006418 _PICKLE_UNPICKLER_LOAD_METHODDEF
6419 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006420 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006421 {NULL, NULL} /* sentinel */
6422};
6423
6424static void
6425Unpickler_dealloc(UnpicklerObject *self)
6426{
6427 PyObject_GC_UnTrack((PyObject *)self);
6428 Py_XDECREF(self->readline);
6429 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006430 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006431 Py_XDECREF(self->stack);
6432 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006433 if (self->buffer.buf != NULL) {
6434 PyBuffer_Release(&self->buffer);
6435 self->buffer.buf = NULL;
6436 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006437
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006438 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006439 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006440 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006441 PyMem_Free(self->encoding);
6442 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006443
6444 Py_TYPE(self)->tp_free((PyObject *)self);
6445}
6446
6447static int
6448Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6449{
6450 Py_VISIT(self->readline);
6451 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006452 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006453 Py_VISIT(self->stack);
6454 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 return 0;
6456}
6457
6458static int
6459Unpickler_clear(UnpicklerObject *self)
6460{
6461 Py_CLEAR(self->readline);
6462 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006463 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006464 Py_CLEAR(self->stack);
6465 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006466 if (self->buffer.buf != NULL) {
6467 PyBuffer_Release(&self->buffer);
6468 self->buffer.buf = NULL;
6469 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006470
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006471 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006472 PyMem_Free(self->marks);
6473 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006474 PyMem_Free(self->input_line);
6475 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006476 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006477 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006478 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006479 self->errors = NULL;
6480
6481 return 0;
6482}
6483
Larry Hastings61272b72014-01-07 12:41:53 -08006484/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006485
6486_pickle.Unpickler.__init__
6487
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006488 file: object
6489 *
6490 fix_imports: bool = True
6491 encoding: str = 'ASCII'
6492 errors: str = 'strict'
6493
6494This takes a binary file for reading a pickle data stream.
6495
6496The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006497protocol argument is needed. Bytes past the pickled object's
6498representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006499
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006500The argument *file* must have two methods, a read() method that takes
6501an integer argument, and a readline() method that requires no
6502arguments. Both methods should return bytes. Thus *file* can be a
6503binary file object opened for reading, a io.BytesIO object, or any
6504other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006505
6506Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6507which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006508generated by Python 2. If *fix_imports* is True, pickle will try to
6509map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006510*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006511instances pickled by Python 2; these default to 'ASCII' and 'strict',
6512respectively. The *encoding* can be 'bytes' to read these 8-bit
6513string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006514[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006515
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006516static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006517_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08006518/*[clinic end generated code: output=b9ed1d84d315f3b5 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006519{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006520 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006522 /* In case of multiple __init__() calls, clear previous content. */
6523 if (self->read != NULL)
6524 (void)Unpickler_clear(self);
6525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006526 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006527 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006529 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006530 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006531
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006532 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006533 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006534 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006535
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006536 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006537 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6538 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006539 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006540 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 }
6542 else {
6543 self->pers_func = NULL;
6544 }
6545
6546 self->stack = (Pdata *)Pdata_New();
6547 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006548 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006550 self->memo_size = 32;
6551 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006552 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006553 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006554
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006555 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006556
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006557 return 0;
6558}
6559
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006560
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006561/* Define a proxy object for the Unpickler's internal memo object. This is to
6562 * avoid breaking code like:
6563 * unpickler.memo.clear()
6564 * and
6565 * unpickler.memo = saved_memo
6566 * Is this a good idea? Not really, but we don't want to break code that uses
6567 * it. Note that we don't implement the entire mapping API here. This is
6568 * intentional, as these should be treated as black-box implementation details.
6569 *
6570 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006571 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006572 */
6573
Larry Hastings61272b72014-01-07 12:41:53 -08006574/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575_pickle.UnpicklerMemoProxy.clear
6576
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006578[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006579
Larry Hastings3cceb382014-01-04 11:09:09 -08006580static PyObject *
6581_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006582/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583{
6584 _Unpickler_MemoCleanup(self->unpickler);
6585 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6586 if (self->unpickler->memo == NULL)
6587 return NULL;
6588 Py_RETURN_NONE;
6589}
6590
Larry Hastings61272b72014-01-07 12:41:53 -08006591/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006592_pickle.UnpicklerMemoProxy.copy
6593
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006594Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006595[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006596
Larry Hastings3cceb382014-01-04 11:09:09 -08006597static PyObject *
6598_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006599/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006600{
6601 Py_ssize_t i;
6602 PyObject *new_memo = PyDict_New();
6603 if (new_memo == NULL)
6604 return NULL;
6605
6606 for (i = 0; i < self->unpickler->memo_size; i++) {
6607 int status;
6608 PyObject *key, *value;
6609
6610 value = self->unpickler->memo[i];
6611 if (value == NULL)
6612 continue;
6613
6614 key = PyLong_FromSsize_t(i);
6615 if (key == NULL)
6616 goto error;
6617 status = PyDict_SetItem(new_memo, key, value);
6618 Py_DECREF(key);
6619 if (status < 0)
6620 goto error;
6621 }
6622 return new_memo;
6623
6624error:
6625 Py_DECREF(new_memo);
6626 return NULL;
6627}
6628
Larry Hastings61272b72014-01-07 12:41:53 -08006629/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006630_pickle.UnpicklerMemoProxy.__reduce__
6631
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006632Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006633[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006634
Larry Hastings3cceb382014-01-04 11:09:09 -08006635static PyObject *
6636_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006637/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006638{
6639 PyObject *reduce_value;
6640 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006641 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006642 if (contents == NULL)
6643 return NULL;
6644
6645 reduce_value = PyTuple_New(2);
6646 if (reduce_value == NULL) {
6647 Py_DECREF(contents);
6648 return NULL;
6649 }
6650 constructor_args = PyTuple_New(1);
6651 if (constructor_args == NULL) {
6652 Py_DECREF(contents);
6653 Py_DECREF(reduce_value);
6654 return NULL;
6655 }
6656 PyTuple_SET_ITEM(constructor_args, 0, contents);
6657 Py_INCREF((PyObject *)&PyDict_Type);
6658 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6659 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6660 return reduce_value;
6661}
6662
6663static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006664 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6665 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6666 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006667 {NULL, NULL} /* sentinel */
6668};
6669
6670static void
6671UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6672{
6673 PyObject_GC_UnTrack(self);
6674 Py_XDECREF(self->unpickler);
6675 PyObject_GC_Del((PyObject *)self);
6676}
6677
6678static int
6679UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6680 visitproc visit, void *arg)
6681{
6682 Py_VISIT(self->unpickler);
6683 return 0;
6684}
6685
6686static int
6687UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6688{
6689 Py_CLEAR(self->unpickler);
6690 return 0;
6691}
6692
6693static PyTypeObject UnpicklerMemoProxyType = {
6694 PyVarObject_HEAD_INIT(NULL, 0)
6695 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6696 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6697 0,
6698 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6699 0, /* tp_print */
6700 0, /* tp_getattr */
6701 0, /* tp_setattr */
6702 0, /* tp_compare */
6703 0, /* tp_repr */
6704 0, /* tp_as_number */
6705 0, /* tp_as_sequence */
6706 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006707 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006708 0, /* tp_call */
6709 0, /* tp_str */
6710 PyObject_GenericGetAttr, /* tp_getattro */
6711 PyObject_GenericSetAttr, /* tp_setattro */
6712 0, /* tp_as_buffer */
6713 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6714 0, /* tp_doc */
6715 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6716 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6717 0, /* tp_richcompare */
6718 0, /* tp_weaklistoffset */
6719 0, /* tp_iter */
6720 0, /* tp_iternext */
6721 unpicklerproxy_methods, /* tp_methods */
6722};
6723
6724static PyObject *
6725UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6726{
6727 UnpicklerMemoProxyObject *self;
6728
6729 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6730 &UnpicklerMemoProxyType);
6731 if (self == NULL)
6732 return NULL;
6733 Py_INCREF(unpickler);
6734 self->unpickler = unpickler;
6735 PyObject_GC_Track(self);
6736 return (PyObject *)self;
6737}
6738
6739/*****************************************************************************/
6740
6741
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006742static PyObject *
6743Unpickler_get_memo(UnpicklerObject *self)
6744{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006745 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006746}
6747
6748static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006749Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006750{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006751 PyObject **new_memo;
6752 Py_ssize_t new_memo_size = 0;
6753 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006755 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006756 PyErr_SetString(PyExc_TypeError,
6757 "attribute deletion is not supported");
6758 return -1;
6759 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006760
6761 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6762 UnpicklerObject *unpickler =
6763 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6764
6765 new_memo_size = unpickler->memo_size;
6766 new_memo = _Unpickler_NewMemo(new_memo_size);
6767 if (new_memo == NULL)
6768 return -1;
6769
6770 for (i = 0; i < new_memo_size; i++) {
6771 Py_XINCREF(unpickler->memo[i]);
6772 new_memo[i] = unpickler->memo[i];
6773 }
6774 }
6775 else if (PyDict_Check(obj)) {
6776 Py_ssize_t i = 0;
6777 PyObject *key, *value;
6778
6779 new_memo_size = PyDict_Size(obj);
6780 new_memo = _Unpickler_NewMemo(new_memo_size);
6781 if (new_memo == NULL)
6782 return -1;
6783
6784 while (PyDict_Next(obj, &i, &key, &value)) {
6785 Py_ssize_t idx;
6786 if (!PyLong_Check(key)) {
6787 PyErr_SetString(PyExc_TypeError,
6788 "memo key must be integers");
6789 goto error;
6790 }
6791 idx = PyLong_AsSsize_t(key);
6792 if (idx == -1 && PyErr_Occurred())
6793 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006794 if (idx < 0) {
6795 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006796 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006797 goto error;
6798 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006799 if (_Unpickler_MemoPut(self, idx, value) < 0)
6800 goto error;
6801 }
6802 }
6803 else {
6804 PyErr_Format(PyExc_TypeError,
6805 "'memo' attribute must be an UnpicklerMemoProxy object"
6806 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006807 return -1;
6808 }
6809
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006810 _Unpickler_MemoCleanup(self);
6811 self->memo_size = new_memo_size;
6812 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006813
6814 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006815
6816 error:
6817 if (new_memo_size) {
6818 i = new_memo_size;
6819 while (--i >= 0) {
6820 Py_XDECREF(new_memo[i]);
6821 }
6822 PyMem_FREE(new_memo);
6823 }
6824 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006825}
6826
6827static PyObject *
6828Unpickler_get_persload(UnpicklerObject *self)
6829{
6830 if (self->pers_func == NULL)
6831 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6832 else
6833 Py_INCREF(self->pers_func);
6834 return self->pers_func;
6835}
6836
6837static int
6838Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6839{
6840 PyObject *tmp;
6841
6842 if (value == NULL) {
6843 PyErr_SetString(PyExc_TypeError,
6844 "attribute deletion is not supported");
6845 return -1;
6846 }
6847 if (!PyCallable_Check(value)) {
6848 PyErr_SetString(PyExc_TypeError,
6849 "persistent_load must be a callable taking "
6850 "one argument");
6851 return -1;
6852 }
6853
6854 tmp = self->pers_func;
6855 Py_INCREF(value);
6856 self->pers_func = value;
6857 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6858
6859 return 0;
6860}
6861
6862static PyGetSetDef Unpickler_getsets[] = {
6863 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6864 {"persistent_load", (getter)Unpickler_get_persload,
6865 (setter)Unpickler_set_persload},
6866 {NULL}
6867};
6868
6869static PyTypeObject Unpickler_Type = {
6870 PyVarObject_HEAD_INIT(NULL, 0)
6871 "_pickle.Unpickler", /*tp_name*/
6872 sizeof(UnpicklerObject), /*tp_basicsize*/
6873 0, /*tp_itemsize*/
6874 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6875 0, /*tp_print*/
6876 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006878 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006879 0, /*tp_repr*/
6880 0, /*tp_as_number*/
6881 0, /*tp_as_sequence*/
6882 0, /*tp_as_mapping*/
6883 0, /*tp_hash*/
6884 0, /*tp_call*/
6885 0, /*tp_str*/
6886 0, /*tp_getattro*/
6887 0, /*tp_setattro*/
6888 0, /*tp_as_buffer*/
6889 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006890 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006891 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6892 (inquiry)Unpickler_clear, /*tp_clear*/
6893 0, /*tp_richcompare*/
6894 0, /*tp_weaklistoffset*/
6895 0, /*tp_iter*/
6896 0, /*tp_iternext*/
6897 Unpickler_methods, /*tp_methods*/
6898 0, /*tp_members*/
6899 Unpickler_getsets, /*tp_getset*/
6900 0, /*tp_base*/
6901 0, /*tp_dict*/
6902 0, /*tp_descr_get*/
6903 0, /*tp_descr_set*/
6904 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006905 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006906 PyType_GenericAlloc, /*tp_alloc*/
6907 PyType_GenericNew, /*tp_new*/
6908 PyObject_GC_Del, /*tp_free*/
6909 0, /*tp_is_gc*/
6910};
6911
Larry Hastings61272b72014-01-07 12:41:53 -08006912/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006913
6914_pickle.dump
6915
6916 obj: object
6917 file: object
6918 protocol: object = NULL
6919 *
6920 fix_imports: bool = True
6921
6922Write a pickled representation of obj to the open file object file.
6923
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006924This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6925be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006926
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006927The optional *protocol* argument tells the pickler to use the given
6928protocol supported protocols are 0, 1, 2, 3 and 4. The default
6929protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006930
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006931Specifying a negative protocol version selects the highest protocol
6932version supported. The higher the protocol used, the more recent the
6933version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006934
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006935The *file* argument must have a write() method that accepts a single
6936bytes argument. It can thus be a file object opened for binary
6937writing, a io.BytesIO instance, or any other custom object that meets
6938this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006939
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006940If *fix_imports* is True and protocol is less than 3, pickle will try
6941to map the new Python 3 names to the old module names used in Python
69422, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006943[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006944
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006945static PyObject *
6946_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08006947/*[clinic end generated code: output=a606e626d553850d input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006948{
6949 PicklerObject *pickler = _Pickler_New();
6950
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006951 if (pickler == NULL)
6952 return NULL;
6953
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006954 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006955 goto error;
6956
6957 if (_Pickler_SetOutputStream(pickler, file) < 0)
6958 goto error;
6959
6960 if (dump(pickler, obj) < 0)
6961 goto error;
6962
6963 if (_Pickler_FlushToFile(pickler) < 0)
6964 goto error;
6965
6966 Py_DECREF(pickler);
6967 Py_RETURN_NONE;
6968
6969 error:
6970 Py_XDECREF(pickler);
6971 return NULL;
6972}
6973
Larry Hastings61272b72014-01-07 12:41:53 -08006974/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006975
6976_pickle.dumps
6977
6978 obj: object
6979 protocol: object = NULL
6980 *
6981 fix_imports: bool = True
6982
6983Return the pickled representation of the object as a bytes object.
6984
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006985The optional *protocol* argument tells the pickler to use the given
6986protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6987protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006988
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006989Specifying a negative protocol version selects the highest protocol
6990version supported. The higher the protocol used, the more recent the
6991version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006992
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006993If *fix_imports* is True and *protocol* is less than 3, pickle will
6994try to map the new Python 3 names to the old module names used in
6995Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006996[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006997
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006998static PyObject *
6999_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, int fix_imports)
Larry Hastings581ee362014-01-28 05:00:08 -08007000/*[clinic end generated code: output=777f0deefe5b88ee input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007001{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007002 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007003 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007005 if (pickler == NULL)
7006 return NULL;
7007
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 goto error;
7010
7011 if (dump(pickler, obj) < 0)
7012 goto error;
7013
7014 result = _Pickler_GetString(pickler);
7015 Py_DECREF(pickler);
7016 return result;
7017
7018 error:
7019 Py_XDECREF(pickler);
7020 return NULL;
7021}
7022
Larry Hastings61272b72014-01-07 12:41:53 -08007023/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007024
7025_pickle.load
7026
7027 file: object
7028 *
7029 fix_imports: bool = True
7030 encoding: str = 'ASCII'
7031 errors: str = 'strict'
7032
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007033Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007034
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007035This is equivalent to ``Unpickler(file).load()``, but may be more
7036efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007037
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007038The protocol version of the pickle is detected automatically, so no
7039protocol argument is needed. Bytes past the pickled object's
7040representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042The argument *file* must have two methods, a read() method that takes
7043an integer argument, and a readline() method that requires no
7044arguments. Both methods should return bytes. Thus *file* can be a
7045binary file object opened for reading, a io.BytesIO object, or any
7046other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007047
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007048Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7049which are used to control compatiblity support for pickle stream
7050generated by Python 2. If *fix_imports* is True, pickle will try to
7051map the old Python 2 names to the new names used in Python 3. The
7052*encoding* and *errors* tell pickle how to decode 8-bit string
7053instances pickled by Python 2; these default to 'ASCII' and 'strict',
7054respectively. The *encoding* can be 'bytes' to read these 8-bit
7055string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007056[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007057
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058static PyObject *
7059_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007060/*[clinic end generated code: output=568c61356c172654 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007062 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007063 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065 if (unpickler == NULL)
7066 return NULL;
7067
7068 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7069 goto error;
7070
7071 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7072 goto error;
7073
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007074 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007075
7076 result = load(unpickler);
7077 Py_DECREF(unpickler);
7078 return result;
7079
7080 error:
7081 Py_XDECREF(unpickler);
7082 return NULL;
7083}
7084
Larry Hastings61272b72014-01-07 12:41:53 -08007085/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007086
7087_pickle.loads
7088
7089 data: object
7090 *
7091 fix_imports: bool = True
7092 encoding: str = 'ASCII'
7093 errors: str = 'strict'
7094
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007095Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007097The protocol version of the pickle is detected automatically, so no
7098protocol argument is needed. Bytes past the pickled object's
7099representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007101Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7102which are used to control compatiblity support for pickle stream
7103generated by Python 2. If *fix_imports* is True, pickle will try to
7104map the old Python 2 names to the new names used in Python 3. The
7105*encoding* and *errors* tell pickle how to decode 8-bit string
7106instances pickled by Python 2; these default to 'ASCII' and 'strict',
7107respectively. The *encoding* can be 'bytes' to read these 8-bit
7108string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007109[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007110
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007111static PyObject *
7112_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, const char *encoding, const char *errors)
Larry Hastings581ee362014-01-28 05:00:08 -08007113/*[clinic end generated code: output=0b3845ad110b2522 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007114{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007115 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007116 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007117
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007118 if (unpickler == NULL)
7119 return NULL;
7120
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007122 goto error;
7123
7124 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7125 goto error;
7126
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007127 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007128
7129 result = load(unpickler);
7130 Py_DECREF(unpickler);
7131 return result;
7132
7133 error:
7134 Py_XDECREF(unpickler);
7135 return NULL;
7136}
7137
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007138static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007139 _PICKLE_DUMP_METHODDEF
7140 _PICKLE_DUMPS_METHODDEF
7141 _PICKLE_LOAD_METHODDEF
7142 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007143 {NULL, NULL} /* sentinel */
7144};
7145
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007146static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007147pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007148{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007149 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007150 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007151}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007152
Stefan Krahf483b0f2013-12-14 13:43:10 +01007153static void
7154pickle_free(PyObject *m)
7155{
7156 _Pickle_ClearState(_Pickle_GetState(m));
7157}
7158
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007159static int
7160pickle_traverse(PyObject *m, visitproc visit, void *arg)
7161{
7162 PickleState *st = _Pickle_GetState(m);
7163 Py_VISIT(st->PickleError);
7164 Py_VISIT(st->PicklingError);
7165 Py_VISIT(st->UnpicklingError);
7166 Py_VISIT(st->dispatch_table);
7167 Py_VISIT(st->extension_registry);
7168 Py_VISIT(st->extension_cache);
7169 Py_VISIT(st->inverted_registry);
7170 Py_VISIT(st->name_mapping_2to3);
7171 Py_VISIT(st->import_mapping_2to3);
7172 Py_VISIT(st->name_mapping_3to2);
7173 Py_VISIT(st->import_mapping_3to2);
7174 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007175 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007176 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007177}
7178
7179static struct PyModuleDef _picklemodule = {
7180 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007181 "_pickle", /* m_name */
7182 pickle_module_doc, /* m_doc */
7183 sizeof(PickleState), /* m_size */
7184 pickle_methods, /* m_methods */
7185 NULL, /* m_reload */
7186 pickle_traverse, /* m_traverse */
7187 pickle_clear, /* m_clear */
7188 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007189};
7190
7191PyMODINIT_FUNC
7192PyInit__pickle(void)
7193{
7194 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007195 PickleState *st;
7196
7197 m = PyState_FindModule(&_picklemodule);
7198 if (m) {
7199 Py_INCREF(m);
7200 return m;
7201 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007202
7203 if (PyType_Ready(&Unpickler_Type) < 0)
7204 return NULL;
7205 if (PyType_Ready(&Pickler_Type) < 0)
7206 return NULL;
7207 if (PyType_Ready(&Pdata_Type) < 0)
7208 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007209 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7210 return NULL;
7211 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7212 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007213
7214 /* Create the module and add the functions. */
7215 m = PyModule_Create(&_picklemodule);
7216 if (m == NULL)
7217 return NULL;
7218
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007219 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007220 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7221 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007222 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007223 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7224 return NULL;
7225
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007226 st = _Pickle_GetState(m);
7227
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007228 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007229 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7230 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007231 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007232 st->PicklingError = \
7233 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7234 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007235 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007236 st->UnpicklingError = \
7237 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7238 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007239 return NULL;
7240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007241 Py_INCREF(st->PickleError);
7242 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007243 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007244 Py_INCREF(st->PicklingError);
7245 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007246 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007247 Py_INCREF(st->UnpicklingError);
7248 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007249 return NULL;
7250
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007251 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007252 return NULL;
7253
7254 return m;
7255}