blob: 3ad9a97641073edc213229eadbb2c93bdee5fb38 [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{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800462 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000463 if (Py_SIZE(self) == 0) {
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
553 where the current frame begins. -1 if there
554 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{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000849 Py_CLEAR(self->output_buffer);
850 self->output_buffer =
851 PyBytes_FromStringAndSize(NULL, self->max_output_len);
852 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000853 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000854 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100855 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000856 return 0;
857}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000858
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100859static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100860_write_size64(char *out, size_t value)
861{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200862 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800863
864 assert(sizeof(size_t) <= 8);
865
866 for (i = 0; i < sizeof(size_t); i++) {
867 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
868 }
869 for (i = sizeof(size_t); i < 8; i++) {
870 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800871 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100872}
873
874static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100875_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
876{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100877 qdata[0] = FRAME;
878 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100879}
880
881static int
882_Pickler_CommitFrame(PicklerObject *self)
883{
884 size_t frame_len;
885 char *qdata;
886
887 if (!self->framing || self->frame_start == -1)
888 return 0;
889 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
890 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
891 _Pickler_WriteFrameHeader(self, qdata, frame_len);
892 self->frame_start = -1;
893 return 0;
894}
895
896static int
897_Pickler_OpcodeBoundary(PicklerObject *self)
898{
899 Py_ssize_t frame_len;
900
901 if (!self->framing || self->frame_start == -1)
902 return 0;
903 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
904 if (frame_len >= FRAME_SIZE_TARGET)
905 return _Pickler_CommitFrame(self);
906 else
907 return 0;
908}
909
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000910static PyObject *
911_Pickler_GetString(PicklerObject *self)
912{
913 PyObject *output_buffer = self->output_buffer;
914
915 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100916
917 if (_Pickler_CommitFrame(self))
918 return NULL;
919
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000920 self->output_buffer = NULL;
921 /* Resize down to exact size */
922 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
923 return NULL;
924 return output_buffer;
925}
926
927static int
928_Pickler_FlushToFile(PicklerObject *self)
929{
930 PyObject *output, *result;
931
932 assert(self->write != NULL);
933
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100934 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000935 output = _Pickler_GetString(self);
936 if (output == NULL)
937 return -1;
938
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800939 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000940 Py_XDECREF(result);
941 return (result == NULL) ? -1 : 0;
942}
943
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200944static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100945_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000946{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100947 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000948 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000950
951 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100952 need_new_frame = (self->framing && self->frame_start == -1);
953
954 if (need_new_frame)
955 n = data_len + FRAME_HEADER_SIZE;
956 else
957 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000958
959 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 if (required > self->max_output_len) {
961 /* Make place in buffer for the pickle chunk */
962 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
963 PyErr_NoMemory();
964 return -1;
965 }
966 self->max_output_len = (self->output_len + n) / 2 * 3;
967 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
968 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000969 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 if (need_new_frame) {
972 /* Setup new frame */
973 Py_ssize_t frame_start = self->output_len;
974 self->frame_start = frame_start;
975 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
976 /* Write an invalid value, for debugging */
977 buffer[frame_start + i] = 0xFE;
978 }
979 self->output_len += FRAME_HEADER_SIZE;
980 }
981 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000982 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100983 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 buffer[self->output_len + i] = s[i];
985 }
986 }
987 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100988 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000989 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100990 self->output_len += data_len;
991 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000992}
993
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000994static PicklerObject *
995_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000996{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000997 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000998
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1000 if (self == NULL)
1001 return NULL;
1002
1003 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001004 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001005 self->write = NULL;
1006 self->proto = 0;
1007 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001008 self->framing = 0;
1009 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 self->fast = 0;
1011 self->fast_nesting = 0;
1012 self->fix_imports = 0;
1013 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001014 self->max_output_len = WRITE_BUF_SIZE;
1015 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001016
1017 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001018 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1019 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001020
1021 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001022 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023 return NULL;
1024 }
1025 return self;
1026}
1027
1028static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001029_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001030{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001031 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001033 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001035 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001037 proto = PyLong_AsLong(protocol);
1038 if (proto < 0) {
1039 if (proto == -1 && PyErr_Occurred())
1040 return -1;
1041 proto = HIGHEST_PROTOCOL;
1042 }
1043 else if (proto > HIGHEST_PROTOCOL) {
1044 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1045 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001047 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 self->bin = proto > 0;
1051 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 return 0;
1053}
1054
1055/* Returns -1 (with an exception set) on failure, 0 on success. This may
1056 be called once on a freshly created Pickler. */
1057static int
1058_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1059{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001060 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001061 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001062 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 if (self->write == NULL) {
1064 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1065 PyErr_SetString(PyExc_TypeError,
1066 "file must have a 'write' attribute");
1067 return -1;
1068 }
1069
1070 return 0;
1071}
1072
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001073/* Returns the size of the input on success, -1 on failure. This takes its
1074 own reference to `input`. */
1075static Py_ssize_t
1076_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1077{
1078 if (self->buffer.buf != NULL)
1079 PyBuffer_Release(&self->buffer);
1080 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1081 return -1;
1082 self->input_buffer = self->buffer.buf;
1083 self->input_len = self->buffer.len;
1084 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001085 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086 return self->input_len;
1087}
1088
Antoine Pitrou04248a82010-10-12 20:51:21 +00001089static int
1090_Unpickler_SkipConsumed(UnpicklerObject *self)
1091{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001092 Py_ssize_t consumed;
1093 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001094
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001095 consumed = self->next_read_idx - self->prefetched_idx;
1096 if (consumed <= 0)
1097 return 0;
1098
1099 assert(self->peek); /* otherwise we did something wrong */
1100 /* This makes an useless copy... */
1101 r = PyObject_CallFunction(self->read, "n", consumed);
1102 if (r == NULL)
1103 return -1;
1104 Py_DECREF(r);
1105
1106 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107 return 0;
1108}
1109
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001110static const Py_ssize_t READ_WHOLE_LINE = -1;
1111
1112/* If reading from a file, we need to only pull the bytes we need, since there
1113 may be multiple pickle objects arranged contiguously in the same input
1114 buffer.
1115
1116 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1117 bytes from the input stream/buffer.
1118
1119 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1120 failure; on success, returns the number of bytes read from the file.
1121
1122 On success, self->input_len will be 0; this is intentional so that when
1123 unpickling from a file, the "we've run out of data" code paths will trigger,
1124 causing the Unpickler to go back to the file for more data. Use the returned
1125 size to tell you how much data you can process. */
1126static Py_ssize_t
1127_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1128{
1129 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001130 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001131
1132 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001133
Antoine Pitrou04248a82010-10-12 20:51:21 +00001134 if (_Unpickler_SkipConsumed(self) < 0)
1135 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001136
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001137 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001138 PyObject *empty_tuple = PyTuple_New(0);
1139 data = PyObject_Call(self->readline, empty_tuple, NULL);
1140 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001141 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001142 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001143 PyObject *len;
1144 /* Prefetch some data without advancing the file pointer, if possible */
1145 if (self->peek && n < PREFETCH) {
1146 len = PyLong_FromSsize_t(PREFETCH);
1147 if (len == NULL)
1148 return -1;
1149 data = _Pickle_FastCall(self->peek, len);
1150 if (data == NULL) {
1151 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1152 return -1;
1153 /* peek() is probably not supported by the given file object */
1154 PyErr_Clear();
1155 Py_CLEAR(self->peek);
1156 }
1157 else {
1158 read_size = _Unpickler_SetStringInput(self, data);
1159 Py_DECREF(data);
1160 self->prefetched_idx = 0;
1161 if (n <= read_size)
1162 return n;
1163 }
1164 }
1165 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001166 if (len == NULL)
1167 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001168 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001169 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001170 if (data == NULL)
1171 return -1;
1172
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001173 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001174 Py_DECREF(data);
1175 return read_size;
1176}
1177
1178/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1179
1180 This should be used for all data reads, rather than accessing the unpickler's
1181 input buffer directly. This method deals correctly with reading from input
1182 streams, which the input buffer doesn't deal with.
1183
1184 Note that when reading from a file-like object, self->next_read_idx won't
1185 be updated (it should remain at 0 for the entire unpickling process). You
1186 should use this function's return value to know how many bytes you can
1187 consume.
1188
1189 Returns -1 (with an exception set) on failure. On success, return the
1190 number of chars read. */
1191static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001192_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001193{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001194 Py_ssize_t num_read;
1195
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196 if (self->next_read_idx + n <= self->input_len) {
1197 *s = self->input_buffer + self->next_read_idx;
1198 self->next_read_idx += n;
1199 return n;
1200 }
1201 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001202 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001203 return -1;
1204 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001205 num_read = _Unpickler_ReadFromFile(self, n);
1206 if (num_read < 0)
1207 return -1;
1208 if (num_read < n) {
1209 PyErr_Format(PyExc_EOFError, "Ran out of input");
1210 return -1;
1211 }
1212 *s = self->input_buffer;
1213 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001214 return n;
1215}
1216
1217static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001218_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1219 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001220{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001221 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001222 if (input_line == NULL) {
1223 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001224 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001225 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001226
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001227 memcpy(input_line, line, len);
1228 input_line[len] = '\0';
1229 self->input_line = input_line;
1230 *result = self->input_line;
1231 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001232}
1233
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001234/* Read a line from the input stream/buffer. If we run off the end of the input
1235 before hitting \n, return the data we found.
1236
1237 Returns the number of chars read, or -1 on failure. */
1238static Py_ssize_t
1239_Unpickler_Readline(UnpicklerObject *self, char **result)
1240{
1241 Py_ssize_t i, num_read;
1242
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001243 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001244 if (self->input_buffer[i] == '\n') {
1245 char *line_start = self->input_buffer + self->next_read_idx;
1246 num_read = i - self->next_read_idx + 1;
1247 self->next_read_idx = i + 1;
1248 return _Unpickler_CopyLine(self, line_start, num_read, result);
1249 }
1250 }
1251 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001252 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1253 if (num_read < 0)
1254 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001255 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001256 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001257 }
Victor Stinner121aab42011-09-29 23:40:53 +02001258
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001259 /* If we get here, we've run off the end of the input string. Return the
1260 remaining string and let the caller figure it out. */
1261 *result = self->input_buffer + self->next_read_idx;
1262 num_read = i - self->next_read_idx;
1263 self->next_read_idx = i;
1264 return num_read;
1265}
1266
1267/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1268 will be modified in place. */
1269static int
1270_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1271{
1272 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273
1274 assert(new_size > self->memo_size);
1275
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001276 PyMem_RESIZE(self->memo, PyObject *, new_size);
1277 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001278 PyErr_NoMemory();
1279 return -1;
1280 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001281 for (i = self->memo_size; i < new_size; i++)
1282 self->memo[i] = NULL;
1283 self->memo_size = new_size;
1284 return 0;
1285}
1286
1287/* Returns NULL if idx is out of bounds. */
1288static PyObject *
1289_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1290{
1291 if (idx < 0 || idx >= self->memo_size)
1292 return NULL;
1293
1294 return self->memo[idx];
1295}
1296
1297/* Returns -1 (with an exception set) on failure, 0 on success.
1298 This takes its own reference to `value`. */
1299static int
1300_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1301{
1302 PyObject *old_item;
1303
1304 if (idx >= self->memo_size) {
1305 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1306 return -1;
1307 assert(idx < self->memo_size);
1308 }
1309 Py_INCREF(value);
1310 old_item = self->memo[idx];
1311 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001312 if (old_item != NULL) {
1313 Py_DECREF(old_item);
1314 }
1315 else {
1316 self->memo_len++;
1317 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001318 return 0;
1319}
1320
1321static PyObject **
1322_Unpickler_NewMemo(Py_ssize_t new_size)
1323{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001324 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001325 if (memo == NULL) {
1326 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001327 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001328 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001329 memset(memo, 0, new_size * sizeof(PyObject *));
1330 return memo;
1331}
1332
1333/* Free the unpickler's memo, taking care to decref any items left in it. */
1334static void
1335_Unpickler_MemoCleanup(UnpicklerObject *self)
1336{
1337 Py_ssize_t i;
1338 PyObject **memo = self->memo;
1339
1340 if (self->memo == NULL)
1341 return;
1342 self->memo = NULL;
1343 i = self->memo_size;
1344 while (--i >= 0) {
1345 Py_XDECREF(memo[i]);
1346 }
1347 PyMem_FREE(memo);
1348}
1349
1350static UnpicklerObject *
1351_Unpickler_New(void)
1352{
1353 UnpicklerObject *self;
1354
1355 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1356 if (self == NULL)
1357 return NULL;
1358
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001359 self->pers_func = NULL;
1360 self->input_buffer = NULL;
1361 self->input_line = NULL;
1362 self->input_len = 0;
1363 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001364 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001365 self->read = NULL;
1366 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001367 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001368 self->encoding = NULL;
1369 self->errors = NULL;
1370 self->marks = NULL;
1371 self->num_marks = 0;
1372 self->marks_size = 0;
1373 self->proto = 0;
1374 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001375 memset(&self->buffer, 0, sizeof(Py_buffer));
1376 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001377 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001378 self->memo = _Unpickler_NewMemo(self->memo_size);
1379 self->stack = (Pdata *)Pdata_New();
1380
1381 if (self->memo == NULL || self->stack == NULL) {
1382 Py_DECREF(self);
1383 return NULL;
1384 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001385
1386 return self;
1387}
1388
1389/* Returns -1 (with an exception set) on failure, 0 on success. This may
1390 be called once on a freshly created Pickler. */
1391static int
1392_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1393{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001394 _Py_IDENTIFIER(peek);
1395 _Py_IDENTIFIER(read);
1396 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001397
1398 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001399 if (self->peek == NULL) {
1400 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1401 PyErr_Clear();
1402 else
1403 return -1;
1404 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001405 self->read = _PyObject_GetAttrId(file, &PyId_read);
1406 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001407 if (self->readline == NULL || self->read == NULL) {
1408 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1409 PyErr_SetString(PyExc_TypeError,
1410 "file must have 'read' and 'readline' attributes");
1411 Py_CLEAR(self->read);
1412 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001413 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001414 return -1;
1415 }
1416 return 0;
1417}
1418
1419/* Returns -1 (with an exception set) on failure, 0 on success. This may
1420 be called once on a freshly created Pickler. */
1421static int
1422_Unpickler_SetInputEncoding(UnpicklerObject *self,
1423 const char *encoding,
1424 const char *errors)
1425{
1426 if (encoding == NULL)
1427 encoding = "ASCII";
1428 if (errors == NULL)
1429 errors = "strict";
1430
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001431 self->encoding = _PyMem_Strdup(encoding);
1432 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001433 if (self->encoding == NULL || self->errors == NULL) {
1434 PyErr_NoMemory();
1435 return -1;
1436 }
1437 return 0;
1438}
1439
1440/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001441static int
1442memo_get(PicklerObject *self, PyObject *key)
1443{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001444 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001445 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001446 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001447
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001448 value = PyMemoTable_Get(self->memo, key);
1449 if (value == NULL) {
1450 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001451 return -1;
1452 }
1453
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001454 if (!self->bin) {
1455 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001456 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1457 "%" PY_FORMAT_SIZE_T "d\n", *value);
1458 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001459 }
1460 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001461 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001462 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001463 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001464 len = 2;
1465 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001466 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001467 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001468 pdata[1] = (unsigned char)(*value & 0xff);
1469 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1470 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1471 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001472 len = 5;
1473 }
1474 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001475 PickleState *st = _Pickle_GetGlobalState();
1476 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001477 "memo id too large for LONG_BINGET");
1478 return -1;
1479 }
1480 }
1481
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001482 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 return -1;
1484
1485 return 0;
1486}
1487
1488/* Store an object in the memo, assign it a new unique ID based on the number
1489 of objects currently stored in the memo and generate a PUT opcode. */
1490static int
1491memo_put(PicklerObject *self, PyObject *obj)
1492{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001493 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001494 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001495 Py_ssize_t idx;
1496
1497 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001498
1499 if (self->fast)
1500 return 0;
1501
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001502 idx = PyMemoTable_Size(self->memo);
1503 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1504 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001506 if (self->proto >= 4) {
1507 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1508 return -1;
1509 return 0;
1510 }
1511 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001512 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001513 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001514 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001515 len = strlen(pdata);
1516 }
1517 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001518 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001520 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001521 len = 2;
1522 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001523 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001524 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001525 pdata[1] = (unsigned char)(idx & 0xff);
1526 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1527 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1528 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001529 len = 5;
1530 }
1531 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001532 PickleState *st = _Pickle_GetGlobalState();
1533 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001534 "memo id too large for LONG_BINPUT");
1535 return -1;
1536 }
1537 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001538 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001539 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001540
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001542}
1543
1544static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001545get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001546 _Py_static_string(PyId_dot, ".");
1547 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001548 PyObject *dotted_path;
1549 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550
1551 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001552 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001553 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001554 n = PyList_GET_SIZE(dotted_path);
1555 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001556 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001557 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001558 PyObject *result = PyUnicode_RichCompare(
1559 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1560 int is_equal = (result == Py_True);
1561 assert(PyBool_Check(result));
1562 Py_DECREF(result);
1563 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001564 if (obj == NULL)
1565 PyErr_Format(PyExc_AttributeError,
1566 "Can't pickle local object %R", name);
1567 else
1568 PyErr_Format(PyExc_AttributeError,
1569 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001570 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571 return NULL;
1572 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001573 }
1574 return dotted_path;
1575}
1576
1577static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001578get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001579{
1580 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001581 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001582
1583 assert(PyList_CheckExact(names));
1584 Py_INCREF(obj);
1585 n = PyList_GET_SIZE(names);
1586 for (i = 0; i < n; i++) {
1587 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001588 Py_XDECREF(parent);
1589 parent = obj;
1590 obj = PyObject_GetAttr(parent, name);
1591 if (obj == NULL) {
1592 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001593 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001594 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001595 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001596 if (pparent != NULL)
1597 *pparent = parent;
1598 else
1599 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 return obj;
1601}
1602
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001603static void
1604reformat_attribute_error(PyObject *obj, PyObject *name)
1605{
1606 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1607 PyErr_Clear();
1608 PyErr_Format(PyExc_AttributeError,
1609 "Can't get attribute %R on %R", name, obj);
1610 }
1611}
1612
1613
1614static PyObject *
1615getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1616{
1617 PyObject *dotted_path, *attr;
1618
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001619 if (allow_qualname) {
1620 dotted_path = get_dotted_path(obj, name);
1621 if (dotted_path == NULL)
1622 return NULL;
1623 attr = get_deep_attribute(obj, dotted_path, NULL);
1624 Py_DECREF(dotted_path);
1625 }
1626 else
1627 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001628 if (attr == NULL)
1629 reformat_attribute_error(obj, name);
1630 return attr;
1631}
1632
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001633static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001634whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001635{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001636 PyObject *module_name;
1637 PyObject *modules_dict;
1638 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001639 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001640 _Py_IDENTIFIER(__module__);
1641 _Py_IDENTIFIER(modules);
1642 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001643
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1645
1646 if (module_name == NULL) {
1647 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001648 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001649 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001650 }
1651 else {
1652 /* In some rare cases (e.g., bound methods of extension types),
1653 __module__ can be None. If it is so, then search sys.modules for
1654 the module of global. */
1655 if (module_name != Py_None)
1656 return module_name;
1657 Py_CLEAR(module_name);
1658 }
1659 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001660
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001661 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001662 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001663 if (modules_dict == NULL) {
1664 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001665 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001666 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001667
1668 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001669 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1670 PyObject *candidate;
1671 if (PyUnicode_Check(module_name) &&
1672 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001673 continue;
1674 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001675 continue;
1676
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001677 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001678 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001679 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001680 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001681 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001682 continue;
1683 }
1684
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001685 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001686 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001687 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001688 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001689 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001690 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001691 }
1692
1693 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 Py_INCREF(module_name);
1696 return module_name;
1697}
1698
1699/* fast_save_enter() and fast_save_leave() are guards against recursive
1700 objects when Pickler is used with the "fast mode" (i.e., with object
1701 memoization disabled). If the nesting of a list or dict object exceed
1702 FAST_NESTING_LIMIT, these guards will start keeping an internal
1703 reference to the seen list or dict objects and check whether these objects
1704 are recursive. These are not strictly necessary, since save() has a
1705 hard-coded recursion limit, but they give a nicer error message than the
1706 typical RuntimeError. */
1707static int
1708fast_save_enter(PicklerObject *self, PyObject *obj)
1709{
1710 /* if fast_nesting < 0, we're doing an error exit. */
1711 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1712 PyObject *key = NULL;
1713 if (self->fast_memo == NULL) {
1714 self->fast_memo = PyDict_New();
1715 if (self->fast_memo == NULL) {
1716 self->fast_nesting = -1;
1717 return 0;
1718 }
1719 }
1720 key = PyLong_FromVoidPtr(obj);
1721 if (key == NULL)
1722 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001723 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001724 Py_DECREF(key);
1725 PyErr_Format(PyExc_ValueError,
1726 "fast mode: can't pickle cyclic objects "
1727 "including object type %.200s at %p",
1728 obj->ob_type->tp_name, obj);
1729 self->fast_nesting = -1;
1730 return 0;
1731 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001732 if (PyErr_Occurred()) {
1733 return 0;
1734 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001735 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1736 Py_DECREF(key);
1737 self->fast_nesting = -1;
1738 return 0;
1739 }
1740 Py_DECREF(key);
1741 }
1742 return 1;
1743}
1744
1745static int
1746fast_save_leave(PicklerObject *self, PyObject *obj)
1747{
1748 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1749 PyObject *key = PyLong_FromVoidPtr(obj);
1750 if (key == NULL)
1751 return 0;
1752 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1753 Py_DECREF(key);
1754 return 0;
1755 }
1756 Py_DECREF(key);
1757 }
1758 return 1;
1759}
1760
1761static int
1762save_none(PicklerObject *self, PyObject *obj)
1763{
1764 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001765 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001766 return -1;
1767
1768 return 0;
1769}
1770
1771static int
1772save_bool(PicklerObject *self, PyObject *obj)
1773{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001774 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001775 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001776 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001777 return -1;
1778 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001779 else {
1780 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1781 * so that unpicklers written before bools were introduced unpickle them
1782 * as ints, but unpicklers after can recognize that bools were intended.
1783 * Note that protocol 2 added direct ways to pickle bools.
1784 */
1785 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1786 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1787 return -1;
1788 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001789 return 0;
1790}
1791
1792static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001793save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001795 PyObject *repr = NULL;
1796 Py_ssize_t size;
1797 long val;
1798 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001799
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001800 const char long_op = LONG;
1801
1802 val= PyLong_AsLong(obj);
1803 if (val == -1 && PyErr_Occurred()) {
1804 /* out of range for int pickling */
1805 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001807 else if (self->bin &&
1808 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001809 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001810 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001811
1812 Note: we can't use -0x80000000L in the above condition because some
1813 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1814 before applying the unary minus when sizeof(long) <= 4. The
1815 resulting value stays unsigned which is commonly not what we want,
1816 so MSVC happily warns us about it. However, that result would have
1817 been fine because we guard for sizeof(long) <= 4 which turns the
1818 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001819 char pdata[32];
1820 Py_ssize_t len = 0;
1821
1822 pdata[1] = (unsigned char)(val & 0xff);
1823 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1824 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1825 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826
1827 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1828 if (pdata[2] == 0) {
1829 pdata[0] = BININT1;
1830 len = 2;
1831 }
1832 else {
1833 pdata[0] = BININT2;
1834 len = 3;
1835 }
1836 }
1837 else {
1838 pdata[0] = BININT;
1839 len = 5;
1840 }
1841
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001842 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001843 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001844
1845 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846 }
1847
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001848 if (self->proto >= 2) {
1849 /* Linear-time pickling. */
1850 size_t nbits;
1851 size_t nbytes;
1852 unsigned char *pdata;
1853 char header[5];
1854 int i;
1855 int sign = _PyLong_Sign(obj);
1856
1857 if (sign == 0) {
1858 header[0] = LONG1;
1859 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001860 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001861 goto error;
1862 return 0;
1863 }
1864 nbits = _PyLong_NumBits(obj);
1865 if (nbits == (size_t)-1 && PyErr_Occurred())
1866 goto error;
1867 /* How many bytes do we need? There are nbits >> 3 full
1868 * bytes of data, and nbits & 7 leftover bits. If there
1869 * are any leftover bits, then we clearly need another
1870 * byte. Wnat's not so obvious is that we *probably*
1871 * need another byte even if there aren't any leftovers:
1872 * the most-significant bit of the most-significant byte
1873 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001874 * opposite of the one we need. The exception is ints
1875 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001876 * its own 256's-complement, so has the right sign bit
1877 * even without the extra byte. That's a pain to check
1878 * for in advance, though, so we always grab an extra
1879 * byte at the start, and cut it back later if possible.
1880 */
1881 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001882 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001883 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001884 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001885 goto error;
1886 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001887 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001888 if (repr == NULL)
1889 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001890 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001891 i = _PyLong_AsByteArray((PyLongObject *)obj,
1892 pdata, nbytes,
1893 1 /* little endian */ , 1 /* signed */ );
1894 if (i < 0)
1895 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001896 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 * needed. This is so iff the MSB is all redundant sign
1898 * bits.
1899 */
1900 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001901 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001902 pdata[nbytes - 1] == 0xff &&
1903 (pdata[nbytes - 2] & 0x80) != 0) {
1904 nbytes--;
1905 }
1906
1907 if (nbytes < 256) {
1908 header[0] = LONG1;
1909 header[1] = (unsigned char)nbytes;
1910 size = 2;
1911 }
1912 else {
1913 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001914 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001915 for (i = 1; i < 5; i++) {
1916 header[i] = (unsigned char)(size & 0xff);
1917 size >>= 8;
1918 }
1919 size = 5;
1920 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001921 if (_Pickler_Write(self, header, size) < 0 ||
1922 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923 goto error;
1924 }
1925 else {
1926 char *string;
1927
Mark Dickinson8dd05142009-01-20 20:43:58 +00001928 /* proto < 2: write the repr and newline. This is quadratic-time (in
1929 the number of digits), in both directions. We add a trailing 'L'
1930 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931
1932 repr = PyObject_Repr(obj);
1933 if (repr == NULL)
1934 goto error;
1935
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001936 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001937 if (string == NULL)
1938 goto error;
1939
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001940 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1941 _Pickler_Write(self, string, size) < 0 ||
1942 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001943 goto error;
1944 }
1945
1946 if (0) {
1947 error:
1948 status = -1;
1949 }
1950 Py_XDECREF(repr);
1951
1952 return status;
1953}
1954
1955static int
1956save_float(PicklerObject *self, PyObject *obj)
1957{
1958 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1959
1960 if (self->bin) {
1961 char pdata[9];
1962 pdata[0] = BINFLOAT;
1963 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1964 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001965 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001966 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001967 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001968 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001969 int result = -1;
1970 char *buf = NULL;
1971 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001973 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001974 goto done;
1975
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001976 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001977 if (!buf) {
1978 PyErr_NoMemory();
1979 goto done;
1980 }
1981
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001982 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001983 goto done;
1984
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001985 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001986 goto done;
1987
1988 result = 0;
1989done:
1990 PyMem_Free(buf);
1991 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992 }
1993
1994 return 0;
1995}
1996
1997static int
1998save_bytes(PicklerObject *self, PyObject *obj)
1999{
2000 if (self->proto < 3) {
2001 /* Older pickle protocols do not have an opcode for pickling bytes
2002 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002003 the __reduce__ method) to permit bytes object unpickling.
2004
2005 Here we use a hack to be compatible with Python 2. Since in Python
2006 2 'bytes' is just an alias for 'str' (which has different
2007 parameters than the actual bytes object), we use codecs.encode
2008 to create the appropriate 'str' object when unpickled using
2009 Python 2 *and* the appropriate 'bytes' object when unpickled
2010 using Python 3. Again this is a hack and we don't need to do this
2011 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 int status;
2014
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002015 if (PyBytes_GET_SIZE(obj) == 0) {
2016 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2017 }
2018 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002019 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002020 PyObject *unicode_str =
2021 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2022 PyBytes_GET_SIZE(obj),
2023 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002024 _Py_IDENTIFIER(latin1);
2025
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002026 if (unicode_str == NULL)
2027 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002028 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002029 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002030 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002031 Py_DECREF(unicode_str);
2032 }
2033
2034 if (reduce_value == NULL)
2035 return -1;
2036
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002037 /* save_reduce() will memoize the object automatically. */
2038 status = save_reduce(self, reduce_value, obj);
2039 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002040 return status;
2041 }
2042 else {
2043 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002044 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002045 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002046
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002047 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002048 if (size < 0)
2049 return -1;
2050
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002051 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052 header[0] = SHORT_BINBYTES;
2053 header[1] = (unsigned char)size;
2054 len = 2;
2055 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002056 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057 header[0] = BINBYTES;
2058 header[1] = (unsigned char)(size & 0xff);
2059 header[2] = (unsigned char)((size >> 8) & 0xff);
2060 header[3] = (unsigned char)((size >> 16) & 0xff);
2061 header[4] = (unsigned char)((size >> 24) & 0xff);
2062 len = 5;
2063 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002064 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002065 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002066 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002067 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002068 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002070 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002071 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 return -1; /* string too large */
2073 }
2074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002075 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 return -1;
2077
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002078 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002079 return -1;
2080
2081 if (memo_put(self, obj) < 0)
2082 return -1;
2083
2084 return 0;
2085 }
2086}
2087
2088/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2089 backslash and newline characters to \uXXXX escapes. */
2090static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002091raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002093 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002094 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002095 Py_ssize_t i, size;
2096 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002097 void *data;
2098 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002100 if (PyUnicode_READY(obj))
2101 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002102
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002103 size = PyUnicode_GET_LENGTH(obj);
2104 data = PyUnicode_DATA(obj);
2105 kind = PyUnicode_KIND(obj);
2106 if (kind == PyUnicode_4BYTE_KIND)
2107 expandsize = 10;
2108 else
2109 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002110
Victor Stinner049e5092014-08-17 22:20:00 +02002111 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002112 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002113 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 if (repr == NULL)
2115 return NULL;
2116 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002117 return repr;
2118 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002119
Victor Stinner7270b7f2014-08-17 21:14:46 +02002120 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002121 for (i=0; i < size; i++) {
2122 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123 /* Map 32-bit characters to '\Uxxxxxxxx' */
2124 if (ch >= 0x10000) {
2125 *p++ = '\\';
2126 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002127 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2128 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2129 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2130 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2131 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2132 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2133 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2134 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002136 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002137 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002138 *p++ = '\\';
2139 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002140 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2141 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2142 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2143 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002144 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002145 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002146 else
2147 *p++ = (char) ch;
2148 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002149 size = p - PyBytes_AS_STRING(repr);
2150 if (_PyBytes_Resize(&repr, size) < 0)
2151 return NULL;
2152 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002153}
2154
2155static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002156write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2157{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002158 char header[9];
2159 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002160
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002161 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002162 if (size <= 0xff && self->proto >= 4) {
2163 header[0] = SHORT_BINUNICODE;
2164 header[1] = (unsigned char)(size & 0xff);
2165 len = 2;
2166 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002167 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002168 header[0] = BINUNICODE;
2169 header[1] = (unsigned char)(size & 0xff);
2170 header[2] = (unsigned char)((size >> 8) & 0xff);
2171 header[3] = (unsigned char)((size >> 16) & 0xff);
2172 header[4] = (unsigned char)((size >> 24) & 0xff);
2173 len = 5;
2174 }
2175 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002176 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002177 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002178 len = 9;
2179 }
2180 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002181 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002182 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002183 return -1;
2184 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002185
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002186 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002187 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002188 if (_Pickler_Write(self, data, size) < 0)
2189 return -1;
2190
2191 return 0;
2192}
2193
2194static int
2195write_unicode_binary(PicklerObject *self, PyObject *obj)
2196{
2197 PyObject *encoded = NULL;
2198 Py_ssize_t size;
2199 char *data;
2200 int r;
2201
2202 if (PyUnicode_READY(obj))
2203 return -1;
2204
2205 data = PyUnicode_AsUTF8AndSize(obj, &size);
2206 if (data != NULL)
2207 return write_utf8(self, data, size);
2208
2209 /* Issue #8383: for strings with lone surrogates, fallback on the
2210 "surrogatepass" error handler. */
2211 PyErr_Clear();
2212 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2213 if (encoded == NULL)
2214 return -1;
2215
2216 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2217 PyBytes_GET_SIZE(encoded));
2218 Py_DECREF(encoded);
2219 return r;
2220}
2221
2222static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002223save_unicode(PicklerObject *self, PyObject *obj)
2224{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002225 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002226 if (write_unicode_binary(self, obj) < 0)
2227 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002228 }
2229 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002230 PyObject *encoded;
2231 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002232 const char unicode_op = UNICODE;
2233
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002234 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002235 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002236 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002237
Antoine Pitrou299978d2013-04-07 17:38:11 +02002238 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2239 Py_DECREF(encoded);
2240 return -1;
2241 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002242
2243 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002244 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2245 Py_DECREF(encoded);
2246 return -1;
2247 }
2248 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002250 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002251 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 }
2253 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002254 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257}
2258
2259/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2260static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002261store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002263 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002264
2265 assert(PyTuple_Size(t) == len);
2266
2267 for (i = 0; i < len; i++) {
2268 PyObject *element = PyTuple_GET_ITEM(t, i);
2269
2270 if (element == NULL)
2271 return -1;
2272 if (save(self, element, 0) < 0)
2273 return -1;
2274 }
2275
2276 return 0;
2277}
2278
2279/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2280 * used across protocols to minimize the space needed to pickle them.
2281 * Tuples are also the only builtin immutable type that can be recursive
2282 * (a tuple can be reached from itself), and that requires some subtle
2283 * magic so that it works in all cases. IOW, this is a long routine.
2284 */
2285static int
2286save_tuple(PicklerObject *self, PyObject *obj)
2287{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002288 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
2290 const char mark_op = MARK;
2291 const char tuple_op = TUPLE;
2292 const char pop_op = POP;
2293 const char pop_mark_op = POP_MARK;
2294 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2295
2296 if ((len = PyTuple_Size(obj)) < 0)
2297 return -1;
2298
2299 if (len == 0) {
2300 char pdata[2];
2301
2302 if (self->proto) {
2303 pdata[0] = EMPTY_TUPLE;
2304 len = 1;
2305 }
2306 else {
2307 pdata[0] = MARK;
2308 pdata[1] = TUPLE;
2309 len = 2;
2310 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002311 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002312 return -1;
2313 return 0;
2314 }
2315
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002316 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002317 * saving the tuple elements, the tuple must be recursive, in
2318 * which case we'll pop everything we put on the stack, and fetch
2319 * its value from the memo.
2320 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 if (len <= 3 && self->proto >= 2) {
2322 /* Use TUPLE{1,2,3} opcodes. */
2323 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002324 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002326 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002327 /* pop the len elements */
2328 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002329 if (_Pickler_Write(self, &pop_op, 1) < 0)
2330 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002332 if (memo_get(self, obj) < 0)
2333 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002334
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 return 0;
2336 }
2337 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002338 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2339 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002340 }
2341 goto memoize;
2342 }
2343
2344 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2345 * Generate MARK e1 e2 ... TUPLE
2346 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002347 if (_Pickler_Write(self, &mark_op, 1) < 0)
2348 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349
2350 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002353 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354 /* pop the stack stuff we pushed */
2355 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002356 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2357 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 }
2359 else {
2360 /* Note that we pop one more than len, to remove
2361 * the MARK too.
2362 */
2363 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002364 if (_Pickler_Write(self, &pop_op, 1) < 0)
2365 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002366 }
2367 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002368 if (memo_get(self, obj) < 0)
2369 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002370
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002371 return 0;
2372 }
2373 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002374 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2375 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002376 }
2377
2378 memoize:
2379 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002380 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002381
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002382 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383}
2384
2385/* iter is an iterator giving items, and we batch up chunks of
2386 * MARK item item ... item APPENDS
2387 * opcode sequences. Calling code should have arranged to first create an
2388 * empty list, or list-like object, for the APPENDS to operate on.
2389 * Returns 0 on success, <0 on error.
2390 */
2391static int
2392batch_list(PicklerObject *self, PyObject *iter)
2393{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002394 PyObject *obj = NULL;
2395 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002396 int i, n;
2397
2398 const char mark_op = MARK;
2399 const char append_op = APPEND;
2400 const char appends_op = APPENDS;
2401
2402 assert(iter != NULL);
2403
2404 /* XXX: I think this function could be made faster by avoiding the
2405 iterator interface and fetching objects directly from list using
2406 PyList_GET_ITEM.
2407 */
2408
2409 if (self->proto == 0) {
2410 /* APPENDS isn't available; do one at a time. */
2411 for (;;) {
2412 obj = PyIter_Next(iter);
2413 if (obj == NULL) {
2414 if (PyErr_Occurred())
2415 return -1;
2416 break;
2417 }
2418 i = save(self, obj, 0);
2419 Py_DECREF(obj);
2420 if (i < 0)
2421 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002422 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002423 return -1;
2424 }
2425 return 0;
2426 }
2427
2428 /* proto > 0: write in batches of BATCHSIZE. */
2429 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002430 /* Get first item */
2431 firstitem = PyIter_Next(iter);
2432 if (firstitem == NULL) {
2433 if (PyErr_Occurred())
2434 goto error;
2435
2436 /* nothing more to add */
2437 break;
2438 }
2439
2440 /* Try to get a second item */
2441 obj = PyIter_Next(iter);
2442 if (obj == NULL) {
2443 if (PyErr_Occurred())
2444 goto error;
2445
2446 /* Only one item to write */
2447 if (save(self, firstitem, 0) < 0)
2448 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002449 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002450 goto error;
2451 Py_CLEAR(firstitem);
2452 break;
2453 }
2454
2455 /* More than one item to write */
2456
2457 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002458 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002459 goto error;
2460
2461 if (save(self, firstitem, 0) < 0)
2462 goto error;
2463 Py_CLEAR(firstitem);
2464 n = 1;
2465
2466 /* Fetch and save up to BATCHSIZE items */
2467 while (obj) {
2468 if (save(self, obj, 0) < 0)
2469 goto error;
2470 Py_CLEAR(obj);
2471 n += 1;
2472
2473 if (n == BATCHSIZE)
2474 break;
2475
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002476 obj = PyIter_Next(iter);
2477 if (obj == NULL) {
2478 if (PyErr_Occurred())
2479 goto error;
2480 break;
2481 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002482 }
2483
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002484 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002485 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002487 } while (n == BATCHSIZE);
2488 return 0;
2489
2490 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002491 Py_XDECREF(firstitem);
2492 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002493 return -1;
2494}
2495
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002496/* This is a variant of batch_list() above, specialized for lists (with no
2497 * support for list subclasses). Like batch_list(), we batch up chunks of
2498 * MARK item item ... item APPENDS
2499 * opcode sequences. Calling code should have arranged to first create an
2500 * empty list, or list-like object, for the APPENDS to operate on.
2501 * Returns 0 on success, -1 on error.
2502 *
2503 * This version is considerably faster than batch_list(), if less general.
2504 *
2505 * Note that this only works for protocols > 0.
2506 */
2507static int
2508batch_list_exact(PicklerObject *self, PyObject *obj)
2509{
2510 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002511 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002512
2513 const char append_op = APPEND;
2514 const char appends_op = APPENDS;
2515 const char mark_op = MARK;
2516
2517 assert(obj != NULL);
2518 assert(self->proto > 0);
2519 assert(PyList_CheckExact(obj));
2520
2521 if (PyList_GET_SIZE(obj) == 1) {
2522 item = PyList_GET_ITEM(obj, 0);
2523 if (save(self, item, 0) < 0)
2524 return -1;
2525 if (_Pickler_Write(self, &append_op, 1) < 0)
2526 return -1;
2527 return 0;
2528 }
2529
2530 /* Write in batches of BATCHSIZE. */
2531 total = 0;
2532 do {
2533 this_batch = 0;
2534 if (_Pickler_Write(self, &mark_op, 1) < 0)
2535 return -1;
2536 while (total < PyList_GET_SIZE(obj)) {
2537 item = PyList_GET_ITEM(obj, total);
2538 if (save(self, item, 0) < 0)
2539 return -1;
2540 total++;
2541 if (++this_batch == BATCHSIZE)
2542 break;
2543 }
2544 if (_Pickler_Write(self, &appends_op, 1) < 0)
2545 return -1;
2546
2547 } while (total < PyList_GET_SIZE(obj));
2548
2549 return 0;
2550}
2551
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002552static int
2553save_list(PicklerObject *self, PyObject *obj)
2554{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002555 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002556 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002557 int status = 0;
2558
2559 if (self->fast && !fast_save_enter(self, obj))
2560 goto error;
2561
2562 /* Create an empty list. */
2563 if (self->bin) {
2564 header[0] = EMPTY_LIST;
2565 len = 1;
2566 }
2567 else {
2568 header[0] = MARK;
2569 header[1] = LIST;
2570 len = 2;
2571 }
2572
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002573 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002574 goto error;
2575
2576 /* Get list length, and bow out early if empty. */
2577 if ((len = PyList_Size(obj)) < 0)
2578 goto error;
2579
2580 if (memo_put(self, obj) < 0)
2581 goto error;
2582
2583 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002584 /* Materialize the list elements. */
2585 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002586 if (Py_EnterRecursiveCall(" while pickling an object"))
2587 goto error;
2588 status = batch_list_exact(self, obj);
2589 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002590 } else {
2591 PyObject *iter = PyObject_GetIter(obj);
2592 if (iter == NULL)
2593 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002594
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002595 if (Py_EnterRecursiveCall(" while pickling an object")) {
2596 Py_DECREF(iter);
2597 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002598 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002599 status = batch_list(self, iter);
2600 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002601 Py_DECREF(iter);
2602 }
2603 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002604 if (0) {
2605 error:
2606 status = -1;
2607 }
2608
2609 if (self->fast && !fast_save_leave(self, obj))
2610 status = -1;
2611
2612 return status;
2613}
2614
2615/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2616 * MARK key value ... key value SETITEMS
2617 * opcode sequences. Calling code should have arranged to first create an
2618 * empty dict, or dict-like object, for the SETITEMS to operate on.
2619 * Returns 0 on success, <0 on error.
2620 *
2621 * This is very much like batch_list(). The difference between saving
2622 * elements directly, and picking apart two-tuples, is so long-winded at
2623 * the C level, though, that attempts to combine these routines were too
2624 * ugly to bear.
2625 */
2626static int
2627batch_dict(PicklerObject *self, PyObject *iter)
2628{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002629 PyObject *obj = NULL;
2630 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002631 int i, n;
2632
2633 const char mark_op = MARK;
2634 const char setitem_op = SETITEM;
2635 const char setitems_op = SETITEMS;
2636
2637 assert(iter != NULL);
2638
2639 if (self->proto == 0) {
2640 /* SETITEMS isn't available; do one at a time. */
2641 for (;;) {
2642 obj = PyIter_Next(iter);
2643 if (obj == NULL) {
2644 if (PyErr_Occurred())
2645 return -1;
2646 break;
2647 }
2648 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2649 PyErr_SetString(PyExc_TypeError, "dict items "
2650 "iterator must return 2-tuples");
2651 return -1;
2652 }
2653 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2654 if (i >= 0)
2655 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2656 Py_DECREF(obj);
2657 if (i < 0)
2658 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002659 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002660 return -1;
2661 }
2662 return 0;
2663 }
2664
2665 /* proto > 0: write in batches of BATCHSIZE. */
2666 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002667 /* Get first item */
2668 firstitem = PyIter_Next(iter);
2669 if (firstitem == NULL) {
2670 if (PyErr_Occurred())
2671 goto error;
2672
2673 /* nothing more to add */
2674 break;
2675 }
2676 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2677 PyErr_SetString(PyExc_TypeError, "dict items "
2678 "iterator must return 2-tuples");
2679 goto error;
2680 }
2681
2682 /* Try to get a second item */
2683 obj = PyIter_Next(iter);
2684 if (obj == NULL) {
2685 if (PyErr_Occurred())
2686 goto error;
2687
2688 /* Only one item to write */
2689 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2690 goto error;
2691 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2692 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002693 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002694 goto error;
2695 Py_CLEAR(firstitem);
2696 break;
2697 }
2698
2699 /* More than one item to write */
2700
2701 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002702 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002703 goto error;
2704
2705 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2706 goto error;
2707 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2708 goto error;
2709 Py_CLEAR(firstitem);
2710 n = 1;
2711
2712 /* Fetch and save up to BATCHSIZE items */
2713 while (obj) {
2714 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2715 PyErr_SetString(PyExc_TypeError, "dict items "
2716 "iterator must return 2-tuples");
2717 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002718 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002719 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2720 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2721 goto error;
2722 Py_CLEAR(obj);
2723 n += 1;
2724
2725 if (n == BATCHSIZE)
2726 break;
2727
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002728 obj = PyIter_Next(iter);
2729 if (obj == NULL) {
2730 if (PyErr_Occurred())
2731 goto error;
2732 break;
2733 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002734 }
2735
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002736 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002737 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002738
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002739 } while (n == BATCHSIZE);
2740 return 0;
2741
2742 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002743 Py_XDECREF(firstitem);
2744 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002745 return -1;
2746}
2747
Collin Winter5c9b02d2009-05-25 05:43:30 +00002748/* This is a variant of batch_dict() above that specializes for dicts, with no
2749 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2750 * MARK key value ... key value SETITEMS
2751 * opcode sequences. Calling code should have arranged to first create an
2752 * empty dict, or dict-like object, for the SETITEMS to operate on.
2753 * Returns 0 on success, -1 on error.
2754 *
2755 * Note that this currently doesn't work for protocol 0.
2756 */
2757static int
2758batch_dict_exact(PicklerObject *self, PyObject *obj)
2759{
2760 PyObject *key = NULL, *value = NULL;
2761 int i;
2762 Py_ssize_t dict_size, ppos = 0;
2763
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002764 const char mark_op = MARK;
2765 const char setitem_op = SETITEM;
2766 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002767
2768 assert(obj != NULL);
2769 assert(self->proto > 0);
2770
2771 dict_size = PyDict_Size(obj);
2772
2773 /* Special-case len(d) == 1 to save space. */
2774 if (dict_size == 1) {
2775 PyDict_Next(obj, &ppos, &key, &value);
2776 if (save(self, key, 0) < 0)
2777 return -1;
2778 if (save(self, value, 0) < 0)
2779 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002780 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002781 return -1;
2782 return 0;
2783 }
2784
2785 /* Write in batches of BATCHSIZE. */
2786 do {
2787 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002788 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002789 return -1;
2790 while (PyDict_Next(obj, &ppos, &key, &value)) {
2791 if (save(self, key, 0) < 0)
2792 return -1;
2793 if (save(self, value, 0) < 0)
2794 return -1;
2795 if (++i == BATCHSIZE)
2796 break;
2797 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002798 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002799 return -1;
2800 if (PyDict_Size(obj) != dict_size) {
2801 PyErr_Format(
2802 PyExc_RuntimeError,
2803 "dictionary changed size during iteration");
2804 return -1;
2805 }
2806
2807 } while (i == BATCHSIZE);
2808 return 0;
2809}
2810
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002811static int
2812save_dict(PicklerObject *self, PyObject *obj)
2813{
2814 PyObject *items, *iter;
2815 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002816 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002817 int status = 0;
2818
2819 if (self->fast && !fast_save_enter(self, obj))
2820 goto error;
2821
2822 /* Create an empty dict. */
2823 if (self->bin) {
2824 header[0] = EMPTY_DICT;
2825 len = 1;
2826 }
2827 else {
2828 header[0] = MARK;
2829 header[1] = DICT;
2830 len = 2;
2831 }
2832
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002833 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002834 goto error;
2835
2836 /* Get dict size, and bow out early if empty. */
2837 if ((len = PyDict_Size(obj)) < 0)
2838 goto error;
2839
2840 if (memo_put(self, obj) < 0)
2841 goto error;
2842
2843 if (len != 0) {
2844 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002845 if (PyDict_CheckExact(obj) && self->proto > 0) {
2846 /* We can take certain shortcuts if we know this is a dict and
2847 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002848 if (Py_EnterRecursiveCall(" while pickling an object"))
2849 goto error;
2850 status = batch_dict_exact(self, obj);
2851 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002852 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002853 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002854
2855 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002856 if (items == NULL)
2857 goto error;
2858 iter = PyObject_GetIter(items);
2859 Py_DECREF(items);
2860 if (iter == NULL)
2861 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002862 if (Py_EnterRecursiveCall(" while pickling an object")) {
2863 Py_DECREF(iter);
2864 goto error;
2865 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002866 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002867 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002868 Py_DECREF(iter);
2869 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002870 }
2871
2872 if (0) {
2873 error:
2874 status = -1;
2875 }
2876
2877 if (self->fast && !fast_save_leave(self, obj))
2878 status = -1;
2879
2880 return status;
2881}
2882
2883static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002884save_set(PicklerObject *self, PyObject *obj)
2885{
2886 PyObject *item;
2887 int i;
2888 Py_ssize_t set_size, ppos = 0;
2889 Py_hash_t hash;
2890
2891 const char empty_set_op = EMPTY_SET;
2892 const char mark_op = MARK;
2893 const char additems_op = ADDITEMS;
2894
2895 if (self->proto < 4) {
2896 PyObject *items;
2897 PyObject *reduce_value;
2898 int status;
2899
2900 items = PySequence_List(obj);
2901 if (items == NULL) {
2902 return -1;
2903 }
2904 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2905 Py_DECREF(items);
2906 if (reduce_value == NULL) {
2907 return -1;
2908 }
2909 /* save_reduce() will memoize the object automatically. */
2910 status = save_reduce(self, reduce_value, obj);
2911 Py_DECREF(reduce_value);
2912 return status;
2913 }
2914
2915 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2916 return -1;
2917
2918 if (memo_put(self, obj) < 0)
2919 return -1;
2920
2921 set_size = PySet_GET_SIZE(obj);
2922 if (set_size == 0)
2923 return 0; /* nothing to do */
2924
2925 /* Write in batches of BATCHSIZE. */
2926 do {
2927 i = 0;
2928 if (_Pickler_Write(self, &mark_op, 1) < 0)
2929 return -1;
2930 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2931 if (save(self, item, 0) < 0)
2932 return -1;
2933 if (++i == BATCHSIZE)
2934 break;
2935 }
2936 if (_Pickler_Write(self, &additems_op, 1) < 0)
2937 return -1;
2938 if (PySet_GET_SIZE(obj) != set_size) {
2939 PyErr_Format(
2940 PyExc_RuntimeError,
2941 "set changed size during iteration");
2942 return -1;
2943 }
2944 } while (i == BATCHSIZE);
2945
2946 return 0;
2947}
2948
2949static int
2950save_frozenset(PicklerObject *self, PyObject *obj)
2951{
2952 PyObject *iter;
2953
2954 const char mark_op = MARK;
2955 const char frozenset_op = FROZENSET;
2956
2957 if (self->fast && !fast_save_enter(self, obj))
2958 return -1;
2959
2960 if (self->proto < 4) {
2961 PyObject *items;
2962 PyObject *reduce_value;
2963 int status;
2964
2965 items = PySequence_List(obj);
2966 if (items == NULL) {
2967 return -1;
2968 }
2969 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2970 items);
2971 Py_DECREF(items);
2972 if (reduce_value == NULL) {
2973 return -1;
2974 }
2975 /* save_reduce() will memoize the object automatically. */
2976 status = save_reduce(self, reduce_value, obj);
2977 Py_DECREF(reduce_value);
2978 return status;
2979 }
2980
2981 if (_Pickler_Write(self, &mark_op, 1) < 0)
2982 return -1;
2983
2984 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002985 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002986 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002987 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002988 for (;;) {
2989 PyObject *item;
2990
2991 item = PyIter_Next(iter);
2992 if (item == NULL) {
2993 if (PyErr_Occurred()) {
2994 Py_DECREF(iter);
2995 return -1;
2996 }
2997 break;
2998 }
2999 if (save(self, item, 0) < 0) {
3000 Py_DECREF(item);
3001 Py_DECREF(iter);
3002 return -1;
3003 }
3004 Py_DECREF(item);
3005 }
3006 Py_DECREF(iter);
3007
3008 /* If the object is already in the memo, this means it is
3009 recursive. In this case, throw away everything we put on the
3010 stack, and fetch the object back from the memo. */
3011 if (PyMemoTable_Get(self->memo, obj)) {
3012 const char pop_mark_op = POP_MARK;
3013
3014 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3015 return -1;
3016 if (memo_get(self, obj) < 0)
3017 return -1;
3018 return 0;
3019 }
3020
3021 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3022 return -1;
3023 if (memo_put(self, obj) < 0)
3024 return -1;
3025
3026 return 0;
3027}
3028
3029static int
3030fix_imports(PyObject **module_name, PyObject **global_name)
3031{
3032 PyObject *key;
3033 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003034 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003035
3036 key = PyTuple_Pack(2, *module_name, *global_name);
3037 if (key == NULL)
3038 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003039 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003040 Py_DECREF(key);
3041 if (item) {
3042 PyObject *fixed_module_name;
3043 PyObject *fixed_global_name;
3044
3045 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3046 PyErr_Format(PyExc_RuntimeError,
3047 "_compat_pickle.REVERSE_NAME_MAPPING values "
3048 "should be 2-tuples, not %.200s",
3049 Py_TYPE(item)->tp_name);
3050 return -1;
3051 }
3052 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3053 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3054 if (!PyUnicode_Check(fixed_module_name) ||
3055 !PyUnicode_Check(fixed_global_name)) {
3056 PyErr_Format(PyExc_RuntimeError,
3057 "_compat_pickle.REVERSE_NAME_MAPPING values "
3058 "should be pairs of str, not (%.200s, %.200s)",
3059 Py_TYPE(fixed_module_name)->tp_name,
3060 Py_TYPE(fixed_global_name)->tp_name);
3061 return -1;
3062 }
3063
3064 Py_CLEAR(*module_name);
3065 Py_CLEAR(*global_name);
3066 Py_INCREF(fixed_module_name);
3067 Py_INCREF(fixed_global_name);
3068 *module_name = fixed_module_name;
3069 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003070 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003071 }
3072 else if (PyErr_Occurred()) {
3073 return -1;
3074 }
3075
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003076 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003077 if (item) {
3078 if (!PyUnicode_Check(item)) {
3079 PyErr_Format(PyExc_RuntimeError,
3080 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3081 "should be strings, not %.200s",
3082 Py_TYPE(item)->tp_name);
3083 return -1;
3084 }
3085 Py_CLEAR(*module_name);
3086 Py_INCREF(item);
3087 *module_name = item;
3088 }
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 {
3404 PyObject *pid_str = NULL;
3405 char *pid_ascii_bytes;
3406 Py_ssize_t size;
3407
3408 pid_str = PyObject_Str(pid);
3409 if (pid_str == NULL)
3410 goto error;
3411
3412 /* XXX: Should it check whether the persistent id only contains
3413 ASCII characters? And what if the pid contains embedded
3414 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003415 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003416 Py_DECREF(pid_str);
3417 if (pid_ascii_bytes == NULL)
3418 goto error;
3419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003420 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3421 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3422 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003423 goto error;
3424 }
3425 status = 1;
3426 }
3427
3428 if (0) {
3429 error:
3430 status = -1;
3431 }
3432 Py_XDECREF(pid);
3433
3434 return status;
3435}
3436
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003437static PyObject *
3438get_class(PyObject *obj)
3439{
3440 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003441 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003442
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003443 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003444 if (cls == NULL) {
3445 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3446 PyErr_Clear();
3447 cls = (PyObject *) Py_TYPE(obj);
3448 Py_INCREF(cls);
3449 }
3450 }
3451 return cls;
3452}
3453
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003454/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3455 * appropriate __reduce__ method for obj.
3456 */
3457static int
3458save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3459{
3460 PyObject *callable;
3461 PyObject *argtup;
3462 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003463 PyObject *listitems = Py_None;
3464 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003465 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003466 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003468
3469 const char reduce_op = REDUCE;
3470 const char build_op = BUILD;
3471 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003472 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003473
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003474 size = PyTuple_Size(args);
3475 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003476 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003477 "__reduce__ must contain 2 through 5 elements");
3478 return -1;
3479 }
3480
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003481 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3482 &callable, &argtup, &state, &listitems, &dictitems))
3483 return -1;
3484
3485 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003486 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003487 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003488 return -1;
3489 }
3490 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003491 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003492 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003493 return -1;
3494 }
3495
3496 if (state == Py_None)
3497 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003498
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003499 if (listitems == Py_None)
3500 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003501 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003502 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003503 "returned by __reduce__ must be an iterator, not %s",
3504 Py_TYPE(listitems)->tp_name);
3505 return -1;
3506 }
3507
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508 if (dictitems == Py_None)
3509 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003510 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003512 "returned by __reduce__ must be an iterator, not %s",
3513 Py_TYPE(dictitems)->tp_name);
3514 return -1;
3515 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003516
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003517 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003518 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003519 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003520
Victor Stinner804e05e2013-11-14 01:26:17 +01003521 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003522 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003523 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003525 }
3526 PyErr_Clear();
3527 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003528 else if (PyUnicode_Check(name)) {
3529 if (self->proto >= 4) {
3530 _Py_IDENTIFIER(__newobj_ex__);
3531 use_newobj_ex = PyUnicode_Compare(
3532 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3533 }
3534 if (!use_newobj_ex) {
3535 _Py_IDENTIFIER(__newobj__);
3536 use_newobj = PyUnicode_Compare(
3537 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3538 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003539 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003540 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003542
3543 if (use_newobj_ex) {
3544 PyObject *cls;
3545 PyObject *args;
3546 PyObject *kwargs;
3547
3548 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003549 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003550 "length of the NEWOBJ_EX argument tuple must be "
3551 "exactly 3, not %zd", Py_SIZE(argtup));
3552 return -1;
3553 }
3554
3555 cls = PyTuple_GET_ITEM(argtup, 0);
3556 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003557 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003558 "first item from NEWOBJ_EX argument tuple must "
3559 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3560 return -1;
3561 }
3562 args = PyTuple_GET_ITEM(argtup, 1);
3563 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003564 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003565 "second item from NEWOBJ_EX argument tuple must "
3566 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3567 return -1;
3568 }
3569 kwargs = PyTuple_GET_ITEM(argtup, 2);
3570 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003571 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572 "third item from NEWOBJ_EX argument tuple must "
3573 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3574 return -1;
3575 }
3576
3577 if (save(self, cls, 0) < 0 ||
3578 save(self, args, 0) < 0 ||
3579 save(self, kwargs, 0) < 0 ||
3580 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3581 return -1;
3582 }
3583 }
3584 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003585 PyObject *cls;
3586 PyObject *newargtup;
3587 PyObject *obj_class;
3588 int p;
3589
3590 /* Sanity checks. */
3591 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003592 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003593 return -1;
3594 }
3595
3596 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003597 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003598 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003599 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003600 return -1;
3601 }
3602
3603 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003604 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003605 p = obj_class != cls; /* true iff a problem */
3606 Py_DECREF(obj_class);
3607 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003608 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 "__newobj__ args has the wrong class");
3610 return -1;
3611 }
3612 }
3613 /* XXX: These calls save() are prone to infinite recursion. Imagine
3614 what happen if the value returned by the __reduce__() method of
3615 some extension type contains another object of the same type. Ouch!
3616
3617 Here is a quick example, that I ran into, to illustrate what I
3618 mean:
3619
3620 >>> import pickle, copyreg
3621 >>> copyreg.dispatch_table.pop(complex)
3622 >>> pickle.dumps(1+2j)
3623 Traceback (most recent call last):
3624 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003625 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003626
3627 Removing the complex class from copyreg.dispatch_table made the
3628 __reduce_ex__() method emit another complex object:
3629
3630 >>> (1+1j).__reduce_ex__(2)
3631 (<function __newobj__ at 0xb7b71c3c>,
3632 (<class 'complex'>, (1+1j)), None, None, None)
3633
3634 Thus when save() was called on newargstup (the 2nd item) recursion
3635 ensued. Of course, the bug was in the complex class which had a
3636 broken __getnewargs__() that emitted another complex object. But,
3637 the point, here, is it is quite easy to end up with a broken reduce
3638 function. */
3639
3640 /* Save the class and its __new__ arguments. */
3641 if (save(self, cls, 0) < 0)
3642 return -1;
3643
3644 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3645 if (newargtup == NULL)
3646 return -1;
3647
3648 p = save(self, newargtup, 0);
3649 Py_DECREF(newargtup);
3650 if (p < 0)
3651 return -1;
3652
3653 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003654 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003655 return -1;
3656 }
3657 else { /* Not using NEWOBJ. */
3658 if (save(self, callable, 0) < 0 ||
3659 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003660 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003661 return -1;
3662 }
3663
3664 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3665 the caller do not want to memoize the object. Not particularly useful,
3666 but that is to mimic the behavior save_reduce() in pickle.py when
3667 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003668 if (obj != NULL) {
3669 /* If the object is already in the memo, this means it is
3670 recursive. In this case, throw away everything we put on the
3671 stack, and fetch the object back from the memo. */
3672 if (PyMemoTable_Get(self->memo, obj)) {
3673 const char pop_op = POP;
3674
3675 if (_Pickler_Write(self, &pop_op, 1) < 0)
3676 return -1;
3677 if (memo_get(self, obj) < 0)
3678 return -1;
3679
3680 return 0;
3681 }
3682 else if (memo_put(self, obj) < 0)
3683 return -1;
3684 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003685
3686 if (listitems && batch_list(self, listitems) < 0)
3687 return -1;
3688
3689 if (dictitems && batch_dict(self, dictitems) < 0)
3690 return -1;
3691
3692 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003693 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003694 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003695 return -1;
3696 }
3697
3698 return 0;
3699}
3700
3701static int
3702save(PicklerObject *self, PyObject *obj, int pers_save)
3703{
3704 PyTypeObject *type;
3705 PyObject *reduce_func = NULL;
3706 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003707 int status = 0;
3708
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003709 if (_Pickler_OpcodeBoundary(self) < 0)
3710 return -1;
3711
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003712 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003713 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003714
3715 /* The extra pers_save argument is necessary to avoid calling save_pers()
3716 on its returned object. */
3717 if (!pers_save && self->pers_func) {
3718 /* save_pers() returns:
3719 -1 to signal an error;
3720 0 if it did nothing successfully;
3721 1 if a persistent id was saved.
3722 */
3723 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3724 goto done;
3725 }
3726
3727 type = Py_TYPE(obj);
3728
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003729 /* The old cPickle had an optimization that used switch-case statement
3730 dispatching on the first letter of the type name. This has was removed
3731 since benchmarks shown that this optimization was actually slowing
3732 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003733
3734 /* Atom types; these aren't memoized, so don't check the memo. */
3735
3736 if (obj == Py_None) {
3737 status = save_none(self, obj);
3738 goto done;
3739 }
3740 else if (obj == Py_False || obj == Py_True) {
3741 status = save_bool(self, obj);
3742 goto done;
3743 }
3744 else if (type == &PyLong_Type) {
3745 status = save_long(self, obj);
3746 goto done;
3747 }
3748 else if (type == &PyFloat_Type) {
3749 status = save_float(self, obj);
3750 goto done;
3751 }
3752
3753 /* Check the memo to see if it has the object. If so, generate
3754 a GET (or BINGET) opcode, instead of pickling the object
3755 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003756 if (PyMemoTable_Get(self->memo, obj)) {
3757 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003758 goto error;
3759 goto done;
3760 }
3761
3762 if (type == &PyBytes_Type) {
3763 status = save_bytes(self, obj);
3764 goto done;
3765 }
3766 else if (type == &PyUnicode_Type) {
3767 status = save_unicode(self, obj);
3768 goto done;
3769 }
3770 else if (type == &PyDict_Type) {
3771 status = save_dict(self, obj);
3772 goto done;
3773 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003774 else if (type == &PySet_Type) {
3775 status = save_set(self, obj);
3776 goto done;
3777 }
3778 else if (type == &PyFrozenSet_Type) {
3779 status = save_frozenset(self, obj);
3780 goto done;
3781 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003782 else if (type == &PyList_Type) {
3783 status = save_list(self, obj);
3784 goto done;
3785 }
3786 else if (type == &PyTuple_Type) {
3787 status = save_tuple(self, obj);
3788 goto done;
3789 }
3790 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003791 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003792 goto done;
3793 }
3794 else if (type == &PyFunction_Type) {
3795 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003796 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003797 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003798
3799 /* XXX: This part needs some unit tests. */
3800
3801 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003802 * self.dispatch_table, copyreg.dispatch_table, the object's
3803 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003804 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003805 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003806 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003807 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3808 (PyObject *)type);
3809 if (reduce_func == NULL) {
3810 if (PyErr_Occurred()) {
3811 goto error;
3812 }
3813 } else {
3814 /* PyDict_GetItemWithError() returns a borrowed reference.
3815 Increase the reference count to be consistent with
3816 PyObject_GetItem and _PyObject_GetAttrId used below. */
3817 Py_INCREF(reduce_func);
3818 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003819 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003820 reduce_func = PyObject_GetItem(self->dispatch_table,
3821 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003822 if (reduce_func == NULL) {
3823 if (PyErr_ExceptionMatches(PyExc_KeyError))
3824 PyErr_Clear();
3825 else
3826 goto error;
3827 }
3828 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003829 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003830 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003831 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003832 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003833 else if (PyType_IsSubtype(type, &PyType_Type)) {
3834 status = save_global(self, obj, NULL);
3835 goto done;
3836 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003837 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003838 _Py_IDENTIFIER(__reduce__);
3839 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003840
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841
3842 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3843 automatically defined as __reduce__. While this is convenient, this
3844 make it impossible to know which method was actually called. Of
3845 course, this is not a big deal. But still, it would be nice to let
3846 the user know which method was called when something go
3847 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3848 don't actually have to check for a __reduce__ method. */
3849
3850 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003851 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003852 if (reduce_func != NULL) {
3853 PyObject *proto;
3854 proto = PyLong_FromLong(self->proto);
3855 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003856 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003857 }
3858 }
3859 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003860 PickleState *st = _Pickle_GetGlobalState();
3861
3862 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003863 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003864 }
3865 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003867 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003868 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003869 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003870 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003871 PyObject *empty_tuple = PyTuple_New(0);
3872 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003873 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003874 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003875 }
3876 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003877 PyErr_Format(st->PicklingError,
3878 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003879 type->tp_name, obj);
3880 goto error;
3881 }
3882 }
3883 }
3884
3885 if (reduce_value == NULL)
3886 goto error;
3887
3888 if (PyUnicode_Check(reduce_value)) {
3889 status = save_global(self, obj, reduce_value);
3890 goto done;
3891 }
3892
3893 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003894 PickleState *st = _Pickle_GetGlobalState();
3895 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003896 "__reduce__ must return a string or tuple");
3897 goto error;
3898 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899
3900 status = save_reduce(self, reduce_value, obj);
3901
3902 if (0) {
3903 error:
3904 status = -1;
3905 }
3906 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003907
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003908 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909 Py_XDECREF(reduce_func);
3910 Py_XDECREF(reduce_value);
3911
3912 return status;
3913}
3914
3915static int
3916dump(PicklerObject *self, PyObject *obj)
3917{
3918 const char stop_op = STOP;
3919
3920 if (self->proto >= 2) {
3921 char header[2];
3922
3923 header[0] = PROTO;
3924 assert(self->proto >= 0 && self->proto < 256);
3925 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003926 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003928 if (self->proto >= 4)
3929 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003930 }
3931
3932 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003933 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 return -1;
3935
3936 return 0;
3937}
3938
Larry Hastings61272b72014-01-07 12:41:53 -08003939/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003940
3941_pickle.Pickler.clear_memo
3942
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003943Clears the pickler's "memo".
3944
3945The memo is the data structure that remembers which objects the
3946pickler has already seen, so that shared or recursive objects are
3947pickled by reference and not by value. This method is useful when
3948re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003949[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003950
Larry Hastings3cceb382014-01-04 11:09:09 -08003951static PyObject *
3952_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003953/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003954{
3955 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003956 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003957
3958 Py_RETURN_NONE;
3959}
3960
Larry Hastings61272b72014-01-07 12:41:53 -08003961/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003962
3963_pickle.Pickler.dump
3964
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003965 obj: object
3966 /
3967
3968Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003969[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003970
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003971static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003972_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003973/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003975 /* Check whether the Pickler was initialized correctly (issue3664).
3976 Developers often forget to call __init__() in their subclasses, which
3977 would trigger a segfault without this check. */
3978 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003979 PickleState *st = _Pickle_GetGlobalState();
3980 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003981 "Pickler.__init__() was not called by %s.__init__()",
3982 Py_TYPE(self)->tp_name);
3983 return NULL;
3984 }
3985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003986 if (_Pickler_ClearBuffer(self) < 0)
3987 return NULL;
3988
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003989 if (dump(self, obj) < 0)
3990 return NULL;
3991
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003992 if (_Pickler_FlushToFile(self) < 0)
3993 return NULL;
3994
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003995 Py_RETURN_NONE;
3996}
3997
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02003998/*[clinic input]
3999
4000_pickle.Pickler.__sizeof__ -> Py_ssize_t
4001
4002Returns size in memory, in bytes.
4003[clinic start generated code]*/
4004
4005static Py_ssize_t
4006_pickle_Pickler___sizeof___impl(PicklerObject *self)
4007/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4008{
4009 Py_ssize_t res, s;
4010
4011 res = sizeof(PicklerObject);
4012 if (self->memo != NULL) {
4013 res += sizeof(PyMemoTable);
4014 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4015 }
4016 if (self->output_buffer != NULL) {
4017 s = _PySys_GetSizeOf(self->output_buffer);
4018 if (s == -1)
4019 return -1;
4020 res += s;
4021 }
4022 return res;
4023}
4024
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004025static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004026 _PICKLE_PICKLER_DUMP_METHODDEF
4027 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004028 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029 {NULL, NULL} /* sentinel */
4030};
4031
4032static void
4033Pickler_dealloc(PicklerObject *self)
4034{
4035 PyObject_GC_UnTrack(self);
4036
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004037 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004038 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004039 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004040 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004041 Py_XDECREF(self->fast_memo);
4042
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004043 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004044
4045 Py_TYPE(self)->tp_free((PyObject *)self);
4046}
4047
4048static int
4049Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4050{
4051 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004052 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004053 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004054 Py_VISIT(self->fast_memo);
4055 return 0;
4056}
4057
4058static int
4059Pickler_clear(PicklerObject *self)
4060{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004061 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004062 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004063 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004064 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 Py_CLEAR(self->fast_memo);
4066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004067 if (self->memo != NULL) {
4068 PyMemoTable *memo = self->memo;
4069 self->memo = NULL;
4070 PyMemoTable_Del(memo);
4071 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004072 return 0;
4073}
4074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004075
Larry Hastings61272b72014-01-07 12:41:53 -08004076/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004077
4078_pickle.Pickler.__init__
4079
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004080 file: object
4081 protocol: object = NULL
4082 fix_imports: bool = True
4083
4084This takes a binary file for writing a pickle data stream.
4085
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004086The optional *protocol* argument tells the pickler to use the given
4087protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4088protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004089
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004090Specifying a negative protocol version selects the highest protocol
4091version supported. The higher the protocol used, the more recent the
4092version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004093
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004094The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004095bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004096writing, a io.BytesIO instance, or any other custom object that meets
4097this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004098
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004099If *fix_imports* is True and protocol is less than 3, pickle will try
4100to map the new Python 3 names to the old module names used in Python
41012, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004102[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004103
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004104static int
Larry Hastings89964c42015-04-14 18:07:59 -04004105_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4106 PyObject *protocol, int fix_imports)
4107/*[clinic end generated code: output=b5f31078dab17fb0 input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004108{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004109 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004110 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004111
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004112 /* In case of multiple __init__() calls, clear previous content. */
4113 if (self->write != NULL)
4114 (void)Pickler_clear(self);
4115
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004116 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004117 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004118
4119 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004120 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004121
4122 /* memo and output_buffer may have already been created in _Pickler_New */
4123 if (self->memo == NULL) {
4124 self->memo = PyMemoTable_New();
4125 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004126 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004127 }
4128 self->output_len = 0;
4129 if (self->output_buffer == NULL) {
4130 self->max_output_len = WRITE_BUF_SIZE;
4131 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4132 self->max_output_len);
4133 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004134 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004135 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004136
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004137 self->fast = 0;
4138 self->fast_nesting = 0;
4139 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004141 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4142 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4143 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004144 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004145 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004146 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004147 self->dispatch_table = NULL;
4148 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4149 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4150 &PyId_dispatch_table);
4151 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004152 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004153 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004154
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004155 return 0;
4156}
4157
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004158
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004159/* Define a proxy object for the Pickler's internal memo object. This is to
4160 * avoid breaking code like:
4161 * pickler.memo.clear()
4162 * and
4163 * pickler.memo = saved_memo
4164 * Is this a good idea? Not really, but we don't want to break code that uses
4165 * it. Note that we don't implement the entire mapping API here. This is
4166 * intentional, as these should be treated as black-box implementation details.
4167 */
4168
Larry Hastings61272b72014-01-07 12:41:53 -08004169/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170_pickle.PicklerMemoProxy.clear
4171
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004173[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174
Larry Hastings3cceb382014-01-04 11:09:09 -08004175static PyObject *
4176_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004177/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004178{
4179 if (self->pickler->memo)
4180 PyMemoTable_Clear(self->pickler->memo);
4181 Py_RETURN_NONE;
4182}
4183
Larry Hastings61272b72014-01-07 12:41:53 -08004184/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185_pickle.PicklerMemoProxy.copy
4186
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004187Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004188[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004189
Larry Hastings3cceb382014-01-04 11:09:09 -08004190static PyObject *
4191_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004192/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004193{
4194 Py_ssize_t i;
4195 PyMemoTable *memo;
4196 PyObject *new_memo = PyDict_New();
4197 if (new_memo == NULL)
4198 return NULL;
4199
4200 memo = self->pickler->memo;
4201 for (i = 0; i < memo->mt_allocated; ++i) {
4202 PyMemoEntry entry = memo->mt_table[i];
4203 if (entry.me_key != NULL) {
4204 int status;
4205 PyObject *key, *value;
4206
4207 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004208 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004209
4210 if (key == NULL || value == NULL) {
4211 Py_XDECREF(key);
4212 Py_XDECREF(value);
4213 goto error;
4214 }
4215 status = PyDict_SetItem(new_memo, key, value);
4216 Py_DECREF(key);
4217 Py_DECREF(value);
4218 if (status < 0)
4219 goto error;
4220 }
4221 }
4222 return new_memo;
4223
4224 error:
4225 Py_XDECREF(new_memo);
4226 return NULL;
4227}
4228
Larry Hastings61272b72014-01-07 12:41:53 -08004229/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004230_pickle.PicklerMemoProxy.__reduce__
4231
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004232Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004233[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004234
Larry Hastings3cceb382014-01-04 11:09:09 -08004235static PyObject *
4236_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004237/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004238{
4239 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004240 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004241 if (contents == NULL)
4242 return NULL;
4243
4244 reduce_value = PyTuple_New(2);
4245 if (reduce_value == NULL) {
4246 Py_DECREF(contents);
4247 return NULL;
4248 }
4249 dict_args = PyTuple_New(1);
4250 if (dict_args == NULL) {
4251 Py_DECREF(contents);
4252 Py_DECREF(reduce_value);
4253 return NULL;
4254 }
4255 PyTuple_SET_ITEM(dict_args, 0, contents);
4256 Py_INCREF((PyObject *)&PyDict_Type);
4257 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4258 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4259 return reduce_value;
4260}
4261
4262static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004263 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4264 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4265 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004266 {NULL, NULL} /* sentinel */
4267};
4268
4269static void
4270PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4271{
4272 PyObject_GC_UnTrack(self);
4273 Py_XDECREF(self->pickler);
4274 PyObject_GC_Del((PyObject *)self);
4275}
4276
4277static int
4278PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4279 visitproc visit, void *arg)
4280{
4281 Py_VISIT(self->pickler);
4282 return 0;
4283}
4284
4285static int
4286PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4287{
4288 Py_CLEAR(self->pickler);
4289 return 0;
4290}
4291
4292static PyTypeObject PicklerMemoProxyType = {
4293 PyVarObject_HEAD_INIT(NULL, 0)
4294 "_pickle.PicklerMemoProxy", /*tp_name*/
4295 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4296 0,
4297 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4298 0, /* tp_print */
4299 0, /* tp_getattr */
4300 0, /* tp_setattr */
4301 0, /* tp_compare */
4302 0, /* tp_repr */
4303 0, /* tp_as_number */
4304 0, /* tp_as_sequence */
4305 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004306 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004307 0, /* tp_call */
4308 0, /* tp_str */
4309 PyObject_GenericGetAttr, /* tp_getattro */
4310 PyObject_GenericSetAttr, /* tp_setattro */
4311 0, /* tp_as_buffer */
4312 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4313 0, /* tp_doc */
4314 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4315 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4316 0, /* tp_richcompare */
4317 0, /* tp_weaklistoffset */
4318 0, /* tp_iter */
4319 0, /* tp_iternext */
4320 picklerproxy_methods, /* tp_methods */
4321};
4322
4323static PyObject *
4324PicklerMemoProxy_New(PicklerObject *pickler)
4325{
4326 PicklerMemoProxyObject *self;
4327
4328 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4329 if (self == NULL)
4330 return NULL;
4331 Py_INCREF(pickler);
4332 self->pickler = pickler;
4333 PyObject_GC_Track(self);
4334 return (PyObject *)self;
4335}
4336
4337/*****************************************************************************/
4338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004339static PyObject *
4340Pickler_get_memo(PicklerObject *self)
4341{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004342 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343}
4344
4345static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004346Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004347{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004348 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004351 PyErr_SetString(PyExc_TypeError,
4352 "attribute deletion is not supported");
4353 return -1;
4354 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004355
4356 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4357 PicklerObject *pickler =
4358 ((PicklerMemoProxyObject *)obj)->pickler;
4359
4360 new_memo = PyMemoTable_Copy(pickler->memo);
4361 if (new_memo == NULL)
4362 return -1;
4363 }
4364 else if (PyDict_Check(obj)) {
4365 Py_ssize_t i = 0;
4366 PyObject *key, *value;
4367
4368 new_memo = PyMemoTable_New();
4369 if (new_memo == NULL)
4370 return -1;
4371
4372 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004373 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004374 PyObject *memo_obj;
4375
4376 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4377 PyErr_SetString(PyExc_TypeError,
4378 "'memo' values must be 2-item tuples");
4379 goto error;
4380 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004381 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004382 if (memo_id == -1 && PyErr_Occurred())
4383 goto error;
4384 memo_obj = PyTuple_GET_ITEM(value, 1);
4385 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4386 goto error;
4387 }
4388 }
4389 else {
4390 PyErr_Format(PyExc_TypeError,
4391 "'memo' attribute must be an PicklerMemoProxy object"
4392 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004393 return -1;
4394 }
4395
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004396 PyMemoTable_Del(self->memo);
4397 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004398
4399 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004400
4401 error:
4402 if (new_memo)
4403 PyMemoTable_Del(new_memo);
4404 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004405}
4406
4407static PyObject *
4408Pickler_get_persid(PicklerObject *self)
4409{
4410 if (self->pers_func == NULL)
4411 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4412 else
4413 Py_INCREF(self->pers_func);
4414 return self->pers_func;
4415}
4416
4417static int
4418Pickler_set_persid(PicklerObject *self, PyObject *value)
4419{
4420 PyObject *tmp;
4421
4422 if (value == NULL) {
4423 PyErr_SetString(PyExc_TypeError,
4424 "attribute deletion is not supported");
4425 return -1;
4426 }
4427 if (!PyCallable_Check(value)) {
4428 PyErr_SetString(PyExc_TypeError,
4429 "persistent_id must be a callable taking one argument");
4430 return -1;
4431 }
4432
4433 tmp = self->pers_func;
4434 Py_INCREF(value);
4435 self->pers_func = value;
4436 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4437
4438 return 0;
4439}
4440
4441static PyMemberDef Pickler_members[] = {
4442 {"bin", T_INT, offsetof(PicklerObject, bin)},
4443 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004444 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004445 {NULL}
4446};
4447
4448static PyGetSetDef Pickler_getsets[] = {
4449 {"memo", (getter)Pickler_get_memo,
4450 (setter)Pickler_set_memo},
4451 {"persistent_id", (getter)Pickler_get_persid,
4452 (setter)Pickler_set_persid},
4453 {NULL}
4454};
4455
4456static PyTypeObject Pickler_Type = {
4457 PyVarObject_HEAD_INIT(NULL, 0)
4458 "_pickle.Pickler" , /*tp_name*/
4459 sizeof(PicklerObject), /*tp_basicsize*/
4460 0, /*tp_itemsize*/
4461 (destructor)Pickler_dealloc, /*tp_dealloc*/
4462 0, /*tp_print*/
4463 0, /*tp_getattr*/
4464 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004465 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004466 0, /*tp_repr*/
4467 0, /*tp_as_number*/
4468 0, /*tp_as_sequence*/
4469 0, /*tp_as_mapping*/
4470 0, /*tp_hash*/
4471 0, /*tp_call*/
4472 0, /*tp_str*/
4473 0, /*tp_getattro*/
4474 0, /*tp_setattro*/
4475 0, /*tp_as_buffer*/
4476 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004477 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004478 (traverseproc)Pickler_traverse, /*tp_traverse*/
4479 (inquiry)Pickler_clear, /*tp_clear*/
4480 0, /*tp_richcompare*/
4481 0, /*tp_weaklistoffset*/
4482 0, /*tp_iter*/
4483 0, /*tp_iternext*/
4484 Pickler_methods, /*tp_methods*/
4485 Pickler_members, /*tp_members*/
4486 Pickler_getsets, /*tp_getset*/
4487 0, /*tp_base*/
4488 0, /*tp_dict*/
4489 0, /*tp_descr_get*/
4490 0, /*tp_descr_set*/
4491 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004492 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004493 PyType_GenericAlloc, /*tp_alloc*/
4494 PyType_GenericNew, /*tp_new*/
4495 PyObject_GC_Del, /*tp_free*/
4496 0, /*tp_is_gc*/
4497};
4498
Victor Stinner121aab42011-09-29 23:40:53 +02004499/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004500
4501 XXX: It would be nice to able to avoid Python function call overhead, by
4502 using directly the C version of find_class(), when find_class() is not
4503 overridden by a subclass. Although, this could become rather hackish. A
4504 simpler optimization would be to call the C function when self is not a
4505 subclass instance. */
4506static PyObject *
4507find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4508{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004509 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004510
4511 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4512 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004513}
4514
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004515static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004516marker(UnpicklerObject *self)
4517{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004518 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004519 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004520 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004521 return -1;
4522 }
4523
4524 return self->marks[--self->num_marks];
4525}
4526
4527static int
4528load_none(UnpicklerObject *self)
4529{
4530 PDATA_APPEND(self->stack, Py_None, -1);
4531 return 0;
4532}
4533
4534static int
4535bad_readline(void)
4536{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004537 PickleState *st = _Pickle_GetGlobalState();
4538 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004539 return -1;
4540}
4541
4542static int
4543load_int(UnpicklerObject *self)
4544{
4545 PyObject *value;
4546 char *endptr, *s;
4547 Py_ssize_t len;
4548 long x;
4549
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004550 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004551 return -1;
4552 if (len < 2)
4553 return bad_readline();
4554
4555 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004556 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004557 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004558 x = strtol(s, &endptr, 0);
4559
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004560 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004561 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004562 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563 errno = 0;
4564 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004565 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566 if (value == NULL) {
4567 PyErr_SetString(PyExc_ValueError,
4568 "could not convert string to int");
4569 return -1;
4570 }
4571 }
4572 else {
4573 if (len == 3 && (x == 0 || x == 1)) {
4574 if ((value = PyBool_FromLong(x)) == NULL)
4575 return -1;
4576 }
4577 else {
4578 if ((value = PyLong_FromLong(x)) == NULL)
4579 return -1;
4580 }
4581 }
4582
4583 PDATA_PUSH(self->stack, value, -1);
4584 return 0;
4585}
4586
4587static int
4588load_bool(UnpicklerObject *self, PyObject *boolean)
4589{
4590 assert(boolean == Py_True || boolean == Py_False);
4591 PDATA_APPEND(self->stack, boolean, -1);
4592 return 0;
4593}
4594
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004595/* s contains x bytes of an unsigned little-endian integer. Return its value
4596 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4597 */
4598static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004599calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004600{
4601 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004602 Py_ssize_t i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004603 size_t x = 0;
4604
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004605 for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004606 x |= (size_t) s[i] << (8 * i);
4607 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004608
4609 if (x > PY_SSIZE_T_MAX)
4610 return -1;
4611 else
4612 return (Py_ssize_t) x;
4613}
4614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004615/* s contains x bytes of a little-endian integer. Return its value as a
4616 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4617 * int, but when x is 4 it's a signed one. This is an historical source
4618 * of x-platform bugs.
4619 */
4620static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004621calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622{
4623 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004624 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004625 long x = 0;
4626
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004627 for (i = 0; i < nbytes; i++) {
4628 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629 }
4630
4631 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4632 * is signed, so on a box with longs bigger than 4 bytes we need
4633 * to extend a BININT's sign bit to the full width.
4634 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004635 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 x |= -(x & (1L << 31));
4637 }
4638
4639 return x;
4640}
4641
4642static int
4643load_binintx(UnpicklerObject *self, char *s, int size)
4644{
4645 PyObject *value;
4646 long x;
4647
4648 x = calc_binint(s, size);
4649
4650 if ((value = PyLong_FromLong(x)) == NULL)
4651 return -1;
4652
4653 PDATA_PUSH(self->stack, value, -1);
4654 return 0;
4655}
4656
4657static int
4658load_binint(UnpicklerObject *self)
4659{
4660 char *s;
4661
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004662 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004663 return -1;
4664
4665 return load_binintx(self, s, 4);
4666}
4667
4668static int
4669load_binint1(UnpicklerObject *self)
4670{
4671 char *s;
4672
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004673 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004674 return -1;
4675
4676 return load_binintx(self, s, 1);
4677}
4678
4679static int
4680load_binint2(UnpicklerObject *self)
4681{
4682 char *s;
4683
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004684 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004685 return -1;
4686
4687 return load_binintx(self, s, 2);
4688}
4689
4690static int
4691load_long(UnpicklerObject *self)
4692{
4693 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004694 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695 Py_ssize_t len;
4696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004697 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004698 return -1;
4699 if (len < 2)
4700 return bad_readline();
4701
Mark Dickinson8dd05142009-01-20 20:43:58 +00004702 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4703 the 'L' before calling PyLong_FromString. In order to maintain
4704 compatibility with Python 3.0.0, we don't actually *require*
4705 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004706 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004707 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004708 /* XXX: Should the base argument explicitly set to 10? */
4709 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004710 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711 return -1;
4712
4713 PDATA_PUSH(self->stack, value, -1);
4714 return 0;
4715}
4716
4717/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4718 * data following.
4719 */
4720static int
4721load_counted_long(UnpicklerObject *self, int size)
4722{
4723 PyObject *value;
4724 char *nbytes;
4725 char *pdata;
4726
4727 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004728 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004729 return -1;
4730
4731 size = calc_binint(nbytes, size);
4732 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004733 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004734 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004735 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004736 "LONG pickle has negative byte count");
4737 return -1;
4738 }
4739
4740 if (size == 0)
4741 value = PyLong_FromLong(0L);
4742 else {
4743 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004744 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004745 return -1;
4746 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4747 1 /* little endian */ , 1 /* signed */ );
4748 }
4749 if (value == NULL)
4750 return -1;
4751 PDATA_PUSH(self->stack, value, -1);
4752 return 0;
4753}
4754
4755static int
4756load_float(UnpicklerObject *self)
4757{
4758 PyObject *value;
4759 char *endptr, *s;
4760 Py_ssize_t len;
4761 double d;
4762
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004763 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 return -1;
4765 if (len < 2)
4766 return bad_readline();
4767
4768 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004769 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4770 if (d == -1.0 && PyErr_Occurred())
4771 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004772 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4774 return -1;
4775 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004776 value = PyFloat_FromDouble(d);
4777 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778 return -1;
4779
4780 PDATA_PUSH(self->stack, value, -1);
4781 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004782}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004783
4784static int
4785load_binfloat(UnpicklerObject *self)
4786{
4787 PyObject *value;
4788 double x;
4789 char *s;
4790
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004791 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004792 return -1;
4793
4794 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4795 if (x == -1.0 && PyErr_Occurred())
4796 return -1;
4797
4798 if ((value = PyFloat_FromDouble(x)) == NULL)
4799 return -1;
4800
4801 PDATA_PUSH(self->stack, value, -1);
4802 return 0;
4803}
4804
4805static int
4806load_string(UnpicklerObject *self)
4807{
4808 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004809 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004810 Py_ssize_t len;
4811 char *s, *p;
4812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004813 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004815 /* Strip the newline */
4816 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004817 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004818 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004819 p = s + 1;
4820 len -= 2;
4821 }
4822 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004823 PickleState *st = _Pickle_GetGlobalState();
4824 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004825 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004826 return -1;
4827 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004828 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829
4830 /* Use the PyBytes API to decode the string, since that is what is used
4831 to encode, and then coerce the result to Unicode. */
4832 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004833 if (bytes == NULL)
4834 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004835
4836 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4837 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4838 if (strcmp(self->encoding, "bytes") == 0) {
4839 obj = bytes;
4840 }
4841 else {
4842 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4843 Py_DECREF(bytes);
4844 if (obj == NULL) {
4845 return -1;
4846 }
4847 }
4848
4849 PDATA_PUSH(self->stack, obj, -1);
4850 return 0;
4851}
4852
4853static int
4854load_counted_binstring(UnpicklerObject *self, int nbytes)
4855{
4856 PyObject *obj;
4857 Py_ssize_t size;
4858 char *s;
4859
4860 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004861 return -1;
4862
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004863 size = calc_binsize(s, nbytes);
4864 if (size < 0) {
4865 PickleState *st = _Pickle_GetGlobalState();
4866 PyErr_Format(st->UnpicklingError,
4867 "BINSTRING exceeds system's maximum size of %zd bytes",
4868 PY_SSIZE_T_MAX);
4869 return -1;
4870 }
4871
4872 if (_Unpickler_Read(self, &s, size) < 0)
4873 return -1;
4874
4875 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4876 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4877 if (strcmp(self->encoding, "bytes") == 0) {
4878 obj = PyBytes_FromStringAndSize(s, size);
4879 }
4880 else {
4881 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4882 }
4883 if (obj == NULL) {
4884 return -1;
4885 }
4886
4887 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004888 return 0;
4889}
4890
4891static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004892load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893{
4894 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004895 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 char *s;
4897
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004898 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 return -1;
4900
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004901 size = calc_binsize(s, nbytes);
4902 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004903 PyErr_Format(PyExc_OverflowError,
4904 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004905 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 return -1;
4907 }
4908
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004909 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004911
4912 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004913 if (bytes == NULL)
4914 return -1;
4915
4916 PDATA_PUSH(self->stack, bytes, -1);
4917 return 0;
4918}
4919
4920static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004921load_unicode(UnpicklerObject *self)
4922{
4923 PyObject *str;
4924 Py_ssize_t len;
4925 char *s;
4926
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004927 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004928 return -1;
4929 if (len < 1)
4930 return bad_readline();
4931
4932 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4933 if (str == NULL)
4934 return -1;
4935
4936 PDATA_PUSH(self->stack, str, -1);
4937 return 0;
4938}
4939
4940static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004941load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004942{
4943 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004944 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004945 char *s;
4946
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004947 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004948 return -1;
4949
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004950 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004951 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004952 PyErr_Format(PyExc_OverflowError,
4953 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004954 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004955 return -1;
4956 }
4957
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004958 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 return -1;
4960
Victor Stinner485fb562010-04-13 11:07:24 +00004961 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004962 if (str == NULL)
4963 return -1;
4964
4965 PDATA_PUSH(self->stack, str, -1);
4966 return 0;
4967}
4968
4969static int
4970load_tuple(UnpicklerObject *self)
4971{
4972 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004973 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004974
4975 if ((i = marker(self)) < 0)
4976 return -1;
4977
4978 tuple = Pdata_poptuple(self->stack, i);
4979 if (tuple == NULL)
4980 return -1;
4981 PDATA_PUSH(self->stack, tuple, -1);
4982 return 0;
4983}
4984
4985static int
4986load_counted_tuple(UnpicklerObject *self, int len)
4987{
4988 PyObject *tuple;
4989
4990 tuple = PyTuple_New(len);
4991 if (tuple == NULL)
4992 return -1;
4993
4994 while (--len >= 0) {
4995 PyObject *item;
4996
4997 PDATA_POP(self->stack, item);
4998 if (item == NULL)
4999 return -1;
5000 PyTuple_SET_ITEM(tuple, len, item);
5001 }
5002 PDATA_PUSH(self->stack, tuple, -1);
5003 return 0;
5004}
5005
5006static int
5007load_empty_list(UnpicklerObject *self)
5008{
5009 PyObject *list;
5010
5011 if ((list = PyList_New(0)) == NULL)
5012 return -1;
5013 PDATA_PUSH(self->stack, list, -1);
5014 return 0;
5015}
5016
5017static int
5018load_empty_dict(UnpicklerObject *self)
5019{
5020 PyObject *dict;
5021
5022 if ((dict = PyDict_New()) == NULL)
5023 return -1;
5024 PDATA_PUSH(self->stack, dict, -1);
5025 return 0;
5026}
5027
5028static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005029load_empty_set(UnpicklerObject *self)
5030{
5031 PyObject *set;
5032
5033 if ((set = PySet_New(NULL)) == NULL)
5034 return -1;
5035 PDATA_PUSH(self->stack, set, -1);
5036 return 0;
5037}
5038
5039static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040load_list(UnpicklerObject *self)
5041{
5042 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005043 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044
5045 if ((i = marker(self)) < 0)
5046 return -1;
5047
5048 list = Pdata_poplist(self->stack, i);
5049 if (list == NULL)
5050 return -1;
5051 PDATA_PUSH(self->stack, list, -1);
5052 return 0;
5053}
5054
5055static int
5056load_dict(UnpicklerObject *self)
5057{
5058 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005059 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060
5061 if ((i = marker(self)) < 0)
5062 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005063 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064
5065 if ((dict = PyDict_New()) == NULL)
5066 return -1;
5067
5068 for (k = i + 1; k < j; k += 2) {
5069 key = self->stack->data[k - 1];
5070 value = self->stack->data[k];
5071 if (PyDict_SetItem(dict, key, value) < 0) {
5072 Py_DECREF(dict);
5073 return -1;
5074 }
5075 }
5076 Pdata_clear(self->stack, i);
5077 PDATA_PUSH(self->stack, dict, -1);
5078 return 0;
5079}
5080
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005081static int
5082load_frozenset(UnpicklerObject *self)
5083{
5084 PyObject *items;
5085 PyObject *frozenset;
5086 Py_ssize_t i;
5087
5088 if ((i = marker(self)) < 0)
5089 return -1;
5090
5091 items = Pdata_poptuple(self->stack, i);
5092 if (items == NULL)
5093 return -1;
5094
5095 frozenset = PyFrozenSet_New(items);
5096 Py_DECREF(items);
5097 if (frozenset == NULL)
5098 return -1;
5099
5100 PDATA_PUSH(self->stack, frozenset, -1);
5101 return 0;
5102}
5103
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005104static PyObject *
5105instantiate(PyObject *cls, PyObject *args)
5106{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005107 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005108 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005109 /* Caller must assure args are a tuple. Normally, args come from
5110 Pdata_poptuple which packs objects from the top of the stack
5111 into a newly created tuple. */
5112 assert(PyTuple_Check(args));
5113 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005114 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005115 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005116 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005117 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005118 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005119
5120 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005121 }
5122 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005123}
5124
5125static int
5126load_obj(UnpicklerObject *self)
5127{
5128 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005129 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005130
5131 if ((i = marker(self)) < 0)
5132 return -1;
5133
5134 args = Pdata_poptuple(self->stack, i + 1);
5135 if (args == NULL)
5136 return -1;
5137
5138 PDATA_POP(self->stack, cls);
5139 if (cls) {
5140 obj = instantiate(cls, args);
5141 Py_DECREF(cls);
5142 }
5143 Py_DECREF(args);
5144 if (obj == NULL)
5145 return -1;
5146
5147 PDATA_PUSH(self->stack, obj, -1);
5148 return 0;
5149}
5150
5151static int
5152load_inst(UnpicklerObject *self)
5153{
5154 PyObject *cls = NULL;
5155 PyObject *args = NULL;
5156 PyObject *obj = NULL;
5157 PyObject *module_name;
5158 PyObject *class_name;
5159 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005160 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005161 char *s;
5162
5163 if ((i = marker(self)) < 0)
5164 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005165 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005166 return -1;
5167 if (len < 2)
5168 return bad_readline();
5169
5170 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5171 identifiers are permitted in Python 3.0, since the INST opcode is only
5172 supported by older protocols on Python 2.x. */
5173 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5174 if (module_name == NULL)
5175 return -1;
5176
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005177 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005178 if (len < 2)
5179 return bad_readline();
5180 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005181 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 cls = find_class(self, module_name, class_name);
5183 Py_DECREF(class_name);
5184 }
5185 }
5186 Py_DECREF(module_name);
5187
5188 if (cls == NULL)
5189 return -1;
5190
5191 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5192 obj = instantiate(cls, args);
5193 Py_DECREF(args);
5194 }
5195 Py_DECREF(cls);
5196
5197 if (obj == NULL)
5198 return -1;
5199
5200 PDATA_PUSH(self->stack, obj, -1);
5201 return 0;
5202}
5203
5204static int
5205load_newobj(UnpicklerObject *self)
5206{
5207 PyObject *args = NULL;
5208 PyObject *clsraw = NULL;
5209 PyTypeObject *cls; /* clsraw cast to its true type */
5210 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005211 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212
5213 /* Stack is ... cls argtuple, and we want to call
5214 * cls.__new__(cls, *argtuple).
5215 */
5216 PDATA_POP(self->stack, args);
5217 if (args == NULL)
5218 goto error;
5219 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005220 PyErr_SetString(st->UnpicklingError,
5221 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 goto error;
5223 }
5224
5225 PDATA_POP(self->stack, clsraw);
5226 cls = (PyTypeObject *)clsraw;
5227 if (cls == NULL)
5228 goto error;
5229 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005230 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005231 "isn't a type object");
5232 goto error;
5233 }
5234 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005235 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005236 "has NULL tp_new");
5237 goto error;
5238 }
5239
5240 /* Call __new__. */
5241 obj = cls->tp_new(cls, args, NULL);
5242 if (obj == NULL)
5243 goto error;
5244
5245 Py_DECREF(args);
5246 Py_DECREF(clsraw);
5247 PDATA_PUSH(self->stack, obj, -1);
5248 return 0;
5249
5250 error:
5251 Py_XDECREF(args);
5252 Py_XDECREF(clsraw);
5253 return -1;
5254}
5255
5256static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005257load_newobj_ex(UnpicklerObject *self)
5258{
5259 PyObject *cls, *args, *kwargs;
5260 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005261 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005262
5263 PDATA_POP(self->stack, kwargs);
5264 if (kwargs == NULL) {
5265 return -1;
5266 }
5267 PDATA_POP(self->stack, args);
5268 if (args == NULL) {
5269 Py_DECREF(kwargs);
5270 return -1;
5271 }
5272 PDATA_POP(self->stack, cls);
5273 if (cls == NULL) {
5274 Py_DECREF(kwargs);
5275 Py_DECREF(args);
5276 return -1;
5277 }
Larry Hastings61272b72014-01-07 12:41:53 -08005278
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005279 if (!PyType_Check(cls)) {
5280 Py_DECREF(kwargs);
5281 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005282 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005283 "NEWOBJ_EX class argument must be a type, not %.200s",
5284 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005285 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005286 return -1;
5287 }
5288
5289 if (((PyTypeObject *)cls)->tp_new == NULL) {
5290 Py_DECREF(kwargs);
5291 Py_DECREF(args);
5292 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005293 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005294 "NEWOBJ_EX class argument doesn't have __new__");
5295 return -1;
5296 }
5297 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5298 Py_DECREF(kwargs);
5299 Py_DECREF(args);
5300 Py_DECREF(cls);
5301 if (obj == NULL) {
5302 return -1;
5303 }
5304 PDATA_PUSH(self->stack, obj, -1);
5305 return 0;
5306}
5307
5308static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005309load_global(UnpicklerObject *self)
5310{
5311 PyObject *global = NULL;
5312 PyObject *module_name;
5313 PyObject *global_name;
5314 Py_ssize_t len;
5315 char *s;
5316
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005317 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005318 return -1;
5319 if (len < 2)
5320 return bad_readline();
5321 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5322 if (!module_name)
5323 return -1;
5324
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005325 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005326 if (len < 2) {
5327 Py_DECREF(module_name);
5328 return bad_readline();
5329 }
5330 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5331 if (global_name) {
5332 global = find_class(self, module_name, global_name);
5333 Py_DECREF(global_name);
5334 }
5335 }
5336 Py_DECREF(module_name);
5337
5338 if (global == NULL)
5339 return -1;
5340 PDATA_PUSH(self->stack, global, -1);
5341 return 0;
5342}
5343
5344static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005345load_stack_global(UnpicklerObject *self)
5346{
5347 PyObject *global;
5348 PyObject *module_name;
5349 PyObject *global_name;
5350
5351 PDATA_POP(self->stack, global_name);
5352 PDATA_POP(self->stack, module_name);
5353 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5354 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005355 PickleState *st = _Pickle_GetGlobalState();
5356 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005357 Py_XDECREF(global_name);
5358 Py_XDECREF(module_name);
5359 return -1;
5360 }
5361 global = find_class(self, module_name, global_name);
5362 Py_DECREF(global_name);
5363 Py_DECREF(module_name);
5364 if (global == NULL)
5365 return -1;
5366 PDATA_PUSH(self->stack, global, -1);
5367 return 0;
5368}
5369
5370static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005371load_persid(UnpicklerObject *self)
5372{
5373 PyObject *pid;
5374 Py_ssize_t len;
5375 char *s;
5376
5377 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005378 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005379 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005380 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005381 return bad_readline();
5382
5383 pid = PyBytes_FromStringAndSize(s, len - 1);
5384 if (pid == NULL)
5385 return -1;
5386
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005387 /* This does not leak since _Pickle_FastCall() steals the reference
5388 to pid first. */
5389 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005390 if (pid == NULL)
5391 return -1;
5392
5393 PDATA_PUSH(self->stack, pid, -1);
5394 return 0;
5395 }
5396 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005397 PickleState *st = _Pickle_GetGlobalState();
5398 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399 "A load persistent id instruction was encountered,\n"
5400 "but no persistent_load function was specified.");
5401 return -1;
5402 }
5403}
5404
5405static int
5406load_binpersid(UnpicklerObject *self)
5407{
5408 PyObject *pid;
5409
5410 if (self->pers_func) {
5411 PDATA_POP(self->stack, pid);
5412 if (pid == NULL)
5413 return -1;
5414
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005415 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005416 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005417 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005418 if (pid == NULL)
5419 return -1;
5420
5421 PDATA_PUSH(self->stack, pid, -1);
5422 return 0;
5423 }
5424 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005425 PickleState *st = _Pickle_GetGlobalState();
5426 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005427 "A load persistent id instruction was encountered,\n"
5428 "but no persistent_load function was specified.");
5429 return -1;
5430 }
5431}
5432
5433static int
5434load_pop(UnpicklerObject *self)
5435{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005436 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005437
5438 /* Note that we split the (pickle.py) stack into two stacks,
5439 * an object stack and a mark stack. We have to be clever and
5440 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005441 * mark stack first, and only signalling a stack underflow if
5442 * the object stack is empty and the mark stack doesn't match
5443 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005444 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005445 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005447 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448 len--;
5449 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005450 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005451 } else {
5452 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005453 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005454 return 0;
5455}
5456
5457static int
5458load_pop_mark(UnpicklerObject *self)
5459{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005460 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461
5462 if ((i = marker(self)) < 0)
5463 return -1;
5464
5465 Pdata_clear(self->stack, i);
5466
5467 return 0;
5468}
5469
5470static int
5471load_dup(UnpicklerObject *self)
5472{
5473 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005474 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005476 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005477 return stack_underflow();
5478 last = self->stack->data[len - 1];
5479 PDATA_APPEND(self->stack, last, -1);
5480 return 0;
5481}
5482
5483static int
5484load_get(UnpicklerObject *self)
5485{
5486 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005487 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005488 Py_ssize_t len;
5489 char *s;
5490
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005491 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492 return -1;
5493 if (len < 2)
5494 return bad_readline();
5495
5496 key = PyLong_FromString(s, NULL, 10);
5497 if (key == NULL)
5498 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005499 idx = PyLong_AsSsize_t(key);
5500 if (idx == -1 && PyErr_Occurred()) {
5501 Py_DECREF(key);
5502 return -1;
5503 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005504
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005505 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506 if (value == NULL) {
5507 if (!PyErr_Occurred())
5508 PyErr_SetObject(PyExc_KeyError, key);
5509 Py_DECREF(key);
5510 return -1;
5511 }
5512 Py_DECREF(key);
5513
5514 PDATA_APPEND(self->stack, value, -1);
5515 return 0;
5516}
5517
5518static int
5519load_binget(UnpicklerObject *self)
5520{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005521 PyObject *value;
5522 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 char *s;
5524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005525 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005526 return -1;
5527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005528 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005529
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005530 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005532 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005533 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005535 Py_DECREF(key);
5536 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005537 return -1;
5538 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005539
5540 PDATA_APPEND(self->stack, value, -1);
5541 return 0;
5542}
5543
5544static int
5545load_long_binget(UnpicklerObject *self)
5546{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005547 PyObject *value;
5548 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005549 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 return -1;
5553
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005554 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005556 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005558 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005559 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005560 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005561 Py_DECREF(key);
5562 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563 return -1;
5564 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005565
5566 PDATA_APPEND(self->stack, value, -1);
5567 return 0;
5568}
5569
5570/* Push an object from the extension registry (EXT[124]). nbytes is
5571 * the number of bytes following the opcode, holding the index (code) value.
5572 */
5573static int
5574load_extension(UnpicklerObject *self, int nbytes)
5575{
5576 char *codebytes; /* the nbytes bytes after the opcode */
5577 long code; /* calc_binint returns long */
5578 PyObject *py_code; /* code as a Python int */
5579 PyObject *obj; /* the object to push */
5580 PyObject *pair; /* (module_name, class_name) */
5581 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005582 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005583
5584 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005585 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005586 return -1;
5587 code = calc_binint(codebytes, nbytes);
5588 if (code <= 0) { /* note that 0 is forbidden */
5589 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005590 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005591 return -1;
5592 }
5593
5594 /* Look for the code in the cache. */
5595 py_code = PyLong_FromLong(code);
5596 if (py_code == NULL)
5597 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005598 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 if (obj != NULL) {
5600 /* Bingo. */
5601 Py_DECREF(py_code);
5602 PDATA_APPEND(self->stack, obj, -1);
5603 return 0;
5604 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005605 if (PyErr_Occurred()) {
5606 Py_DECREF(py_code);
5607 return -1;
5608 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609
5610 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005611 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 if (pair == NULL) {
5613 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005614 if (!PyErr_Occurred()) {
5615 PyErr_Format(PyExc_ValueError, "unregistered extension "
5616 "code %ld", code);
5617 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618 return -1;
5619 }
5620 /* Since the extension registry is manipulable via Python code,
5621 * confirm that pair is really a 2-tuple of strings.
5622 */
5623 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5624 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5625 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5626 Py_DECREF(py_code);
5627 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5628 "isn't a 2-tuple of strings", code);
5629 return -1;
5630 }
5631 /* Load the object. */
5632 obj = find_class(self, module_name, class_name);
5633 if (obj == NULL) {
5634 Py_DECREF(py_code);
5635 return -1;
5636 }
5637 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005638 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639 Py_DECREF(py_code);
5640 if (code < 0) {
5641 Py_DECREF(obj);
5642 return -1;
5643 }
5644 PDATA_PUSH(self->stack, obj, -1);
5645 return 0;
5646}
5647
5648static int
5649load_put(UnpicklerObject *self)
5650{
5651 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005652 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653 Py_ssize_t len;
5654 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005655
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005656 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 return -1;
5658 if (len < 2)
5659 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005660 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005662 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663
5664 key = PyLong_FromString(s, NULL, 10);
5665 if (key == NULL)
5666 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005667 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005668 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005669 if (idx < 0) {
5670 if (!PyErr_Occurred())
5671 PyErr_SetString(PyExc_ValueError,
5672 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005673 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005674 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005675
5676 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005677}
5678
5679static int
5680load_binput(UnpicklerObject *self)
5681{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005682 PyObject *value;
5683 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005685
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005688
5689 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005690 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005691 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005693 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696}
5697
5698static int
5699load_long_binput(UnpicklerObject *self)
5700{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005701 PyObject *value;
5702 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005703 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005704
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005705 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005706 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005707
5708 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005710 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005712 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005713 if (idx < 0) {
5714 PyErr_SetString(PyExc_ValueError,
5715 "negative LONG_BINPUT argument");
5716 return -1;
5717 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005718
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005719 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005720}
5721
5722static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005723load_memoize(UnpicklerObject *self)
5724{
5725 PyObject *value;
5726
5727 if (Py_SIZE(self->stack) <= 0)
5728 return stack_underflow();
5729 value = self->stack->data[Py_SIZE(self->stack) - 1];
5730
5731 return _Unpickler_MemoPut(self, self->memo_len, value);
5732}
5733
5734static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005735do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736{
5737 PyObject *value;
5738 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005739 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005741 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742 if (x > len || x <= 0)
5743 return stack_underflow();
5744 if (len == x) /* nothing to do */
5745 return 0;
5746
5747 list = self->stack->data[x - 1];
5748
5749 if (PyList_Check(list)) {
5750 PyObject *slice;
5751 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005752 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005753
5754 slice = Pdata_poplist(self->stack, x);
5755 if (!slice)
5756 return -1;
5757 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005758 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005759 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005760 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005761 }
5762 else {
5763 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005764 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005766 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 if (append_func == NULL)
5768 return -1;
5769 for (i = x; i < len; i++) {
5770 PyObject *result;
5771
5772 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005773 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774 if (result == NULL) {
5775 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005776 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005777 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 return -1;
5779 }
5780 Py_DECREF(result);
5781 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005782 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005783 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 }
5785
5786 return 0;
5787}
5788
5789static int
5790load_append(UnpicklerObject *self)
5791{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793}
5794
5795static int
5796load_appends(UnpicklerObject *self)
5797{
5798 return do_append(self, marker(self));
5799}
5800
5801static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005802do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803{
5804 PyObject *value, *key;
5805 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005806 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005807 int status = 0;
5808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005809 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005810 if (x > len || x <= 0)
5811 return stack_underflow();
5812 if (len == x) /* nothing to do */
5813 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005814 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005815 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005816 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005817 PyErr_SetString(st->UnpicklingError,
5818 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819 return -1;
5820 }
5821
5822 /* Here, dict does not actually need to be a PyDict; it could be anything
5823 that supports the __setitem__ attribute. */
5824 dict = self->stack->data[x - 1];
5825
5826 for (i = x + 1; i < len; i += 2) {
5827 key = self->stack->data[i - 1];
5828 value = self->stack->data[i];
5829 if (PyObject_SetItem(dict, key, value) < 0) {
5830 status = -1;
5831 break;
5832 }
5833 }
5834
5835 Pdata_clear(self->stack, x);
5836 return status;
5837}
5838
5839static int
5840load_setitem(UnpicklerObject *self)
5841{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005842 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005843}
5844
5845static int
5846load_setitems(UnpicklerObject *self)
5847{
5848 return do_setitems(self, marker(self));
5849}
5850
5851static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005852load_additems(UnpicklerObject *self)
5853{
5854 PyObject *set;
5855 Py_ssize_t mark, len, i;
5856
5857 mark = marker(self);
5858 len = Py_SIZE(self->stack);
5859 if (mark > len || mark <= 0)
5860 return stack_underflow();
5861 if (len == mark) /* nothing to do */
5862 return 0;
5863
5864 set = self->stack->data[mark - 1];
5865
5866 if (PySet_Check(set)) {
5867 PyObject *items;
5868 int status;
5869
5870 items = Pdata_poptuple(self->stack, mark);
5871 if (items == NULL)
5872 return -1;
5873
5874 status = _PySet_Update(set, items);
5875 Py_DECREF(items);
5876 return status;
5877 }
5878 else {
5879 PyObject *add_func;
5880 _Py_IDENTIFIER(add);
5881
5882 add_func = _PyObject_GetAttrId(set, &PyId_add);
5883 if (add_func == NULL)
5884 return -1;
5885 for (i = mark; i < len; i++) {
5886 PyObject *result;
5887 PyObject *item;
5888
5889 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005890 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005891 if (result == NULL) {
5892 Pdata_clear(self->stack, i + 1);
5893 Py_SIZE(self->stack) = mark;
5894 return -1;
5895 }
5896 Py_DECREF(result);
5897 }
5898 Py_SIZE(self->stack) = mark;
5899 }
5900
5901 return 0;
5902}
5903
5904static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905load_build(UnpicklerObject *self)
5906{
5907 PyObject *state, *inst, *slotstate;
5908 PyObject *setstate;
5909 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005910 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005911
5912 /* Stack is ... instance, state. We want to leave instance at
5913 * the stack top, possibly mutated via instance.__setstate__(state).
5914 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005915 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005916 return stack_underflow();
5917
5918 PDATA_POP(self->stack, state);
5919 if (state == NULL)
5920 return -1;
5921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005922 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005924 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005925 if (setstate == NULL) {
5926 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5927 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005928 else {
5929 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005930 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005931 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005932 }
5933 else {
5934 PyObject *result;
5935
5936 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005937 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005938 Py_DECREF(setstate);
5939 if (result == NULL)
5940 return -1;
5941 Py_DECREF(result);
5942 return 0;
5943 }
5944
5945 /* A default __setstate__. First see whether state embeds a
5946 * slot state dict too (a proto 2 addition).
5947 */
5948 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5949 PyObject *tmp = state;
5950
5951 state = PyTuple_GET_ITEM(tmp, 0);
5952 slotstate = PyTuple_GET_ITEM(tmp, 1);
5953 Py_INCREF(state);
5954 Py_INCREF(slotstate);
5955 Py_DECREF(tmp);
5956 }
5957 else
5958 slotstate = NULL;
5959
5960 /* Set inst.__dict__ from the state dict (if any). */
5961 if (state != Py_None) {
5962 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005963 PyObject *d_key, *d_value;
5964 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005965 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005966
5967 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005968 PickleState *st = _Pickle_GetGlobalState();
5969 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970 goto error;
5971 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005972 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005973 if (dict == NULL)
5974 goto error;
5975
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005976 i = 0;
5977 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5978 /* normally the keys for instance attributes are
5979 interned. we should try to do that here. */
5980 Py_INCREF(d_key);
5981 if (PyUnicode_CheckExact(d_key))
5982 PyUnicode_InternInPlace(&d_key);
5983 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5984 Py_DECREF(d_key);
5985 goto error;
5986 }
5987 Py_DECREF(d_key);
5988 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005989 Py_DECREF(dict);
5990 }
5991
5992 /* Also set instance attributes from the slotstate dict (if any). */
5993 if (slotstate != NULL) {
5994 PyObject *d_key, *d_value;
5995 Py_ssize_t i;
5996
5997 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005998 PickleState *st = _Pickle_GetGlobalState();
5999 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006000 "slot state is not a dictionary");
6001 goto error;
6002 }
6003 i = 0;
6004 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6005 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6006 goto error;
6007 }
6008 }
6009
6010 if (0) {
6011 error:
6012 status = -1;
6013 }
6014
6015 Py_DECREF(state);
6016 Py_XDECREF(slotstate);
6017 return status;
6018}
6019
6020static int
6021load_mark(UnpicklerObject *self)
6022{
6023
6024 /* Note that we split the (pickle.py) stack into two stacks, an
6025 * object stack and a mark stack. Here we push a mark onto the
6026 * mark stack.
6027 */
6028
6029 if ((self->num_marks + 1) >= self->marks_size) {
6030 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006031
6032 /* Use the size_t type to check for overflow. */
6033 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006034 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006035 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006036 PyErr_NoMemory();
6037 return -1;
6038 }
6039
6040 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006041 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006042 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006043 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6044 if (self->marks == NULL) {
6045 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046 PyErr_NoMemory();
6047 return -1;
6048 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006049 self->marks_size = (Py_ssize_t)alloc;
6050 }
6051
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006052 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006053
6054 return 0;
6055}
6056
6057static int
6058load_reduce(UnpicklerObject *self)
6059{
6060 PyObject *callable = NULL;
6061 PyObject *argtup = NULL;
6062 PyObject *obj = NULL;
6063
6064 PDATA_POP(self->stack, argtup);
6065 if (argtup == NULL)
6066 return -1;
6067 PDATA_POP(self->stack, callable);
6068 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006069 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006070 Py_DECREF(callable);
6071 }
6072 Py_DECREF(argtup);
6073
6074 if (obj == NULL)
6075 return -1;
6076
6077 PDATA_PUSH(self->stack, obj, -1);
6078 return 0;
6079}
6080
6081/* Just raises an error if we don't know the protocol specified. PROTO
6082 * is the first opcode for protocols >= 2.
6083 */
6084static int
6085load_proto(UnpicklerObject *self)
6086{
6087 char *s;
6088 int i;
6089
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006090 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006091 return -1;
6092
6093 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006094 if (i <= HIGHEST_PROTOCOL) {
6095 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006098
6099 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6100 return -1;
6101}
6102
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006103static int
6104load_frame(UnpicklerObject *self)
6105{
6106 char *s;
6107 Py_ssize_t frame_len;
6108
6109 if (_Unpickler_Read(self, &s, 8) < 0)
6110 return -1;
6111
6112 frame_len = calc_binsize(s, 8);
6113 if (frame_len < 0) {
6114 PyErr_Format(PyExc_OverflowError,
6115 "FRAME length exceeds system's maximum of %zd bytes",
6116 PY_SSIZE_T_MAX);
6117 return -1;
6118 }
6119
6120 if (_Unpickler_Read(self, &s, frame_len) < 0)
6121 return -1;
6122
6123 /* Rewind to start of frame */
6124 self->next_read_idx -= frame_len;
6125 return 0;
6126}
6127
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128static PyObject *
6129load(UnpicklerObject *self)
6130{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006131 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006132 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006133
6134 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006135 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006136 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 Pdata_clear(self->stack, 0);
6138
6139 /* Convenient macros for the dispatch while-switch loop just below. */
6140#define OP(opcode, load_func) \
6141 case opcode: if (load_func(self) < 0) break; continue;
6142
6143#define OP_ARG(opcode, load_func, arg) \
6144 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6145
6146 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006147 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006148 break;
6149
6150 switch ((enum opcode)s[0]) {
6151 OP(NONE, load_none)
6152 OP(BININT, load_binint)
6153 OP(BININT1, load_binint1)
6154 OP(BININT2, load_binint2)
6155 OP(INT, load_int)
6156 OP(LONG, load_long)
6157 OP_ARG(LONG1, load_counted_long, 1)
6158 OP_ARG(LONG4, load_counted_long, 4)
6159 OP(FLOAT, load_float)
6160 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006161 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6162 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6163 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6164 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6165 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006166 OP(STRING, load_string)
6167 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006168 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6169 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6170 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6172 OP_ARG(TUPLE1, load_counted_tuple, 1)
6173 OP_ARG(TUPLE2, load_counted_tuple, 2)
6174 OP_ARG(TUPLE3, load_counted_tuple, 3)
6175 OP(TUPLE, load_tuple)
6176 OP(EMPTY_LIST, load_empty_list)
6177 OP(LIST, load_list)
6178 OP(EMPTY_DICT, load_empty_dict)
6179 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006180 OP(EMPTY_SET, load_empty_set)
6181 OP(ADDITEMS, load_additems)
6182 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006183 OP(OBJ, load_obj)
6184 OP(INST, load_inst)
6185 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006186 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006187 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006188 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189 OP(APPEND, load_append)
6190 OP(APPENDS, load_appends)
6191 OP(BUILD, load_build)
6192 OP(DUP, load_dup)
6193 OP(BINGET, load_binget)
6194 OP(LONG_BINGET, load_long_binget)
6195 OP(GET, load_get)
6196 OP(MARK, load_mark)
6197 OP(BINPUT, load_binput)
6198 OP(LONG_BINPUT, load_long_binput)
6199 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006200 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006201 OP(POP, load_pop)
6202 OP(POP_MARK, load_pop_mark)
6203 OP(SETITEM, load_setitem)
6204 OP(SETITEMS, load_setitems)
6205 OP(PERSID, load_persid)
6206 OP(BINPERSID, load_binpersid)
6207 OP(REDUCE, load_reduce)
6208 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006209 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 OP_ARG(EXT1, load_extension, 1)
6211 OP_ARG(EXT2, load_extension, 2)
6212 OP_ARG(EXT4, load_extension, 4)
6213 OP_ARG(NEWTRUE, load_bool, Py_True)
6214 OP_ARG(NEWFALSE, load_bool, Py_False)
6215
6216 case STOP:
6217 break;
6218
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006219 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006220 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006221 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006222 }
6223 else {
6224 PickleState *st = _Pickle_GetGlobalState();
6225 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006226 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228 return NULL;
6229 }
6230
6231 break; /* and we are done! */
6232 }
6233
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006234 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006235 return NULL;
6236 }
6237
Victor Stinner2ae57e32013-10-31 13:39:23 +01006238 if (_Unpickler_SkipConsumed(self) < 0)
6239 return NULL;
6240
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 PDATA_POP(self->stack, value);
6242 return value;
6243}
6244
Larry Hastings61272b72014-01-07 12:41:53 -08006245/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006246
6247_pickle.Unpickler.load
6248
6249Load a pickle.
6250
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006251Read a pickled object representation from the open file object given
6252in the constructor, and return the reconstituted object hierarchy
6253specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006254[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006255
Larry Hastings3cceb382014-01-04 11:09:09 -08006256static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006257_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006258/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006259{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006260 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006261
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006262 /* Check whether the Unpickler was initialized correctly. This prevents
6263 segfaulting if a subclass overridden __init__ with a function that does
6264 not call Unpickler.__init__(). Here, we simply ensure that self->read
6265 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006266 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006267 PickleState *st = _Pickle_GetGlobalState();
6268 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006270 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006271 return NULL;
6272 }
6273
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006274 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275}
6276
6277/* The name of find_class() is misleading. In newer pickle protocols, this
6278 function is used for loading any global (i.e., functions), not just
6279 classes. The name is kept only for backward compatibility. */
6280
Larry Hastings61272b72014-01-07 12:41:53 -08006281/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006282
6283_pickle.Unpickler.find_class
6284
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006285 module_name: object
6286 global_name: object
6287 /
6288
6289Return an object from a specified module.
6290
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006291If necessary, the module will be imported. Subclasses may override
6292this method (e.g. to restrict unpickling of arbitrary classes and
6293functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006294
6295This method is called whenever a class or a function object is
6296needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006297[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006298
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006299static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006300_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6301 PyObject *module_name,
6302 PyObject *global_name)
6303/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006304{
6305 PyObject *global;
6306 PyObject *modules_dict;
6307 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006308 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006309
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006310 /* Try to map the old names used in Python 2.x to the new ones used in
6311 Python 3.x. We do this only with old pickle protocols and when the
6312 user has not disabled the feature. */
6313 if (self->proto < 3 && self->fix_imports) {
6314 PyObject *key;
6315 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006316 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006317
6318 /* Check if the global (i.e., a function or a class) was renamed
6319 or moved to another module. */
6320 key = PyTuple_Pack(2, module_name, global_name);
6321 if (key == NULL)
6322 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006323 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006324 Py_DECREF(key);
6325 if (item) {
6326 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6327 PyErr_Format(PyExc_RuntimeError,
6328 "_compat_pickle.NAME_MAPPING values should be "
6329 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6330 return NULL;
6331 }
6332 module_name = PyTuple_GET_ITEM(item, 0);
6333 global_name = PyTuple_GET_ITEM(item, 1);
6334 if (!PyUnicode_Check(module_name) ||
6335 !PyUnicode_Check(global_name)) {
6336 PyErr_Format(PyExc_RuntimeError,
6337 "_compat_pickle.NAME_MAPPING values should be "
6338 "pairs of str, not (%.200s, %.200s)",
6339 Py_TYPE(module_name)->tp_name,
6340 Py_TYPE(global_name)->tp_name);
6341 return NULL;
6342 }
6343 }
6344 else if (PyErr_Occurred()) {
6345 return NULL;
6346 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006347 else {
6348 /* Check if the module was renamed. */
6349 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6350 if (item) {
6351 if (!PyUnicode_Check(item)) {
6352 PyErr_Format(PyExc_RuntimeError,
6353 "_compat_pickle.IMPORT_MAPPING values should be "
6354 "strings, not %.200s", Py_TYPE(item)->tp_name);
6355 return NULL;
6356 }
6357 module_name = item;
6358 }
6359 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006360 return NULL;
6361 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006362 }
6363 }
6364
Victor Stinnerbb520202013-11-06 22:40:41 +01006365 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006366 if (modules_dict == NULL) {
6367 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006368 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006369 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006370
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006371 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006372 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006373 if (PyErr_Occurred())
6374 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006375 module = PyImport_Import(module_name);
6376 if (module == NULL)
6377 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006378 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006379 Py_DECREF(module);
6380 }
Victor Stinner121aab42011-09-29 23:40:53 +02006381 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006382 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383 }
6384 return global;
6385}
6386
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006387/*[clinic input]
6388
6389_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6390
6391Returns size in memory, in bytes.
6392[clinic start generated code]*/
6393
6394static Py_ssize_t
6395_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6396/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6397{
6398 Py_ssize_t res;
6399
6400 res = sizeof(UnpicklerObject);
6401 if (self->memo != NULL)
6402 res += self->memo_size * sizeof(PyObject *);
6403 if (self->marks != NULL)
6404 res += self->marks_size * sizeof(Py_ssize_t);
6405 if (self->input_line != NULL)
6406 res += strlen(self->input_line) + 1;
6407 if (self->encoding != NULL)
6408 res += strlen(self->encoding) + 1;
6409 if (self->errors != NULL)
6410 res += strlen(self->errors) + 1;
6411 return res;
6412}
6413
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006414static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006415 _PICKLE_UNPICKLER_LOAD_METHODDEF
6416 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006417 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006418 {NULL, NULL} /* sentinel */
6419};
6420
6421static void
6422Unpickler_dealloc(UnpicklerObject *self)
6423{
6424 PyObject_GC_UnTrack((PyObject *)self);
6425 Py_XDECREF(self->readline);
6426 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006427 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006428 Py_XDECREF(self->stack);
6429 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006430 if (self->buffer.buf != NULL) {
6431 PyBuffer_Release(&self->buffer);
6432 self->buffer.buf = NULL;
6433 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006434
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006435 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006436 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006437 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006438 PyMem_Free(self->encoding);
6439 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006440
6441 Py_TYPE(self)->tp_free((PyObject *)self);
6442}
6443
6444static int
6445Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6446{
6447 Py_VISIT(self->readline);
6448 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006449 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006450 Py_VISIT(self->stack);
6451 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006452 return 0;
6453}
6454
6455static int
6456Unpickler_clear(UnpicklerObject *self)
6457{
6458 Py_CLEAR(self->readline);
6459 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006460 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006461 Py_CLEAR(self->stack);
6462 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006463 if (self->buffer.buf != NULL) {
6464 PyBuffer_Release(&self->buffer);
6465 self->buffer.buf = NULL;
6466 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006468 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006469 PyMem_Free(self->marks);
6470 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006471 PyMem_Free(self->input_line);
6472 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006473 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006475 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476 self->errors = NULL;
6477
6478 return 0;
6479}
6480
Larry Hastings61272b72014-01-07 12:41:53 -08006481/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006482
6483_pickle.Unpickler.__init__
6484
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006485 file: object
6486 *
6487 fix_imports: bool = True
6488 encoding: str = 'ASCII'
6489 errors: str = 'strict'
6490
6491This takes a binary file for reading a pickle data stream.
6492
6493The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006494protocol argument is needed. Bytes past the pickled object's
6495representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006496
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006497The argument *file* must have two methods, a read() method that takes
6498an integer argument, and a readline() method that requires no
6499arguments. Both methods should return bytes. Thus *file* can be a
6500binary file object opened for reading, a io.BytesIO object, or any
6501other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006502
6503Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6504which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006505generated by Python 2. If *fix_imports* is True, pickle will try to
6506map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006507*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006508instances pickled by Python 2; these default to 'ASCII' and 'strict',
6509respectively. The *encoding* can be 'bytes' to read these 8-bit
6510string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006511[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006512
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006513static int
Larry Hastings89964c42015-04-14 18:07:59 -04006514_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6515 int fix_imports, const char *encoding,
6516 const char *errors)
6517/*[clinic end generated code: output=e2c8ce748edc57b0 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006519 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006520
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006521 /* In case of multiple __init__() calls, clear previous content. */
6522 if (self->read != NULL)
6523 (void)Unpickler_clear(self);
6524
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006525 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006526 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006527
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006528 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006529 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006530
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006531 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006532 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006535 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006536 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6537 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006539 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 }
6541 else {
6542 self->pers_func = NULL;
6543 }
6544
6545 self->stack = (Pdata *)Pdata_New();
6546 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006547 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006549 self->memo_size = 32;
6550 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006552 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006553
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006554 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006555
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006556 return 0;
6557}
6558
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006559
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006560/* Define a proxy object for the Unpickler's internal memo object. This is to
6561 * avoid breaking code like:
6562 * unpickler.memo.clear()
6563 * and
6564 * unpickler.memo = saved_memo
6565 * Is this a good idea? Not really, but we don't want to break code that uses
6566 * it. Note that we don't implement the entire mapping API here. This is
6567 * intentional, as these should be treated as black-box implementation details.
6568 *
6569 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006570 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006571 */
6572
Larry Hastings61272b72014-01-07 12:41:53 -08006573/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006574_pickle.UnpicklerMemoProxy.clear
6575
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006576Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006577[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006578
Larry Hastings3cceb382014-01-04 11:09:09 -08006579static PyObject *
6580_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006581/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006582{
6583 _Unpickler_MemoCleanup(self->unpickler);
6584 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6585 if (self->unpickler->memo == NULL)
6586 return NULL;
6587 Py_RETURN_NONE;
6588}
6589
Larry Hastings61272b72014-01-07 12:41:53 -08006590/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006591_pickle.UnpicklerMemoProxy.copy
6592
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006593Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006594[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006595
Larry Hastings3cceb382014-01-04 11:09:09 -08006596static PyObject *
6597_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006598/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006599{
6600 Py_ssize_t i;
6601 PyObject *new_memo = PyDict_New();
6602 if (new_memo == NULL)
6603 return NULL;
6604
6605 for (i = 0; i < self->unpickler->memo_size; i++) {
6606 int status;
6607 PyObject *key, *value;
6608
6609 value = self->unpickler->memo[i];
6610 if (value == NULL)
6611 continue;
6612
6613 key = PyLong_FromSsize_t(i);
6614 if (key == NULL)
6615 goto error;
6616 status = PyDict_SetItem(new_memo, key, value);
6617 Py_DECREF(key);
6618 if (status < 0)
6619 goto error;
6620 }
6621 return new_memo;
6622
6623error:
6624 Py_DECREF(new_memo);
6625 return NULL;
6626}
6627
Larry Hastings61272b72014-01-07 12:41:53 -08006628/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006629_pickle.UnpicklerMemoProxy.__reduce__
6630
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006631Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006632[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006633
Larry Hastings3cceb382014-01-04 11:09:09 -08006634static PyObject *
6635_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006636/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006637{
6638 PyObject *reduce_value;
6639 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006640 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006641 if (contents == NULL)
6642 return NULL;
6643
6644 reduce_value = PyTuple_New(2);
6645 if (reduce_value == NULL) {
6646 Py_DECREF(contents);
6647 return NULL;
6648 }
6649 constructor_args = PyTuple_New(1);
6650 if (constructor_args == NULL) {
6651 Py_DECREF(contents);
6652 Py_DECREF(reduce_value);
6653 return NULL;
6654 }
6655 PyTuple_SET_ITEM(constructor_args, 0, contents);
6656 Py_INCREF((PyObject *)&PyDict_Type);
6657 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6658 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6659 return reduce_value;
6660}
6661
6662static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006663 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6664 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6665 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006666 {NULL, NULL} /* sentinel */
6667};
6668
6669static void
6670UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6671{
6672 PyObject_GC_UnTrack(self);
6673 Py_XDECREF(self->unpickler);
6674 PyObject_GC_Del((PyObject *)self);
6675}
6676
6677static int
6678UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6679 visitproc visit, void *arg)
6680{
6681 Py_VISIT(self->unpickler);
6682 return 0;
6683}
6684
6685static int
6686UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6687{
6688 Py_CLEAR(self->unpickler);
6689 return 0;
6690}
6691
6692static PyTypeObject UnpicklerMemoProxyType = {
6693 PyVarObject_HEAD_INIT(NULL, 0)
6694 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6695 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6696 0,
6697 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6698 0, /* tp_print */
6699 0, /* tp_getattr */
6700 0, /* tp_setattr */
6701 0, /* tp_compare */
6702 0, /* tp_repr */
6703 0, /* tp_as_number */
6704 0, /* tp_as_sequence */
6705 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006706 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006707 0, /* tp_call */
6708 0, /* tp_str */
6709 PyObject_GenericGetAttr, /* tp_getattro */
6710 PyObject_GenericSetAttr, /* tp_setattro */
6711 0, /* tp_as_buffer */
6712 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6713 0, /* tp_doc */
6714 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6715 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6716 0, /* tp_richcompare */
6717 0, /* tp_weaklistoffset */
6718 0, /* tp_iter */
6719 0, /* tp_iternext */
6720 unpicklerproxy_methods, /* tp_methods */
6721};
6722
6723static PyObject *
6724UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6725{
6726 UnpicklerMemoProxyObject *self;
6727
6728 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6729 &UnpicklerMemoProxyType);
6730 if (self == NULL)
6731 return NULL;
6732 Py_INCREF(unpickler);
6733 self->unpickler = unpickler;
6734 PyObject_GC_Track(self);
6735 return (PyObject *)self;
6736}
6737
6738/*****************************************************************************/
6739
6740
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006741static PyObject *
6742Unpickler_get_memo(UnpicklerObject *self)
6743{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006744 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006745}
6746
6747static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006748Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006749{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006750 PyObject **new_memo;
6751 Py_ssize_t new_memo_size = 0;
6752 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006753
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006754 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006755 PyErr_SetString(PyExc_TypeError,
6756 "attribute deletion is not supported");
6757 return -1;
6758 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006759
6760 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6761 UnpicklerObject *unpickler =
6762 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6763
6764 new_memo_size = unpickler->memo_size;
6765 new_memo = _Unpickler_NewMemo(new_memo_size);
6766 if (new_memo == NULL)
6767 return -1;
6768
6769 for (i = 0; i < new_memo_size; i++) {
6770 Py_XINCREF(unpickler->memo[i]);
6771 new_memo[i] = unpickler->memo[i];
6772 }
6773 }
6774 else if (PyDict_Check(obj)) {
6775 Py_ssize_t i = 0;
6776 PyObject *key, *value;
6777
6778 new_memo_size = PyDict_Size(obj);
6779 new_memo = _Unpickler_NewMemo(new_memo_size);
6780 if (new_memo == NULL)
6781 return -1;
6782
6783 while (PyDict_Next(obj, &i, &key, &value)) {
6784 Py_ssize_t idx;
6785 if (!PyLong_Check(key)) {
6786 PyErr_SetString(PyExc_TypeError,
6787 "memo key must be integers");
6788 goto error;
6789 }
6790 idx = PyLong_AsSsize_t(key);
6791 if (idx == -1 && PyErr_Occurred())
6792 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006793 if (idx < 0) {
6794 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006795 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006796 goto error;
6797 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006798 if (_Unpickler_MemoPut(self, idx, value) < 0)
6799 goto error;
6800 }
6801 }
6802 else {
6803 PyErr_Format(PyExc_TypeError,
6804 "'memo' attribute must be an UnpicklerMemoProxy object"
6805 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006806 return -1;
6807 }
6808
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006809 _Unpickler_MemoCleanup(self);
6810 self->memo_size = new_memo_size;
6811 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812
6813 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006814
6815 error:
6816 if (new_memo_size) {
6817 i = new_memo_size;
6818 while (--i >= 0) {
6819 Py_XDECREF(new_memo[i]);
6820 }
6821 PyMem_FREE(new_memo);
6822 }
6823 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006824}
6825
6826static PyObject *
6827Unpickler_get_persload(UnpicklerObject *self)
6828{
6829 if (self->pers_func == NULL)
6830 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6831 else
6832 Py_INCREF(self->pers_func);
6833 return self->pers_func;
6834}
6835
6836static int
6837Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6838{
6839 PyObject *tmp;
6840
6841 if (value == NULL) {
6842 PyErr_SetString(PyExc_TypeError,
6843 "attribute deletion is not supported");
6844 return -1;
6845 }
6846 if (!PyCallable_Check(value)) {
6847 PyErr_SetString(PyExc_TypeError,
6848 "persistent_load must be a callable taking "
6849 "one argument");
6850 return -1;
6851 }
6852
6853 tmp = self->pers_func;
6854 Py_INCREF(value);
6855 self->pers_func = value;
6856 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6857
6858 return 0;
6859}
6860
6861static PyGetSetDef Unpickler_getsets[] = {
6862 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6863 {"persistent_load", (getter)Unpickler_get_persload,
6864 (setter)Unpickler_set_persload},
6865 {NULL}
6866};
6867
6868static PyTypeObject Unpickler_Type = {
6869 PyVarObject_HEAD_INIT(NULL, 0)
6870 "_pickle.Unpickler", /*tp_name*/
6871 sizeof(UnpicklerObject), /*tp_basicsize*/
6872 0, /*tp_itemsize*/
6873 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6874 0, /*tp_print*/
6875 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006876 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006877 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006878 0, /*tp_repr*/
6879 0, /*tp_as_number*/
6880 0, /*tp_as_sequence*/
6881 0, /*tp_as_mapping*/
6882 0, /*tp_hash*/
6883 0, /*tp_call*/
6884 0, /*tp_str*/
6885 0, /*tp_getattro*/
6886 0, /*tp_setattro*/
6887 0, /*tp_as_buffer*/
6888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006889 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006890 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6891 (inquiry)Unpickler_clear, /*tp_clear*/
6892 0, /*tp_richcompare*/
6893 0, /*tp_weaklistoffset*/
6894 0, /*tp_iter*/
6895 0, /*tp_iternext*/
6896 Unpickler_methods, /*tp_methods*/
6897 0, /*tp_members*/
6898 Unpickler_getsets, /*tp_getset*/
6899 0, /*tp_base*/
6900 0, /*tp_dict*/
6901 0, /*tp_descr_get*/
6902 0, /*tp_descr_set*/
6903 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006904 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006905 PyType_GenericAlloc, /*tp_alloc*/
6906 PyType_GenericNew, /*tp_new*/
6907 PyObject_GC_Del, /*tp_free*/
6908 0, /*tp_is_gc*/
6909};
6910
Larry Hastings61272b72014-01-07 12:41:53 -08006911/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006912
6913_pickle.dump
6914
6915 obj: object
6916 file: object
6917 protocol: object = NULL
6918 *
6919 fix_imports: bool = True
6920
6921Write a pickled representation of obj to the open file object file.
6922
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006923This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6924be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006925
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006926The optional *protocol* argument tells the pickler to use the given
6927protocol supported protocols are 0, 1, 2, 3 and 4. The default
6928protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006929
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006930Specifying a negative protocol version selects the highest protocol
6931version supported. The higher the protocol used, the more recent the
6932version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006933
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006934The *file* argument must have a write() method that accepts a single
6935bytes argument. It can thus be a file object opened for binary
6936writing, a io.BytesIO instance, or any other custom object that meets
6937this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006938
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006939If *fix_imports* is True and protocol is less than 3, pickle will try
6940to map the new Python 3 names to the old module names used in Python
69412, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006942[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006943
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006944static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006945_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
6946 PyObject *protocol, int fix_imports)
6947/*[clinic end generated code: output=0de7dff89c406816 input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006948{
6949 PicklerObject *pickler = _Pickler_New();
6950
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006951 if (pickler == NULL)
6952 return NULL;
6953
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006954 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006955 goto error;
6956
6957 if (_Pickler_SetOutputStream(pickler, file) < 0)
6958 goto error;
6959
6960 if (dump(pickler, obj) < 0)
6961 goto error;
6962
6963 if (_Pickler_FlushToFile(pickler) < 0)
6964 goto error;
6965
6966 Py_DECREF(pickler);
6967 Py_RETURN_NONE;
6968
6969 error:
6970 Py_XDECREF(pickler);
6971 return NULL;
6972}
6973
Larry Hastings61272b72014-01-07 12:41:53 -08006974/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006975
6976_pickle.dumps
6977
6978 obj: object
6979 protocol: object = NULL
6980 *
6981 fix_imports: bool = True
6982
6983Return the pickled representation of the object as a bytes object.
6984
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006985The optional *protocol* argument tells the pickler to use the given
6986protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6987protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006988
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006989Specifying a negative protocol version selects the highest protocol
6990version supported. The higher the protocol used, the more recent the
6991version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006992
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006993If *fix_imports* is True and *protocol* is less than 3, pickle will
6994try to map the new Python 3 names to the old module names used in
6995Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006996[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006997
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006998static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006999_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7000 int fix_imports)
7001/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007002{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007003 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007006 if (pickler == NULL)
7007 return NULL;
7008
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007009 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007010 goto error;
7011
7012 if (dump(pickler, obj) < 0)
7013 goto error;
7014
7015 result = _Pickler_GetString(pickler);
7016 Py_DECREF(pickler);
7017 return result;
7018
7019 error:
7020 Py_XDECREF(pickler);
7021 return NULL;
7022}
7023
Larry Hastings61272b72014-01-07 12:41:53 -08007024/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007025
7026_pickle.load
7027
7028 file: object
7029 *
7030 fix_imports: bool = True
7031 encoding: str = 'ASCII'
7032 errors: str = 'strict'
7033
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007034Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007035
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007036This is equivalent to ``Unpickler(file).load()``, but may be more
7037efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007038
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007039The protocol version of the pickle is detected automatically, so no
7040protocol argument is needed. Bytes past the pickled object's
7041representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007042
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007043The argument *file* must have two methods, a read() method that takes
7044an integer argument, and a readline() method that requires no
7045arguments. Both methods should return bytes. Thus *file* can be a
7046binary file object opened for reading, a io.BytesIO object, or any
7047other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007048
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007049Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7050which are used to control compatiblity support for pickle stream
7051generated by Python 2. If *fix_imports* is True, pickle will try to
7052map the old Python 2 names to the new names used in Python 3. The
7053*encoding* and *errors* tell pickle how to decode 8-bit string
7054instances pickled by Python 2; these default to 'ASCII' and 'strict',
7055respectively. The *encoding* can be 'bytes' to read these 8-bit
7056string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007057[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007059static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007060_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7061 const char *encoding, const char *errors)
7062/*[clinic end generated code: output=798f1c57cb2b4eb1 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007063{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007064 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007066
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007067 if (unpickler == NULL)
7068 return NULL;
7069
7070 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7071 goto error;
7072
7073 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7074 goto error;
7075
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007076 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007077
7078 result = load(unpickler);
7079 Py_DECREF(unpickler);
7080 return result;
7081
7082 error:
7083 Py_XDECREF(unpickler);
7084 return NULL;
7085}
7086
Larry Hastings61272b72014-01-07 12:41:53 -08007087/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007088
7089_pickle.loads
7090
7091 data: object
7092 *
7093 fix_imports: bool = True
7094 encoding: str = 'ASCII'
7095 errors: str = 'strict'
7096
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007097Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007098
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007099The protocol version of the pickle is detected automatically, so no
7100protocol argument is needed. Bytes past the pickled object's
7101representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007103Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7104which are used to control compatiblity support for pickle stream
7105generated by Python 2. If *fix_imports* is True, pickle will try to
7106map the old Python 2 names to the new names used in Python 3. The
7107*encoding* and *errors* tell pickle how to decode 8-bit string
7108instances pickled by Python 2; these default to 'ASCII' and 'strict',
7109respectively. The *encoding* can be 'bytes' to read these 8-bit
7110string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007111[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007112
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007113static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007114_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7115 const char *encoding, const char *errors)
7116/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007117{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007118 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007119 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007120
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007121 if (unpickler == NULL)
7122 return NULL;
7123
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007124 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007125 goto error;
7126
7127 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7128 goto error;
7129
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007130 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007131
7132 result = load(unpickler);
7133 Py_DECREF(unpickler);
7134 return result;
7135
7136 error:
7137 Py_XDECREF(unpickler);
7138 return NULL;
7139}
7140
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007141static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007142 _PICKLE_DUMP_METHODDEF
7143 _PICKLE_DUMPS_METHODDEF
7144 _PICKLE_LOAD_METHODDEF
7145 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007146 {NULL, NULL} /* sentinel */
7147};
7148
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007149static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007150pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007151{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007152 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007153 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007154}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007155
Stefan Krahf483b0f2013-12-14 13:43:10 +01007156static void
7157pickle_free(PyObject *m)
7158{
7159 _Pickle_ClearState(_Pickle_GetState(m));
7160}
7161
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007162static int
7163pickle_traverse(PyObject *m, visitproc visit, void *arg)
7164{
7165 PickleState *st = _Pickle_GetState(m);
7166 Py_VISIT(st->PickleError);
7167 Py_VISIT(st->PicklingError);
7168 Py_VISIT(st->UnpicklingError);
7169 Py_VISIT(st->dispatch_table);
7170 Py_VISIT(st->extension_registry);
7171 Py_VISIT(st->extension_cache);
7172 Py_VISIT(st->inverted_registry);
7173 Py_VISIT(st->name_mapping_2to3);
7174 Py_VISIT(st->import_mapping_2to3);
7175 Py_VISIT(st->name_mapping_3to2);
7176 Py_VISIT(st->import_mapping_3to2);
7177 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007178 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007179 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007180}
7181
7182static struct PyModuleDef _picklemodule = {
7183 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007184 "_pickle", /* m_name */
7185 pickle_module_doc, /* m_doc */
7186 sizeof(PickleState), /* m_size */
7187 pickle_methods, /* m_methods */
7188 NULL, /* m_reload */
7189 pickle_traverse, /* m_traverse */
7190 pickle_clear, /* m_clear */
7191 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007192};
7193
7194PyMODINIT_FUNC
7195PyInit__pickle(void)
7196{
7197 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007198 PickleState *st;
7199
7200 m = PyState_FindModule(&_picklemodule);
7201 if (m) {
7202 Py_INCREF(m);
7203 return m;
7204 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007205
7206 if (PyType_Ready(&Unpickler_Type) < 0)
7207 return NULL;
7208 if (PyType_Ready(&Pickler_Type) < 0)
7209 return NULL;
7210 if (PyType_Ready(&Pdata_Type) < 0)
7211 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007212 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7213 return NULL;
7214 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7215 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007216
7217 /* Create the module and add the functions. */
7218 m = PyModule_Create(&_picklemodule);
7219 if (m == NULL)
7220 return NULL;
7221
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007222 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007223 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7224 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007225 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007226 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7227 return NULL;
7228
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007229 st = _Pickle_GetState(m);
7230
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007231 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007232 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7233 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007234 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007235 st->PicklingError = \
7236 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7237 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007238 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007239 st->UnpicklingError = \
7240 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7241 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007242 return NULL;
7243
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007244 Py_INCREF(st->PickleError);
7245 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007246 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007247 Py_INCREF(st->PicklingError);
7248 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007249 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007250 Py_INCREF(st->UnpicklingError);
7251 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007252 return NULL;
7253
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007254 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007255 return NULL;
7256
7257 return m;
7258}