blob: 0f62b1c019c9785ac3e68698d4657115336bd365 [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;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500442 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000443 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000444 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000445
446 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200447 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000448 return 0;
449
450 nomemory:
451 PyErr_NoMemory();
452 return -1;
453}
454
455/* D is a Pdata*. Pop the topmost element and store it into V, which
456 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
457 * is raised and V is set to NULL.
458 */
459static PyObject *
460Pdata_pop(Pdata *self)
461{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000462 if (Py_SIZE(self) == 0) {
Serhiy Storchakae9b30742015-11-23 15:17:43 +0200463 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800464 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000465 return NULL;
466 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000467 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000468}
469#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
470
471static int
472Pdata_push(Pdata *self, PyObject *obj)
473{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000474 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475 return -1;
476 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000477 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478 return 0;
479}
480
481/* Push an object on stack, transferring its ownership to the stack. */
482#define PDATA_PUSH(D, O, ER) do { \
483 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
484
485/* Push an object on stack, adding a new reference to the object. */
486#define PDATA_APPEND(D, O, ER) do { \
487 Py_INCREF((O)); \
488 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
489
490static PyObject *
491Pdata_poptuple(Pdata *self, Py_ssize_t start)
492{
493 PyObject *tuple;
494 Py_ssize_t len, i, j;
495
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497 tuple = PyTuple_New(len);
498 if (tuple == NULL)
499 return NULL;
500 for (i = start, j = 0; j < len; i++, j++)
501 PyTuple_SET_ITEM(tuple, j, self->data[i]);
502
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000503 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000504 return tuple;
505}
506
507static PyObject *
508Pdata_poplist(Pdata *self, Py_ssize_t start)
509{
510 PyObject *list;
511 Py_ssize_t len, i, j;
512
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000513 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000514 list = PyList_New(len);
515 if (list == NULL)
516 return NULL;
517 for (i = start, j = 0; j < len; i++, j++)
518 PyList_SET_ITEM(list, j, self->data[i]);
519
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000520 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000521 return list;
522}
523
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000524typedef struct {
525 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200526 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000527} PyMemoEntry;
528
529typedef struct {
530 Py_ssize_t mt_mask;
531 Py_ssize_t mt_used;
532 Py_ssize_t mt_allocated;
533 PyMemoEntry *mt_table;
534} PyMemoTable;
535
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000536typedef struct PicklerObject {
537 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000538 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000539 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000541 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100542 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543
544 PyObject *write; /* write() method of the output stream. */
545 PyObject *output_buffer; /* Write into a local bytearray buffer before
546 flushing to the stream. */
547 Py_ssize_t output_len; /* Length of output_buffer. */
548 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549 int proto; /* Pickle protocol number, >= 0 */
550 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100551 int framing; /* True when framing is enabled, proto >= 4 */
552 Py_ssize_t frame_start; /* Position in output_buffer where the
Martin Pantera90a4a92016-05-30 04:04:50 +0000553 current frame begins. -1 if there
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100554 is no frame currently open. */
555
556 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000557 int fast; /* Enable fast mode if set to a true value.
558 The fast mode disable the usage of memo,
559 therefore speeding the pickling process by
560 not generating superfluous PUT opcodes. It
561 should not be used if with self-referential
562 objects. */
563 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000564 int fix_imports; /* Indicate whether Pickler should fix
565 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000566 PyObject *fast_memo;
567} PicklerObject;
568
569typedef struct UnpicklerObject {
570 PyObject_HEAD
571 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000572
573 /* The unpickler memo is just an array of PyObject *s. Using a dict
574 is unnecessary, since the keys are contiguous ints. */
575 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100576 Py_ssize_t memo_size; /* Capacity of the memo array */
577 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000578
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000580
581 Py_buffer buffer;
582 char *input_buffer;
583 char *input_line;
584 Py_ssize_t input_len;
585 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000586 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100587
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000588 PyObject *read; /* read() method of the input stream. */
589 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000590 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 char *encoding; /* Name of the encoding to be used for
593 decoding strings pickled using Python
594 2.x. The default value is "ASCII" */
595 char *errors; /* Name of errors handling scheme to used when
596 decoding strings. The default value is
597 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500598 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000599 objects. */
600 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
601 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000602 int proto; /* Protocol of the pickle loaded. */
603 int fix_imports; /* Indicate whether Unpickler should fix
604 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605} UnpicklerObject;
606
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200607typedef struct {
608 PyObject_HEAD
609 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
610} PicklerMemoProxyObject;
611
612typedef struct {
613 PyObject_HEAD
614 UnpicklerObject *unpickler;
615} UnpicklerMemoProxyObject;
616
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000617/* Forward declarations */
618static int save(PicklerObject *, PyObject *, int);
619static int save_reduce(PicklerObject *, PyObject *, PyObject *);
620static PyTypeObject Pickler_Type;
621static PyTypeObject Unpickler_Type;
622
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200623#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000625/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300626 A custom hashtable mapping void* to Python ints. This is used by the pickler
627 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000628 a bunch of unnecessary object creation. This makes a huge performance
629 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000631#define MT_MINSIZE 8
632#define PERTURB_SHIFT 5
633
634
635static PyMemoTable *
636PyMemoTable_New(void)
637{
638 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
639 if (memo == NULL) {
640 PyErr_NoMemory();
641 return NULL;
642 }
643
644 memo->mt_used = 0;
645 memo->mt_allocated = MT_MINSIZE;
646 memo->mt_mask = MT_MINSIZE - 1;
647 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
648 if (memo->mt_table == NULL) {
649 PyMem_FREE(memo);
650 PyErr_NoMemory();
651 return NULL;
652 }
653 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
654
655 return memo;
656}
657
658static PyMemoTable *
659PyMemoTable_Copy(PyMemoTable *self)
660{
661 Py_ssize_t i;
662 PyMemoTable *new = PyMemoTable_New();
663 if (new == NULL)
664 return NULL;
665
666 new->mt_used = self->mt_used;
667 new->mt_allocated = self->mt_allocated;
668 new->mt_mask = self->mt_mask;
669 /* The table we get from _New() is probably smaller than we wanted.
670 Free it and allocate one that's the right size. */
671 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500672 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000673 if (new->mt_table == NULL) {
674 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200675 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000676 return NULL;
677 }
678 for (i = 0; i < self->mt_allocated; i++) {
679 Py_XINCREF(self->mt_table[i].me_key);
680 }
681 memcpy(new->mt_table, self->mt_table,
682 sizeof(PyMemoEntry) * self->mt_allocated);
683
684 return new;
685}
686
687static Py_ssize_t
688PyMemoTable_Size(PyMemoTable *self)
689{
690 return self->mt_used;
691}
692
693static int
694PyMemoTable_Clear(PyMemoTable *self)
695{
696 Py_ssize_t i = self->mt_allocated;
697
698 while (--i >= 0) {
699 Py_XDECREF(self->mt_table[i].me_key);
700 }
701 self->mt_used = 0;
702 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
703 return 0;
704}
705
706static void
707PyMemoTable_Del(PyMemoTable *self)
708{
709 if (self == NULL)
710 return;
711 PyMemoTable_Clear(self);
712
713 PyMem_FREE(self->mt_table);
714 PyMem_FREE(self);
715}
716
717/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
718 can be considerably simpler than dictobject.c's lookdict(). */
719static PyMemoEntry *
720_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
721{
722 size_t i;
723 size_t perturb;
724 size_t mask = (size_t)self->mt_mask;
725 PyMemoEntry *table = self->mt_table;
726 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000727 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000728
729 i = hash & mask;
730 entry = &table[i];
731 if (entry->me_key == NULL || entry->me_key == key)
732 return entry;
733
734 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
735 i = (i << 2) + i + perturb + 1;
736 entry = &table[i & mask];
737 if (entry->me_key == NULL || entry->me_key == key)
738 return entry;
739 }
740 assert(0); /* Never reached */
741 return NULL;
742}
743
744/* Returns -1 on failure, 0 on success. */
745static int
746_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
747{
748 PyMemoEntry *oldtable = NULL;
749 PyMemoEntry *oldentry, *newentry;
750 Py_ssize_t new_size = MT_MINSIZE;
751 Py_ssize_t to_process;
752
753 assert(min_size > 0);
754
755 /* Find the smallest valid table size >= min_size. */
756 while (new_size < min_size && new_size > 0)
757 new_size <<= 1;
758 if (new_size <= 0) {
759 PyErr_NoMemory();
760 return -1;
761 }
762 /* new_size needs to be a power of two. */
763 assert((new_size & (new_size - 1)) == 0);
764
765 /* Allocate new table. */
766 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500767 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000768 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200769 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000770 PyErr_NoMemory();
771 return -1;
772 }
773 self->mt_allocated = new_size;
774 self->mt_mask = new_size - 1;
775 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
776
777 /* Copy entries from the old table. */
778 to_process = self->mt_used;
779 for (oldentry = oldtable; to_process > 0; oldentry++) {
780 if (oldentry->me_key != NULL) {
781 to_process--;
782 /* newentry is a pointer to a chunk of the new
783 mt_table, so we're setting the key:value pair
784 in-place. */
785 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
786 newentry->me_key = oldentry->me_key;
787 newentry->me_value = oldentry->me_value;
788 }
789 }
790
791 /* Deallocate the old table. */
792 PyMem_FREE(oldtable);
793 return 0;
794}
795
796/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200797static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000798PyMemoTable_Get(PyMemoTable *self, PyObject *key)
799{
800 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
801 if (entry->me_key == NULL)
802 return NULL;
803 return &entry->me_value;
804}
805
806/* Returns -1 on failure, 0 on success. */
807static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200808PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000809{
810 PyMemoEntry *entry;
811
812 assert(key != NULL);
813
814 entry = _PyMemoTable_Lookup(self, key);
815 if (entry->me_key != NULL) {
816 entry->me_value = value;
817 return 0;
818 }
819 Py_INCREF(key);
820 entry->me_key = key;
821 entry->me_value = value;
822 self->mt_used++;
823
824 /* If we added a key, we can safely resize. Otherwise just return!
825 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
826 *
827 * Quadrupling the size improves average table sparseness
828 * (reducing collisions) at the cost of some memory. It also halves
829 * the number of expensive resize operations in a growing memo table.
830 *
831 * Very large memo tables (over 50K items) use doubling instead.
832 * This may help applications with severe memory constraints.
833 */
834 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
835 return 0;
836 return _PyMemoTable_ResizeTable(self,
837 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
838}
839
840#undef MT_MINSIZE
841#undef PERTURB_SHIFT
842
843/*************************************************************************/
844
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000845
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000846static int
847_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000848{
Serhiy Storchaka48842712016-04-06 09:45:48 +0300849 Py_XSETREF(self->output_buffer,
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200850 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000851 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000852 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000853 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100854 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000855 return 0;
856}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000857
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100858static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100859_write_size64(char *out, size_t value)
860{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200861 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800862
863 assert(sizeof(size_t) <= 8);
864
865 for (i = 0; i < sizeof(size_t); i++) {
866 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
867 }
868 for (i = sizeof(size_t); i < 8; i++) {
869 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800870 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100871}
872
873static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100874_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
875{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100876 qdata[0] = FRAME;
877 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100878}
879
880static int
881_Pickler_CommitFrame(PicklerObject *self)
882{
883 size_t frame_len;
884 char *qdata;
885
886 if (!self->framing || self->frame_start == -1)
887 return 0;
888 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
889 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
890 _Pickler_WriteFrameHeader(self, qdata, frame_len);
891 self->frame_start = -1;
892 return 0;
893}
894
895static int
896_Pickler_OpcodeBoundary(PicklerObject *self)
897{
898 Py_ssize_t frame_len;
899
900 if (!self->framing || self->frame_start == -1)
901 return 0;
902 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
903 if (frame_len >= FRAME_SIZE_TARGET)
904 return _Pickler_CommitFrame(self);
905 else
906 return 0;
907}
908
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000909static PyObject *
910_Pickler_GetString(PicklerObject *self)
911{
912 PyObject *output_buffer = self->output_buffer;
913
914 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100915
916 if (_Pickler_CommitFrame(self))
917 return NULL;
918
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000919 self->output_buffer = NULL;
920 /* Resize down to exact size */
921 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
922 return NULL;
923 return output_buffer;
924}
925
926static int
927_Pickler_FlushToFile(PicklerObject *self)
928{
929 PyObject *output, *result;
930
931 assert(self->write != NULL);
932
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100933 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 output = _Pickler_GetString(self);
935 if (output == NULL)
936 return -1;
937
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800938 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000939 Py_XDECREF(result);
940 return (result == NULL) ? -1 : 0;
941}
942
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200943static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100944_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000945{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100946 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000947 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100948 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000949
950 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951 need_new_frame = (self->framing && self->frame_start == -1);
952
953 if (need_new_frame)
954 n = data_len + FRAME_HEADER_SIZE;
955 else
956 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957
958 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100959 if (required > self->max_output_len) {
960 /* Make place in buffer for the pickle chunk */
961 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
962 PyErr_NoMemory();
963 return -1;
964 }
965 self->max_output_len = (self->output_len + n) / 2 * 3;
966 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
967 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000968 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000969 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100970 if (need_new_frame) {
971 /* Setup new frame */
972 Py_ssize_t frame_start = self->output_len;
973 self->frame_start = frame_start;
974 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
975 /* Write an invalid value, for debugging */
976 buffer[frame_start + i] = 0xFE;
977 }
978 self->output_len += FRAME_HEADER_SIZE;
979 }
980 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000981 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983 buffer[self->output_len + i] = s[i];
984 }
985 }
986 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100987 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000988 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100989 self->output_len += data_len;
990 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000991}
992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000993static PicklerObject *
994_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000995{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000997
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000998 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
999 if (self == NULL)
1000 return NULL;
1001
1002 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001003 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 self->write = NULL;
1005 self->proto = 0;
1006 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001007 self->framing = 0;
1008 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001009 self->fast = 0;
1010 self->fast_nesting = 0;
1011 self->fix_imports = 0;
1012 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001013 self->max_output_len = WRITE_BUF_SIZE;
1014 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001015
1016 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001017 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1018 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001019
1020 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001021 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001022 return NULL;
1023 }
1024 return self;
1025}
1026
1027static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001028_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001029{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001030 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001031
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001032 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001033 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001034 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001035 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001036 proto = PyLong_AsLong(protocol);
1037 if (proto < 0) {
1038 if (proto == -1 && PyErr_Occurred())
1039 return -1;
1040 proto = HIGHEST_PROTOCOL;
1041 }
1042 else if (proto > HIGHEST_PROTOCOL) {
1043 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1044 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001046 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001047 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001048 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001049 self->bin = proto > 0;
1050 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001051 return 0;
1052}
1053
1054/* Returns -1 (with an exception set) on failure, 0 on success. This may
1055 be called once on a freshly created Pickler. */
1056static int
1057_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1058{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001059 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001060 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001061 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 if (self->write == NULL) {
1063 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1064 PyErr_SetString(PyExc_TypeError,
1065 "file must have a 'write' attribute");
1066 return -1;
1067 }
1068
1069 return 0;
1070}
1071
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072/* Returns the size of the input on success, -1 on failure. This takes its
1073 own reference to `input`. */
1074static Py_ssize_t
1075_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1076{
1077 if (self->buffer.buf != NULL)
1078 PyBuffer_Release(&self->buffer);
1079 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1080 return -1;
1081 self->input_buffer = self->buffer.buf;
1082 self->input_len = self->buffer.len;
1083 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001084 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 return self->input_len;
1086}
1087
Antoine Pitrou04248a82010-10-12 20:51:21 +00001088static int
1089_Unpickler_SkipConsumed(UnpicklerObject *self)
1090{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001091 Py_ssize_t consumed;
1092 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001093
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001094 consumed = self->next_read_idx - self->prefetched_idx;
1095 if (consumed <= 0)
1096 return 0;
1097
1098 assert(self->peek); /* otherwise we did something wrong */
Martin Panter6245cb32016-04-15 02:14:19 +00001099 /* This makes a useless copy... */
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001100 r = PyObject_CallFunction(self->read, "n", consumed);
1101 if (r == NULL)
1102 return -1;
1103 Py_DECREF(r);
1104
1105 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001106 return 0;
1107}
1108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001109static const Py_ssize_t READ_WHOLE_LINE = -1;
1110
1111/* If reading from a file, we need to only pull the bytes we need, since there
1112 may be multiple pickle objects arranged contiguously in the same input
1113 buffer.
1114
1115 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1116 bytes from the input stream/buffer.
1117
1118 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1119 failure; on success, returns the number of bytes read from the file.
1120
1121 On success, self->input_len will be 0; this is intentional so that when
1122 unpickling from a file, the "we've run out of data" code paths will trigger,
1123 causing the Unpickler to go back to the file for more data. Use the returned
1124 size to tell you how much data you can process. */
1125static Py_ssize_t
1126_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1127{
1128 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001129 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001130
1131 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001132
Antoine Pitrou04248a82010-10-12 20:51:21 +00001133 if (_Unpickler_SkipConsumed(self) < 0)
1134 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001135
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001136 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001137 PyObject *empty_tuple = PyTuple_New(0);
1138 data = PyObject_Call(self->readline, empty_tuple, NULL);
1139 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001140 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001141 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001142 PyObject *len;
1143 /* Prefetch some data without advancing the file pointer, if possible */
1144 if (self->peek && n < PREFETCH) {
1145 len = PyLong_FromSsize_t(PREFETCH);
1146 if (len == NULL)
1147 return -1;
1148 data = _Pickle_FastCall(self->peek, len);
1149 if (data == NULL) {
1150 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1151 return -1;
1152 /* peek() is probably not supported by the given file object */
1153 PyErr_Clear();
1154 Py_CLEAR(self->peek);
1155 }
1156 else {
1157 read_size = _Unpickler_SetStringInput(self, data);
1158 Py_DECREF(data);
1159 self->prefetched_idx = 0;
1160 if (n <= read_size)
1161 return n;
1162 }
1163 }
1164 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001165 if (len == NULL)
1166 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001167 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001169 if (data == NULL)
1170 return -1;
1171
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001172 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001173 Py_DECREF(data);
1174 return read_size;
1175}
1176
1177/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1178
1179 This should be used for all data reads, rather than accessing the unpickler's
1180 input buffer directly. This method deals correctly with reading from input
1181 streams, which the input buffer doesn't deal with.
1182
1183 Note that when reading from a file-like object, self->next_read_idx won't
1184 be updated (it should remain at 0 for the entire unpickling process). You
1185 should use this function's return value to know how many bytes you can
1186 consume.
1187
1188 Returns -1 (with an exception set) on failure. On success, return the
1189 number of chars read. */
1190static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001191_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001192{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001193 Py_ssize_t num_read;
1194
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001195 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001196 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1197 PickleState *st = _Pickle_GetGlobalState();
1198 PyErr_SetString(st->UnpicklingError,
1199 "read would overflow (invalid bytecode)");
1200 return -1;
1201 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001202 if (self->next_read_idx + n <= self->input_len) {
1203 *s = self->input_buffer + self->next_read_idx;
1204 self->next_read_idx += n;
1205 return n;
1206 }
1207 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001208 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001209 return -1;
1210 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001211 num_read = _Unpickler_ReadFromFile(self, n);
1212 if (num_read < 0)
1213 return -1;
1214 if (num_read < n) {
1215 PyErr_Format(PyExc_EOFError, "Ran out of input");
1216 return -1;
1217 }
1218 *s = self->input_buffer;
1219 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001220 return n;
1221}
1222
1223static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001224_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1225 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001226{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001227 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001228 if (input_line == NULL) {
1229 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001230 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001231 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001232
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001233 memcpy(input_line, line, len);
1234 input_line[len] = '\0';
1235 self->input_line = input_line;
1236 *result = self->input_line;
1237 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001238}
1239
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001240/* Read a line from the input stream/buffer. If we run off the end of the input
1241 before hitting \n, return the data we found.
1242
1243 Returns the number of chars read, or -1 on failure. */
1244static Py_ssize_t
1245_Unpickler_Readline(UnpicklerObject *self, char **result)
1246{
1247 Py_ssize_t i, num_read;
1248
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001249 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001250 if (self->input_buffer[i] == '\n') {
1251 char *line_start = self->input_buffer + self->next_read_idx;
1252 num_read = i - self->next_read_idx + 1;
1253 self->next_read_idx = i + 1;
1254 return _Unpickler_CopyLine(self, line_start, num_read, result);
1255 }
1256 }
1257 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001258 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1259 if (num_read < 0)
1260 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001261 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001262 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263 }
Victor Stinner121aab42011-09-29 23:40:53 +02001264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265 /* If we get here, we've run off the end of the input string. Return the
1266 remaining string and let the caller figure it out. */
1267 *result = self->input_buffer + self->next_read_idx;
1268 num_read = i - self->next_read_idx;
1269 self->next_read_idx = i;
1270 return num_read;
1271}
1272
1273/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1274 will be modified in place. */
1275static int
1276_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1277{
1278 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279
1280 assert(new_size > self->memo_size);
1281
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001282 PyMem_RESIZE(self->memo, PyObject *, new_size);
1283 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001284 PyErr_NoMemory();
1285 return -1;
1286 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001287 for (i = self->memo_size; i < new_size; i++)
1288 self->memo[i] = NULL;
1289 self->memo_size = new_size;
1290 return 0;
1291}
1292
1293/* Returns NULL if idx is out of bounds. */
1294static PyObject *
1295_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1296{
1297 if (idx < 0 || idx >= self->memo_size)
1298 return NULL;
1299
1300 return self->memo[idx];
1301}
1302
1303/* Returns -1 (with an exception set) on failure, 0 on success.
1304 This takes its own reference to `value`. */
1305static int
1306_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1307{
1308 PyObject *old_item;
1309
1310 if (idx >= self->memo_size) {
1311 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1312 return -1;
1313 assert(idx < self->memo_size);
1314 }
1315 Py_INCREF(value);
1316 old_item = self->memo[idx];
1317 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001318 if (old_item != NULL) {
1319 Py_DECREF(old_item);
1320 }
1321 else {
1322 self->memo_len++;
1323 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001324 return 0;
1325}
1326
1327static PyObject **
1328_Unpickler_NewMemo(Py_ssize_t new_size)
1329{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001330 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001331 if (memo == NULL) {
1332 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001334 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001335 memset(memo, 0, new_size * sizeof(PyObject *));
1336 return memo;
1337}
1338
1339/* Free the unpickler's memo, taking care to decref any items left in it. */
1340static void
1341_Unpickler_MemoCleanup(UnpicklerObject *self)
1342{
1343 Py_ssize_t i;
1344 PyObject **memo = self->memo;
1345
1346 if (self->memo == NULL)
1347 return;
1348 self->memo = NULL;
1349 i = self->memo_size;
1350 while (--i >= 0) {
1351 Py_XDECREF(memo[i]);
1352 }
1353 PyMem_FREE(memo);
1354}
1355
1356static UnpicklerObject *
1357_Unpickler_New(void)
1358{
1359 UnpicklerObject *self;
1360
1361 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1362 if (self == NULL)
1363 return NULL;
1364
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001365 self->pers_func = NULL;
1366 self->input_buffer = NULL;
1367 self->input_line = NULL;
1368 self->input_len = 0;
1369 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001370 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001371 self->read = NULL;
1372 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001373 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001374 self->encoding = NULL;
1375 self->errors = NULL;
1376 self->marks = NULL;
1377 self->num_marks = 0;
1378 self->marks_size = 0;
1379 self->proto = 0;
1380 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001381 memset(&self->buffer, 0, sizeof(Py_buffer));
1382 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001383 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001384 self->memo = _Unpickler_NewMemo(self->memo_size);
1385 self->stack = (Pdata *)Pdata_New();
1386
1387 if (self->memo == NULL || self->stack == NULL) {
1388 Py_DECREF(self);
1389 return NULL;
1390 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001391
1392 return self;
1393}
1394
1395/* Returns -1 (with an exception set) on failure, 0 on success. This may
1396 be called once on a freshly created Pickler. */
1397static int
1398_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1399{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001400 _Py_IDENTIFIER(peek);
1401 _Py_IDENTIFIER(read);
1402 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001403
1404 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001405 if (self->peek == NULL) {
1406 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1407 PyErr_Clear();
1408 else
1409 return -1;
1410 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001411 self->read = _PyObject_GetAttrId(file, &PyId_read);
1412 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001413 if (self->readline == NULL || self->read == NULL) {
1414 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1415 PyErr_SetString(PyExc_TypeError,
1416 "file must have 'read' and 'readline' attributes");
1417 Py_CLEAR(self->read);
1418 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001419 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001420 return -1;
1421 }
1422 return 0;
1423}
1424
1425/* Returns -1 (with an exception set) on failure, 0 on success. This may
1426 be called once on a freshly created Pickler. */
1427static int
1428_Unpickler_SetInputEncoding(UnpicklerObject *self,
1429 const char *encoding,
1430 const char *errors)
1431{
1432 if (encoding == NULL)
1433 encoding = "ASCII";
1434 if (errors == NULL)
1435 errors = "strict";
1436
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001437 self->encoding = _PyMem_Strdup(encoding);
1438 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001439 if (self->encoding == NULL || self->errors == NULL) {
1440 PyErr_NoMemory();
1441 return -1;
1442 }
1443 return 0;
1444}
1445
1446/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447static int
1448memo_get(PicklerObject *self, PyObject *key)
1449{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001450 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001451 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001452 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001453
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001454 value = PyMemoTable_Get(self->memo, key);
1455 if (value == NULL) {
1456 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001457 return -1;
1458 }
1459
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001460 if (!self->bin) {
1461 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001462 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1463 "%" PY_FORMAT_SIZE_T "d\n", *value);
1464 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465 }
1466 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001467 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001469 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001470 len = 2;
1471 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001472 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001473 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001474 pdata[1] = (unsigned char)(*value & 0xff);
1475 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1476 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1477 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001478 len = 5;
1479 }
1480 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001481 PickleState *st = _Pickle_GetGlobalState();
1482 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 "memo id too large for LONG_BINGET");
1484 return -1;
1485 }
1486 }
1487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001488 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001489 return -1;
1490
1491 return 0;
1492}
1493
1494/* Store an object in the memo, assign it a new unique ID based on the number
1495 of objects currently stored in the memo and generate a PUT opcode. */
1496static int
1497memo_put(PicklerObject *self, PyObject *obj)
1498{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001499 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001500 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001501 Py_ssize_t idx;
1502
1503 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001504
1505 if (self->fast)
1506 return 0;
1507
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001508 idx = PyMemoTable_Size(self->memo);
1509 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1510 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001511
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001512 if (self->proto >= 4) {
1513 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1514 return -1;
1515 return 0;
1516 }
1517 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001518 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001519 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001520 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001521 len = strlen(pdata);
1522 }
1523 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001524 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001526 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001527 len = 2;
1528 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001529 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001530 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001531 pdata[1] = (unsigned char)(idx & 0xff);
1532 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1533 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1534 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001535 len = 5;
1536 }
1537 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001538 PickleState *st = _Pickle_GetGlobalState();
1539 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001540 "memo id too large for LONG_BINPUT");
1541 return -1;
1542 }
1543 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001544 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001545 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001546
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001547 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548}
1549
1550static PyObject *
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001551get_dotted_path(PyObject *obj, PyObject *name)
1552{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001553 _Py_static_string(PyId_dot, ".");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001554 PyObject *dotted_path;
1555 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001556
1557 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001558 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001559 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001560 n = PyList_GET_SIZE(dotted_path);
1561 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001562 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001563 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02001564 if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001565 if (obj == NULL)
1566 PyErr_Format(PyExc_AttributeError,
1567 "Can't pickle local object %R", name);
1568 else
1569 PyErr_Format(PyExc_AttributeError,
1570 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001572 return NULL;
1573 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001574 }
1575 return dotted_path;
1576}
1577
1578static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001579get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001580{
1581 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001582 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001583
1584 assert(PyList_CheckExact(names));
1585 Py_INCREF(obj);
1586 n = PyList_GET_SIZE(names);
1587 for (i = 0; i < n; i++) {
1588 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001589 Py_XDECREF(parent);
1590 parent = obj;
1591 obj = PyObject_GetAttr(parent, name);
1592 if (obj == NULL) {
1593 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001594 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001595 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001596 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001597 if (pparent != NULL)
1598 *pparent = parent;
1599 else
1600 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001601 return obj;
1602}
1603
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001604static void
1605reformat_attribute_error(PyObject *obj, PyObject *name)
1606{
1607 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1608 PyErr_Clear();
1609 PyErr_Format(PyExc_AttributeError,
1610 "Can't get attribute %R on %R", name, obj);
1611 }
1612}
1613
1614
1615static PyObject *
1616getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1617{
1618 PyObject *dotted_path, *attr;
1619
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001620 if (allow_qualname) {
1621 dotted_path = get_dotted_path(obj, name);
1622 if (dotted_path == NULL)
1623 return NULL;
1624 attr = get_deep_attribute(obj, dotted_path, NULL);
1625 Py_DECREF(dotted_path);
1626 }
1627 else
1628 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001629 if (attr == NULL)
1630 reformat_attribute_error(obj, name);
1631 return attr;
1632}
1633
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001634static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001635whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001636{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001637 PyObject *module_name;
1638 PyObject *modules_dict;
1639 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001640 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001641 _Py_IDENTIFIER(__module__);
1642 _Py_IDENTIFIER(modules);
1643 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001644
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001645 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1646
1647 if (module_name == NULL) {
1648 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001650 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001651 }
1652 else {
1653 /* In some rare cases (e.g., bound methods of extension types),
1654 __module__ can be None. If it is so, then search sys.modules for
1655 the module of global. */
1656 if (module_name != Py_None)
1657 return module_name;
1658 Py_CLEAR(module_name);
1659 }
1660 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001661
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001662 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001663 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001664 if (modules_dict == NULL) {
1665 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001666 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001667 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001668
1669 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001670 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1671 PyObject *candidate;
1672 if (PyUnicode_Check(module_name) &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001673 _PyUnicode_EqualToASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001674 continue;
1675 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001676 continue;
1677
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001678 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001679 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001680 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001681 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001682 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001683 continue;
1684 }
1685
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001686 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001687 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001688 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001689 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001690 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001692 }
1693
1694 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001695 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001696 Py_INCREF(module_name);
1697 return module_name;
1698}
1699
1700/* fast_save_enter() and fast_save_leave() are guards against recursive
1701 objects when Pickler is used with the "fast mode" (i.e., with object
1702 memoization disabled). If the nesting of a list or dict object exceed
1703 FAST_NESTING_LIMIT, these guards will start keeping an internal
1704 reference to the seen list or dict objects and check whether these objects
1705 are recursive. These are not strictly necessary, since save() has a
1706 hard-coded recursion limit, but they give a nicer error message than the
1707 typical RuntimeError. */
1708static int
1709fast_save_enter(PicklerObject *self, PyObject *obj)
1710{
1711 /* if fast_nesting < 0, we're doing an error exit. */
1712 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1713 PyObject *key = NULL;
1714 if (self->fast_memo == NULL) {
1715 self->fast_memo = PyDict_New();
1716 if (self->fast_memo == NULL) {
1717 self->fast_nesting = -1;
1718 return 0;
1719 }
1720 }
1721 key = PyLong_FromVoidPtr(obj);
1722 if (key == NULL)
1723 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001724 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001725 Py_DECREF(key);
1726 PyErr_Format(PyExc_ValueError,
1727 "fast mode: can't pickle cyclic objects "
1728 "including object type %.200s at %p",
1729 obj->ob_type->tp_name, obj);
1730 self->fast_nesting = -1;
1731 return 0;
1732 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001733 if (PyErr_Occurred()) {
1734 return 0;
1735 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001736 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1737 Py_DECREF(key);
1738 self->fast_nesting = -1;
1739 return 0;
1740 }
1741 Py_DECREF(key);
1742 }
1743 return 1;
1744}
1745
1746static int
1747fast_save_leave(PicklerObject *self, PyObject *obj)
1748{
1749 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1750 PyObject *key = PyLong_FromVoidPtr(obj);
1751 if (key == NULL)
1752 return 0;
1753 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1754 Py_DECREF(key);
1755 return 0;
1756 }
1757 Py_DECREF(key);
1758 }
1759 return 1;
1760}
1761
1762static int
1763save_none(PicklerObject *self, PyObject *obj)
1764{
1765 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001766 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001767 return -1;
1768
1769 return 0;
1770}
1771
1772static int
1773save_bool(PicklerObject *self, PyObject *obj)
1774{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001775 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001776 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001777 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 return -1;
1779 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001780 else {
1781 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1782 * so that unpicklers written before bools were introduced unpickle them
1783 * as ints, but unpicklers after can recognize that bools were intended.
1784 * Note that protocol 2 added direct ways to pickle bools.
1785 */
1786 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1787 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1788 return -1;
1789 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001790 return 0;
1791}
1792
1793static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001794save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001796 PyObject *repr = NULL;
1797 Py_ssize_t size;
1798 long val;
1799 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001800
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001801 const char long_op = LONG;
1802
1803 val= PyLong_AsLong(obj);
1804 if (val == -1 && PyErr_Occurred()) {
1805 /* out of range for int pickling */
1806 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001807 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001808 else if (self->bin &&
1809 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001810 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001811 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001812
1813 Note: we can't use -0x80000000L in the above condition because some
1814 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1815 before applying the unary minus when sizeof(long) <= 4. The
1816 resulting value stays unsigned which is commonly not what we want,
1817 so MSVC happily warns us about it. However, that result would have
1818 been fine because we guard for sizeof(long) <= 4 which turns the
1819 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001820 char pdata[32];
1821 Py_ssize_t len = 0;
1822
1823 pdata[1] = (unsigned char)(val & 0xff);
1824 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1825 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1826 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001827
1828 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1829 if (pdata[2] == 0) {
1830 pdata[0] = BININT1;
1831 len = 2;
1832 }
1833 else {
1834 pdata[0] = BININT2;
1835 len = 3;
1836 }
1837 }
1838 else {
1839 pdata[0] = BININT;
1840 len = 5;
1841 }
1842
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001843 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001844 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001845
1846 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 }
1848
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001849 if (self->proto >= 2) {
1850 /* Linear-time pickling. */
1851 size_t nbits;
1852 size_t nbytes;
1853 unsigned char *pdata;
1854 char header[5];
1855 int i;
1856 int sign = _PyLong_Sign(obj);
1857
1858 if (sign == 0) {
1859 header[0] = LONG1;
1860 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001861 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001862 goto error;
1863 return 0;
1864 }
1865 nbits = _PyLong_NumBits(obj);
1866 if (nbits == (size_t)-1 && PyErr_Occurred())
1867 goto error;
1868 /* How many bytes do we need? There are nbits >> 3 full
1869 * bytes of data, and nbits & 7 leftover bits. If there
1870 * are any leftover bits, then we clearly need another
1871 * byte. Wnat's not so obvious is that we *probably*
1872 * need another byte even if there aren't any leftovers:
1873 * the most-significant bit of the most-significant byte
1874 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001875 * opposite of the one we need. The exception is ints
1876 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001877 * its own 256's-complement, so has the right sign bit
1878 * even without the extra byte. That's a pain to check
1879 * for in advance, though, so we always grab an extra
1880 * byte at the start, and cut it back later if possible.
1881 */
1882 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001883 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001884 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001885 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001886 goto error;
1887 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001888 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 if (repr == NULL)
1890 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001891 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001892 i = _PyLong_AsByteArray((PyLongObject *)obj,
1893 pdata, nbytes,
1894 1 /* little endian */ , 1 /* signed */ );
1895 if (i < 0)
1896 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001897 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001898 * needed. This is so iff the MSB is all redundant sign
1899 * bits.
1900 */
1901 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001902 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 pdata[nbytes - 1] == 0xff &&
1904 (pdata[nbytes - 2] & 0x80) != 0) {
1905 nbytes--;
1906 }
1907
1908 if (nbytes < 256) {
1909 header[0] = LONG1;
1910 header[1] = (unsigned char)nbytes;
1911 size = 2;
1912 }
1913 else {
1914 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001915 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001916 for (i = 1; i < 5; i++) {
1917 header[i] = (unsigned char)(size & 0xff);
1918 size >>= 8;
1919 }
1920 size = 5;
1921 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001922 if (_Pickler_Write(self, header, size) < 0 ||
1923 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001924 goto error;
1925 }
1926 else {
1927 char *string;
1928
Mark Dickinson8dd05142009-01-20 20:43:58 +00001929 /* proto < 2: write the repr and newline. This is quadratic-time (in
1930 the number of digits), in both directions. We add a trailing 'L'
1931 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001932
1933 repr = PyObject_Repr(obj);
1934 if (repr == NULL)
1935 goto error;
1936
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001937 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001938 if (string == NULL)
1939 goto error;
1940
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001941 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1942 _Pickler_Write(self, string, size) < 0 ||
1943 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 goto error;
1945 }
1946
1947 if (0) {
1948 error:
1949 status = -1;
1950 }
1951 Py_XDECREF(repr);
1952
1953 return status;
1954}
1955
1956static int
1957save_float(PicklerObject *self, PyObject *obj)
1958{
1959 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1960
1961 if (self->bin) {
1962 char pdata[9];
1963 pdata[0] = BINFLOAT;
1964 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1965 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001966 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001967 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001968 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001969 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001970 int result = -1;
1971 char *buf = NULL;
1972 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001973
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001974 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001975 goto done;
1976
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001977 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001978 if (!buf) {
1979 PyErr_NoMemory();
1980 goto done;
1981 }
1982
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001983 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001984 goto done;
1985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001986 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001987 goto done;
1988
1989 result = 0;
1990done:
1991 PyMem_Free(buf);
1992 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001993 }
1994
1995 return 0;
1996}
1997
1998static int
1999save_bytes(PicklerObject *self, PyObject *obj)
2000{
2001 if (self->proto < 3) {
2002 /* Older pickle protocols do not have an opcode for pickling bytes
2003 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002004 the __reduce__ method) to permit bytes object unpickling.
2005
2006 Here we use a hack to be compatible with Python 2. Since in Python
2007 2 'bytes' is just an alias for 'str' (which has different
2008 parameters than the actual bytes object), we use codecs.encode
2009 to create the appropriate 'str' object when unpickled using
2010 Python 2 *and* the appropriate 'bytes' object when unpickled
2011 using Python 3. Again this is a hack and we don't need to do this
2012 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002014 int status;
2015
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002016 if (PyBytes_GET_SIZE(obj) == 0) {
2017 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2018 }
2019 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002020 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002021 PyObject *unicode_str =
2022 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2023 PyBytes_GET_SIZE(obj),
2024 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002025 _Py_IDENTIFIER(latin1);
2026
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002027 if (unicode_str == NULL)
2028 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002029 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002030 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002031 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002032 Py_DECREF(unicode_str);
2033 }
2034
2035 if (reduce_value == NULL)
2036 return -1;
2037
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002038 /* save_reduce() will memoize the object automatically. */
2039 status = save_reduce(self, reduce_value, obj);
2040 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 return status;
2042 }
2043 else {
2044 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002045 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002046 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002047
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002048 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002049 if (size < 0)
2050 return -1;
2051
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002052 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002053 header[0] = SHORT_BINBYTES;
2054 header[1] = (unsigned char)size;
2055 len = 2;
2056 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002057 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058 header[0] = BINBYTES;
2059 header[1] = (unsigned char)(size & 0xff);
2060 header[2] = (unsigned char)((size >> 8) & 0xff);
2061 header[3] = (unsigned char)((size >> 16) & 0xff);
2062 header[4] = (unsigned char)((size >> 24) & 0xff);
2063 len = 5;
2064 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002065 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002066 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002067 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002068 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002069 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002070 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002071 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002072 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 return -1; /* string too large */
2074 }
2075
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002076 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 return -1;
2078
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002079 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002080 return -1;
2081
2082 if (memo_put(self, obj) < 0)
2083 return -1;
2084
2085 return 0;
2086 }
2087}
2088
2089/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2090 backslash and newline characters to \uXXXX escapes. */
2091static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002092raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002093{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002094 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002095 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002096 Py_ssize_t i, size;
2097 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002098 void *data;
2099 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002101 if (PyUnicode_READY(obj))
2102 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002104 size = PyUnicode_GET_LENGTH(obj);
2105 data = PyUnicode_DATA(obj);
2106 kind = PyUnicode_KIND(obj);
2107 if (kind == PyUnicode_4BYTE_KIND)
2108 expandsize = 10;
2109 else
2110 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002111
Victor Stinner049e5092014-08-17 22:20:00 +02002112 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002113 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002114 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002115 if (repr == NULL)
2116 return NULL;
2117 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002118 return repr;
2119 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002120
Victor Stinner7270b7f2014-08-17 21:14:46 +02002121 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002122 for (i=0; i < size; i++) {
2123 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002124 /* Map 32-bit characters to '\Uxxxxxxxx' */
2125 if (ch >= 0x10000) {
2126 *p++ = '\\';
2127 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002128 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2129 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2130 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2131 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2132 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2133 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2134 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2135 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002136 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002137 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002138 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002139 *p++ = '\\';
2140 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002141 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2142 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2143 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2144 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002145 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002146 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002147 else
2148 *p++ = (char) ch;
2149 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002150 size = p - PyBytes_AS_STRING(repr);
2151 if (_PyBytes_Resize(&repr, size) < 0)
2152 return NULL;
2153 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002154}
2155
2156static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002157write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2158{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002159 char header[9];
2160 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002161
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002162 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002163 if (size <= 0xff && self->proto >= 4) {
2164 header[0] = SHORT_BINUNICODE;
2165 header[1] = (unsigned char)(size & 0xff);
2166 len = 2;
2167 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002168 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002169 header[0] = BINUNICODE;
2170 header[1] = (unsigned char)(size & 0xff);
2171 header[2] = (unsigned char)((size >> 8) & 0xff);
2172 header[3] = (unsigned char)((size >> 16) & 0xff);
2173 header[4] = (unsigned char)((size >> 24) & 0xff);
2174 len = 5;
2175 }
2176 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002177 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002178 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002179 len = 9;
2180 }
2181 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002182 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002183 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002184 return -1;
2185 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002186
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002187 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002188 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002189 if (_Pickler_Write(self, data, size) < 0)
2190 return -1;
2191
2192 return 0;
2193}
2194
2195static int
2196write_unicode_binary(PicklerObject *self, PyObject *obj)
2197{
2198 PyObject *encoded = NULL;
2199 Py_ssize_t size;
2200 char *data;
2201 int r;
2202
2203 if (PyUnicode_READY(obj))
2204 return -1;
2205
2206 data = PyUnicode_AsUTF8AndSize(obj, &size);
2207 if (data != NULL)
2208 return write_utf8(self, data, size);
2209
2210 /* Issue #8383: for strings with lone surrogates, fallback on the
2211 "surrogatepass" error handler. */
2212 PyErr_Clear();
2213 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2214 if (encoded == NULL)
2215 return -1;
2216
2217 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2218 PyBytes_GET_SIZE(encoded));
2219 Py_DECREF(encoded);
2220 return r;
2221}
2222
2223static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002224save_unicode(PicklerObject *self, PyObject *obj)
2225{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002226 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002227 if (write_unicode_binary(self, obj) < 0)
2228 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002229 }
2230 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002231 PyObject *encoded;
2232 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002233 const char unicode_op = UNICODE;
2234
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002235 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002236 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002237 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002238
Antoine Pitrou299978d2013-04-07 17:38:11 +02002239 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2240 Py_DECREF(encoded);
2241 return -1;
2242 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002243
2244 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002245 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2246 Py_DECREF(encoded);
2247 return -1;
2248 }
2249 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002251 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002252 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253 }
2254 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002258}
2259
2260/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2261static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002262store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002263{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002264 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265
2266 assert(PyTuple_Size(t) == len);
2267
2268 for (i = 0; i < len; i++) {
2269 PyObject *element = PyTuple_GET_ITEM(t, i);
2270
2271 if (element == NULL)
2272 return -1;
2273 if (save(self, element, 0) < 0)
2274 return -1;
2275 }
2276
2277 return 0;
2278}
2279
2280/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2281 * used across protocols to minimize the space needed to pickle them.
2282 * Tuples are also the only builtin immutable type that can be recursive
2283 * (a tuple can be reached from itself), and that requires some subtle
2284 * magic so that it works in all cases. IOW, this is a long routine.
2285 */
2286static int
2287save_tuple(PicklerObject *self, PyObject *obj)
2288{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002289 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002290
2291 const char mark_op = MARK;
2292 const char tuple_op = TUPLE;
2293 const char pop_op = POP;
2294 const char pop_mark_op = POP_MARK;
2295 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2296
2297 if ((len = PyTuple_Size(obj)) < 0)
2298 return -1;
2299
2300 if (len == 0) {
2301 char pdata[2];
2302
2303 if (self->proto) {
2304 pdata[0] = EMPTY_TUPLE;
2305 len = 1;
2306 }
2307 else {
2308 pdata[0] = MARK;
2309 pdata[1] = TUPLE;
2310 len = 2;
2311 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002312 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002313 return -1;
2314 return 0;
2315 }
2316
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002317 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002318 * saving the tuple elements, the tuple must be recursive, in
2319 * which case we'll pop everything we put on the stack, and fetch
2320 * its value from the memo.
2321 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322 if (len <= 3 && self->proto >= 2) {
2323 /* Use TUPLE{1,2,3} opcodes. */
2324 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002325 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002326
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002327 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002328 /* pop the len elements */
2329 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (_Pickler_Write(self, &pop_op, 1) < 0)
2331 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002332 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002333 if (memo_get(self, obj) < 0)
2334 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 return 0;
2337 }
2338 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002339 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2340 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 }
2342 goto memoize;
2343 }
2344
2345 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2346 * Generate MARK e1 e2 ... TUPLE
2347 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 if (_Pickler_Write(self, &mark_op, 1) < 0)
2349 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350
2351 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002352 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002354 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 /* pop the stack stuff we pushed */
2356 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002357 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2358 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359 }
2360 else {
2361 /* Note that we pop one more than len, to remove
2362 * the MARK too.
2363 */
2364 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002365 if (_Pickler_Write(self, &pop_op, 1) < 0)
2366 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002367 }
2368 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002369 if (memo_get(self, obj) < 0)
2370 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002372 return 0;
2373 }
2374 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002375 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2376 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002377 }
2378
2379 memoize:
2380 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002381 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002382
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002383 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002384}
2385
2386/* iter is an iterator giving items, and we batch up chunks of
2387 * MARK item item ... item APPENDS
2388 * opcode sequences. Calling code should have arranged to first create an
2389 * empty list, or list-like object, for the APPENDS to operate on.
2390 * Returns 0 on success, <0 on error.
2391 */
2392static int
2393batch_list(PicklerObject *self, PyObject *iter)
2394{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002395 PyObject *obj = NULL;
2396 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002397 int i, n;
2398
2399 const char mark_op = MARK;
2400 const char append_op = APPEND;
2401 const char appends_op = APPENDS;
2402
2403 assert(iter != NULL);
2404
2405 /* XXX: I think this function could be made faster by avoiding the
2406 iterator interface and fetching objects directly from list using
2407 PyList_GET_ITEM.
2408 */
2409
2410 if (self->proto == 0) {
2411 /* APPENDS isn't available; do one at a time. */
2412 for (;;) {
2413 obj = PyIter_Next(iter);
2414 if (obj == NULL) {
2415 if (PyErr_Occurred())
2416 return -1;
2417 break;
2418 }
2419 i = save(self, obj, 0);
2420 Py_DECREF(obj);
2421 if (i < 0)
2422 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002423 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002424 return -1;
2425 }
2426 return 0;
2427 }
2428
2429 /* proto > 0: write in batches of BATCHSIZE. */
2430 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002431 /* Get first item */
2432 firstitem = PyIter_Next(iter);
2433 if (firstitem == NULL) {
2434 if (PyErr_Occurred())
2435 goto error;
2436
2437 /* nothing more to add */
2438 break;
2439 }
2440
2441 /* Try to get a second item */
2442 obj = PyIter_Next(iter);
2443 if (obj == NULL) {
2444 if (PyErr_Occurred())
2445 goto error;
2446
2447 /* Only one item to write */
2448 if (save(self, firstitem, 0) < 0)
2449 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002450 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002451 goto error;
2452 Py_CLEAR(firstitem);
2453 break;
2454 }
2455
2456 /* More than one item to write */
2457
2458 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002459 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002460 goto error;
2461
2462 if (save(self, firstitem, 0) < 0)
2463 goto error;
2464 Py_CLEAR(firstitem);
2465 n = 1;
2466
2467 /* Fetch and save up to BATCHSIZE items */
2468 while (obj) {
2469 if (save(self, obj, 0) < 0)
2470 goto error;
2471 Py_CLEAR(obj);
2472 n += 1;
2473
2474 if (n == BATCHSIZE)
2475 break;
2476
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002477 obj = PyIter_Next(iter);
2478 if (obj == NULL) {
2479 if (PyErr_Occurred())
2480 goto error;
2481 break;
2482 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002483 }
2484
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002485 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002486 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002487
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002488 } while (n == BATCHSIZE);
2489 return 0;
2490
2491 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002492 Py_XDECREF(firstitem);
2493 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002494 return -1;
2495}
2496
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002497/* This is a variant of batch_list() above, specialized for lists (with no
2498 * support for list subclasses). Like batch_list(), we batch up chunks of
2499 * MARK item item ... item APPENDS
2500 * opcode sequences. Calling code should have arranged to first create an
2501 * empty list, or list-like object, for the APPENDS to operate on.
2502 * Returns 0 on success, -1 on error.
2503 *
2504 * This version is considerably faster than batch_list(), if less general.
2505 *
2506 * Note that this only works for protocols > 0.
2507 */
2508static int
2509batch_list_exact(PicklerObject *self, PyObject *obj)
2510{
2511 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002512 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002513
2514 const char append_op = APPEND;
2515 const char appends_op = APPENDS;
2516 const char mark_op = MARK;
2517
2518 assert(obj != NULL);
2519 assert(self->proto > 0);
2520 assert(PyList_CheckExact(obj));
2521
2522 if (PyList_GET_SIZE(obj) == 1) {
2523 item = PyList_GET_ITEM(obj, 0);
2524 if (save(self, item, 0) < 0)
2525 return -1;
2526 if (_Pickler_Write(self, &append_op, 1) < 0)
2527 return -1;
2528 return 0;
2529 }
2530
2531 /* Write in batches of BATCHSIZE. */
2532 total = 0;
2533 do {
2534 this_batch = 0;
2535 if (_Pickler_Write(self, &mark_op, 1) < 0)
2536 return -1;
2537 while (total < PyList_GET_SIZE(obj)) {
2538 item = PyList_GET_ITEM(obj, total);
2539 if (save(self, item, 0) < 0)
2540 return -1;
2541 total++;
2542 if (++this_batch == BATCHSIZE)
2543 break;
2544 }
2545 if (_Pickler_Write(self, &appends_op, 1) < 0)
2546 return -1;
2547
2548 } while (total < PyList_GET_SIZE(obj));
2549
2550 return 0;
2551}
2552
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002553static int
2554save_list(PicklerObject *self, PyObject *obj)
2555{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002557 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002558 int status = 0;
2559
2560 if (self->fast && !fast_save_enter(self, obj))
2561 goto error;
2562
2563 /* Create an empty list. */
2564 if (self->bin) {
2565 header[0] = EMPTY_LIST;
2566 len = 1;
2567 }
2568 else {
2569 header[0] = MARK;
2570 header[1] = LIST;
2571 len = 2;
2572 }
2573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002574 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002575 goto error;
2576
2577 /* Get list length, and bow out early if empty. */
2578 if ((len = PyList_Size(obj)) < 0)
2579 goto error;
2580
2581 if (memo_put(self, obj) < 0)
2582 goto error;
2583
2584 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002585 /* Materialize the list elements. */
2586 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002587 if (Py_EnterRecursiveCall(" while pickling an object"))
2588 goto error;
2589 status = batch_list_exact(self, obj);
2590 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002591 } else {
2592 PyObject *iter = PyObject_GetIter(obj);
2593 if (iter == NULL)
2594 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002595
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002596 if (Py_EnterRecursiveCall(" while pickling an object")) {
2597 Py_DECREF(iter);
2598 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002599 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002600 status = batch_list(self, iter);
2601 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002602 Py_DECREF(iter);
2603 }
2604 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002605 if (0) {
2606 error:
2607 status = -1;
2608 }
2609
2610 if (self->fast && !fast_save_leave(self, obj))
2611 status = -1;
2612
2613 return status;
2614}
2615
2616/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2617 * MARK key value ... key value SETITEMS
2618 * opcode sequences. Calling code should have arranged to first create an
2619 * empty dict, or dict-like object, for the SETITEMS to operate on.
2620 * Returns 0 on success, <0 on error.
2621 *
2622 * This is very much like batch_list(). The difference between saving
2623 * elements directly, and picking apart two-tuples, is so long-winded at
2624 * the C level, though, that attempts to combine these routines were too
2625 * ugly to bear.
2626 */
2627static int
2628batch_dict(PicklerObject *self, PyObject *iter)
2629{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002630 PyObject *obj = NULL;
2631 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002632 int i, n;
2633
2634 const char mark_op = MARK;
2635 const char setitem_op = SETITEM;
2636 const char setitems_op = SETITEMS;
2637
2638 assert(iter != NULL);
2639
2640 if (self->proto == 0) {
2641 /* SETITEMS isn't available; do one at a time. */
2642 for (;;) {
2643 obj = PyIter_Next(iter);
2644 if (obj == NULL) {
2645 if (PyErr_Occurred())
2646 return -1;
2647 break;
2648 }
2649 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2650 PyErr_SetString(PyExc_TypeError, "dict items "
2651 "iterator must return 2-tuples");
2652 return -1;
2653 }
2654 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2655 if (i >= 0)
2656 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2657 Py_DECREF(obj);
2658 if (i < 0)
2659 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002660 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002661 return -1;
2662 }
2663 return 0;
2664 }
2665
2666 /* proto > 0: write in batches of BATCHSIZE. */
2667 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002668 /* Get first item */
2669 firstitem = PyIter_Next(iter);
2670 if (firstitem == NULL) {
2671 if (PyErr_Occurred())
2672 goto error;
2673
2674 /* nothing more to add */
2675 break;
2676 }
2677 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2678 PyErr_SetString(PyExc_TypeError, "dict items "
2679 "iterator must return 2-tuples");
2680 goto error;
2681 }
2682
2683 /* Try to get a second item */
2684 obj = PyIter_Next(iter);
2685 if (obj == NULL) {
2686 if (PyErr_Occurred())
2687 goto error;
2688
2689 /* Only one item to write */
2690 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2691 goto error;
2692 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2693 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002694 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002695 goto error;
2696 Py_CLEAR(firstitem);
2697 break;
2698 }
2699
2700 /* More than one item to write */
2701
2702 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002703 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002704 goto error;
2705
2706 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2707 goto error;
2708 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2709 goto error;
2710 Py_CLEAR(firstitem);
2711 n = 1;
2712
2713 /* Fetch and save up to BATCHSIZE items */
2714 while (obj) {
2715 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2716 PyErr_SetString(PyExc_TypeError, "dict items "
2717 "iterator must return 2-tuples");
2718 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002719 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002720 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2721 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2722 goto error;
2723 Py_CLEAR(obj);
2724 n += 1;
2725
2726 if (n == BATCHSIZE)
2727 break;
2728
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002729 obj = PyIter_Next(iter);
2730 if (obj == NULL) {
2731 if (PyErr_Occurred())
2732 goto error;
2733 break;
2734 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002735 }
2736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002737 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002738 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002739
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002740 } while (n == BATCHSIZE);
2741 return 0;
2742
2743 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002744 Py_XDECREF(firstitem);
2745 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002746 return -1;
2747}
2748
Collin Winter5c9b02d2009-05-25 05:43:30 +00002749/* This is a variant of batch_dict() above that specializes for dicts, with no
2750 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2751 * MARK key value ... key value SETITEMS
2752 * opcode sequences. Calling code should have arranged to first create an
2753 * empty dict, or dict-like object, for the SETITEMS to operate on.
2754 * Returns 0 on success, -1 on error.
2755 *
2756 * Note that this currently doesn't work for protocol 0.
2757 */
2758static int
2759batch_dict_exact(PicklerObject *self, PyObject *obj)
2760{
2761 PyObject *key = NULL, *value = NULL;
2762 int i;
2763 Py_ssize_t dict_size, ppos = 0;
2764
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002765 const char mark_op = MARK;
2766 const char setitem_op = SETITEM;
2767 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002768
2769 assert(obj != NULL);
2770 assert(self->proto > 0);
2771
2772 dict_size = PyDict_Size(obj);
2773
2774 /* Special-case len(d) == 1 to save space. */
2775 if (dict_size == 1) {
2776 PyDict_Next(obj, &ppos, &key, &value);
2777 if (save(self, key, 0) < 0)
2778 return -1;
2779 if (save(self, value, 0) < 0)
2780 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002781 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002782 return -1;
2783 return 0;
2784 }
2785
2786 /* Write in batches of BATCHSIZE. */
2787 do {
2788 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002789 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002790 return -1;
2791 while (PyDict_Next(obj, &ppos, &key, &value)) {
2792 if (save(self, key, 0) < 0)
2793 return -1;
2794 if (save(self, value, 0) < 0)
2795 return -1;
2796 if (++i == BATCHSIZE)
2797 break;
2798 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002799 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002800 return -1;
2801 if (PyDict_Size(obj) != dict_size) {
2802 PyErr_Format(
2803 PyExc_RuntimeError,
2804 "dictionary changed size during iteration");
2805 return -1;
2806 }
2807
2808 } while (i == BATCHSIZE);
2809 return 0;
2810}
2811
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002812static int
2813save_dict(PicklerObject *self, PyObject *obj)
2814{
2815 PyObject *items, *iter;
2816 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002817 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002818 int status = 0;
2819
2820 if (self->fast && !fast_save_enter(self, obj))
2821 goto error;
2822
2823 /* Create an empty dict. */
2824 if (self->bin) {
2825 header[0] = EMPTY_DICT;
2826 len = 1;
2827 }
2828 else {
2829 header[0] = MARK;
2830 header[1] = DICT;
2831 len = 2;
2832 }
2833
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002834 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002835 goto error;
2836
2837 /* Get dict size, and bow out early if empty. */
2838 if ((len = PyDict_Size(obj)) < 0)
2839 goto error;
2840
2841 if (memo_put(self, obj) < 0)
2842 goto error;
2843
2844 if (len != 0) {
2845 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002846 if (PyDict_CheckExact(obj) && self->proto > 0) {
2847 /* We can take certain shortcuts if we know this is a dict and
2848 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002849 if (Py_EnterRecursiveCall(" while pickling an object"))
2850 goto error;
2851 status = batch_dict_exact(self, obj);
2852 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002853 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002854 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002855
2856 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002857 if (items == NULL)
2858 goto error;
2859 iter = PyObject_GetIter(items);
2860 Py_DECREF(items);
2861 if (iter == NULL)
2862 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002863 if (Py_EnterRecursiveCall(" while pickling an object")) {
2864 Py_DECREF(iter);
2865 goto error;
2866 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002867 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002868 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002869 Py_DECREF(iter);
2870 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002871 }
2872
2873 if (0) {
2874 error:
2875 status = -1;
2876 }
2877
2878 if (self->fast && !fast_save_leave(self, obj))
2879 status = -1;
2880
2881 return status;
2882}
2883
2884static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002885save_set(PicklerObject *self, PyObject *obj)
2886{
2887 PyObject *item;
2888 int i;
2889 Py_ssize_t set_size, ppos = 0;
2890 Py_hash_t hash;
2891
2892 const char empty_set_op = EMPTY_SET;
2893 const char mark_op = MARK;
2894 const char additems_op = ADDITEMS;
2895
2896 if (self->proto < 4) {
2897 PyObject *items;
2898 PyObject *reduce_value;
2899 int status;
2900
2901 items = PySequence_List(obj);
2902 if (items == NULL) {
2903 return -1;
2904 }
2905 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2906 Py_DECREF(items);
2907 if (reduce_value == NULL) {
2908 return -1;
2909 }
2910 /* save_reduce() will memoize the object automatically. */
2911 status = save_reduce(self, reduce_value, obj);
2912 Py_DECREF(reduce_value);
2913 return status;
2914 }
2915
2916 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2917 return -1;
2918
2919 if (memo_put(self, obj) < 0)
2920 return -1;
2921
2922 set_size = PySet_GET_SIZE(obj);
2923 if (set_size == 0)
2924 return 0; /* nothing to do */
2925
2926 /* Write in batches of BATCHSIZE. */
2927 do {
2928 i = 0;
2929 if (_Pickler_Write(self, &mark_op, 1) < 0)
2930 return -1;
2931 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2932 if (save(self, item, 0) < 0)
2933 return -1;
2934 if (++i == BATCHSIZE)
2935 break;
2936 }
2937 if (_Pickler_Write(self, &additems_op, 1) < 0)
2938 return -1;
2939 if (PySet_GET_SIZE(obj) != set_size) {
2940 PyErr_Format(
2941 PyExc_RuntimeError,
2942 "set changed size during iteration");
2943 return -1;
2944 }
2945 } while (i == BATCHSIZE);
2946
2947 return 0;
2948}
2949
2950static int
2951save_frozenset(PicklerObject *self, PyObject *obj)
2952{
2953 PyObject *iter;
2954
2955 const char mark_op = MARK;
2956 const char frozenset_op = FROZENSET;
2957
2958 if (self->fast && !fast_save_enter(self, obj))
2959 return -1;
2960
2961 if (self->proto < 4) {
2962 PyObject *items;
2963 PyObject *reduce_value;
2964 int status;
2965
2966 items = PySequence_List(obj);
2967 if (items == NULL) {
2968 return -1;
2969 }
2970 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2971 items);
2972 Py_DECREF(items);
2973 if (reduce_value == NULL) {
2974 return -1;
2975 }
2976 /* save_reduce() will memoize the object automatically. */
2977 status = save_reduce(self, reduce_value, obj);
2978 Py_DECREF(reduce_value);
2979 return status;
2980 }
2981
2982 if (_Pickler_Write(self, &mark_op, 1) < 0)
2983 return -1;
2984
2985 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002986 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002987 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002988 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002989 for (;;) {
2990 PyObject *item;
2991
2992 item = PyIter_Next(iter);
2993 if (item == NULL) {
2994 if (PyErr_Occurred()) {
2995 Py_DECREF(iter);
2996 return -1;
2997 }
2998 break;
2999 }
3000 if (save(self, item, 0) < 0) {
3001 Py_DECREF(item);
3002 Py_DECREF(iter);
3003 return -1;
3004 }
3005 Py_DECREF(item);
3006 }
3007 Py_DECREF(iter);
3008
3009 /* If the object is already in the memo, this means it is
3010 recursive. In this case, throw away everything we put on the
3011 stack, and fetch the object back from the memo. */
3012 if (PyMemoTable_Get(self->memo, obj)) {
3013 const char pop_mark_op = POP_MARK;
3014
3015 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3016 return -1;
3017 if (memo_get(self, obj) < 0)
3018 return -1;
3019 return 0;
3020 }
3021
3022 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3023 return -1;
3024 if (memo_put(self, obj) < 0)
3025 return -1;
3026
3027 return 0;
3028}
3029
3030static int
3031fix_imports(PyObject **module_name, PyObject **global_name)
3032{
3033 PyObject *key;
3034 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003035 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003036
3037 key = PyTuple_Pack(2, *module_name, *global_name);
3038 if (key == NULL)
3039 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003040 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003041 Py_DECREF(key);
3042 if (item) {
3043 PyObject *fixed_module_name;
3044 PyObject *fixed_global_name;
3045
3046 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3047 PyErr_Format(PyExc_RuntimeError,
3048 "_compat_pickle.REVERSE_NAME_MAPPING values "
3049 "should be 2-tuples, not %.200s",
3050 Py_TYPE(item)->tp_name);
3051 return -1;
3052 }
3053 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3054 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3055 if (!PyUnicode_Check(fixed_module_name) ||
3056 !PyUnicode_Check(fixed_global_name)) {
3057 PyErr_Format(PyExc_RuntimeError,
3058 "_compat_pickle.REVERSE_NAME_MAPPING values "
3059 "should be pairs of str, not (%.200s, %.200s)",
3060 Py_TYPE(fixed_module_name)->tp_name,
3061 Py_TYPE(fixed_global_name)->tp_name);
3062 return -1;
3063 }
3064
3065 Py_CLEAR(*module_name);
3066 Py_CLEAR(*global_name);
3067 Py_INCREF(fixed_module_name);
3068 Py_INCREF(fixed_global_name);
3069 *module_name = fixed_module_name;
3070 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003071 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003072 }
3073 else if (PyErr_Occurred()) {
3074 return -1;
3075 }
3076
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003077 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003078 if (item) {
3079 if (!PyUnicode_Check(item)) {
3080 PyErr_Format(PyExc_RuntimeError,
3081 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3082 "should be strings, not %.200s",
3083 Py_TYPE(item)->tp_name);
3084 return -1;
3085 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003086 Py_INCREF(item);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003087 Py_XSETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003088 }
3089 else if (PyErr_Occurred()) {
3090 return -1;
3091 }
3092
3093 return 0;
3094}
3095
3096static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003097save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3098{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003099 PyObject *global_name = NULL;
3100 PyObject *module_name = NULL;
3101 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003102 PyObject *parent = NULL;
3103 PyObject *dotted_path = NULL;
3104 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003105 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003106 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003107 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003108 _Py_IDENTIFIER(__name__);
3109 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003110
3111 const char global_op = GLOBAL;
3112
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003113 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003114 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003115 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003116 }
3117 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003118 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3119 if (global_name == NULL) {
3120 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3121 goto error;
3122 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003123 }
3124 if (global_name == NULL) {
3125 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3126 if (global_name == NULL)
3127 goto error;
3128 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129 }
3130
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003131 dotted_path = get_dotted_path(module, global_name);
3132 if (dotted_path == NULL)
3133 goto error;
3134 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135 if (module_name == NULL)
3136 goto error;
3137
3138 /* XXX: Change to use the import C API directly with level=0 to disallow
3139 relative imports.
3140
3141 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3142 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3143 custom import functions (IMHO, this would be a nice security
3144 feature). The import C API would need to be extended to support the
3145 extra parameters of __import__ to fix that. */
3146 module = PyImport_Import(module_name);
3147 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003148 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149 "Can't pickle %R: import of module %R failed",
3150 obj, module_name);
3151 goto error;
3152 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003153 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3154 Py_INCREF(lastname);
3155 cls = get_deep_attribute(module, dotted_path, &parent);
3156 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003157 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003158 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003159 "Can't pickle %R: attribute lookup %S on %S failed",
3160 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003161 goto error;
3162 }
3163 if (cls != obj) {
3164 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003165 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003166 "Can't pickle %R: it's not the same object as %S.%S",
3167 obj, module_name, global_name);
3168 goto error;
3169 }
3170 Py_DECREF(cls);
3171
3172 if (self->proto >= 2) {
3173 /* See whether this is in the extension registry, and if
3174 * so generate an EXT opcode.
3175 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003176 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003177 PyObject *code_obj; /* extension code as Python object */
3178 long code; /* extension code as C value */
3179 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003180 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003182 extension_key = PyTuple_Pack(2, module_name, global_name);
3183 if (extension_key == NULL) {
3184 goto error;
3185 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003186 code_obj = PyDict_GetItemWithError(st->extension_registry,
3187 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003188 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003189 /* The object is not registered in the extension registry.
3190 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003191 if (code_obj == NULL) {
3192 if (PyErr_Occurred()) {
3193 goto error;
3194 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003195 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003196 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197
3198 /* XXX: pickle.py doesn't check neither the type, nor the range
3199 of the value returned by the extension_registry. It should for
3200 consistency. */
3201
3202 /* Verify code_obj has the right type and value. */
3203 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003204 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003205 "Can't pickle %R: extension code %R isn't an integer",
3206 obj, code_obj);
3207 goto error;
3208 }
3209 code = PyLong_AS_LONG(code_obj);
3210 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003211 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003212 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3213 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003214 goto error;
3215 }
3216
3217 /* Generate an EXT opcode. */
3218 if (code <= 0xff) {
3219 pdata[0] = EXT1;
3220 pdata[1] = (unsigned char)code;
3221 n = 2;
3222 }
3223 else if (code <= 0xffff) {
3224 pdata[0] = EXT2;
3225 pdata[1] = (unsigned char)(code & 0xff);
3226 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3227 n = 3;
3228 }
3229 else {
3230 pdata[0] = EXT4;
3231 pdata[1] = (unsigned char)(code & 0xff);
3232 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3233 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3234 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3235 n = 5;
3236 }
3237
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003238 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003239 goto error;
3240 }
3241 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003242 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003243 if (parent == module) {
3244 Py_INCREF(lastname);
3245 Py_DECREF(global_name);
3246 global_name = lastname;
3247 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003248 if (self->proto >= 4) {
3249 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003250
Christian Heimese8b1ba12013-11-23 21:13:39 +01003251 if (save(self, module_name, 0) < 0)
3252 goto error;
3253 if (save(self, global_name, 0) < 0)
3254 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003255
3256 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3257 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003258 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003259 else if (parent != module) {
3260 PickleState *st = _Pickle_GetGlobalState();
3261 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3262 st->getattr, parent, lastname);
3263 status = save_reduce(self, reduce_value, NULL);
3264 Py_DECREF(reduce_value);
3265 if (status < 0)
3266 goto error;
3267 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003268 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003269 /* Generate a normal global opcode if we are using a pickle
3270 protocol < 4, or if the object is not registered in the
3271 extension registry. */
3272 PyObject *encoded;
3273 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003275 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003276 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003277
3278 /* For protocol < 3 and if the user didn't request against doing
3279 so, we convert module names to the old 2.x module names. */
3280 if (self->proto < 3 && self->fix_imports) {
3281 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003282 goto error;
3283 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003284 }
3285
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003286 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3287 both the module name and the global name using UTF-8. We do so
3288 only when we are using the pickle protocol newer than version
3289 3. This is to ensure compatibility with older Unpickler running
3290 on Python 2.x. */
3291 if (self->proto == 3) {
3292 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003293 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003294 else {
3295 unicode_encoder = PyUnicode_AsASCIIString;
3296 }
3297 encoded = unicode_encoder(module_name);
3298 if (encoded == NULL) {
3299 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003300 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003301 "can't pickle module identifier '%S' using "
3302 "pickle protocol %i",
3303 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003304 goto error;
3305 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3307 PyBytes_GET_SIZE(encoded)) < 0) {
3308 Py_DECREF(encoded);
3309 goto error;
3310 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003311 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003312 if(_Pickler_Write(self, "\n", 1) < 0)
3313 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003314
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003315 /* Save the name of the module. */
3316 encoded = unicode_encoder(global_name);
3317 if (encoded == NULL) {
3318 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003319 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003320 "can't pickle global identifier '%S' using "
3321 "pickle protocol %i",
3322 global_name, self->proto);
3323 goto error;
3324 }
3325 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3326 PyBytes_GET_SIZE(encoded)) < 0) {
3327 Py_DECREF(encoded);
3328 goto error;
3329 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003330 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003331 if (_Pickler_Write(self, "\n", 1) < 0)
3332 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003333 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003334 /* Memoize the object. */
3335 if (memo_put(self, obj) < 0)
3336 goto error;
3337 }
3338
3339 if (0) {
3340 error:
3341 status = -1;
3342 }
3343 Py_XDECREF(module_name);
3344 Py_XDECREF(global_name);
3345 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003346 Py_XDECREF(parent);
3347 Py_XDECREF(dotted_path);
3348 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003349
3350 return status;
3351}
3352
3353static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003354save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3355{
3356 PyObject *reduce_value;
3357 int status;
3358
3359 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3360 if (reduce_value == NULL) {
3361 return -1;
3362 }
3363 status = save_reduce(self, reduce_value, obj);
3364 Py_DECREF(reduce_value);
3365 return status;
3366}
3367
3368static int
3369save_type(PicklerObject *self, PyObject *obj)
3370{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003371 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003372 return save_singleton_type(self, obj, Py_None);
3373 }
3374 else if (obj == (PyObject *)&PyEllipsis_Type) {
3375 return save_singleton_type(self, obj, Py_Ellipsis);
3376 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003377 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003378 return save_singleton_type(self, obj, Py_NotImplemented);
3379 }
3380 return save_global(self, obj, NULL);
3381}
3382
3383static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003384save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3385{
3386 PyObject *pid = NULL;
3387 int status = 0;
3388
3389 const char persid_op = PERSID;
3390 const char binpersid_op = BINPERSID;
3391
3392 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003393 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003394 if (pid == NULL)
3395 return -1;
3396
3397 if (pid != Py_None) {
3398 if (self->bin) {
3399 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003400 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003401 goto error;
3402 }
3403 else {
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003404 PyObject *pid_str;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405
3406 pid_str = PyObject_Str(pid);
3407 if (pid_str == NULL)
3408 goto error;
3409
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003410 /* XXX: Should it check whether the pid contains embedded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003411 newlines? */
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003412 if (!PyUnicode_IS_ASCII(pid_str)) {
3413 PyErr_SetString(_Pickle_GetGlobalState()->PicklingError,
3414 "persistent IDs in protocol 0 must be "
3415 "ASCII strings");
3416 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003417 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003418 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003420 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003421 _Pickler_Write(self, PyUnicode_DATA(pid_str),
3422 PyUnicode_GET_LENGTH(pid_str)) < 0 ||
3423 _Pickler_Write(self, "\n", 1) < 0) {
3424 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 goto error;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03003426 }
3427 Py_DECREF(pid_str);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003428 }
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__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003535 use_newobj_ex = _PyUnicode_EqualToASCIIId(
3536 name, &PyId___newobj_ex__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003537 }
3538 if (!use_newobj_ex) {
3539 _Py_IDENTIFIER(__newobj__);
Serhiy Storchaka9937d902017-01-09 10:04:34 +02003540 use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__);
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003541 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003542 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003543 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003545
3546 if (use_newobj_ex) {
3547 PyObject *cls;
3548 PyObject *args;
3549 PyObject *kwargs;
3550
3551 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003552 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003553 "length of the NEWOBJ_EX argument tuple must be "
3554 "exactly 3, not %zd", Py_SIZE(argtup));
3555 return -1;
3556 }
3557
3558 cls = PyTuple_GET_ITEM(argtup, 0);
3559 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003560 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003561 "first item from NEWOBJ_EX argument tuple must "
3562 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3563 return -1;
3564 }
3565 args = PyTuple_GET_ITEM(argtup, 1);
3566 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003567 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003568 "second item from NEWOBJ_EX argument tuple must "
3569 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3570 return -1;
3571 }
3572 kwargs = PyTuple_GET_ITEM(argtup, 2);
3573 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003574 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003575 "third item from NEWOBJ_EX argument tuple must "
3576 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3577 return -1;
3578 }
3579
3580 if (save(self, cls, 0) < 0 ||
3581 save(self, args, 0) < 0 ||
3582 save(self, kwargs, 0) < 0 ||
3583 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3584 return -1;
3585 }
3586 }
3587 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003588 PyObject *cls;
3589 PyObject *newargtup;
3590 PyObject *obj_class;
3591 int p;
3592
3593 /* Sanity checks. */
3594 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003595 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003596 return -1;
3597 }
3598
3599 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003600 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003601 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003602 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003603 return -1;
3604 }
3605
3606 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003607 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003608 p = obj_class != cls; /* true iff a problem */
3609 Py_DECREF(obj_class);
3610 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003611 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003612 "__newobj__ args has the wrong class");
3613 return -1;
3614 }
3615 }
3616 /* XXX: These calls save() are prone to infinite recursion. Imagine
3617 what happen if the value returned by the __reduce__() method of
3618 some extension type contains another object of the same type. Ouch!
3619
3620 Here is a quick example, that I ran into, to illustrate what I
3621 mean:
3622
3623 >>> import pickle, copyreg
3624 >>> copyreg.dispatch_table.pop(complex)
3625 >>> pickle.dumps(1+2j)
3626 Traceback (most recent call last):
3627 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003628 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003629
3630 Removing the complex class from copyreg.dispatch_table made the
3631 __reduce_ex__() method emit another complex object:
3632
3633 >>> (1+1j).__reduce_ex__(2)
3634 (<function __newobj__ at 0xb7b71c3c>,
3635 (<class 'complex'>, (1+1j)), None, None, None)
3636
3637 Thus when save() was called on newargstup (the 2nd item) recursion
3638 ensued. Of course, the bug was in the complex class which had a
3639 broken __getnewargs__() that emitted another complex object. But,
3640 the point, here, is it is quite easy to end up with a broken reduce
3641 function. */
3642
3643 /* Save the class and its __new__ arguments. */
3644 if (save(self, cls, 0) < 0)
3645 return -1;
3646
3647 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3648 if (newargtup == NULL)
3649 return -1;
3650
3651 p = save(self, newargtup, 0);
3652 Py_DECREF(newargtup);
3653 if (p < 0)
3654 return -1;
3655
3656 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003657 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003658 return -1;
3659 }
3660 else { /* Not using NEWOBJ. */
3661 if (save(self, callable, 0) < 0 ||
3662 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003663 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003664 return -1;
3665 }
3666
3667 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3668 the caller do not want to memoize the object. Not particularly useful,
3669 but that is to mimic the behavior save_reduce() in pickle.py when
3670 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003671 if (obj != NULL) {
3672 /* If the object is already in the memo, this means it is
3673 recursive. In this case, throw away everything we put on the
3674 stack, and fetch the object back from the memo. */
3675 if (PyMemoTable_Get(self->memo, obj)) {
3676 const char pop_op = POP;
3677
3678 if (_Pickler_Write(self, &pop_op, 1) < 0)
3679 return -1;
3680 if (memo_get(self, obj) < 0)
3681 return -1;
3682
3683 return 0;
3684 }
3685 else if (memo_put(self, obj) < 0)
3686 return -1;
3687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003688
3689 if (listitems && batch_list(self, listitems) < 0)
3690 return -1;
3691
3692 if (dictitems && batch_dict(self, dictitems) < 0)
3693 return -1;
3694
3695 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003696 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003697 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003698 return -1;
3699 }
3700
3701 return 0;
3702}
3703
3704static int
3705save(PicklerObject *self, PyObject *obj, int pers_save)
3706{
3707 PyTypeObject *type;
3708 PyObject *reduce_func = NULL;
3709 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003710 int status = 0;
3711
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003712 if (_Pickler_OpcodeBoundary(self) < 0)
3713 return -1;
3714
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003715 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003716 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003717
3718 /* The extra pers_save argument is necessary to avoid calling save_pers()
3719 on its returned object. */
3720 if (!pers_save && self->pers_func) {
3721 /* save_pers() returns:
3722 -1 to signal an error;
3723 0 if it did nothing successfully;
3724 1 if a persistent id was saved.
3725 */
3726 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3727 goto done;
3728 }
3729
3730 type = Py_TYPE(obj);
3731
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003732 /* The old cPickle had an optimization that used switch-case statement
3733 dispatching on the first letter of the type name. This has was removed
3734 since benchmarks shown that this optimization was actually slowing
3735 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003736
3737 /* Atom types; these aren't memoized, so don't check the memo. */
3738
3739 if (obj == Py_None) {
3740 status = save_none(self, obj);
3741 goto done;
3742 }
3743 else if (obj == Py_False || obj == Py_True) {
3744 status = save_bool(self, obj);
3745 goto done;
3746 }
3747 else if (type == &PyLong_Type) {
3748 status = save_long(self, obj);
3749 goto done;
3750 }
3751 else if (type == &PyFloat_Type) {
3752 status = save_float(self, obj);
3753 goto done;
3754 }
3755
3756 /* Check the memo to see if it has the object. If so, generate
3757 a GET (or BINGET) opcode, instead of pickling the object
3758 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003759 if (PyMemoTable_Get(self->memo, obj)) {
3760 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003761 goto error;
3762 goto done;
3763 }
3764
3765 if (type == &PyBytes_Type) {
3766 status = save_bytes(self, obj);
3767 goto done;
3768 }
3769 else if (type == &PyUnicode_Type) {
3770 status = save_unicode(self, obj);
3771 goto done;
3772 }
3773 else if (type == &PyDict_Type) {
3774 status = save_dict(self, obj);
3775 goto done;
3776 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003777 else if (type == &PySet_Type) {
3778 status = save_set(self, obj);
3779 goto done;
3780 }
3781 else if (type == &PyFrozenSet_Type) {
3782 status = save_frozenset(self, obj);
3783 goto done;
3784 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003785 else if (type == &PyList_Type) {
3786 status = save_list(self, obj);
3787 goto done;
3788 }
3789 else if (type == &PyTuple_Type) {
3790 status = save_tuple(self, obj);
3791 goto done;
3792 }
3793 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003794 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003795 goto done;
3796 }
3797 else if (type == &PyFunction_Type) {
3798 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003799 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003800 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801
3802 /* XXX: This part needs some unit tests. */
3803
3804 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003805 * self.dispatch_table, copyreg.dispatch_table, the object's
3806 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003807 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003808 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003809 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003810 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3811 (PyObject *)type);
3812 if (reduce_func == NULL) {
3813 if (PyErr_Occurred()) {
3814 goto error;
3815 }
3816 } else {
3817 /* PyDict_GetItemWithError() returns a borrowed reference.
3818 Increase the reference count to be consistent with
3819 PyObject_GetItem and _PyObject_GetAttrId used below. */
3820 Py_INCREF(reduce_func);
3821 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003822 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003823 reduce_func = PyObject_GetItem(self->dispatch_table,
3824 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003825 if (reduce_func == NULL) {
3826 if (PyErr_ExceptionMatches(PyExc_KeyError))
3827 PyErr_Clear();
3828 else
3829 goto error;
3830 }
3831 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003832 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003833 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003834 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003836 else if (PyType_IsSubtype(type, &PyType_Type)) {
3837 status = save_global(self, obj, NULL);
3838 goto done;
3839 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003841 _Py_IDENTIFIER(__reduce__);
3842 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003843
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844
3845 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3846 automatically defined as __reduce__. While this is convenient, this
3847 make it impossible to know which method was actually called. Of
3848 course, this is not a big deal. But still, it would be nice to let
3849 the user know which method was called when something go
3850 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3851 don't actually have to check for a __reduce__ method. */
3852
3853 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003854 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003855 if (reduce_func != NULL) {
3856 PyObject *proto;
3857 proto = PyLong_FromLong(self->proto);
3858 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003859 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003860 }
3861 }
3862 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003863 PickleState *st = _Pickle_GetGlobalState();
3864
3865 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003867 }
3868 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003870 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003871 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003872 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003873 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003874 PyObject *empty_tuple = PyTuple_New(0);
3875 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003876 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003877 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003878 }
3879 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003880 PyErr_Format(st->PicklingError,
3881 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003882 type->tp_name, obj);
3883 goto error;
3884 }
3885 }
3886 }
3887
3888 if (reduce_value == NULL)
3889 goto error;
3890
3891 if (PyUnicode_Check(reduce_value)) {
3892 status = save_global(self, obj, reduce_value);
3893 goto done;
3894 }
3895
3896 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003897 PickleState *st = _Pickle_GetGlobalState();
3898 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 "__reduce__ must return a string or tuple");
3900 goto error;
3901 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902
3903 status = save_reduce(self, reduce_value, obj);
3904
3905 if (0) {
3906 error:
3907 status = -1;
3908 }
3909 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003910
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003911 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003912 Py_XDECREF(reduce_func);
3913 Py_XDECREF(reduce_value);
3914
3915 return status;
3916}
3917
3918static int
3919dump(PicklerObject *self, PyObject *obj)
3920{
3921 const char stop_op = STOP;
3922
3923 if (self->proto >= 2) {
3924 char header[2];
3925
3926 header[0] = PROTO;
3927 assert(self->proto >= 0 && self->proto < 256);
3928 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003929 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003930 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003931 if (self->proto >= 4)
3932 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 }
3934
3935 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003936 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 return -1;
3938
3939 return 0;
3940}
3941
Larry Hastings61272b72014-01-07 12:41:53 -08003942/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003943
3944_pickle.Pickler.clear_memo
3945
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003946Clears the pickler's "memo".
3947
3948The memo is the data structure that remembers which objects the
3949pickler has already seen, so that shared or recursive objects are
3950pickled by reference and not by value. This method is useful when
3951re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003952[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003953
Larry Hastings3cceb382014-01-04 11:09:09 -08003954static PyObject *
3955_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003956/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957{
3958 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003959 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003960
3961 Py_RETURN_NONE;
3962}
3963
Larry Hastings61272b72014-01-07 12:41:53 -08003964/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003965
3966_pickle.Pickler.dump
3967
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003968 obj: object
3969 /
3970
3971Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003972[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003973
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003975_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003976/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003977{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003978 /* Check whether the Pickler was initialized correctly (issue3664).
3979 Developers often forget to call __init__() in their subclasses, which
3980 would trigger a segfault without this check. */
3981 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003982 PickleState *st = _Pickle_GetGlobalState();
3983 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003984 "Pickler.__init__() was not called by %s.__init__()",
3985 Py_TYPE(self)->tp_name);
3986 return NULL;
3987 }
3988
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003989 if (_Pickler_ClearBuffer(self) < 0)
3990 return NULL;
3991
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 if (dump(self, obj) < 0)
3993 return NULL;
3994
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003995 if (_Pickler_FlushToFile(self) < 0)
3996 return NULL;
3997
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003998 Py_RETURN_NONE;
3999}
4000
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004001/*[clinic input]
4002
4003_pickle.Pickler.__sizeof__ -> Py_ssize_t
4004
4005Returns size in memory, in bytes.
4006[clinic start generated code]*/
4007
4008static Py_ssize_t
4009_pickle_Pickler___sizeof___impl(PicklerObject *self)
4010/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4011{
4012 Py_ssize_t res, s;
4013
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004014 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004015 if (self->memo != NULL) {
4016 res += sizeof(PyMemoTable);
4017 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4018 }
4019 if (self->output_buffer != NULL) {
4020 s = _PySys_GetSizeOf(self->output_buffer);
4021 if (s == -1)
4022 return -1;
4023 res += s;
4024 }
4025 return res;
4026}
4027
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004028static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004029 _PICKLE_PICKLER_DUMP_METHODDEF
4030 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004031 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004032 {NULL, NULL} /* sentinel */
4033};
4034
4035static void
4036Pickler_dealloc(PicklerObject *self)
4037{
4038 PyObject_GC_UnTrack(self);
4039
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004040 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004041 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004043 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004044 Py_XDECREF(self->fast_memo);
4045
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004046 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004047
4048 Py_TYPE(self)->tp_free((PyObject *)self);
4049}
4050
4051static int
4052Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4053{
4054 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004055 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004056 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004057 Py_VISIT(self->fast_memo);
4058 return 0;
4059}
4060
4061static int
4062Pickler_clear(PicklerObject *self)
4063{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004064 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004067 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004068 Py_CLEAR(self->fast_memo);
4069
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004070 if (self->memo != NULL) {
4071 PyMemoTable *memo = self->memo;
4072 self->memo = NULL;
4073 PyMemoTable_Del(memo);
4074 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004075 return 0;
4076}
4077
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004078
Larry Hastings61272b72014-01-07 12:41:53 -08004079/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004080
4081_pickle.Pickler.__init__
4082
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004083 file: object
4084 protocol: object = NULL
4085 fix_imports: bool = True
4086
4087This takes a binary file for writing a pickle data stream.
4088
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004089The optional *protocol* argument tells the pickler to use the given
4090protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4091protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004092
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004093Specifying a negative protocol version selects the highest protocol
4094version supported. The higher the protocol used, the more recent the
4095version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004097The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004098bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004099writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004100this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004101
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004102If *fix_imports* is True and protocol is less than 3, pickle will try
4103to map the new Python 3 names to the old module names used in Python
41042, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004105[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004106
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004107static int
Larry Hastings89964c42015-04-14 18:07:59 -04004108_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4109 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004110/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
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;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004605 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004606 size_t x = 0;
4607
Serhiy Storchakae0606192015-09-29 22:10:07 +03004608 if (nbytes > (int)sizeof(size_t)) {
4609 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4610 * have 64-bit size that can't be represented on 32-bit platform.
4611 */
4612 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4613 if (s[i])
4614 return -1;
4615 }
4616 nbytes = (int)sizeof(size_t);
4617 }
4618 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004619 x |= (size_t) s[i] << (8 * i);
4620 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004621
4622 if (x > PY_SSIZE_T_MAX)
4623 return -1;
4624 else
4625 return (Py_ssize_t) x;
4626}
4627
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628/* s contains x bytes of a little-endian integer. Return its value as a
4629 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +03004630 * int, but when x is 4 it's a signed one. This is a historical source
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 * of x-platform bugs.
4632 */
4633static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004634calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004635{
4636 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004637 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004638 long x = 0;
4639
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004640 for (i = 0; i < nbytes; i++) {
4641 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642 }
4643
4644 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4645 * is signed, so on a box with longs bigger than 4 bytes we need
4646 * to extend a BININT's sign bit to the full width.
4647 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004648 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649 x |= -(x & (1L << 31));
4650 }
4651
4652 return x;
4653}
4654
4655static int
4656load_binintx(UnpicklerObject *self, char *s, int size)
4657{
4658 PyObject *value;
4659 long x;
4660
4661 x = calc_binint(s, size);
4662
4663 if ((value = PyLong_FromLong(x)) == NULL)
4664 return -1;
4665
4666 PDATA_PUSH(self->stack, value, -1);
4667 return 0;
4668}
4669
4670static int
4671load_binint(UnpicklerObject *self)
4672{
4673 char *s;
4674
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004675 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004676 return -1;
4677
4678 return load_binintx(self, s, 4);
4679}
4680
4681static int
4682load_binint1(UnpicklerObject *self)
4683{
4684 char *s;
4685
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004686 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004687 return -1;
4688
4689 return load_binintx(self, s, 1);
4690}
4691
4692static int
4693load_binint2(UnpicklerObject *self)
4694{
4695 char *s;
4696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004697 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 return -1;
4699
4700 return load_binintx(self, s, 2);
4701}
4702
4703static int
4704load_long(UnpicklerObject *self)
4705{
4706 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004707 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004708 Py_ssize_t len;
4709
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004710 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711 return -1;
4712 if (len < 2)
4713 return bad_readline();
4714
Mark Dickinson8dd05142009-01-20 20:43:58 +00004715 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4716 the 'L' before calling PyLong_FromString. In order to maintain
4717 compatibility with Python 3.0.0, we don't actually *require*
4718 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004719 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004720 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004721 /* XXX: Should the base argument explicitly set to 10? */
4722 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004723 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004724 return -1;
4725
4726 PDATA_PUSH(self->stack, value, -1);
4727 return 0;
4728}
4729
4730/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4731 * data following.
4732 */
4733static int
4734load_counted_long(UnpicklerObject *self, int size)
4735{
4736 PyObject *value;
4737 char *nbytes;
4738 char *pdata;
4739
4740 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004741 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742 return -1;
4743
4744 size = calc_binint(nbytes, size);
4745 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004746 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004747 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004748 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004749 "LONG pickle has negative byte count");
4750 return -1;
4751 }
4752
4753 if (size == 0)
4754 value = PyLong_FromLong(0L);
4755 else {
4756 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004757 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004758 return -1;
4759 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4760 1 /* little endian */ , 1 /* signed */ );
4761 }
4762 if (value == NULL)
4763 return -1;
4764 PDATA_PUSH(self->stack, value, -1);
4765 return 0;
4766}
4767
4768static int
4769load_float(UnpicklerObject *self)
4770{
4771 PyObject *value;
4772 char *endptr, *s;
4773 Py_ssize_t len;
4774 double d;
4775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004776 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777 return -1;
4778 if (len < 2)
4779 return bad_readline();
4780
4781 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004782 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4783 if (d == -1.0 && PyErr_Occurred())
4784 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004785 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4787 return -1;
4788 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004789 value = PyFloat_FromDouble(d);
4790 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 return -1;
4792
4793 PDATA_PUSH(self->stack, value, -1);
4794 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004795}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004796
4797static int
4798load_binfloat(UnpicklerObject *self)
4799{
4800 PyObject *value;
4801 double x;
4802 char *s;
4803
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004804 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004805 return -1;
4806
4807 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4808 if (x == -1.0 && PyErr_Occurred())
4809 return -1;
4810
4811 if ((value = PyFloat_FromDouble(x)) == NULL)
4812 return -1;
4813
4814 PDATA_PUSH(self->stack, value, -1);
4815 return 0;
4816}
4817
4818static int
4819load_string(UnpicklerObject *self)
4820{
4821 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004822 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 Py_ssize_t len;
4824 char *s, *p;
4825
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004826 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004827 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004828 /* Strip the newline */
4829 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004830 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004831 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004832 p = s + 1;
4833 len -= 2;
4834 }
4835 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004836 PickleState *st = _Pickle_GetGlobalState();
4837 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004838 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004839 return -1;
4840 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004841 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004842
4843 /* Use the PyBytes API to decode the string, since that is what is used
4844 to encode, and then coerce the result to Unicode. */
4845 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004846 if (bytes == NULL)
4847 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004848
4849 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4850 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4851 if (strcmp(self->encoding, "bytes") == 0) {
4852 obj = bytes;
4853 }
4854 else {
4855 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4856 Py_DECREF(bytes);
4857 if (obj == NULL) {
4858 return -1;
4859 }
4860 }
4861
4862 PDATA_PUSH(self->stack, obj, -1);
4863 return 0;
4864}
4865
4866static int
4867load_counted_binstring(UnpicklerObject *self, int nbytes)
4868{
4869 PyObject *obj;
4870 Py_ssize_t size;
4871 char *s;
4872
4873 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004874 return -1;
4875
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004876 size = calc_binsize(s, nbytes);
4877 if (size < 0) {
4878 PickleState *st = _Pickle_GetGlobalState();
4879 PyErr_Format(st->UnpicklingError,
4880 "BINSTRING exceeds system's maximum size of %zd bytes",
4881 PY_SSIZE_T_MAX);
4882 return -1;
4883 }
4884
4885 if (_Unpickler_Read(self, &s, size) < 0)
4886 return -1;
4887
4888 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4889 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4890 if (strcmp(self->encoding, "bytes") == 0) {
4891 obj = PyBytes_FromStringAndSize(s, size);
4892 }
4893 else {
4894 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4895 }
4896 if (obj == NULL) {
4897 return -1;
4898 }
4899
4900 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004901 return 0;
4902}
4903
4904static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004905load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906{
4907 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004908 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004909 char *s;
4910
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004911 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004912 return -1;
4913
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004914 size = calc_binsize(s, nbytes);
4915 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004916 PyErr_Format(PyExc_OverflowError,
4917 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004918 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004919 return -1;
4920 }
4921
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004922 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004923 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004924
4925 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 if (bytes == NULL)
4927 return -1;
4928
4929 PDATA_PUSH(self->stack, bytes, -1);
4930 return 0;
4931}
4932
4933static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004934load_unicode(UnpicklerObject *self)
4935{
4936 PyObject *str;
4937 Py_ssize_t len;
4938 char *s;
4939
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004940 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941 return -1;
4942 if (len < 1)
4943 return bad_readline();
4944
4945 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4946 if (str == NULL)
4947 return -1;
4948
4949 PDATA_PUSH(self->stack, str, -1);
4950 return 0;
4951}
4952
4953static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004954load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004955{
4956 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004957 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004958 char *s;
4959
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004960 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004961 return -1;
4962
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004963 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004964 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004965 PyErr_Format(PyExc_OverflowError,
4966 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004967 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return -1;
4969 }
4970
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004971 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004972 return -1;
4973
Victor Stinner485fb562010-04-13 11:07:24 +00004974 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975 if (str == NULL)
4976 return -1;
4977
4978 PDATA_PUSH(self->stack, str, -1);
4979 return 0;
4980}
4981
4982static int
Victor Stinner13f0c612016-03-14 18:09:39 +01004983load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004984{
4985 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004986
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004987 if (Py_SIZE(self->stack) < len)
4988 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004990 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004991 if (tuple == NULL)
4992 return -1;
4993 PDATA_PUSH(self->stack, tuple, -1);
4994 return 0;
4995}
4996
4997static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02004998load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005000 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005002 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005003 return -1;
5004
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005005 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006}
5007
5008static int
5009load_empty_list(UnpicklerObject *self)
5010{
5011 PyObject *list;
5012
5013 if ((list = PyList_New(0)) == NULL)
5014 return -1;
5015 PDATA_PUSH(self->stack, list, -1);
5016 return 0;
5017}
5018
5019static int
5020load_empty_dict(UnpicklerObject *self)
5021{
5022 PyObject *dict;
5023
5024 if ((dict = PyDict_New()) == NULL)
5025 return -1;
5026 PDATA_PUSH(self->stack, dict, -1);
5027 return 0;
5028}
5029
5030static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005031load_empty_set(UnpicklerObject *self)
5032{
5033 PyObject *set;
5034
5035 if ((set = PySet_New(NULL)) == NULL)
5036 return -1;
5037 PDATA_PUSH(self->stack, set, -1);
5038 return 0;
5039}
5040
5041static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042load_list(UnpicklerObject *self)
5043{
5044 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005045 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005046
5047 if ((i = marker(self)) < 0)
5048 return -1;
5049
5050 list = Pdata_poplist(self->stack, i);
5051 if (list == NULL)
5052 return -1;
5053 PDATA_PUSH(self->stack, list, -1);
5054 return 0;
5055}
5056
5057static int
5058load_dict(UnpicklerObject *self)
5059{
5060 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005061 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062
5063 if ((i = marker(self)) < 0)
5064 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005065 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005066
5067 if ((dict = PyDict_New()) == NULL)
5068 return -1;
5069
5070 for (k = i + 1; k < j; k += 2) {
5071 key = self->stack->data[k - 1];
5072 value = self->stack->data[k];
5073 if (PyDict_SetItem(dict, key, value) < 0) {
5074 Py_DECREF(dict);
5075 return -1;
5076 }
5077 }
5078 Pdata_clear(self->stack, i);
5079 PDATA_PUSH(self->stack, dict, -1);
5080 return 0;
5081}
5082
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005083static int
5084load_frozenset(UnpicklerObject *self)
5085{
5086 PyObject *items;
5087 PyObject *frozenset;
5088 Py_ssize_t i;
5089
5090 if ((i = marker(self)) < 0)
5091 return -1;
5092
5093 items = Pdata_poptuple(self->stack, i);
5094 if (items == NULL)
5095 return -1;
5096
5097 frozenset = PyFrozenSet_New(items);
5098 Py_DECREF(items);
5099 if (frozenset == NULL)
5100 return -1;
5101
5102 PDATA_PUSH(self->stack, frozenset, -1);
5103 return 0;
5104}
5105
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005106static PyObject *
5107instantiate(PyObject *cls, PyObject *args)
5108{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005109 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005110 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005111 /* Caller must assure args are a tuple. Normally, args come from
5112 Pdata_poptuple which packs objects from the top of the stack
5113 into a newly created tuple. */
5114 assert(PyTuple_Check(args));
5115 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005116 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005117 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005119 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005120 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005121
5122 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005123 }
5124 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005125}
5126
5127static int
5128load_obj(UnpicklerObject *self)
5129{
5130 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005131 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005132
5133 if ((i = marker(self)) < 0)
5134 return -1;
5135
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005136 if (Py_SIZE(self->stack) - i < 1)
5137 return stack_underflow();
5138
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139 args = Pdata_poptuple(self->stack, i + 1);
5140 if (args == NULL)
5141 return -1;
5142
5143 PDATA_POP(self->stack, cls);
5144 if (cls) {
5145 obj = instantiate(cls, args);
5146 Py_DECREF(cls);
5147 }
5148 Py_DECREF(args);
5149 if (obj == NULL)
5150 return -1;
5151
5152 PDATA_PUSH(self->stack, obj, -1);
5153 return 0;
5154}
5155
5156static int
5157load_inst(UnpicklerObject *self)
5158{
5159 PyObject *cls = NULL;
5160 PyObject *args = NULL;
5161 PyObject *obj = NULL;
5162 PyObject *module_name;
5163 PyObject *class_name;
5164 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005165 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005166 char *s;
5167
5168 if ((i = marker(self)) < 0)
5169 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005170 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005171 return -1;
5172 if (len < 2)
5173 return bad_readline();
5174
5175 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5176 identifiers are permitted in Python 3.0, since the INST opcode is only
5177 supported by older protocols on Python 2.x. */
5178 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5179 if (module_name == NULL)
5180 return -1;
5181
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005182 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005183 if (len < 2) {
5184 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005185 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005186 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005187 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005188 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005189 cls = find_class(self, module_name, class_name);
5190 Py_DECREF(class_name);
5191 }
5192 }
5193 Py_DECREF(module_name);
5194
5195 if (cls == NULL)
5196 return -1;
5197
5198 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5199 obj = instantiate(cls, args);
5200 Py_DECREF(args);
5201 }
5202 Py_DECREF(cls);
5203
5204 if (obj == NULL)
5205 return -1;
5206
5207 PDATA_PUSH(self->stack, obj, -1);
5208 return 0;
5209}
5210
5211static int
5212load_newobj(UnpicklerObject *self)
5213{
5214 PyObject *args = NULL;
5215 PyObject *clsraw = NULL;
5216 PyTypeObject *cls; /* clsraw cast to its true type */
5217 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005218 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219
5220 /* Stack is ... cls argtuple, and we want to call
5221 * cls.__new__(cls, *argtuple).
5222 */
5223 PDATA_POP(self->stack, args);
5224 if (args == NULL)
5225 goto error;
5226 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005227 PyErr_SetString(st->UnpicklingError,
5228 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005229 goto error;
5230 }
5231
5232 PDATA_POP(self->stack, clsraw);
5233 cls = (PyTypeObject *)clsraw;
5234 if (cls == NULL)
5235 goto error;
5236 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005237 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005238 "isn't a type object");
5239 goto error;
5240 }
5241 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005242 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005243 "has NULL tp_new");
5244 goto error;
5245 }
5246
5247 /* Call __new__. */
5248 obj = cls->tp_new(cls, args, NULL);
5249 if (obj == NULL)
5250 goto error;
5251
5252 Py_DECREF(args);
5253 Py_DECREF(clsraw);
5254 PDATA_PUSH(self->stack, obj, -1);
5255 return 0;
5256
5257 error:
5258 Py_XDECREF(args);
5259 Py_XDECREF(clsraw);
5260 return -1;
5261}
5262
5263static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005264load_newobj_ex(UnpicklerObject *self)
5265{
5266 PyObject *cls, *args, *kwargs;
5267 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005268 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005269
5270 PDATA_POP(self->stack, kwargs);
5271 if (kwargs == NULL) {
5272 return -1;
5273 }
5274 PDATA_POP(self->stack, args);
5275 if (args == NULL) {
5276 Py_DECREF(kwargs);
5277 return -1;
5278 }
5279 PDATA_POP(self->stack, cls);
5280 if (cls == NULL) {
5281 Py_DECREF(kwargs);
5282 Py_DECREF(args);
5283 return -1;
5284 }
Larry Hastings61272b72014-01-07 12:41:53 -08005285
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005286 if (!PyType_Check(cls)) {
5287 Py_DECREF(kwargs);
5288 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005289 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005290 "NEWOBJ_EX class argument must be a type, not %.200s",
5291 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005292 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005293 return -1;
5294 }
5295
5296 if (((PyTypeObject *)cls)->tp_new == NULL) {
5297 Py_DECREF(kwargs);
5298 Py_DECREF(args);
5299 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005300 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005301 "NEWOBJ_EX class argument doesn't have __new__");
5302 return -1;
5303 }
5304 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5305 Py_DECREF(kwargs);
5306 Py_DECREF(args);
5307 Py_DECREF(cls);
5308 if (obj == NULL) {
5309 return -1;
5310 }
5311 PDATA_PUSH(self->stack, obj, -1);
5312 return 0;
5313}
5314
5315static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005316load_global(UnpicklerObject *self)
5317{
5318 PyObject *global = NULL;
5319 PyObject *module_name;
5320 PyObject *global_name;
5321 Py_ssize_t len;
5322 char *s;
5323
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005324 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005325 return -1;
5326 if (len < 2)
5327 return bad_readline();
5328 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5329 if (!module_name)
5330 return -1;
5331
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005332 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005333 if (len < 2) {
5334 Py_DECREF(module_name);
5335 return bad_readline();
5336 }
5337 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5338 if (global_name) {
5339 global = find_class(self, module_name, global_name);
5340 Py_DECREF(global_name);
5341 }
5342 }
5343 Py_DECREF(module_name);
5344
5345 if (global == NULL)
5346 return -1;
5347 PDATA_PUSH(self->stack, global, -1);
5348 return 0;
5349}
5350
5351static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005352load_stack_global(UnpicklerObject *self)
5353{
5354 PyObject *global;
5355 PyObject *module_name;
5356 PyObject *global_name;
5357
5358 PDATA_POP(self->stack, global_name);
5359 PDATA_POP(self->stack, module_name);
5360 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5361 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005362 PickleState *st = _Pickle_GetGlobalState();
5363 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005364 Py_XDECREF(global_name);
5365 Py_XDECREF(module_name);
5366 return -1;
5367 }
5368 global = find_class(self, module_name, global_name);
5369 Py_DECREF(global_name);
5370 Py_DECREF(module_name);
5371 if (global == NULL)
5372 return -1;
5373 PDATA_PUSH(self->stack, global, -1);
5374 return 0;
5375}
5376
5377static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005378load_persid(UnpicklerObject *self)
5379{
5380 PyObject *pid;
5381 Py_ssize_t len;
5382 char *s;
5383
5384 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005385 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005386 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005387 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005388 return bad_readline();
5389
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005390 pid = PyUnicode_DecodeASCII(s, len - 1, "strict");
5391 if (pid == NULL) {
5392 if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
5393 PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError,
5394 "persistent IDs in protocol 0 must be "
5395 "ASCII strings");
5396 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005397 return -1;
Serhiy Storchakadec25af2016-07-17 11:24:17 +03005398 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005400 /* This does not leak since _Pickle_FastCall() steals the reference
5401 to pid first. */
5402 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403 if (pid == NULL)
5404 return -1;
5405
5406 PDATA_PUSH(self->stack, pid, -1);
5407 return 0;
5408 }
5409 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005410 PickleState *st = _Pickle_GetGlobalState();
5411 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 "A load persistent id instruction was encountered,\n"
5413 "but no persistent_load function was specified.");
5414 return -1;
5415 }
5416}
5417
5418static int
5419load_binpersid(UnpicklerObject *self)
5420{
5421 PyObject *pid;
5422
5423 if (self->pers_func) {
5424 PDATA_POP(self->stack, pid);
5425 if (pid == NULL)
5426 return -1;
5427
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005428 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005429 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005430 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005431 if (pid == NULL)
5432 return -1;
5433
5434 PDATA_PUSH(self->stack, pid, -1);
5435 return 0;
5436 }
5437 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005438 PickleState *st = _Pickle_GetGlobalState();
5439 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005440 "A load persistent id instruction was encountered,\n"
5441 "but no persistent_load function was specified.");
5442 return -1;
5443 }
5444}
5445
5446static int
5447load_pop(UnpicklerObject *self)
5448{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005449 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005450
5451 /* Note that we split the (pickle.py) stack into two stacks,
5452 * an object stack and a mark stack. We have to be clever and
5453 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005454 * mark stack first, and only signalling a stack underflow if
5455 * the object stack is empty and the mark stack doesn't match
5456 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005458 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005460 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461 len--;
5462 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005463 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005464 } else {
5465 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005466 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005467 return 0;
5468}
5469
5470static int
5471load_pop_mark(UnpicklerObject *self)
5472{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005473 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474
5475 if ((i = marker(self)) < 0)
5476 return -1;
5477
5478 Pdata_clear(self->stack, i);
5479
5480 return 0;
5481}
5482
5483static int
5484load_dup(UnpicklerObject *self)
5485{
5486 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005487 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005489 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005490 return stack_underflow();
5491 last = self->stack->data[len - 1];
5492 PDATA_APPEND(self->stack, last, -1);
5493 return 0;
5494}
5495
5496static int
5497load_get(UnpicklerObject *self)
5498{
5499 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005500 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005501 Py_ssize_t len;
5502 char *s;
5503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005504 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005505 return -1;
5506 if (len < 2)
5507 return bad_readline();
5508
5509 key = PyLong_FromString(s, NULL, 10);
5510 if (key == NULL)
5511 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005512 idx = PyLong_AsSsize_t(key);
5513 if (idx == -1 && PyErr_Occurred()) {
5514 Py_DECREF(key);
5515 return -1;
5516 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005517
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005518 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519 if (value == NULL) {
5520 if (!PyErr_Occurred())
5521 PyErr_SetObject(PyExc_KeyError, key);
5522 Py_DECREF(key);
5523 return -1;
5524 }
5525 Py_DECREF(key);
5526
5527 PDATA_APPEND(self->stack, value, -1);
5528 return 0;
5529}
5530
5531static int
5532load_binget(UnpicklerObject *self)
5533{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005534 PyObject *value;
5535 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536 char *s;
5537
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005538 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539 return -1;
5540
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005541 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005543 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005544 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005545 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005546 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005548 Py_DECREF(key);
5549 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550 return -1;
5551 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552
5553 PDATA_APPEND(self->stack, value, -1);
5554 return 0;
5555}
5556
5557static int
5558load_long_binget(UnpicklerObject *self)
5559{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005560 PyObject *value;
5561 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005562 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005564 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005565 return -1;
5566
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005567 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005569 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005570 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005571 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005572 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005573 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005574 Py_DECREF(key);
5575 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005576 return -1;
5577 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005578
5579 PDATA_APPEND(self->stack, value, -1);
5580 return 0;
5581}
5582
5583/* Push an object from the extension registry (EXT[124]). nbytes is
5584 * the number of bytes following the opcode, holding the index (code) value.
5585 */
5586static int
5587load_extension(UnpicklerObject *self, int nbytes)
5588{
5589 char *codebytes; /* the nbytes bytes after the opcode */
5590 long code; /* calc_binint returns long */
5591 PyObject *py_code; /* code as a Python int */
5592 PyObject *obj; /* the object to push */
5593 PyObject *pair; /* (module_name, class_name) */
5594 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005595 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005596
5597 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 return -1;
5600 code = calc_binint(codebytes, nbytes);
5601 if (code <= 0) { /* note that 0 is forbidden */
5602 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005603 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604 return -1;
5605 }
5606
5607 /* Look for the code in the cache. */
5608 py_code = PyLong_FromLong(code);
5609 if (py_code == NULL)
5610 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005611 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 if (obj != NULL) {
5613 /* Bingo. */
5614 Py_DECREF(py_code);
5615 PDATA_APPEND(self->stack, obj, -1);
5616 return 0;
5617 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005618 if (PyErr_Occurred()) {
5619 Py_DECREF(py_code);
5620 return -1;
5621 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622
5623 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005624 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625 if (pair == NULL) {
5626 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005627 if (!PyErr_Occurred()) {
5628 PyErr_Format(PyExc_ValueError, "unregistered extension "
5629 "code %ld", code);
5630 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 return -1;
5632 }
5633 /* Since the extension registry is manipulable via Python code,
5634 * confirm that pair is really a 2-tuple of strings.
5635 */
5636 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5637 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5638 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5639 Py_DECREF(py_code);
5640 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5641 "isn't a 2-tuple of strings", code);
5642 return -1;
5643 }
5644 /* Load the object. */
5645 obj = find_class(self, module_name, class_name);
5646 if (obj == NULL) {
5647 Py_DECREF(py_code);
5648 return -1;
5649 }
5650 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005651 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005652 Py_DECREF(py_code);
5653 if (code < 0) {
5654 Py_DECREF(obj);
5655 return -1;
5656 }
5657 PDATA_PUSH(self->stack, obj, -1);
5658 return 0;
5659}
5660
5661static int
5662load_put(UnpicklerObject *self)
5663{
5664 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005665 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666 Py_ssize_t len;
5667 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005669 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 return -1;
5671 if (len < 2)
5672 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005673 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005675 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005676
5677 key = PyLong_FromString(s, NULL, 10);
5678 if (key == NULL)
5679 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005680 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005682 if (idx < 0) {
5683 if (!PyErr_Occurred())
5684 PyErr_SetString(PyExc_ValueError,
5685 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005687 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005688
5689 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690}
5691
5692static int
5693load_binput(UnpicklerObject *self)
5694{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 PyObject *value;
5696 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005699 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005700 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005701
5702 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005703 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005704 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005705
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005706 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005708 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709}
5710
5711static int
5712load_long_binput(UnpicklerObject *self)
5713{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005714 PyObject *value;
5715 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005716 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005717
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005718 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005720
5721 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005723 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005724
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005725 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005726 if (idx < 0) {
5727 PyErr_SetString(PyExc_ValueError,
5728 "negative LONG_BINPUT argument");
5729 return -1;
5730 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005731
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005732 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733}
5734
5735static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005736load_memoize(UnpicklerObject *self)
5737{
5738 PyObject *value;
5739
5740 if (Py_SIZE(self->stack) <= 0)
5741 return stack_underflow();
5742 value = self->stack->data[Py_SIZE(self->stack) - 1];
5743
5744 return _Unpickler_MemoPut(self, self->memo_len, value);
5745}
5746
5747static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005748do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005749{
5750 PyObject *value;
5751 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005752 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005753
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005754 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005755 if (x > len || x <= 0)
5756 return stack_underflow();
5757 if (len == x) /* nothing to do */
5758 return 0;
5759
5760 list = self->stack->data[x - 1];
5761
5762 if (PyList_Check(list)) {
5763 PyObject *slice;
5764 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005765 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766
5767 slice = Pdata_poplist(self->stack, x);
5768 if (!slice)
5769 return -1;
5770 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005771 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005773 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774 }
5775 else {
5776 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005777 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005779 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005780 if (append_func == NULL)
5781 return -1;
5782 for (i = x; i < len; i++) {
5783 PyObject *result;
5784
5785 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005786 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787 if (result == NULL) {
5788 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005790 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791 return -1;
5792 }
5793 Py_DECREF(result);
5794 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005795 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005796 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797 }
5798
5799 return 0;
5800}
5801
5802static int
5803load_append(UnpicklerObject *self)
5804{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005805 if (Py_SIZE(self->stack) - 1 <= 0)
5806 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005807 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808}
5809
5810static int
5811load_appends(UnpicklerObject *self)
5812{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005813 Py_ssize_t i = marker(self);
5814 if (i < 0)
5815 return -1;
5816 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817}
5818
5819static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005820do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821{
5822 PyObject *value, *key;
5823 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005824 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005825 int status = 0;
5826
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005827 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828 if (x > len || x <= 0)
5829 return stack_underflow();
5830 if (len == x) /* nothing to do */
5831 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005832 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005833 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005834 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005835 PyErr_SetString(st->UnpicklingError,
5836 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 return -1;
5838 }
5839
5840 /* Here, dict does not actually need to be a PyDict; it could be anything
5841 that supports the __setitem__ attribute. */
5842 dict = self->stack->data[x - 1];
5843
5844 for (i = x + 1; i < len; i += 2) {
5845 key = self->stack->data[i - 1];
5846 value = self->stack->data[i];
5847 if (PyObject_SetItem(dict, key, value) < 0) {
5848 status = -1;
5849 break;
5850 }
5851 }
5852
5853 Pdata_clear(self->stack, x);
5854 return status;
5855}
5856
5857static int
5858load_setitem(UnpicklerObject *self)
5859{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005860 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005861}
5862
5863static int
5864load_setitems(UnpicklerObject *self)
5865{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005866 Py_ssize_t i = marker(self);
5867 if (i < 0)
5868 return -1;
5869 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005870}
5871
5872static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005873load_additems(UnpicklerObject *self)
5874{
5875 PyObject *set;
5876 Py_ssize_t mark, len, i;
5877
5878 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005879 if (mark < 0)
5880 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005881 len = Py_SIZE(self->stack);
5882 if (mark > len || mark <= 0)
5883 return stack_underflow();
5884 if (len == mark) /* nothing to do */
5885 return 0;
5886
5887 set = self->stack->data[mark - 1];
5888
5889 if (PySet_Check(set)) {
5890 PyObject *items;
5891 int status;
5892
5893 items = Pdata_poptuple(self->stack, mark);
5894 if (items == NULL)
5895 return -1;
5896
5897 status = _PySet_Update(set, items);
5898 Py_DECREF(items);
5899 return status;
5900 }
5901 else {
5902 PyObject *add_func;
5903 _Py_IDENTIFIER(add);
5904
5905 add_func = _PyObject_GetAttrId(set, &PyId_add);
5906 if (add_func == NULL)
5907 return -1;
5908 for (i = mark; i < len; i++) {
5909 PyObject *result;
5910 PyObject *item;
5911
5912 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005913 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005914 if (result == NULL) {
5915 Pdata_clear(self->stack, i + 1);
5916 Py_SIZE(self->stack) = mark;
5917 return -1;
5918 }
5919 Py_DECREF(result);
5920 }
5921 Py_SIZE(self->stack) = mark;
5922 }
5923
5924 return 0;
5925}
5926
5927static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005928load_build(UnpicklerObject *self)
5929{
5930 PyObject *state, *inst, *slotstate;
5931 PyObject *setstate;
5932 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005933 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005934
5935 /* Stack is ... instance, state. We want to leave instance at
5936 * the stack top, possibly mutated via instance.__setstate__(state).
5937 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005938 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005939 return stack_underflow();
5940
5941 PDATA_POP(self->stack, state);
5942 if (state == NULL)
5943 return -1;
5944
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005945 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005946
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005947 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005948 if (setstate == NULL) {
5949 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5950 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005951 else {
5952 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005953 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005954 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005955 }
5956 else {
5957 PyObject *result;
5958
5959 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005960 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005961 Py_DECREF(setstate);
5962 if (result == NULL)
5963 return -1;
5964 Py_DECREF(result);
5965 return 0;
5966 }
5967
5968 /* A default __setstate__. First see whether state embeds a
5969 * slot state dict too (a proto 2 addition).
5970 */
5971 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5972 PyObject *tmp = state;
5973
5974 state = PyTuple_GET_ITEM(tmp, 0);
5975 slotstate = PyTuple_GET_ITEM(tmp, 1);
5976 Py_INCREF(state);
5977 Py_INCREF(slotstate);
5978 Py_DECREF(tmp);
5979 }
5980 else
5981 slotstate = NULL;
5982
5983 /* Set inst.__dict__ from the state dict (if any). */
5984 if (state != Py_None) {
5985 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005986 PyObject *d_key, *d_value;
5987 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005988 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005989
5990 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005991 PickleState *st = _Pickle_GetGlobalState();
5992 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993 goto error;
5994 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005995 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996 if (dict == NULL)
5997 goto error;
5998
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005999 i = 0;
6000 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6001 /* normally the keys for instance attributes are
6002 interned. we should try to do that here. */
6003 Py_INCREF(d_key);
6004 if (PyUnicode_CheckExact(d_key))
6005 PyUnicode_InternInPlace(&d_key);
6006 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6007 Py_DECREF(d_key);
6008 goto error;
6009 }
6010 Py_DECREF(d_key);
6011 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012 Py_DECREF(dict);
6013 }
6014
6015 /* Also set instance attributes from the slotstate dict (if any). */
6016 if (slotstate != NULL) {
6017 PyObject *d_key, *d_value;
6018 Py_ssize_t i;
6019
6020 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006021 PickleState *st = _Pickle_GetGlobalState();
6022 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006023 "slot state is not a dictionary");
6024 goto error;
6025 }
6026 i = 0;
6027 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6028 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6029 goto error;
6030 }
6031 }
6032
6033 if (0) {
6034 error:
6035 status = -1;
6036 }
6037
6038 Py_DECREF(state);
6039 Py_XDECREF(slotstate);
6040 return status;
6041}
6042
6043static int
6044load_mark(UnpicklerObject *self)
6045{
6046
6047 /* Note that we split the (pickle.py) stack into two stacks, an
6048 * object stack and a mark stack. Here we push a mark onto the
6049 * mark stack.
6050 */
6051
6052 if ((self->num_marks + 1) >= self->marks_size) {
6053 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006054
6055 /* Use the size_t type to check for overflow. */
6056 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006057 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006058 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059 PyErr_NoMemory();
6060 return -1;
6061 }
6062
6063 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006064 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006065 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006066 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6067 if (self->marks == NULL) {
6068 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069 PyErr_NoMemory();
6070 return -1;
6071 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006072 self->marks_size = (Py_ssize_t)alloc;
6073 }
6074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006075 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076
6077 return 0;
6078}
6079
6080static int
6081load_reduce(UnpicklerObject *self)
6082{
6083 PyObject *callable = NULL;
6084 PyObject *argtup = NULL;
6085 PyObject *obj = NULL;
6086
6087 PDATA_POP(self->stack, argtup);
6088 if (argtup == NULL)
6089 return -1;
6090 PDATA_POP(self->stack, callable);
6091 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006092 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006093 Py_DECREF(callable);
6094 }
6095 Py_DECREF(argtup);
6096
6097 if (obj == NULL)
6098 return -1;
6099
6100 PDATA_PUSH(self->stack, obj, -1);
6101 return 0;
6102}
6103
6104/* Just raises an error if we don't know the protocol specified. PROTO
6105 * is the first opcode for protocols >= 2.
6106 */
6107static int
6108load_proto(UnpicklerObject *self)
6109{
6110 char *s;
6111 int i;
6112
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006113 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006114 return -1;
6115
6116 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006117 if (i <= HIGHEST_PROTOCOL) {
6118 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006119 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006120 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121
6122 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6123 return -1;
6124}
6125
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006126static int
6127load_frame(UnpicklerObject *self)
6128{
6129 char *s;
6130 Py_ssize_t frame_len;
6131
6132 if (_Unpickler_Read(self, &s, 8) < 0)
6133 return -1;
6134
6135 frame_len = calc_binsize(s, 8);
6136 if (frame_len < 0) {
6137 PyErr_Format(PyExc_OverflowError,
6138 "FRAME length exceeds system's maximum of %zd bytes",
6139 PY_SSIZE_T_MAX);
6140 return -1;
6141 }
6142
6143 if (_Unpickler_Read(self, &s, frame_len) < 0)
6144 return -1;
6145
6146 /* Rewind to start of frame */
6147 self->next_read_idx -= frame_len;
6148 return 0;
6149}
6150
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006151static PyObject *
6152load(UnpicklerObject *self)
6153{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006154 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006155 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006156
6157 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006158 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006159 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006160 Pdata_clear(self->stack, 0);
6161
6162 /* Convenient macros for the dispatch while-switch loop just below. */
6163#define OP(opcode, load_func) \
6164 case opcode: if (load_func(self) < 0) break; continue;
6165
6166#define OP_ARG(opcode, load_func, arg) \
6167 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6168
6169 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006170 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 break;
6172
6173 switch ((enum opcode)s[0]) {
6174 OP(NONE, load_none)
6175 OP(BININT, load_binint)
6176 OP(BININT1, load_binint1)
6177 OP(BININT2, load_binint2)
6178 OP(INT, load_int)
6179 OP(LONG, load_long)
6180 OP_ARG(LONG1, load_counted_long, 1)
6181 OP_ARG(LONG4, load_counted_long, 4)
6182 OP(FLOAT, load_float)
6183 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006184 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6185 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6186 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6187 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6188 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189 OP(STRING, load_string)
6190 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006191 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6192 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6193 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006194 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6195 OP_ARG(TUPLE1, load_counted_tuple, 1)
6196 OP_ARG(TUPLE2, load_counted_tuple, 2)
6197 OP_ARG(TUPLE3, load_counted_tuple, 3)
6198 OP(TUPLE, load_tuple)
6199 OP(EMPTY_LIST, load_empty_list)
6200 OP(LIST, load_list)
6201 OP(EMPTY_DICT, load_empty_dict)
6202 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006203 OP(EMPTY_SET, load_empty_set)
6204 OP(ADDITEMS, load_additems)
6205 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 OP(OBJ, load_obj)
6207 OP(INST, load_inst)
6208 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006209 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006211 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006212 OP(APPEND, load_append)
6213 OP(APPENDS, load_appends)
6214 OP(BUILD, load_build)
6215 OP(DUP, load_dup)
6216 OP(BINGET, load_binget)
6217 OP(LONG_BINGET, load_long_binget)
6218 OP(GET, load_get)
6219 OP(MARK, load_mark)
6220 OP(BINPUT, load_binput)
6221 OP(LONG_BINPUT, load_long_binput)
6222 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006223 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006224 OP(POP, load_pop)
6225 OP(POP_MARK, load_pop_mark)
6226 OP(SETITEM, load_setitem)
6227 OP(SETITEMS, load_setitems)
6228 OP(PERSID, load_persid)
6229 OP(BINPERSID, load_binpersid)
6230 OP(REDUCE, load_reduce)
6231 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006232 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006233 OP_ARG(EXT1, load_extension, 1)
6234 OP_ARG(EXT2, load_extension, 2)
6235 OP_ARG(EXT4, load_extension, 4)
6236 OP_ARG(NEWTRUE, load_bool, Py_True)
6237 OP_ARG(NEWFALSE, load_bool, Py_False)
6238
6239 case STOP:
6240 break;
6241
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006242 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006243 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006244 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006245 }
6246 else {
6247 PickleState *st = _Pickle_GetGlobalState();
6248 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006249 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006250 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006251 return NULL;
6252 }
6253
6254 break; /* and we are done! */
6255 }
6256
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006257 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258 return NULL;
6259 }
6260
Victor Stinner2ae57e32013-10-31 13:39:23 +01006261 if (_Unpickler_SkipConsumed(self) < 0)
6262 return NULL;
6263
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006264 PDATA_POP(self->stack, value);
6265 return value;
6266}
6267
Larry Hastings61272b72014-01-07 12:41:53 -08006268/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006269
6270_pickle.Unpickler.load
6271
6272Load a pickle.
6273
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006274Read a pickled object representation from the open file object given
6275in the constructor, and return the reconstituted object hierarchy
6276specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006277[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006278
Larry Hastings3cceb382014-01-04 11:09:09 -08006279static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006280_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006281/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006282{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006283 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006284
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285 /* Check whether the Unpickler was initialized correctly. This prevents
6286 segfaulting if a subclass overridden __init__ with a function that does
6287 not call Unpickler.__init__(). Here, we simply ensure that self->read
6288 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006289 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006290 PickleState *st = _Pickle_GetGlobalState();
6291 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006292 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006293 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006294 return NULL;
6295 }
6296
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006297 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006298}
6299
6300/* The name of find_class() is misleading. In newer pickle protocols, this
6301 function is used for loading any global (i.e., functions), not just
6302 classes. The name is kept only for backward compatibility. */
6303
Larry Hastings61272b72014-01-07 12:41:53 -08006304/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006305
6306_pickle.Unpickler.find_class
6307
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006308 module_name: object
6309 global_name: object
6310 /
6311
6312Return an object from a specified module.
6313
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006314If necessary, the module will be imported. Subclasses may override
6315this method (e.g. to restrict unpickling of arbitrary classes and
6316functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006317
6318This method is called whenever a class or a function object is
6319needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006320[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006321
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006322static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006323_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6324 PyObject *module_name,
6325 PyObject *global_name)
6326/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006327{
6328 PyObject *global;
6329 PyObject *modules_dict;
6330 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006331 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006332
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006333 /* Try to map the old names used in Python 2.x to the new ones used in
6334 Python 3.x. We do this only with old pickle protocols and when the
6335 user has not disabled the feature. */
6336 if (self->proto < 3 && self->fix_imports) {
6337 PyObject *key;
6338 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006339 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006340
6341 /* Check if the global (i.e., a function or a class) was renamed
6342 or moved to another module. */
6343 key = PyTuple_Pack(2, module_name, global_name);
6344 if (key == NULL)
6345 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006346 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006347 Py_DECREF(key);
6348 if (item) {
6349 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6350 PyErr_Format(PyExc_RuntimeError,
6351 "_compat_pickle.NAME_MAPPING values should be "
6352 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6353 return NULL;
6354 }
6355 module_name = PyTuple_GET_ITEM(item, 0);
6356 global_name = PyTuple_GET_ITEM(item, 1);
6357 if (!PyUnicode_Check(module_name) ||
6358 !PyUnicode_Check(global_name)) {
6359 PyErr_Format(PyExc_RuntimeError,
6360 "_compat_pickle.NAME_MAPPING values should be "
6361 "pairs of str, not (%.200s, %.200s)",
6362 Py_TYPE(module_name)->tp_name,
6363 Py_TYPE(global_name)->tp_name);
6364 return NULL;
6365 }
6366 }
6367 else if (PyErr_Occurred()) {
6368 return NULL;
6369 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006370 else {
6371 /* Check if the module was renamed. */
6372 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6373 if (item) {
6374 if (!PyUnicode_Check(item)) {
6375 PyErr_Format(PyExc_RuntimeError,
6376 "_compat_pickle.IMPORT_MAPPING values should be "
6377 "strings, not %.200s", Py_TYPE(item)->tp_name);
6378 return NULL;
6379 }
6380 module_name = item;
6381 }
6382 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006383 return NULL;
6384 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006385 }
6386 }
6387
Victor Stinnerbb520202013-11-06 22:40:41 +01006388 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006389 if (modules_dict == NULL) {
6390 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006391 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006392 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006393
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006394 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006395 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006396 if (PyErr_Occurred())
6397 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006398 module = PyImport_Import(module_name);
6399 if (module == NULL)
6400 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006401 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006402 Py_DECREF(module);
6403 }
Victor Stinner121aab42011-09-29 23:40:53 +02006404 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006405 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006406 }
6407 return global;
6408}
6409
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006410/*[clinic input]
6411
6412_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6413
6414Returns size in memory, in bytes.
6415[clinic start generated code]*/
6416
6417static Py_ssize_t
6418_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6419/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6420{
6421 Py_ssize_t res;
6422
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006423 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006424 if (self->memo != NULL)
6425 res += self->memo_size * sizeof(PyObject *);
6426 if (self->marks != NULL)
6427 res += self->marks_size * sizeof(Py_ssize_t);
6428 if (self->input_line != NULL)
6429 res += strlen(self->input_line) + 1;
6430 if (self->encoding != NULL)
6431 res += strlen(self->encoding) + 1;
6432 if (self->errors != NULL)
6433 res += strlen(self->errors) + 1;
6434 return res;
6435}
6436
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006437static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006438 _PICKLE_UNPICKLER_LOAD_METHODDEF
6439 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006440 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006441 {NULL, NULL} /* sentinel */
6442};
6443
6444static void
6445Unpickler_dealloc(UnpicklerObject *self)
6446{
6447 PyObject_GC_UnTrack((PyObject *)self);
6448 Py_XDECREF(self->readline);
6449 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006450 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006451 Py_XDECREF(self->stack);
6452 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006453 if (self->buffer.buf != NULL) {
6454 PyBuffer_Release(&self->buffer);
6455 self->buffer.buf = NULL;
6456 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006457
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006458 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006460 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006461 PyMem_Free(self->encoding);
6462 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006463
6464 Py_TYPE(self)->tp_free((PyObject *)self);
6465}
6466
6467static int
6468Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6469{
6470 Py_VISIT(self->readline);
6471 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006472 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473 Py_VISIT(self->stack);
6474 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475 return 0;
6476}
6477
6478static int
6479Unpickler_clear(UnpicklerObject *self)
6480{
6481 Py_CLEAR(self->readline);
6482 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006483 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006484 Py_CLEAR(self->stack);
6485 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006486 if (self->buffer.buf != NULL) {
6487 PyBuffer_Release(&self->buffer);
6488 self->buffer.buf = NULL;
6489 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006490
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006491 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006492 PyMem_Free(self->marks);
6493 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006494 PyMem_Free(self->input_line);
6495 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006496 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006497 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006498 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006499 self->errors = NULL;
6500
6501 return 0;
6502}
6503
Larry Hastings61272b72014-01-07 12:41:53 -08006504/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006505
6506_pickle.Unpickler.__init__
6507
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006508 file: object
6509 *
6510 fix_imports: bool = True
6511 encoding: str = 'ASCII'
6512 errors: str = 'strict'
6513
6514This takes a binary file for reading a pickle data stream.
6515
6516The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006517protocol argument is needed. Bytes past the pickled object's
6518representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006519
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006520The argument *file* must have two methods, a read() method that takes
6521an integer argument, and a readline() method that requires no
6522arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006523binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006524other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006525
6526Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00006527which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006528generated by Python 2. If *fix_imports* is True, pickle will try to
6529map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006530*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006531instances pickled by Python 2; these default to 'ASCII' and 'strict',
6532respectively. The *encoding* can be 'bytes' to read these 8-bit
6533string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006534[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006535
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006536static int
Larry Hastings89964c42015-04-14 18:07:59 -04006537_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6538 int fix_imports, const char *encoding,
6539 const char *errors)
Martin Panter46f50722016-05-26 05:35:26 +00006540/*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006541{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006542 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006543
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 /* In case of multiple __init__() calls, clear previous content. */
6545 if (self->read != NULL)
6546 (void)Unpickler_clear(self);
6547
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006548 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006549 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006551 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006552 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006553
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006554 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006555 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006556 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006557
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006558 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006559 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6560 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006561 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006562 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006563 }
6564 else {
6565 self->pers_func = NULL;
6566 }
6567
6568 self->stack = (Pdata *)Pdata_New();
6569 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006570 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006571
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006572 self->memo_size = 32;
6573 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006574 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006575 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006576
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006577 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006578
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579 return 0;
6580}
6581
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006582
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006583/* Define a proxy object for the Unpickler's internal memo object. This is to
6584 * avoid breaking code like:
6585 * unpickler.memo.clear()
6586 * and
6587 * unpickler.memo = saved_memo
6588 * Is this a good idea? Not really, but we don't want to break code that uses
6589 * it. Note that we don't implement the entire mapping API here. This is
6590 * intentional, as these should be treated as black-box implementation details.
6591 *
6592 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006593 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006594 */
6595
Larry Hastings61272b72014-01-07 12:41:53 -08006596/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597_pickle.UnpicklerMemoProxy.clear
6598
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006599Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006600[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006601
Larry Hastings3cceb382014-01-04 11:09:09 -08006602static PyObject *
6603_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006604/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006605{
6606 _Unpickler_MemoCleanup(self->unpickler);
6607 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6608 if (self->unpickler->memo == NULL)
6609 return NULL;
6610 Py_RETURN_NONE;
6611}
6612
Larry Hastings61272b72014-01-07 12:41:53 -08006613/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006614_pickle.UnpicklerMemoProxy.copy
6615
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006616Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006617[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006618
Larry Hastings3cceb382014-01-04 11:09:09 -08006619static PyObject *
6620_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006621/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006622{
6623 Py_ssize_t i;
6624 PyObject *new_memo = PyDict_New();
6625 if (new_memo == NULL)
6626 return NULL;
6627
6628 for (i = 0; i < self->unpickler->memo_size; i++) {
6629 int status;
6630 PyObject *key, *value;
6631
6632 value = self->unpickler->memo[i];
6633 if (value == NULL)
6634 continue;
6635
6636 key = PyLong_FromSsize_t(i);
6637 if (key == NULL)
6638 goto error;
6639 status = PyDict_SetItem(new_memo, key, value);
6640 Py_DECREF(key);
6641 if (status < 0)
6642 goto error;
6643 }
6644 return new_memo;
6645
6646error:
6647 Py_DECREF(new_memo);
6648 return NULL;
6649}
6650
Larry Hastings61272b72014-01-07 12:41:53 -08006651/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006652_pickle.UnpicklerMemoProxy.__reduce__
6653
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006654Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006655[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006656
Larry Hastings3cceb382014-01-04 11:09:09 -08006657static PyObject *
6658_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006659/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006660{
6661 PyObject *reduce_value;
6662 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006663 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006664 if (contents == NULL)
6665 return NULL;
6666
6667 reduce_value = PyTuple_New(2);
6668 if (reduce_value == NULL) {
6669 Py_DECREF(contents);
6670 return NULL;
6671 }
6672 constructor_args = PyTuple_New(1);
6673 if (constructor_args == NULL) {
6674 Py_DECREF(contents);
6675 Py_DECREF(reduce_value);
6676 return NULL;
6677 }
6678 PyTuple_SET_ITEM(constructor_args, 0, contents);
6679 Py_INCREF((PyObject *)&PyDict_Type);
6680 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6681 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6682 return reduce_value;
6683}
6684
6685static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006686 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6687 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6688 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006689 {NULL, NULL} /* sentinel */
6690};
6691
6692static void
6693UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6694{
6695 PyObject_GC_UnTrack(self);
6696 Py_XDECREF(self->unpickler);
6697 PyObject_GC_Del((PyObject *)self);
6698}
6699
6700static int
6701UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6702 visitproc visit, void *arg)
6703{
6704 Py_VISIT(self->unpickler);
6705 return 0;
6706}
6707
6708static int
6709UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6710{
6711 Py_CLEAR(self->unpickler);
6712 return 0;
6713}
6714
6715static PyTypeObject UnpicklerMemoProxyType = {
6716 PyVarObject_HEAD_INIT(NULL, 0)
6717 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6718 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6719 0,
6720 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6721 0, /* tp_print */
6722 0, /* tp_getattr */
6723 0, /* tp_setattr */
6724 0, /* tp_compare */
6725 0, /* tp_repr */
6726 0, /* tp_as_number */
6727 0, /* tp_as_sequence */
6728 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006729 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006730 0, /* tp_call */
6731 0, /* tp_str */
6732 PyObject_GenericGetAttr, /* tp_getattro */
6733 PyObject_GenericSetAttr, /* tp_setattro */
6734 0, /* tp_as_buffer */
6735 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6736 0, /* tp_doc */
6737 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6738 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6739 0, /* tp_richcompare */
6740 0, /* tp_weaklistoffset */
6741 0, /* tp_iter */
6742 0, /* tp_iternext */
6743 unpicklerproxy_methods, /* tp_methods */
6744};
6745
6746static PyObject *
6747UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6748{
6749 UnpicklerMemoProxyObject *self;
6750
6751 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6752 &UnpicklerMemoProxyType);
6753 if (self == NULL)
6754 return NULL;
6755 Py_INCREF(unpickler);
6756 self->unpickler = unpickler;
6757 PyObject_GC_Track(self);
6758 return (PyObject *)self;
6759}
6760
6761/*****************************************************************************/
6762
6763
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006764static PyObject *
6765Unpickler_get_memo(UnpicklerObject *self)
6766{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006767 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006768}
6769
6770static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006771Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006772{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006773 PyObject **new_memo;
6774 Py_ssize_t new_memo_size = 0;
6775 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006776
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006777 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006778 PyErr_SetString(PyExc_TypeError,
6779 "attribute deletion is not supported");
6780 return -1;
6781 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006782
6783 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6784 UnpicklerObject *unpickler =
6785 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6786
6787 new_memo_size = unpickler->memo_size;
6788 new_memo = _Unpickler_NewMemo(new_memo_size);
6789 if (new_memo == NULL)
6790 return -1;
6791
6792 for (i = 0; i < new_memo_size; i++) {
6793 Py_XINCREF(unpickler->memo[i]);
6794 new_memo[i] = unpickler->memo[i];
6795 }
6796 }
6797 else if (PyDict_Check(obj)) {
6798 Py_ssize_t i = 0;
6799 PyObject *key, *value;
6800
6801 new_memo_size = PyDict_Size(obj);
6802 new_memo = _Unpickler_NewMemo(new_memo_size);
6803 if (new_memo == NULL)
6804 return -1;
6805
6806 while (PyDict_Next(obj, &i, &key, &value)) {
6807 Py_ssize_t idx;
6808 if (!PyLong_Check(key)) {
6809 PyErr_SetString(PyExc_TypeError,
6810 "memo key must be integers");
6811 goto error;
6812 }
6813 idx = PyLong_AsSsize_t(key);
6814 if (idx == -1 && PyErr_Occurred())
6815 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006816 if (idx < 0) {
6817 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006818 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006819 goto error;
6820 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006821 if (_Unpickler_MemoPut(self, idx, value) < 0)
6822 goto error;
6823 }
6824 }
6825 else {
6826 PyErr_Format(PyExc_TypeError,
6827 "'memo' attribute must be an UnpicklerMemoProxy object"
6828 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006829 return -1;
6830 }
6831
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006832 _Unpickler_MemoCleanup(self);
6833 self->memo_size = new_memo_size;
6834 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006835
6836 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006837
6838 error:
6839 if (new_memo_size) {
6840 i = new_memo_size;
6841 while (--i >= 0) {
6842 Py_XDECREF(new_memo[i]);
6843 }
6844 PyMem_FREE(new_memo);
6845 }
6846 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006847}
6848
6849static PyObject *
6850Unpickler_get_persload(UnpicklerObject *self)
6851{
6852 if (self->pers_func == NULL)
6853 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6854 else
6855 Py_INCREF(self->pers_func);
6856 return self->pers_func;
6857}
6858
6859static int
6860Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6861{
6862 PyObject *tmp;
6863
6864 if (value == NULL) {
6865 PyErr_SetString(PyExc_TypeError,
6866 "attribute deletion is not supported");
6867 return -1;
6868 }
6869 if (!PyCallable_Check(value)) {
6870 PyErr_SetString(PyExc_TypeError,
6871 "persistent_load must be a callable taking "
6872 "one argument");
6873 return -1;
6874 }
6875
6876 tmp = self->pers_func;
6877 Py_INCREF(value);
6878 self->pers_func = value;
6879 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6880
6881 return 0;
6882}
6883
6884static PyGetSetDef Unpickler_getsets[] = {
6885 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6886 {"persistent_load", (getter)Unpickler_get_persload,
6887 (setter)Unpickler_set_persload},
6888 {NULL}
6889};
6890
6891static PyTypeObject Unpickler_Type = {
6892 PyVarObject_HEAD_INIT(NULL, 0)
6893 "_pickle.Unpickler", /*tp_name*/
6894 sizeof(UnpicklerObject), /*tp_basicsize*/
6895 0, /*tp_itemsize*/
6896 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6897 0, /*tp_print*/
6898 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006899 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006900 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006901 0, /*tp_repr*/
6902 0, /*tp_as_number*/
6903 0, /*tp_as_sequence*/
6904 0, /*tp_as_mapping*/
6905 0, /*tp_hash*/
6906 0, /*tp_call*/
6907 0, /*tp_str*/
6908 0, /*tp_getattro*/
6909 0, /*tp_setattro*/
6910 0, /*tp_as_buffer*/
6911 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006912 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006913 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6914 (inquiry)Unpickler_clear, /*tp_clear*/
6915 0, /*tp_richcompare*/
6916 0, /*tp_weaklistoffset*/
6917 0, /*tp_iter*/
6918 0, /*tp_iternext*/
6919 Unpickler_methods, /*tp_methods*/
6920 0, /*tp_members*/
6921 Unpickler_getsets, /*tp_getset*/
6922 0, /*tp_base*/
6923 0, /*tp_dict*/
6924 0, /*tp_descr_get*/
6925 0, /*tp_descr_set*/
6926 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006927 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006928 PyType_GenericAlloc, /*tp_alloc*/
6929 PyType_GenericNew, /*tp_new*/
6930 PyObject_GC_Del, /*tp_free*/
6931 0, /*tp_is_gc*/
6932};
6933
Larry Hastings61272b72014-01-07 12:41:53 -08006934/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006935
6936_pickle.dump
6937
6938 obj: object
6939 file: object
6940 protocol: object = NULL
6941 *
6942 fix_imports: bool = True
6943
6944Write a pickled representation of obj to the open file object file.
6945
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006946This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6947be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006948
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006949The optional *protocol* argument tells the pickler to use the given
6950protocol supported protocols are 0, 1, 2, 3 and 4. The default
6951protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006952
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006953Specifying a negative protocol version selects the highest protocol
6954version supported. The higher the protocol used, the more recent the
6955version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006956
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006957The *file* argument must have a write() method that accepts a single
6958bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00006959writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006960this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006961
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006962If *fix_imports* is True and protocol is less than 3, pickle will try
6963to map the new Python 3 names to the old module names used in Python
69642, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006965[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006966
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006967static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006968_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -04006969 PyObject *protocol, int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006970/*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006971{
6972 PicklerObject *pickler = _Pickler_New();
6973
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006974 if (pickler == NULL)
6975 return NULL;
6976
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006977 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006978 goto error;
6979
6980 if (_Pickler_SetOutputStream(pickler, file) < 0)
6981 goto error;
6982
6983 if (dump(pickler, obj) < 0)
6984 goto error;
6985
6986 if (_Pickler_FlushToFile(pickler) < 0)
6987 goto error;
6988
6989 Py_DECREF(pickler);
6990 Py_RETURN_NONE;
6991
6992 error:
6993 Py_XDECREF(pickler);
6994 return NULL;
6995}
6996
Larry Hastings61272b72014-01-07 12:41:53 -08006997/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006998
6999_pickle.dumps
7000
7001 obj: object
7002 protocol: object = NULL
7003 *
7004 fix_imports: bool = True
7005
7006Return the pickled representation of the object as a bytes object.
7007
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007008The optional *protocol* argument tells the pickler to use the given
7009protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7010protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007011
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007012Specifying a negative protocol version selects the highest protocol
7013version supported. The higher the protocol used, the more recent the
7014version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007015
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007016If *fix_imports* is True and *protocol* is less than 3, pickle will
7017try to map the new Python 3 names to the old module names used in
7018Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007019[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007020
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007022_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -04007023 int fix_imports)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007024/*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007025{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007026 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007027 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007028
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007029 if (pickler == NULL)
7030 return NULL;
7031
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007032 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007033 goto error;
7034
7035 if (dump(pickler, obj) < 0)
7036 goto error;
7037
7038 result = _Pickler_GetString(pickler);
7039 Py_DECREF(pickler);
7040 return result;
7041
7042 error:
7043 Py_XDECREF(pickler);
7044 return NULL;
7045}
7046
Larry Hastings61272b72014-01-07 12:41:53 -08007047/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007048
7049_pickle.load
7050
7051 file: object
7052 *
7053 fix_imports: bool = True
7054 encoding: str = 'ASCII'
7055 errors: str = 'strict'
7056
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007057Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007059This is equivalent to ``Unpickler(file).load()``, but may be more
7060efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007061
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007062The protocol version of the pickle is detected automatically, so no
7063protocol argument is needed. Bytes past the pickled object's
7064representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007066The argument *file* must have two methods, a read() method that takes
7067an integer argument, and a readline() method that requires no
7068arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007069binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007070other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007072Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007073which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007074generated by Python 2. If *fix_imports* is True, pickle will try to
7075map the old Python 2 names to the new names used in Python 3. The
7076*encoding* and *errors* tell pickle how to decode 8-bit string
7077instances pickled by Python 2; these default to 'ASCII' and 'strict',
7078respectively. The *encoding* can be 'bytes' to read these 8-bit
7079string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007080[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007081
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007083_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007084 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007085/*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007086{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007087 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007088 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007090 if (unpickler == NULL)
7091 return NULL;
7092
7093 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7094 goto error;
7095
7096 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7097 goto error;
7098
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007099 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007100
7101 result = load(unpickler);
7102 Py_DECREF(unpickler);
7103 return result;
7104
7105 error:
7106 Py_XDECREF(unpickler);
7107 return NULL;
7108}
7109
Larry Hastings61272b72014-01-07 12:41:53 -08007110/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007111
7112_pickle.loads
7113
7114 data: object
7115 *
7116 fix_imports: bool = True
7117 encoding: str = 'ASCII'
7118 errors: str = 'strict'
7119
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007120Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007122The protocol version of the pickle is detected automatically, so no
7123protocol argument is needed. Bytes past the pickled object's
7124representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007125
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007126Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
Martin Panter46f50722016-05-26 05:35:26 +00007127which are used to control compatibility support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007128generated by Python 2. If *fix_imports* is True, pickle will try to
7129map the old Python 2 names to the new names used in Python 3. The
7130*encoding* and *errors* tell pickle how to decode 8-bit string
7131instances pickled by Python 2; these default to 'ASCII' and 'strict',
7132respectively. The *encoding* can be 'bytes' to read these 8-bit
7133string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007134[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007135
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007136static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007137_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -04007138 const char *encoding, const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007139/*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007140{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007141 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007142 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007143
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007144 if (unpickler == NULL)
7145 return NULL;
7146
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007147 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007148 goto error;
7149
7150 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7151 goto error;
7152
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007153 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007154
7155 result = load(unpickler);
7156 Py_DECREF(unpickler);
7157 return result;
7158
7159 error:
7160 Py_XDECREF(unpickler);
7161 return NULL;
7162}
7163
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007164static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007165 _PICKLE_DUMP_METHODDEF
7166 _PICKLE_DUMPS_METHODDEF
7167 _PICKLE_LOAD_METHODDEF
7168 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007169 {NULL, NULL} /* sentinel */
7170};
7171
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007172static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007173pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007174{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007175 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007176 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007177}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007178
Stefan Krahf483b0f2013-12-14 13:43:10 +01007179static void
7180pickle_free(PyObject *m)
7181{
7182 _Pickle_ClearState(_Pickle_GetState(m));
7183}
7184
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007185static int
7186pickle_traverse(PyObject *m, visitproc visit, void *arg)
7187{
7188 PickleState *st = _Pickle_GetState(m);
7189 Py_VISIT(st->PickleError);
7190 Py_VISIT(st->PicklingError);
7191 Py_VISIT(st->UnpicklingError);
7192 Py_VISIT(st->dispatch_table);
7193 Py_VISIT(st->extension_registry);
7194 Py_VISIT(st->extension_cache);
7195 Py_VISIT(st->inverted_registry);
7196 Py_VISIT(st->name_mapping_2to3);
7197 Py_VISIT(st->import_mapping_2to3);
7198 Py_VISIT(st->name_mapping_3to2);
7199 Py_VISIT(st->import_mapping_3to2);
7200 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007201 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007202 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007203}
7204
7205static struct PyModuleDef _picklemodule = {
7206 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007207 "_pickle", /* m_name */
7208 pickle_module_doc, /* m_doc */
7209 sizeof(PickleState), /* m_size */
7210 pickle_methods, /* m_methods */
7211 NULL, /* m_reload */
7212 pickle_traverse, /* m_traverse */
7213 pickle_clear, /* m_clear */
7214 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007215};
7216
7217PyMODINIT_FUNC
7218PyInit__pickle(void)
7219{
7220 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007221 PickleState *st;
7222
7223 m = PyState_FindModule(&_picklemodule);
7224 if (m) {
7225 Py_INCREF(m);
7226 return m;
7227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007228
7229 if (PyType_Ready(&Unpickler_Type) < 0)
7230 return NULL;
7231 if (PyType_Ready(&Pickler_Type) < 0)
7232 return NULL;
7233 if (PyType_Ready(&Pdata_Type) < 0)
7234 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007235 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7236 return NULL;
7237 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7238 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007239
7240 /* Create the module and add the functions. */
7241 m = PyModule_Create(&_picklemodule);
7242 if (m == NULL)
7243 return NULL;
7244
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007245 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007246 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7247 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007248 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007249 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7250 return NULL;
7251
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007252 st = _Pickle_GetState(m);
7253
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007254 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007255 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7256 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007257 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007258 st->PicklingError = \
7259 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7260 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007261 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007262 st->UnpicklingError = \
7263 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7264 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007265 return NULL;
7266
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007267 Py_INCREF(st->PickleError);
7268 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007269 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007270 Py_INCREF(st->PicklingError);
7271 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007272 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007273 Py_INCREF(st->UnpicklingError);
7274 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007275 return NULL;
7276
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007277 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007278 return NULL;
7279
7280 return m;
7281}