blob: 78a4b15a008a9a16e9040c520fb73311d8a97308 [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800156} PickleState;
157
158/* Forward declaration of the _pickle module definition. */
159static struct PyModuleDef _picklemodule;
160
161/* Given a module object, get its per-module state. */
162static PickleState *
163_Pickle_GetState(PyObject *module)
164{
165 return (PickleState *)PyModule_GetState(module);
166}
167
168/* Find the module instance imported in the currently running sub-interpreter
169 and get its state. */
170static PickleState *
171_Pickle_GetGlobalState(void)
172{
173 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
174}
175
176/* Clear the given pickle module state. */
177static void
178_Pickle_ClearState(PickleState *st)
179{
180 Py_CLEAR(st->PickleError);
181 Py_CLEAR(st->PicklingError);
182 Py_CLEAR(st->UnpicklingError);
183 Py_CLEAR(st->dispatch_table);
184 Py_CLEAR(st->extension_registry);
185 Py_CLEAR(st->extension_cache);
186 Py_CLEAR(st->inverted_registry);
187 Py_CLEAR(st->name_mapping_2to3);
188 Py_CLEAR(st->import_mapping_2to3);
189 Py_CLEAR(st->name_mapping_3to2);
190 Py_CLEAR(st->import_mapping_3to2);
191 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300192 Py_CLEAR(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800193}
194
195/* Initialize the given pickle module state. */
196static int
197_Pickle_InitState(PickleState *st)
198{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300199 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800200 PyObject *copyreg = NULL;
201 PyObject *compat_pickle = NULL;
202 PyObject *codecs = NULL;
203
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300204 builtins = PyEval_GetBuiltins();
205 if (builtins == NULL)
206 goto error;
207 st->getattr = PyDict_GetItemString(builtins, "getattr");
208 if (st->getattr == NULL)
209 goto error;
210 Py_INCREF(st->getattr);
211
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800212 copyreg = PyImport_ImportModule("copyreg");
213 if (!copyreg)
214 goto error;
215 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
216 if (!st->dispatch_table)
217 goto error;
218 if (!PyDict_CheckExact(st->dispatch_table)) {
219 PyErr_Format(PyExc_RuntimeError,
220 "copyreg.dispatch_table should be a dict, not %.200s",
221 Py_TYPE(st->dispatch_table)->tp_name);
222 goto error;
223 }
224 st->extension_registry = \
225 PyObject_GetAttrString(copyreg, "_extension_registry");
226 if (!st->extension_registry)
227 goto error;
228 if (!PyDict_CheckExact(st->extension_registry)) {
229 PyErr_Format(PyExc_RuntimeError,
230 "copyreg._extension_registry should be a dict, "
231 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
232 goto error;
233 }
234 st->inverted_registry = \
235 PyObject_GetAttrString(copyreg, "_inverted_registry");
236 if (!st->inverted_registry)
237 goto error;
238 if (!PyDict_CheckExact(st->inverted_registry)) {
239 PyErr_Format(PyExc_RuntimeError,
240 "copyreg._inverted_registry should be a dict, "
241 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
242 goto error;
243 }
244 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
245 if (!st->extension_cache)
246 goto error;
247 if (!PyDict_CheckExact(st->extension_cache)) {
248 PyErr_Format(PyExc_RuntimeError,
249 "copyreg._extension_cache should be a dict, "
250 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
251 goto error;
252 }
253 Py_CLEAR(copyreg);
254
255 /* Load the 2.x -> 3.x stdlib module mapping tables */
256 compat_pickle = PyImport_ImportModule("_compat_pickle");
257 if (!compat_pickle)
258 goto error;
259 st->name_mapping_2to3 = \
260 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
261 if (!st->name_mapping_2to3)
262 goto error;
263 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
264 PyErr_Format(PyExc_RuntimeError,
265 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
266 Py_TYPE(st->name_mapping_2to3)->tp_name);
267 goto error;
268 }
269 st->import_mapping_2to3 = \
270 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
271 if (!st->import_mapping_2to3)
272 goto error;
273 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
274 PyErr_Format(PyExc_RuntimeError,
275 "_compat_pickle.IMPORT_MAPPING should be a dict, "
276 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
277 goto error;
278 }
279 /* ... and the 3.x -> 2.x mapping tables */
280 st->name_mapping_3to2 = \
281 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
282 if (!st->name_mapping_3to2)
283 goto error;
284 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
285 PyErr_Format(PyExc_RuntimeError,
286 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
287 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
288 goto error;
289 }
290 st->import_mapping_3to2 = \
291 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
292 if (!st->import_mapping_3to2)
293 goto error;
294 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
295 PyErr_Format(PyExc_RuntimeError,
296 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
297 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
298 goto error;
299 }
300 Py_CLEAR(compat_pickle);
301
302 codecs = PyImport_ImportModule("codecs");
303 if (codecs == NULL)
304 goto error;
305 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
306 if (st->codecs_encode == NULL) {
307 goto error;
308 }
309 if (!PyCallable_Check(st->codecs_encode)) {
310 PyErr_Format(PyExc_RuntimeError,
311 "codecs.encode should be a callable, not %.200s",
312 Py_TYPE(st->codecs_encode)->tp_name);
313 goto error;
314 }
315 Py_CLEAR(codecs);
316
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800317 return 0;
318
319 error:
320 Py_CLEAR(copyreg);
321 Py_CLEAR(compat_pickle);
322 Py_CLEAR(codecs);
323 _Pickle_ClearState(st);
324 return -1;
325}
326
327/* Helper for calling a function with a single argument quickly.
328
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800329 This function steals the reference of the given argument. */
330static PyObject *
331_Pickle_FastCall(PyObject *func, PyObject *obj)
332{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800333 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800334 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800335
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800336 /* Note: this function used to reuse the argument tuple. This used to give
337 a slight performance boost with older pickle implementations where many
338 unbuffered reads occurred (thus needing many function calls).
339
340 However, this optimization was removed because it was too complicated
341 to get right. It abused the C API for tuples to mutate them which led
342 to subtle reference counting and concurrency bugs. Furthermore, the
343 introduction of protocol 4 and the prefetching optimization via peek()
344 significantly reduced the number of function calls we do. Thus, the
345 benefits became marginal at best. */
346
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 Py_DECREF(obj);
349 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800350 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800351 PyTuple_SET_ITEM(arg_tuple, 0, obj);
352 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800353 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800354 return result;
355}
356
357/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000358
359static int
360stack_underflow(void)
361{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800362 PickleState *st = _Pickle_GetGlobalState();
363 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000364 return -1;
365}
366
367/* Internal data type used as the unpickling stack. */
368typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000369 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000370 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000371 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372} Pdata;
373
374static void
375Pdata_dealloc(Pdata *self)
376{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200377 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000378 while (--i >= 0) {
379 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000381 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000382 PyObject_Del(self);
383}
384
385static PyTypeObject Pdata_Type = {
386 PyVarObject_HEAD_INIT(NULL, 0)
387 "_pickle.Pdata", /*tp_name*/
388 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200389 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000390 (destructor)Pdata_dealloc, /*tp_dealloc*/
391};
392
393static PyObject *
394Pdata_New(void)
395{
396 Pdata *self;
397
398 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
399 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000400 Py_SIZE(self) = 0;
401 self->allocated = 8;
402 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 if (self->data)
404 return (PyObject *)self;
405 Py_DECREF(self);
406 return PyErr_NoMemory();
407}
408
409
410/* Retain only the initial clearto items. If clearto >= the current
411 * number of items, this is a (non-erroneous) NOP.
412 */
413static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200414Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000415{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200416 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417
418 if (clearto < 0)
419 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000420 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000421 return 0;
422
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000423 while (--i >= clearto) {
424 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000426 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427 return 0;
428}
429
430static int
431Pdata_grow(Pdata *self)
432{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000433 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200434 size_t allocated = (size_t)self->allocated;
435 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437 new_allocated = (allocated >> 3) + 6;
438 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200439 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000441 new_allocated += allocated;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200442 if (new_allocated > ((size_t)PY_SSIZE_T_MAX / sizeof(PyObject *)))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000443 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000444 data = PyMem_REALLOC(data, new_allocated * sizeof(PyObject *));
445 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000446 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000447
448 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200449 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000450 return 0;
451
452 nomemory:
453 PyErr_NoMemory();
454 return -1;
455}
456
457/* D is a Pdata*. Pop the topmost element and store it into V, which
458 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
459 * is raised and V is set to NULL.
460 */
461static PyObject *
462Pdata_pop(Pdata *self)
463{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800464 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000465 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800466 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000467 return NULL;
468 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000469 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000470}
471#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
472
473static int
474Pdata_push(Pdata *self, PyObject *obj)
475{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000477 return -1;
478 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000479 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000480 return 0;
481}
482
483/* Push an object on stack, transferring its ownership to the stack. */
484#define PDATA_PUSH(D, O, ER) do { \
485 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
486
487/* Push an object on stack, adding a new reference to the object. */
488#define PDATA_APPEND(D, O, ER) do { \
489 Py_INCREF((O)); \
490 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
491
492static PyObject *
493Pdata_poptuple(Pdata *self, Py_ssize_t start)
494{
495 PyObject *tuple;
496 Py_ssize_t len, i, j;
497
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000498 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000499 tuple = PyTuple_New(len);
500 if (tuple == NULL)
501 return NULL;
502 for (i = start, j = 0; j < len; i++, j++)
503 PyTuple_SET_ITEM(tuple, j, self->data[i]);
504
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000505 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000506 return tuple;
507}
508
509static PyObject *
510Pdata_poplist(Pdata *self, Py_ssize_t start)
511{
512 PyObject *list;
513 Py_ssize_t len, i, j;
514
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000515 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000516 list = PyList_New(len);
517 if (list == NULL)
518 return NULL;
519 for (i = start, j = 0; j < len; i++, j++)
520 PyList_SET_ITEM(list, j, self->data[i]);
521
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000522 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000523 return list;
524}
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526typedef struct {
527 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200528 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000529} PyMemoEntry;
530
531typedef struct {
532 Py_ssize_t mt_mask;
533 Py_ssize_t mt_used;
534 Py_ssize_t mt_allocated;
535 PyMemoEntry *mt_table;
536} PyMemoTable;
537
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000538typedef struct PicklerObject {
539 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000541 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000542 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000543 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100544 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000545
546 PyObject *write; /* write() method of the output stream. */
547 PyObject *output_buffer; /* Write into a local bytearray buffer before
548 flushing to the stream. */
549 Py_ssize_t output_len; /* Length of output_buffer. */
550 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000551 int proto; /* Pickle protocol number, >= 0 */
552 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100553 int framing; /* True when framing is enabled, proto >= 4 */
554 Py_ssize_t frame_start; /* Position in output_buffer where the
555 where the current frame begins. -1 if there
556 is no frame currently open. */
557
558 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000559 int fast; /* Enable fast mode if set to a true value.
560 The fast mode disable the usage of memo,
561 therefore speeding the pickling process by
562 not generating superfluous PUT opcodes. It
563 should not be used if with self-referential
564 objects. */
565 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000566 int fix_imports; /* Indicate whether Pickler should fix
567 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000568 PyObject *fast_memo;
569} PicklerObject;
570
571typedef struct UnpicklerObject {
572 PyObject_HEAD
573 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000574
575 /* The unpickler memo is just an array of PyObject *s. Using a dict
576 is unnecessary, since the keys are contiguous ints. */
577 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100578 Py_ssize_t memo_size; /* Capacity of the memo array */
579 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000580
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000581 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000582
583 Py_buffer buffer;
584 char *input_buffer;
585 char *input_line;
586 Py_ssize_t input_len;
587 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000588 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000590 PyObject *read; /* read() method of the input stream. */
591 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000592 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000594 char *encoding; /* Name of the encoding to be used for
595 decoding strings pickled using Python
596 2.x. The default value is "ASCII" */
597 char *errors; /* Name of errors handling scheme to used when
598 decoding strings. The default value is
599 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500600 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000601 objects. */
602 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
603 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000604 int proto; /* Protocol of the pickle loaded. */
605 int fix_imports; /* Indicate whether Unpickler should fix
606 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000607} UnpicklerObject;
608
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200609typedef struct {
610 PyObject_HEAD
611 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
612} PicklerMemoProxyObject;
613
614typedef struct {
615 PyObject_HEAD
616 UnpicklerObject *unpickler;
617} UnpicklerMemoProxyObject;
618
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000619/* Forward declarations */
620static int save(PicklerObject *, PyObject *, int);
621static int save_reduce(PicklerObject *, PyObject *, PyObject *);
622static PyTypeObject Pickler_Type;
623static PyTypeObject Unpickler_Type;
624
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200625#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000626
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000627/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300628 A custom hashtable mapping void* to Python ints. This is used by the pickler
629 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000630 a bunch of unnecessary object creation. This makes a huge performance
631 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000633#define MT_MINSIZE 8
634#define PERTURB_SHIFT 5
635
636
637static PyMemoTable *
638PyMemoTable_New(void)
639{
640 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
641 if (memo == NULL) {
642 PyErr_NoMemory();
643 return NULL;
644 }
645
646 memo->mt_used = 0;
647 memo->mt_allocated = MT_MINSIZE;
648 memo->mt_mask = MT_MINSIZE - 1;
649 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
650 if (memo->mt_table == NULL) {
651 PyMem_FREE(memo);
652 PyErr_NoMemory();
653 return NULL;
654 }
655 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
656
657 return memo;
658}
659
660static PyMemoTable *
661PyMemoTable_Copy(PyMemoTable *self)
662{
663 Py_ssize_t i;
664 PyMemoTable *new = PyMemoTable_New();
665 if (new == NULL)
666 return NULL;
667
668 new->mt_used = self->mt_used;
669 new->mt_allocated = self->mt_allocated;
670 new->mt_mask = self->mt_mask;
671 /* The table we get from _New() is probably smaller than we wanted.
672 Free it and allocate one that's the right size. */
673 PyMem_FREE(new->mt_table);
674 new->mt_table = PyMem_MALLOC(self->mt_allocated * sizeof(PyMemoEntry));
675 if (new->mt_table == NULL) {
676 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200677 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000678 return NULL;
679 }
680 for (i = 0; i < self->mt_allocated; i++) {
681 Py_XINCREF(self->mt_table[i].me_key);
682 }
683 memcpy(new->mt_table, self->mt_table,
684 sizeof(PyMemoEntry) * self->mt_allocated);
685
686 return new;
687}
688
689static Py_ssize_t
690PyMemoTable_Size(PyMemoTable *self)
691{
692 return self->mt_used;
693}
694
695static int
696PyMemoTable_Clear(PyMemoTable *self)
697{
698 Py_ssize_t i = self->mt_allocated;
699
700 while (--i >= 0) {
701 Py_XDECREF(self->mt_table[i].me_key);
702 }
703 self->mt_used = 0;
704 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
705 return 0;
706}
707
708static void
709PyMemoTable_Del(PyMemoTable *self)
710{
711 if (self == NULL)
712 return;
713 PyMemoTable_Clear(self);
714
715 PyMem_FREE(self->mt_table);
716 PyMem_FREE(self);
717}
718
719/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
720 can be considerably simpler than dictobject.c's lookdict(). */
721static PyMemoEntry *
722_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
723{
724 size_t i;
725 size_t perturb;
726 size_t mask = (size_t)self->mt_mask;
727 PyMemoEntry *table = self->mt_table;
728 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000729 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000730
731 i = hash & mask;
732 entry = &table[i];
733 if (entry->me_key == NULL || entry->me_key == key)
734 return entry;
735
736 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
737 i = (i << 2) + i + perturb + 1;
738 entry = &table[i & mask];
739 if (entry->me_key == NULL || entry->me_key == key)
740 return entry;
741 }
742 assert(0); /* Never reached */
743 return NULL;
744}
745
746/* Returns -1 on failure, 0 on success. */
747static int
748_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
749{
750 PyMemoEntry *oldtable = NULL;
751 PyMemoEntry *oldentry, *newentry;
752 Py_ssize_t new_size = MT_MINSIZE;
753 Py_ssize_t to_process;
754
755 assert(min_size > 0);
756
757 /* Find the smallest valid table size >= min_size. */
758 while (new_size < min_size && new_size > 0)
759 new_size <<= 1;
760 if (new_size <= 0) {
761 PyErr_NoMemory();
762 return -1;
763 }
764 /* new_size needs to be a power of two. */
765 assert((new_size & (new_size - 1)) == 0);
766
767 /* Allocate new table. */
768 oldtable = self->mt_table;
769 self->mt_table = PyMem_MALLOC(new_size * sizeof(PyMemoEntry));
770 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200771 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000772 PyErr_NoMemory();
773 return -1;
774 }
775 self->mt_allocated = new_size;
776 self->mt_mask = new_size - 1;
777 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
778
779 /* Copy entries from the old table. */
780 to_process = self->mt_used;
781 for (oldentry = oldtable; to_process > 0; oldentry++) {
782 if (oldentry->me_key != NULL) {
783 to_process--;
784 /* newentry is a pointer to a chunk of the new
785 mt_table, so we're setting the key:value pair
786 in-place. */
787 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
788 newentry->me_key = oldentry->me_key;
789 newentry->me_value = oldentry->me_value;
790 }
791 }
792
793 /* Deallocate the old table. */
794 PyMem_FREE(oldtable);
795 return 0;
796}
797
798/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200799static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000800PyMemoTable_Get(PyMemoTable *self, PyObject *key)
801{
802 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
803 if (entry->me_key == NULL)
804 return NULL;
805 return &entry->me_value;
806}
807
808/* Returns -1 on failure, 0 on success. */
809static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200810PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000811{
812 PyMemoEntry *entry;
813
814 assert(key != NULL);
815
816 entry = _PyMemoTable_Lookup(self, key);
817 if (entry->me_key != NULL) {
818 entry->me_value = value;
819 return 0;
820 }
821 Py_INCREF(key);
822 entry->me_key = key;
823 entry->me_value = value;
824 self->mt_used++;
825
826 /* If we added a key, we can safely resize. Otherwise just return!
827 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
828 *
829 * Quadrupling the size improves average table sparseness
830 * (reducing collisions) at the cost of some memory. It also halves
831 * the number of expensive resize operations in a growing memo table.
832 *
833 * Very large memo tables (over 50K items) use doubling instead.
834 * This may help applications with severe memory constraints.
835 */
836 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
837 return 0;
838 return _PyMemoTable_ResizeTable(self,
839 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
840}
841
842#undef MT_MINSIZE
843#undef PERTURB_SHIFT
844
845/*************************************************************************/
846
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000847
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000848static int
849_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000850{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000851 Py_CLEAR(self->output_buffer);
852 self->output_buffer =
853 PyBytes_FromStringAndSize(NULL, self->max_output_len);
854 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000855 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000856 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100857 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000858 return 0;
859}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000860
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100861static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100862_write_size64(char *out, size_t value)
863{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200864 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800865
866 assert(sizeof(size_t) <= 8);
867
868 for (i = 0; i < sizeof(size_t); i++) {
869 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
870 }
871 for (i = sizeof(size_t); i < 8; i++) {
872 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800873 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100874}
875
876static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100877_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
878{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100879 qdata[0] = FRAME;
880 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100881}
882
883static int
884_Pickler_CommitFrame(PicklerObject *self)
885{
886 size_t frame_len;
887 char *qdata;
888
889 if (!self->framing || self->frame_start == -1)
890 return 0;
891 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
892 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
893 _Pickler_WriteFrameHeader(self, qdata, frame_len);
894 self->frame_start = -1;
895 return 0;
896}
897
898static int
899_Pickler_OpcodeBoundary(PicklerObject *self)
900{
901 Py_ssize_t frame_len;
902
903 if (!self->framing || self->frame_start == -1)
904 return 0;
905 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
906 if (frame_len >= FRAME_SIZE_TARGET)
907 return _Pickler_CommitFrame(self);
908 else
909 return 0;
910}
911
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000912static PyObject *
913_Pickler_GetString(PicklerObject *self)
914{
915 PyObject *output_buffer = self->output_buffer;
916
917 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100918
919 if (_Pickler_CommitFrame(self))
920 return NULL;
921
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000922 self->output_buffer = NULL;
923 /* Resize down to exact size */
924 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
925 return NULL;
926 return output_buffer;
927}
928
929static int
930_Pickler_FlushToFile(PicklerObject *self)
931{
932 PyObject *output, *result;
933
934 assert(self->write != NULL);
935
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100936 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000937 output = _Pickler_GetString(self);
938 if (output == NULL)
939 return -1;
940
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800941 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 Py_XDECREF(result);
943 return (result == NULL) ? -1 : 0;
944}
945
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200946static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100947_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000948{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100949 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000950 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100951 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000952
953 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100954 need_new_frame = (self->framing && self->frame_start == -1);
955
956 if (need_new_frame)
957 n = data_len + FRAME_HEADER_SIZE;
958 else
959 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000960
961 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100962 if (required > self->max_output_len) {
963 /* Make place in buffer for the pickle chunk */
964 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
965 PyErr_NoMemory();
966 return -1;
967 }
968 self->max_output_len = (self->output_len + n) / 2 * 3;
969 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
970 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000971 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100973 if (need_new_frame) {
974 /* Setup new frame */
975 Py_ssize_t frame_start = self->output_len;
976 self->frame_start = frame_start;
977 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
978 /* Write an invalid value, for debugging */
979 buffer[frame_start + i] = 0xFE;
980 }
981 self->output_len += FRAME_HEADER_SIZE;
982 }
983 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100985 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000986 buffer[self->output_len + i] = s[i];
987 }
988 }
989 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100990 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000991 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100992 self->output_len += data_len;
993 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000994}
995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996static PicklerObject *
997_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000998{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000999 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001000
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001001 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1002 if (self == NULL)
1003 return NULL;
1004
1005 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001006 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007 self->write = NULL;
1008 self->proto = 0;
1009 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001010 self->framing = 0;
1011 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 self->fast = 0;
1013 self->fast_nesting = 0;
1014 self->fix_imports = 0;
1015 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016 self->max_output_len = WRITE_BUF_SIZE;
1017 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001018
1019 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001020 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1021 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001022
1023 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001024 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001025 return NULL;
1026 }
1027 return self;
1028}
1029
1030static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001031_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001033 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001034
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001035 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001037 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001038 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001039 proto = PyLong_AsLong(protocol);
1040 if (proto < 0) {
1041 if (proto == -1 && PyErr_Occurred())
1042 return -1;
1043 proto = HIGHEST_PROTOCOL;
1044 }
1045 else if (proto > HIGHEST_PROTOCOL) {
1046 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1047 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052 self->bin = proto > 0;
1053 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054 return 0;
1055}
1056
1057/* Returns -1 (with an exception set) on failure, 0 on success. This may
1058 be called once on a freshly created Pickler. */
1059static int
1060_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1061{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001062 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001064 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001065 if (self->write == NULL) {
1066 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1067 PyErr_SetString(PyExc_TypeError,
1068 "file must have a 'write' attribute");
1069 return -1;
1070 }
1071
1072 return 0;
1073}
1074
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075/* Returns the size of the input on success, -1 on failure. This takes its
1076 own reference to `input`. */
1077static Py_ssize_t
1078_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1079{
1080 if (self->buffer.buf != NULL)
1081 PyBuffer_Release(&self->buffer);
1082 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1083 return -1;
1084 self->input_buffer = self->buffer.buf;
1085 self->input_len = self->buffer.len;
1086 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001087 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001088 return self->input_len;
1089}
1090
Antoine Pitrou04248a82010-10-12 20:51:21 +00001091static int
1092_Unpickler_SkipConsumed(UnpicklerObject *self)
1093{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001094 Py_ssize_t consumed;
1095 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001096
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001097 consumed = self->next_read_idx - self->prefetched_idx;
1098 if (consumed <= 0)
1099 return 0;
1100
1101 assert(self->peek); /* otherwise we did something wrong */
1102 /* This makes an useless copy... */
1103 r = PyObject_CallFunction(self->read, "n", consumed);
1104 if (r == NULL)
1105 return -1;
1106 Py_DECREF(r);
1107
1108 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001109 return 0;
1110}
1111
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001112static const Py_ssize_t READ_WHOLE_LINE = -1;
1113
1114/* If reading from a file, we need to only pull the bytes we need, since there
1115 may be multiple pickle objects arranged contiguously in the same input
1116 buffer.
1117
1118 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1119 bytes from the input stream/buffer.
1120
1121 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1122 failure; on success, returns the number of bytes read from the file.
1123
1124 On success, self->input_len will be 0; this is intentional so that when
1125 unpickling from a file, the "we've run out of data" code paths will trigger,
1126 causing the Unpickler to go back to the file for more data. Use the returned
1127 size to tell you how much data you can process. */
1128static Py_ssize_t
1129_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1130{
1131 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001132 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001133
1134 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001135
Antoine Pitrou04248a82010-10-12 20:51:21 +00001136 if (_Unpickler_SkipConsumed(self) < 0)
1137 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001138
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001139 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001140 PyObject *empty_tuple = PyTuple_New(0);
1141 data = PyObject_Call(self->readline, empty_tuple, NULL);
1142 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001143 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001145 PyObject *len;
1146 /* Prefetch some data without advancing the file pointer, if possible */
1147 if (self->peek && n < PREFETCH) {
1148 len = PyLong_FromSsize_t(PREFETCH);
1149 if (len == NULL)
1150 return -1;
1151 data = _Pickle_FastCall(self->peek, len);
1152 if (data == NULL) {
1153 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1154 return -1;
1155 /* peek() is probably not supported by the given file object */
1156 PyErr_Clear();
1157 Py_CLEAR(self->peek);
1158 }
1159 else {
1160 read_size = _Unpickler_SetStringInput(self, data);
1161 Py_DECREF(data);
1162 self->prefetched_idx = 0;
1163 if (n <= read_size)
1164 return n;
1165 }
1166 }
1167 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001168 if (len == NULL)
1169 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001170 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001171 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001172 if (data == NULL)
1173 return -1;
1174
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001175 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001176 Py_DECREF(data);
1177 return read_size;
1178}
1179
1180/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1181
1182 This should be used for all data reads, rather than accessing the unpickler's
1183 input buffer directly. This method deals correctly with reading from input
1184 streams, which the input buffer doesn't deal with.
1185
1186 Note that when reading from a file-like object, self->next_read_idx won't
1187 be updated (it should remain at 0 for the entire unpickling process). You
1188 should use this function's return value to know how many bytes you can
1189 consume.
1190
1191 Returns -1 (with an exception set) on failure. On success, return the
1192 number of chars read. */
1193static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001194_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001195{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001196 Py_ssize_t num_read;
1197
Antoine Pitrou04248a82010-10-12 20:51:21 +00001198 if (self->next_read_idx + n <= self->input_len) {
1199 *s = self->input_buffer + self->next_read_idx;
1200 self->next_read_idx += n;
1201 return n;
1202 }
1203 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001204 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001205 return -1;
1206 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001207 num_read = _Unpickler_ReadFromFile(self, n);
1208 if (num_read < 0)
1209 return -1;
1210 if (num_read < n) {
1211 PyErr_Format(PyExc_EOFError, "Ran out of input");
1212 return -1;
1213 }
1214 *s = self->input_buffer;
1215 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001216 return n;
1217}
1218
1219static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001220_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1221 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001222{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001224 if (input_line == NULL) {
1225 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001226 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001227 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001228
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001229 memcpy(input_line, line, len);
1230 input_line[len] = '\0';
1231 self->input_line = input_line;
1232 *result = self->input_line;
1233 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001234}
1235
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001236/* Read a line from the input stream/buffer. If we run off the end of the input
1237 before hitting \n, return the data we found.
1238
1239 Returns the number of chars read, or -1 on failure. */
1240static Py_ssize_t
1241_Unpickler_Readline(UnpicklerObject *self, char **result)
1242{
1243 Py_ssize_t i, num_read;
1244
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001245 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001246 if (self->input_buffer[i] == '\n') {
1247 char *line_start = self->input_buffer + self->next_read_idx;
1248 num_read = i - self->next_read_idx + 1;
1249 self->next_read_idx = i + 1;
1250 return _Unpickler_CopyLine(self, line_start, num_read, result);
1251 }
1252 }
1253 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001254 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1255 if (num_read < 0)
1256 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001257 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001258 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001259 }
Victor Stinner121aab42011-09-29 23:40:53 +02001260
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001261 /* If we get here, we've run off the end of the input string. Return the
1262 remaining string and let the caller figure it out. */
1263 *result = self->input_buffer + self->next_read_idx;
1264 num_read = i - self->next_read_idx;
1265 self->next_read_idx = i;
1266 return num_read;
1267}
1268
1269/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1270 will be modified in place. */
1271static int
1272_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1273{
1274 Py_ssize_t i;
1275 PyObject **memo;
1276
1277 assert(new_size > self->memo_size);
1278
1279 memo = PyMem_REALLOC(self->memo, new_size * sizeof(PyObject *));
1280 if (memo == NULL) {
1281 PyErr_NoMemory();
1282 return -1;
1283 }
1284 self->memo = memo;
1285 for (i = self->memo_size; i < new_size; i++)
1286 self->memo[i] = NULL;
1287 self->memo_size = new_size;
1288 return 0;
1289}
1290
1291/* Returns NULL if idx is out of bounds. */
1292static PyObject *
1293_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1294{
1295 if (idx < 0 || idx >= self->memo_size)
1296 return NULL;
1297
1298 return self->memo[idx];
1299}
1300
1301/* Returns -1 (with an exception set) on failure, 0 on success.
1302 This takes its own reference to `value`. */
1303static int
1304_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1305{
1306 PyObject *old_item;
1307
1308 if (idx >= self->memo_size) {
1309 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1310 return -1;
1311 assert(idx < self->memo_size);
1312 }
1313 Py_INCREF(value);
1314 old_item = self->memo[idx];
1315 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001316 if (old_item != NULL) {
1317 Py_DECREF(old_item);
1318 }
1319 else {
1320 self->memo_len++;
1321 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001322 return 0;
1323}
1324
1325static PyObject **
1326_Unpickler_NewMemo(Py_ssize_t new_size)
1327{
1328 PyObject **memo = PyMem_MALLOC(new_size * sizeof(PyObject *));
Victor Stinner42024562013-07-12 00:53:57 +02001329 if (memo == NULL) {
1330 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001331 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001332 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001333 memset(memo, 0, new_size * sizeof(PyObject *));
1334 return memo;
1335}
1336
1337/* Free the unpickler's memo, taking care to decref any items left in it. */
1338static void
1339_Unpickler_MemoCleanup(UnpicklerObject *self)
1340{
1341 Py_ssize_t i;
1342 PyObject **memo = self->memo;
1343
1344 if (self->memo == NULL)
1345 return;
1346 self->memo = NULL;
1347 i = self->memo_size;
1348 while (--i >= 0) {
1349 Py_XDECREF(memo[i]);
1350 }
1351 PyMem_FREE(memo);
1352}
1353
1354static UnpicklerObject *
1355_Unpickler_New(void)
1356{
1357 UnpicklerObject *self;
1358
1359 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1360 if (self == NULL)
1361 return NULL;
1362
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001363 self->pers_func = NULL;
1364 self->input_buffer = NULL;
1365 self->input_line = NULL;
1366 self->input_len = 0;
1367 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001368 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001369 self->read = NULL;
1370 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001371 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001372 self->encoding = NULL;
1373 self->errors = NULL;
1374 self->marks = NULL;
1375 self->num_marks = 0;
1376 self->marks_size = 0;
1377 self->proto = 0;
1378 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001379 memset(&self->buffer, 0, sizeof(Py_buffer));
1380 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001381 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001382 self->memo = _Unpickler_NewMemo(self->memo_size);
1383 self->stack = (Pdata *)Pdata_New();
1384
1385 if (self->memo == NULL || self->stack == NULL) {
1386 Py_DECREF(self);
1387 return NULL;
1388 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389
1390 return self;
1391}
1392
1393/* Returns -1 (with an exception set) on failure, 0 on success. This may
1394 be called once on a freshly created Pickler. */
1395static int
1396_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1397{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001398 _Py_IDENTIFIER(peek);
1399 _Py_IDENTIFIER(read);
1400 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001401
1402 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001403 if (self->peek == NULL) {
1404 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1405 PyErr_Clear();
1406 else
1407 return -1;
1408 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001409 self->read = _PyObject_GetAttrId(file, &PyId_read);
1410 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001411 if (self->readline == NULL || self->read == NULL) {
1412 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1413 PyErr_SetString(PyExc_TypeError,
1414 "file must have 'read' and 'readline' attributes");
1415 Py_CLEAR(self->read);
1416 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001417 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001418 return -1;
1419 }
1420 return 0;
1421}
1422
1423/* Returns -1 (with an exception set) on failure, 0 on success. This may
1424 be called once on a freshly created Pickler. */
1425static int
1426_Unpickler_SetInputEncoding(UnpicklerObject *self,
1427 const char *encoding,
1428 const char *errors)
1429{
1430 if (encoding == NULL)
1431 encoding = "ASCII";
1432 if (errors == NULL)
1433 errors = "strict";
1434
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001435 self->encoding = _PyMem_Strdup(encoding);
1436 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001437 if (self->encoding == NULL || self->errors == NULL) {
1438 PyErr_NoMemory();
1439 return -1;
1440 }
1441 return 0;
1442}
1443
1444/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001445static int
1446memo_get(PicklerObject *self, PyObject *key)
1447{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001448 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001449 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001450 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001452 value = PyMemoTable_Get(self->memo, key);
1453 if (value == NULL) {
1454 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001455 return -1;
1456 }
1457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001458 if (!self->bin) {
1459 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001460 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1461 "%" PY_FORMAT_SIZE_T "d\n", *value);
1462 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001463 }
1464 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001465 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001466 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001467 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468 len = 2;
1469 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001470 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001472 pdata[1] = (unsigned char)(*value & 0xff);
1473 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1474 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1475 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001476 len = 5;
1477 }
1478 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001479 PickleState *st = _Pickle_GetGlobalState();
1480 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001481 "memo id too large for LONG_BINGET");
1482 return -1;
1483 }
1484 }
1485
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001486 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 return -1;
1488
1489 return 0;
1490}
1491
1492/* Store an object in the memo, assign it a new unique ID based on the number
1493 of objects currently stored in the memo and generate a PUT opcode. */
1494static int
1495memo_put(PicklerObject *self, PyObject *obj)
1496{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001498 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001499 Py_ssize_t idx;
1500
1501 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001502
1503 if (self->fast)
1504 return 0;
1505
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001506 idx = PyMemoTable_Size(self->memo);
1507 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1508 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001509
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001510 if (self->proto >= 4) {
1511 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1512 return -1;
1513 return 0;
1514 }
1515 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001516 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001517 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001518 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519 len = strlen(pdata);
1520 }
1521 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001522 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001523 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001524 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525 len = 2;
1526 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001527 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001528 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001529 pdata[1] = (unsigned char)(idx & 0xff);
1530 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1531 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1532 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533 len = 5;
1534 }
1535 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001536 PickleState *st = _Pickle_GetGlobalState();
1537 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001538 "memo id too large for LONG_BINPUT");
1539 return -1;
1540 }
1541 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001542 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001544
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001545 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001546}
1547
1548static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001549get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001550 _Py_static_string(PyId_dot, ".");
1551 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001552 PyObject *dotted_path;
1553 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001554
1555 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001556 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001557 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001558 n = PyList_GET_SIZE(dotted_path);
1559 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001560 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001561 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001562 PyObject *result = PyUnicode_RichCompare(
1563 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1564 int is_equal = (result == Py_True);
1565 assert(PyBool_Check(result));
1566 Py_DECREF(result);
1567 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001568 if (obj == NULL)
1569 PyErr_Format(PyExc_AttributeError,
1570 "Can't pickle local object %R", name);
1571 else
1572 PyErr_Format(PyExc_AttributeError,
1573 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575 return NULL;
1576 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001577 }
1578 return dotted_path;
1579}
1580
1581static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001582get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001583{
1584 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001585 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001586
1587 assert(PyList_CheckExact(names));
1588 Py_INCREF(obj);
1589 n = PyList_GET_SIZE(names);
1590 for (i = 0; i < n; i++) {
1591 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001592 Py_XDECREF(parent);
1593 parent = obj;
1594 obj = PyObject_GetAttr(parent, name);
1595 if (obj == NULL) {
1596 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001597 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001598 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001599 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001600 if (pparent != NULL)
1601 *pparent = parent;
1602 else
1603 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001604 return obj;
1605}
1606
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001607static void
1608reformat_attribute_error(PyObject *obj, PyObject *name)
1609{
1610 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1611 PyErr_Clear();
1612 PyErr_Format(PyExc_AttributeError,
1613 "Can't get attribute %R on %R", name, obj);
1614 }
1615}
1616
1617
1618static PyObject *
1619getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1620{
1621 PyObject *dotted_path, *attr;
1622
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001623 if (allow_qualname) {
1624 dotted_path = get_dotted_path(obj, name);
1625 if (dotted_path == NULL)
1626 return NULL;
1627 attr = get_deep_attribute(obj, dotted_path, NULL);
1628 Py_DECREF(dotted_path);
1629 }
1630 else
1631 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001632 if (attr == NULL)
1633 reformat_attribute_error(obj, name);
1634 return attr;
1635}
1636
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001637static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001638whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001639{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001640 PyObject *module_name;
1641 PyObject *modules_dict;
1642 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001643 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001644 _Py_IDENTIFIER(__module__);
1645 _Py_IDENTIFIER(modules);
1646 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001647
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001648 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1649
1650 if (module_name == NULL) {
1651 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001652 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001653 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654 }
1655 else {
1656 /* In some rare cases (e.g., bound methods of extension types),
1657 __module__ can be None. If it is so, then search sys.modules for
1658 the module of global. */
1659 if (module_name != Py_None)
1660 return module_name;
1661 Py_CLEAR(module_name);
1662 }
1663 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001664
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001665 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001666 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001667 if (modules_dict == NULL) {
1668 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001669 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001670 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001671
1672 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001673 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1674 PyObject *candidate;
1675 if (PyUnicode_Check(module_name) &&
1676 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001677 continue;
1678 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001679 continue;
1680
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001681 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001683 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001684 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001685 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001686 continue;
1687 }
1688
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001690 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001691 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001692 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001693 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001694 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 }
1696
1697 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001698 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001699 Py_INCREF(module_name);
1700 return module_name;
1701}
1702
1703/* fast_save_enter() and fast_save_leave() are guards against recursive
1704 objects when Pickler is used with the "fast mode" (i.e., with object
1705 memoization disabled). If the nesting of a list or dict object exceed
1706 FAST_NESTING_LIMIT, these guards will start keeping an internal
1707 reference to the seen list or dict objects and check whether these objects
1708 are recursive. These are not strictly necessary, since save() has a
1709 hard-coded recursion limit, but they give a nicer error message than the
1710 typical RuntimeError. */
1711static int
1712fast_save_enter(PicklerObject *self, PyObject *obj)
1713{
1714 /* if fast_nesting < 0, we're doing an error exit. */
1715 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1716 PyObject *key = NULL;
1717 if (self->fast_memo == NULL) {
1718 self->fast_memo = PyDict_New();
1719 if (self->fast_memo == NULL) {
1720 self->fast_nesting = -1;
1721 return 0;
1722 }
1723 }
1724 key = PyLong_FromVoidPtr(obj);
1725 if (key == NULL)
1726 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001727 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001728 Py_DECREF(key);
1729 PyErr_Format(PyExc_ValueError,
1730 "fast mode: can't pickle cyclic objects "
1731 "including object type %.200s at %p",
1732 obj->ob_type->tp_name, obj);
1733 self->fast_nesting = -1;
1734 return 0;
1735 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001736 if (PyErr_Occurred()) {
1737 return 0;
1738 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001739 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1740 Py_DECREF(key);
1741 self->fast_nesting = -1;
1742 return 0;
1743 }
1744 Py_DECREF(key);
1745 }
1746 return 1;
1747}
1748
1749static int
1750fast_save_leave(PicklerObject *self, PyObject *obj)
1751{
1752 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1753 PyObject *key = PyLong_FromVoidPtr(obj);
1754 if (key == NULL)
1755 return 0;
1756 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1757 Py_DECREF(key);
1758 return 0;
1759 }
1760 Py_DECREF(key);
1761 }
1762 return 1;
1763}
1764
1765static int
1766save_none(PicklerObject *self, PyObject *obj)
1767{
1768 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001769 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001770 return -1;
1771
1772 return 0;
1773}
1774
1775static int
1776save_bool(PicklerObject *self, PyObject *obj)
1777{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001778 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001779 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001780 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001781 return -1;
1782 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001783 else {
1784 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1785 * so that unpicklers written before bools were introduced unpickle them
1786 * as ints, but unpicklers after can recognize that bools were intended.
1787 * Note that protocol 2 added direct ways to pickle bools.
1788 */
1789 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1790 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1791 return -1;
1792 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001793 return 0;
1794}
1795
1796static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001797save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001798{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001799 PyObject *repr = NULL;
1800 Py_ssize_t size;
1801 long val;
1802 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001803
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001804 const char long_op = LONG;
1805
1806 val= PyLong_AsLong(obj);
1807 if (val == -1 && PyErr_Occurred()) {
1808 /* out of range for int pickling */
1809 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001810 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001811 else if (self->bin &&
1812 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001813 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001814 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001815
1816 Note: we can't use -0x80000000L in the above condition because some
1817 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1818 before applying the unary minus when sizeof(long) <= 4. The
1819 resulting value stays unsigned which is commonly not what we want,
1820 so MSVC happily warns us about it. However, that result would have
1821 been fine because we guard for sizeof(long) <= 4 which turns the
1822 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001823 char pdata[32];
1824 Py_ssize_t len = 0;
1825
1826 pdata[1] = (unsigned char)(val & 0xff);
1827 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1828 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1829 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001830
1831 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1832 if (pdata[2] == 0) {
1833 pdata[0] = BININT1;
1834 len = 2;
1835 }
1836 else {
1837 pdata[0] = BININT2;
1838 len = 3;
1839 }
1840 }
1841 else {
1842 pdata[0] = BININT;
1843 len = 5;
1844 }
1845
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001846 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001848
1849 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001850 }
1851
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001852 if (self->proto >= 2) {
1853 /* Linear-time pickling. */
1854 size_t nbits;
1855 size_t nbytes;
1856 unsigned char *pdata;
1857 char header[5];
1858 int i;
1859 int sign = _PyLong_Sign(obj);
1860
1861 if (sign == 0) {
1862 header[0] = LONG1;
1863 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001864 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001865 goto error;
1866 return 0;
1867 }
1868 nbits = _PyLong_NumBits(obj);
1869 if (nbits == (size_t)-1 && PyErr_Occurred())
1870 goto error;
1871 /* How many bytes do we need? There are nbits >> 3 full
1872 * bytes of data, and nbits & 7 leftover bits. If there
1873 * are any leftover bits, then we clearly need another
1874 * byte. Wnat's not so obvious is that we *probably*
1875 * need another byte even if there aren't any leftovers:
1876 * the most-significant bit of the most-significant byte
1877 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001878 * opposite of the one we need. The exception is ints
1879 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001880 * its own 256's-complement, so has the right sign bit
1881 * even without the extra byte. That's a pain to check
1882 * for in advance, though, so we always grab an extra
1883 * byte at the start, and cut it back later if possible.
1884 */
1885 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001886 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001887 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001888 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001889 goto error;
1890 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001891 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001892 if (repr == NULL)
1893 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001894 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001895 i = _PyLong_AsByteArray((PyLongObject *)obj,
1896 pdata, nbytes,
1897 1 /* little endian */ , 1 /* signed */ );
1898 if (i < 0)
1899 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001900 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001901 * needed. This is so iff the MSB is all redundant sign
1902 * bits.
1903 */
1904 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001905 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 pdata[nbytes - 1] == 0xff &&
1907 (pdata[nbytes - 2] & 0x80) != 0) {
1908 nbytes--;
1909 }
1910
1911 if (nbytes < 256) {
1912 header[0] = LONG1;
1913 header[1] = (unsigned char)nbytes;
1914 size = 2;
1915 }
1916 else {
1917 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001918 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001919 for (i = 1; i < 5; i++) {
1920 header[i] = (unsigned char)(size & 0xff);
1921 size >>= 8;
1922 }
1923 size = 5;
1924 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001925 if (_Pickler_Write(self, header, size) < 0 ||
1926 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001927 goto error;
1928 }
1929 else {
1930 char *string;
1931
Mark Dickinson8dd05142009-01-20 20:43:58 +00001932 /* proto < 2: write the repr and newline. This is quadratic-time (in
1933 the number of digits), in both directions. We add a trailing 'L'
1934 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935
1936 repr = PyObject_Repr(obj);
1937 if (repr == NULL)
1938 goto error;
1939
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001940 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001941 if (string == NULL)
1942 goto error;
1943
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001944 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1945 _Pickler_Write(self, string, size) < 0 ||
1946 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001947 goto error;
1948 }
1949
1950 if (0) {
1951 error:
1952 status = -1;
1953 }
1954 Py_XDECREF(repr);
1955
1956 return status;
1957}
1958
1959static int
1960save_float(PicklerObject *self, PyObject *obj)
1961{
1962 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1963
1964 if (self->bin) {
1965 char pdata[9];
1966 pdata[0] = BINFLOAT;
1967 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1968 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001969 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001970 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001971 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001973 int result = -1;
1974 char *buf = NULL;
1975 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001976
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001977 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001978 goto done;
1979
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001980 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001981 if (!buf) {
1982 PyErr_NoMemory();
1983 goto done;
1984 }
1985
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001986 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001987 goto done;
1988
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001989 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001990 goto done;
1991
1992 result = 0;
1993done:
1994 PyMem_Free(buf);
1995 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001996 }
1997
1998 return 0;
1999}
2000
2001static int
2002save_bytes(PicklerObject *self, PyObject *obj)
2003{
2004 if (self->proto < 3) {
2005 /* Older pickle protocols do not have an opcode for pickling bytes
2006 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002007 the __reduce__ method) to permit bytes object unpickling.
2008
2009 Here we use a hack to be compatible with Python 2. Since in Python
2010 2 'bytes' is just an alias for 'str' (which has different
2011 parameters than the actual bytes object), we use codecs.encode
2012 to create the appropriate 'str' object when unpickled using
2013 Python 2 *and* the appropriate 'bytes' object when unpickled
2014 using Python 3. Again this is a hack and we don't need to do this
2015 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002016 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002017 int status;
2018
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002019 if (PyBytes_GET_SIZE(obj) == 0) {
2020 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2021 }
2022 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002023 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002024 PyObject *unicode_str =
2025 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2026 PyBytes_GET_SIZE(obj),
2027 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002028 _Py_IDENTIFIER(latin1);
2029
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002030 if (unicode_str == NULL)
2031 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002032 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002033 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002034 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002035 Py_DECREF(unicode_str);
2036 }
2037
2038 if (reduce_value == NULL)
2039 return -1;
2040
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 /* save_reduce() will memoize the object automatically. */
2042 status = save_reduce(self, reduce_value, obj);
2043 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002044 return status;
2045 }
2046 else {
2047 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002048 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002049 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002050
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002051 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002052 if (size < 0)
2053 return -1;
2054
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002055 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002056 header[0] = SHORT_BINBYTES;
2057 header[1] = (unsigned char)size;
2058 len = 2;
2059 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002060 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061 header[0] = BINBYTES;
2062 header[1] = (unsigned char)(size & 0xff);
2063 header[2] = (unsigned char)((size >> 8) & 0xff);
2064 header[3] = (unsigned char)((size >> 16) & 0xff);
2065 header[4] = (unsigned char)((size >> 24) & 0xff);
2066 len = 5;
2067 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002068 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002069 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002070 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002071 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002072 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002074 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002075 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002076 return -1; /* string too large */
2077 }
2078
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002079 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002080 return -1;
2081
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002082 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002083 return -1;
2084
2085 if (memo_put(self, obj) < 0)
2086 return -1;
2087
2088 return 0;
2089 }
2090}
2091
2092/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2093 backslash and newline characters to \uXXXX escapes. */
2094static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002095raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002097 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002099 Py_ssize_t i, size;
2100 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002101 void *data;
2102 unsigned int kind;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002103
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002104 if (PyUnicode_READY(obj))
2105 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002106
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002107 size = PyUnicode_GET_LENGTH(obj);
2108 data = PyUnicode_DATA(obj);
2109 kind = PyUnicode_KIND(obj);
2110 if (kind == PyUnicode_4BYTE_KIND)
2111 expandsize = 10;
2112 else
2113 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002114
Victor Stinner049e5092014-08-17 22:20:00 +02002115 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002116 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002117 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002118 if (repr == NULL)
2119 return NULL;
2120 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002121 return repr;
2122 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002123
Victor Stinner7270b7f2014-08-17 21:14:46 +02002124 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002125 for (i=0; i < size; i++) {
2126 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002127 /* Map 32-bit characters to '\Uxxxxxxxx' */
2128 if (ch >= 0x10000) {
2129 *p++ = '\\';
2130 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002131 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2132 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2133 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2134 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2135 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2136 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2137 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2138 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002139 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002140 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002141 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 *p++ = '\\';
2143 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002144 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2147 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002148 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002149 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002150 else
2151 *p++ = (char) ch;
2152 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002153 size = p - PyBytes_AS_STRING(repr);
2154 if (_PyBytes_Resize(&repr, size) < 0)
2155 return NULL;
2156 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002157}
2158
2159static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002160write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2161{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002162 char header[9];
2163 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002164
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002165 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002166 if (size <= 0xff && self->proto >= 4) {
2167 header[0] = SHORT_BINUNICODE;
2168 header[1] = (unsigned char)(size & 0xff);
2169 len = 2;
2170 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002171 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002172 header[0] = BINUNICODE;
2173 header[1] = (unsigned char)(size & 0xff);
2174 header[2] = (unsigned char)((size >> 8) & 0xff);
2175 header[3] = (unsigned char)((size >> 16) & 0xff);
2176 header[4] = (unsigned char)((size >> 24) & 0xff);
2177 len = 5;
2178 }
2179 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002180 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002181 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002182 len = 9;
2183 }
2184 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002185 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002186 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002187 return -1;
2188 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002189
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002190 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002191 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002192 if (_Pickler_Write(self, data, size) < 0)
2193 return -1;
2194
2195 return 0;
2196}
2197
2198static int
2199write_unicode_binary(PicklerObject *self, PyObject *obj)
2200{
2201 PyObject *encoded = NULL;
2202 Py_ssize_t size;
2203 char *data;
2204 int r;
2205
2206 if (PyUnicode_READY(obj))
2207 return -1;
2208
2209 data = PyUnicode_AsUTF8AndSize(obj, &size);
2210 if (data != NULL)
2211 return write_utf8(self, data, size);
2212
2213 /* Issue #8383: for strings with lone surrogates, fallback on the
2214 "surrogatepass" error handler. */
2215 PyErr_Clear();
2216 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2217 if (encoded == NULL)
2218 return -1;
2219
2220 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2221 PyBytes_GET_SIZE(encoded));
2222 Py_DECREF(encoded);
2223 return r;
2224}
2225
2226static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002227save_unicode(PicklerObject *self, PyObject *obj)
2228{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002229 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002230 if (write_unicode_binary(self, obj) < 0)
2231 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002232 }
2233 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002234 PyObject *encoded;
2235 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002236 const char unicode_op = UNICODE;
2237
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002238 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002239 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002240 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002241
Antoine Pitrou299978d2013-04-07 17:38:11 +02002242 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2243 Py_DECREF(encoded);
2244 return -1;
2245 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002246
2247 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002248 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2249 Py_DECREF(encoded);
2250 return -1;
2251 }
2252 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002254 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 }
2257 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002258 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261}
2262
2263/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2264static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002265store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002267 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002268
2269 assert(PyTuple_Size(t) == len);
2270
2271 for (i = 0; i < len; i++) {
2272 PyObject *element = PyTuple_GET_ITEM(t, i);
2273
2274 if (element == NULL)
2275 return -1;
2276 if (save(self, element, 0) < 0)
2277 return -1;
2278 }
2279
2280 return 0;
2281}
2282
2283/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2284 * used across protocols to minimize the space needed to pickle them.
2285 * Tuples are also the only builtin immutable type that can be recursive
2286 * (a tuple can be reached from itself), and that requires some subtle
2287 * magic so that it works in all cases. IOW, this is a long routine.
2288 */
2289static int
2290save_tuple(PicklerObject *self, PyObject *obj)
2291{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002292 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002293
2294 const char mark_op = MARK;
2295 const char tuple_op = TUPLE;
2296 const char pop_op = POP;
2297 const char pop_mark_op = POP_MARK;
2298 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2299
2300 if ((len = PyTuple_Size(obj)) < 0)
2301 return -1;
2302
2303 if (len == 0) {
2304 char pdata[2];
2305
2306 if (self->proto) {
2307 pdata[0] = EMPTY_TUPLE;
2308 len = 1;
2309 }
2310 else {
2311 pdata[0] = MARK;
2312 pdata[1] = TUPLE;
2313 len = 2;
2314 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002315 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002316 return -1;
2317 return 0;
2318 }
2319
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002320 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002321 * saving the tuple elements, the tuple must be recursive, in
2322 * which case we'll pop everything we put on the stack, and fetch
2323 * its value from the memo.
2324 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002325 if (len <= 3 && self->proto >= 2) {
2326 /* Use TUPLE{1,2,3} opcodes. */
2327 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002328 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002329
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002330 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002331 /* pop the len elements */
2332 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002333 if (_Pickler_Write(self, &pop_op, 1) < 0)
2334 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002335 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (memo_get(self, obj) < 0)
2337 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002338
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002339 return 0;
2340 }
2341 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002342 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2343 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002344 }
2345 goto memoize;
2346 }
2347
2348 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2349 * Generate MARK e1 e2 ... TUPLE
2350 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 if (_Pickler_Write(self, &mark_op, 1) < 0)
2352 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002353
2354 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002355 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002357 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358 /* pop the stack stuff we pushed */
2359 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002360 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2361 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002362 }
2363 else {
2364 /* Note that we pop one more than len, to remove
2365 * the MARK too.
2366 */
2367 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002368 if (_Pickler_Write(self, &pop_op, 1) < 0)
2369 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002370 }
2371 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002372 if (memo_get(self, obj) < 0)
2373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002375 return 0;
2376 }
2377 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2379 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002380 }
2381
2382 memoize:
2383 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002386 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002387}
2388
2389/* iter is an iterator giving items, and we batch up chunks of
2390 * MARK item item ... item APPENDS
2391 * opcode sequences. Calling code should have arranged to first create an
2392 * empty list, or list-like object, for the APPENDS to operate on.
2393 * Returns 0 on success, <0 on error.
2394 */
2395static int
2396batch_list(PicklerObject *self, PyObject *iter)
2397{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002398 PyObject *obj = NULL;
2399 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 int i, n;
2401
2402 const char mark_op = MARK;
2403 const char append_op = APPEND;
2404 const char appends_op = APPENDS;
2405
2406 assert(iter != NULL);
2407
2408 /* XXX: I think this function could be made faster by avoiding the
2409 iterator interface and fetching objects directly from list using
2410 PyList_GET_ITEM.
2411 */
2412
2413 if (self->proto == 0) {
2414 /* APPENDS isn't available; do one at a time. */
2415 for (;;) {
2416 obj = PyIter_Next(iter);
2417 if (obj == NULL) {
2418 if (PyErr_Occurred())
2419 return -1;
2420 break;
2421 }
2422 i = save(self, obj, 0);
2423 Py_DECREF(obj);
2424 if (i < 0)
2425 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002426 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002427 return -1;
2428 }
2429 return 0;
2430 }
2431
2432 /* proto > 0: write in batches of BATCHSIZE. */
2433 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002434 /* Get first item */
2435 firstitem = PyIter_Next(iter);
2436 if (firstitem == NULL) {
2437 if (PyErr_Occurred())
2438 goto error;
2439
2440 /* nothing more to add */
2441 break;
2442 }
2443
2444 /* Try to get a second item */
2445 obj = PyIter_Next(iter);
2446 if (obj == NULL) {
2447 if (PyErr_Occurred())
2448 goto error;
2449
2450 /* Only one item to write */
2451 if (save(self, firstitem, 0) < 0)
2452 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002453 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002454 goto error;
2455 Py_CLEAR(firstitem);
2456 break;
2457 }
2458
2459 /* More than one item to write */
2460
2461 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002462 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002463 goto error;
2464
2465 if (save(self, firstitem, 0) < 0)
2466 goto error;
2467 Py_CLEAR(firstitem);
2468 n = 1;
2469
2470 /* Fetch and save up to BATCHSIZE items */
2471 while (obj) {
2472 if (save(self, obj, 0) < 0)
2473 goto error;
2474 Py_CLEAR(obj);
2475 n += 1;
2476
2477 if (n == BATCHSIZE)
2478 break;
2479
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002480 obj = PyIter_Next(iter);
2481 if (obj == NULL) {
2482 if (PyErr_Occurred())
2483 goto error;
2484 break;
2485 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002486 }
2487
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002488 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002489 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002490
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002491 } while (n == BATCHSIZE);
2492 return 0;
2493
2494 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002495 Py_XDECREF(firstitem);
2496 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002497 return -1;
2498}
2499
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002500/* This is a variant of batch_list() above, specialized for lists (with no
2501 * support for list subclasses). Like batch_list(), we batch up chunks of
2502 * MARK item item ... item APPENDS
2503 * opcode sequences. Calling code should have arranged to first create an
2504 * empty list, or list-like object, for the APPENDS to operate on.
2505 * Returns 0 on success, -1 on error.
2506 *
2507 * This version is considerably faster than batch_list(), if less general.
2508 *
2509 * Note that this only works for protocols > 0.
2510 */
2511static int
2512batch_list_exact(PicklerObject *self, PyObject *obj)
2513{
2514 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002515 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516
2517 const char append_op = APPEND;
2518 const char appends_op = APPENDS;
2519 const char mark_op = MARK;
2520
2521 assert(obj != NULL);
2522 assert(self->proto > 0);
2523 assert(PyList_CheckExact(obj));
2524
2525 if (PyList_GET_SIZE(obj) == 1) {
2526 item = PyList_GET_ITEM(obj, 0);
2527 if (save(self, item, 0) < 0)
2528 return -1;
2529 if (_Pickler_Write(self, &append_op, 1) < 0)
2530 return -1;
2531 return 0;
2532 }
2533
2534 /* Write in batches of BATCHSIZE. */
2535 total = 0;
2536 do {
2537 this_batch = 0;
2538 if (_Pickler_Write(self, &mark_op, 1) < 0)
2539 return -1;
2540 while (total < PyList_GET_SIZE(obj)) {
2541 item = PyList_GET_ITEM(obj, total);
2542 if (save(self, item, 0) < 0)
2543 return -1;
2544 total++;
2545 if (++this_batch == BATCHSIZE)
2546 break;
2547 }
2548 if (_Pickler_Write(self, &appends_op, 1) < 0)
2549 return -1;
2550
2551 } while (total < PyList_GET_SIZE(obj));
2552
2553 return 0;
2554}
2555
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002556static int
2557save_list(PicklerObject *self, PyObject *obj)
2558{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002559 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002560 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002561 int status = 0;
2562
2563 if (self->fast && !fast_save_enter(self, obj))
2564 goto error;
2565
2566 /* Create an empty list. */
2567 if (self->bin) {
2568 header[0] = EMPTY_LIST;
2569 len = 1;
2570 }
2571 else {
2572 header[0] = MARK;
2573 header[1] = LIST;
2574 len = 2;
2575 }
2576
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002577 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002578 goto error;
2579
2580 /* Get list length, and bow out early if empty. */
2581 if ((len = PyList_Size(obj)) < 0)
2582 goto error;
2583
2584 if (memo_put(self, obj) < 0)
2585 goto error;
2586
2587 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002588 /* Materialize the list elements. */
2589 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002590 if (Py_EnterRecursiveCall(" while pickling an object"))
2591 goto error;
2592 status = batch_list_exact(self, obj);
2593 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002594 } else {
2595 PyObject *iter = PyObject_GetIter(obj);
2596 if (iter == NULL)
2597 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002598
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002599 if (Py_EnterRecursiveCall(" while pickling an object")) {
2600 Py_DECREF(iter);
2601 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002602 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002603 status = batch_list(self, iter);
2604 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002605 Py_DECREF(iter);
2606 }
2607 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002608 if (0) {
2609 error:
2610 status = -1;
2611 }
2612
2613 if (self->fast && !fast_save_leave(self, obj))
2614 status = -1;
2615
2616 return status;
2617}
2618
2619/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2620 * MARK key value ... key value SETITEMS
2621 * opcode sequences. Calling code should have arranged to first create an
2622 * empty dict, or dict-like object, for the SETITEMS to operate on.
2623 * Returns 0 on success, <0 on error.
2624 *
2625 * This is very much like batch_list(). The difference between saving
2626 * elements directly, and picking apart two-tuples, is so long-winded at
2627 * the C level, though, that attempts to combine these routines were too
2628 * ugly to bear.
2629 */
2630static int
2631batch_dict(PicklerObject *self, PyObject *iter)
2632{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002633 PyObject *obj = NULL;
2634 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002635 int i, n;
2636
2637 const char mark_op = MARK;
2638 const char setitem_op = SETITEM;
2639 const char setitems_op = SETITEMS;
2640
2641 assert(iter != NULL);
2642
2643 if (self->proto == 0) {
2644 /* SETITEMS isn't available; do one at a time. */
2645 for (;;) {
2646 obj = PyIter_Next(iter);
2647 if (obj == NULL) {
2648 if (PyErr_Occurred())
2649 return -1;
2650 break;
2651 }
2652 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2653 PyErr_SetString(PyExc_TypeError, "dict items "
2654 "iterator must return 2-tuples");
2655 return -1;
2656 }
2657 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2658 if (i >= 0)
2659 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2660 Py_DECREF(obj);
2661 if (i < 0)
2662 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002663 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002664 return -1;
2665 }
2666 return 0;
2667 }
2668
2669 /* proto > 0: write in batches of BATCHSIZE. */
2670 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002671 /* Get first item */
2672 firstitem = PyIter_Next(iter);
2673 if (firstitem == NULL) {
2674 if (PyErr_Occurred())
2675 goto error;
2676
2677 /* nothing more to add */
2678 break;
2679 }
2680 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2681 PyErr_SetString(PyExc_TypeError, "dict items "
2682 "iterator must return 2-tuples");
2683 goto error;
2684 }
2685
2686 /* Try to get a second item */
2687 obj = PyIter_Next(iter);
2688 if (obj == NULL) {
2689 if (PyErr_Occurred())
2690 goto error;
2691
2692 /* Only one item to write */
2693 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2694 goto error;
2695 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2696 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002697 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002698 goto error;
2699 Py_CLEAR(firstitem);
2700 break;
2701 }
2702
2703 /* More than one item to write */
2704
2705 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002706 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002707 goto error;
2708
2709 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2710 goto error;
2711 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2712 goto error;
2713 Py_CLEAR(firstitem);
2714 n = 1;
2715
2716 /* Fetch and save up to BATCHSIZE items */
2717 while (obj) {
2718 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2719 PyErr_SetString(PyExc_TypeError, "dict items "
2720 "iterator must return 2-tuples");
2721 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002722 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002723 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2724 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2725 goto error;
2726 Py_CLEAR(obj);
2727 n += 1;
2728
2729 if (n == BATCHSIZE)
2730 break;
2731
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002732 obj = PyIter_Next(iter);
2733 if (obj == NULL) {
2734 if (PyErr_Occurred())
2735 goto error;
2736 break;
2737 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002738 }
2739
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002740 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002741 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002742
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002743 } while (n == BATCHSIZE);
2744 return 0;
2745
2746 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002747 Py_XDECREF(firstitem);
2748 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002749 return -1;
2750}
2751
Collin Winter5c9b02d2009-05-25 05:43:30 +00002752/* This is a variant of batch_dict() above that specializes for dicts, with no
2753 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2754 * MARK key value ... key value SETITEMS
2755 * opcode sequences. Calling code should have arranged to first create an
2756 * empty dict, or dict-like object, for the SETITEMS to operate on.
2757 * Returns 0 on success, -1 on error.
2758 *
2759 * Note that this currently doesn't work for protocol 0.
2760 */
2761static int
2762batch_dict_exact(PicklerObject *self, PyObject *obj)
2763{
2764 PyObject *key = NULL, *value = NULL;
2765 int i;
2766 Py_ssize_t dict_size, ppos = 0;
2767
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002768 const char mark_op = MARK;
2769 const char setitem_op = SETITEM;
2770 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002771
2772 assert(obj != NULL);
2773 assert(self->proto > 0);
2774
2775 dict_size = PyDict_Size(obj);
2776
2777 /* Special-case len(d) == 1 to save space. */
2778 if (dict_size == 1) {
2779 PyDict_Next(obj, &ppos, &key, &value);
2780 if (save(self, key, 0) < 0)
2781 return -1;
2782 if (save(self, value, 0) < 0)
2783 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002784 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002785 return -1;
2786 return 0;
2787 }
2788
2789 /* Write in batches of BATCHSIZE. */
2790 do {
2791 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002792 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002793 return -1;
2794 while (PyDict_Next(obj, &ppos, &key, &value)) {
2795 if (save(self, key, 0) < 0)
2796 return -1;
2797 if (save(self, value, 0) < 0)
2798 return -1;
2799 if (++i == BATCHSIZE)
2800 break;
2801 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002802 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002803 return -1;
2804 if (PyDict_Size(obj) != dict_size) {
2805 PyErr_Format(
2806 PyExc_RuntimeError,
2807 "dictionary changed size during iteration");
2808 return -1;
2809 }
2810
2811 } while (i == BATCHSIZE);
2812 return 0;
2813}
2814
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002815static int
2816save_dict(PicklerObject *self, PyObject *obj)
2817{
2818 PyObject *items, *iter;
2819 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002820 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002821 int status = 0;
2822
2823 if (self->fast && !fast_save_enter(self, obj))
2824 goto error;
2825
2826 /* Create an empty dict. */
2827 if (self->bin) {
2828 header[0] = EMPTY_DICT;
2829 len = 1;
2830 }
2831 else {
2832 header[0] = MARK;
2833 header[1] = DICT;
2834 len = 2;
2835 }
2836
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002837 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002838 goto error;
2839
2840 /* Get dict size, and bow out early if empty. */
2841 if ((len = PyDict_Size(obj)) < 0)
2842 goto error;
2843
2844 if (memo_put(self, obj) < 0)
2845 goto error;
2846
2847 if (len != 0) {
2848 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002849 if (PyDict_CheckExact(obj) && self->proto > 0) {
2850 /* We can take certain shortcuts if we know this is a dict and
2851 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002852 if (Py_EnterRecursiveCall(" while pickling an object"))
2853 goto error;
2854 status = batch_dict_exact(self, obj);
2855 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002856 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002857 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002858
2859 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002860 if (items == NULL)
2861 goto error;
2862 iter = PyObject_GetIter(items);
2863 Py_DECREF(items);
2864 if (iter == NULL)
2865 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002866 if (Py_EnterRecursiveCall(" while pickling an object")) {
2867 Py_DECREF(iter);
2868 goto error;
2869 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002870 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002871 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002872 Py_DECREF(iter);
2873 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002874 }
2875
2876 if (0) {
2877 error:
2878 status = -1;
2879 }
2880
2881 if (self->fast && !fast_save_leave(self, obj))
2882 status = -1;
2883
2884 return status;
2885}
2886
2887static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002888save_set(PicklerObject *self, PyObject *obj)
2889{
2890 PyObject *item;
2891 int i;
2892 Py_ssize_t set_size, ppos = 0;
2893 Py_hash_t hash;
2894
2895 const char empty_set_op = EMPTY_SET;
2896 const char mark_op = MARK;
2897 const char additems_op = ADDITEMS;
2898
2899 if (self->proto < 4) {
2900 PyObject *items;
2901 PyObject *reduce_value;
2902 int status;
2903
2904 items = PySequence_List(obj);
2905 if (items == NULL) {
2906 return -1;
2907 }
2908 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2909 Py_DECREF(items);
2910 if (reduce_value == NULL) {
2911 return -1;
2912 }
2913 /* save_reduce() will memoize the object automatically. */
2914 status = save_reduce(self, reduce_value, obj);
2915 Py_DECREF(reduce_value);
2916 return status;
2917 }
2918
2919 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2920 return -1;
2921
2922 if (memo_put(self, obj) < 0)
2923 return -1;
2924
2925 set_size = PySet_GET_SIZE(obj);
2926 if (set_size == 0)
2927 return 0; /* nothing to do */
2928
2929 /* Write in batches of BATCHSIZE. */
2930 do {
2931 i = 0;
2932 if (_Pickler_Write(self, &mark_op, 1) < 0)
2933 return -1;
2934 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2935 if (save(self, item, 0) < 0)
2936 return -1;
2937 if (++i == BATCHSIZE)
2938 break;
2939 }
2940 if (_Pickler_Write(self, &additems_op, 1) < 0)
2941 return -1;
2942 if (PySet_GET_SIZE(obj) != set_size) {
2943 PyErr_Format(
2944 PyExc_RuntimeError,
2945 "set changed size during iteration");
2946 return -1;
2947 }
2948 } while (i == BATCHSIZE);
2949
2950 return 0;
2951}
2952
2953static int
2954save_frozenset(PicklerObject *self, PyObject *obj)
2955{
2956 PyObject *iter;
2957
2958 const char mark_op = MARK;
2959 const char frozenset_op = FROZENSET;
2960
2961 if (self->fast && !fast_save_enter(self, obj))
2962 return -1;
2963
2964 if (self->proto < 4) {
2965 PyObject *items;
2966 PyObject *reduce_value;
2967 int status;
2968
2969 items = PySequence_List(obj);
2970 if (items == NULL) {
2971 return -1;
2972 }
2973 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2974 items);
2975 Py_DECREF(items);
2976 if (reduce_value == NULL) {
2977 return -1;
2978 }
2979 /* save_reduce() will memoize the object automatically. */
2980 status = save_reduce(self, reduce_value, obj);
2981 Py_DECREF(reduce_value);
2982 return status;
2983 }
2984
2985 if (_Pickler_Write(self, &mark_op, 1) < 0)
2986 return -1;
2987
2988 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002989 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01002990 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01002991 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002992 for (;;) {
2993 PyObject *item;
2994
2995 item = PyIter_Next(iter);
2996 if (item == NULL) {
2997 if (PyErr_Occurred()) {
2998 Py_DECREF(iter);
2999 return -1;
3000 }
3001 break;
3002 }
3003 if (save(self, item, 0) < 0) {
3004 Py_DECREF(item);
3005 Py_DECREF(iter);
3006 return -1;
3007 }
3008 Py_DECREF(item);
3009 }
3010 Py_DECREF(iter);
3011
3012 /* If the object is already in the memo, this means it is
3013 recursive. In this case, throw away everything we put on the
3014 stack, and fetch the object back from the memo. */
3015 if (PyMemoTable_Get(self->memo, obj)) {
3016 const char pop_mark_op = POP_MARK;
3017
3018 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3019 return -1;
3020 if (memo_get(self, obj) < 0)
3021 return -1;
3022 return 0;
3023 }
3024
3025 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3026 return -1;
3027 if (memo_put(self, obj) < 0)
3028 return -1;
3029
3030 return 0;
3031}
3032
3033static int
3034fix_imports(PyObject **module_name, PyObject **global_name)
3035{
3036 PyObject *key;
3037 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003038 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003039
3040 key = PyTuple_Pack(2, *module_name, *global_name);
3041 if (key == NULL)
3042 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003043 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003044 Py_DECREF(key);
3045 if (item) {
3046 PyObject *fixed_module_name;
3047 PyObject *fixed_global_name;
3048
3049 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3050 PyErr_Format(PyExc_RuntimeError,
3051 "_compat_pickle.REVERSE_NAME_MAPPING values "
3052 "should be 2-tuples, not %.200s",
3053 Py_TYPE(item)->tp_name);
3054 return -1;
3055 }
3056 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3057 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3058 if (!PyUnicode_Check(fixed_module_name) ||
3059 !PyUnicode_Check(fixed_global_name)) {
3060 PyErr_Format(PyExc_RuntimeError,
3061 "_compat_pickle.REVERSE_NAME_MAPPING values "
3062 "should be pairs of str, not (%.200s, %.200s)",
3063 Py_TYPE(fixed_module_name)->tp_name,
3064 Py_TYPE(fixed_global_name)->tp_name);
3065 return -1;
3066 }
3067
3068 Py_CLEAR(*module_name);
3069 Py_CLEAR(*global_name);
3070 Py_INCREF(fixed_module_name);
3071 Py_INCREF(fixed_global_name);
3072 *module_name = fixed_module_name;
3073 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003074 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003075 }
3076 else if (PyErr_Occurred()) {
3077 return -1;
3078 }
3079
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003080 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003081 if (item) {
3082 if (!PyUnicode_Check(item)) {
3083 PyErr_Format(PyExc_RuntimeError,
3084 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3085 "should be strings, not %.200s",
3086 Py_TYPE(item)->tp_name);
3087 return -1;
3088 }
3089 Py_CLEAR(*module_name);
3090 Py_INCREF(item);
3091 *module_name = item;
3092 }
3093 else if (PyErr_Occurred()) {
3094 return -1;
3095 }
3096
3097 return 0;
3098}
3099
3100static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003101save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3102{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003103 PyObject *global_name = NULL;
3104 PyObject *module_name = NULL;
3105 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003106 PyObject *parent = NULL;
3107 PyObject *dotted_path = NULL;
3108 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003109 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003110 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003111 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003112 _Py_IDENTIFIER(__name__);
3113 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003114
3115 const char global_op = GLOBAL;
3116
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003117 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003118 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003120 }
3121 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003122 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3123 if (global_name == NULL) {
3124 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3125 goto error;
3126 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003127 }
3128 if (global_name == NULL) {
3129 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3130 if (global_name == NULL)
3131 goto error;
3132 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 }
3134
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003135 dotted_path = get_dotted_path(module, global_name);
3136 if (dotted_path == NULL)
3137 goto error;
3138 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 if (module_name == NULL)
3140 goto error;
3141
3142 /* XXX: Change to use the import C API directly with level=0 to disallow
3143 relative imports.
3144
3145 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3146 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3147 custom import functions (IMHO, this would be a nice security
3148 feature). The import C API would need to be extended to support the
3149 extra parameters of __import__ to fix that. */
3150 module = PyImport_Import(module_name);
3151 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003152 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003153 "Can't pickle %R: import of module %R failed",
3154 obj, module_name);
3155 goto error;
3156 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003157 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3158 Py_INCREF(lastname);
3159 cls = get_deep_attribute(module, dotted_path, &parent);
3160 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003161 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003162 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003163 "Can't pickle %R: attribute lookup %S on %S failed",
3164 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003165 goto error;
3166 }
3167 if (cls != obj) {
3168 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003169 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003170 "Can't pickle %R: it's not the same object as %S.%S",
3171 obj, module_name, global_name);
3172 goto error;
3173 }
3174 Py_DECREF(cls);
3175
3176 if (self->proto >= 2) {
3177 /* See whether this is in the extension registry, and if
3178 * so generate an EXT opcode.
3179 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003180 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 PyObject *code_obj; /* extension code as Python object */
3182 long code; /* extension code as C value */
3183 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003184 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003185
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003186 extension_key = PyTuple_Pack(2, module_name, global_name);
3187 if (extension_key == NULL) {
3188 goto error;
3189 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003190 code_obj = PyDict_GetItemWithError(st->extension_registry,
3191 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003192 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193 /* The object is not registered in the extension registry.
3194 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003195 if (code_obj == NULL) {
3196 if (PyErr_Occurred()) {
3197 goto error;
3198 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003199 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003200 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201
3202 /* XXX: pickle.py doesn't check neither the type, nor the range
3203 of the value returned by the extension_registry. It should for
3204 consistency. */
3205
3206 /* Verify code_obj has the right type and value. */
3207 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003208 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003209 "Can't pickle %R: extension code %R isn't an integer",
3210 obj, code_obj);
3211 goto error;
3212 }
3213 code = PyLong_AS_LONG(code_obj);
3214 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003215 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003216 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3217 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003218 goto error;
3219 }
3220
3221 /* Generate an EXT opcode. */
3222 if (code <= 0xff) {
3223 pdata[0] = EXT1;
3224 pdata[1] = (unsigned char)code;
3225 n = 2;
3226 }
3227 else if (code <= 0xffff) {
3228 pdata[0] = EXT2;
3229 pdata[1] = (unsigned char)(code & 0xff);
3230 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3231 n = 3;
3232 }
3233 else {
3234 pdata[0] = EXT4;
3235 pdata[1] = (unsigned char)(code & 0xff);
3236 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3237 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3238 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3239 n = 5;
3240 }
3241
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003242 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003243 goto error;
3244 }
3245 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003246 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003247 if (parent == module) {
3248 Py_INCREF(lastname);
3249 Py_DECREF(global_name);
3250 global_name = lastname;
3251 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003252 if (self->proto >= 4) {
3253 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003254
Christian Heimese8b1ba12013-11-23 21:13:39 +01003255 if (save(self, module_name, 0) < 0)
3256 goto error;
3257 if (save(self, global_name, 0) < 0)
3258 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003259
3260 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3261 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003262 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003263 else if (parent != module) {
3264 PickleState *st = _Pickle_GetGlobalState();
3265 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3266 st->getattr, parent, lastname);
3267 status = save_reduce(self, reduce_value, NULL);
3268 Py_DECREF(reduce_value);
3269 if (status < 0)
3270 goto error;
3271 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003272 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273 /* Generate a normal global opcode if we are using a pickle
3274 protocol < 4, or if the object is not registered in the
3275 extension registry. */
3276 PyObject *encoded;
3277 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003280 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003281
3282 /* For protocol < 3 and if the user didn't request against doing
3283 so, we convert module names to the old 2.x module names. */
3284 if (self->proto < 3 && self->fix_imports) {
3285 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003286 goto error;
3287 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003288 }
3289
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003290 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3291 both the module name and the global name using UTF-8. We do so
3292 only when we are using the pickle protocol newer than version
3293 3. This is to ensure compatibility with older Unpickler running
3294 on Python 2.x. */
3295 if (self->proto == 3) {
3296 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003297 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003298 else {
3299 unicode_encoder = PyUnicode_AsASCIIString;
3300 }
3301 encoded = unicode_encoder(module_name);
3302 if (encoded == NULL) {
3303 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003304 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003305 "can't pickle module identifier '%S' using "
3306 "pickle protocol %i",
3307 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003308 goto error;
3309 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003310 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3311 PyBytes_GET_SIZE(encoded)) < 0) {
3312 Py_DECREF(encoded);
3313 goto error;
3314 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003315 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003316 if(_Pickler_Write(self, "\n", 1) < 0)
3317 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003318
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 /* Save the name of the module. */
3320 encoded = unicode_encoder(global_name);
3321 if (encoded == NULL) {
3322 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003323 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003324 "can't pickle global identifier '%S' using "
3325 "pickle protocol %i",
3326 global_name, self->proto);
3327 goto error;
3328 }
3329 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3330 PyBytes_GET_SIZE(encoded)) < 0) {
3331 Py_DECREF(encoded);
3332 goto error;
3333 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003334 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003335 if (_Pickler_Write(self, "\n", 1) < 0)
3336 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003337 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338 /* Memoize the object. */
3339 if (memo_put(self, obj) < 0)
3340 goto error;
3341 }
3342
3343 if (0) {
3344 error:
3345 status = -1;
3346 }
3347 Py_XDECREF(module_name);
3348 Py_XDECREF(global_name);
3349 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003350 Py_XDECREF(parent);
3351 Py_XDECREF(dotted_path);
3352 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353
3354 return status;
3355}
3356
3357static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003358save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3359{
3360 PyObject *reduce_value;
3361 int status;
3362
3363 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3364 if (reduce_value == NULL) {
3365 return -1;
3366 }
3367 status = save_reduce(self, reduce_value, obj);
3368 Py_DECREF(reduce_value);
3369 return status;
3370}
3371
3372static int
3373save_type(PicklerObject *self, PyObject *obj)
3374{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003375 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003376 return save_singleton_type(self, obj, Py_None);
3377 }
3378 else if (obj == (PyObject *)&PyEllipsis_Type) {
3379 return save_singleton_type(self, obj, Py_Ellipsis);
3380 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003381 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003382 return save_singleton_type(self, obj, Py_NotImplemented);
3383 }
3384 return save_global(self, obj, NULL);
3385}
3386
3387static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003388save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3389{
3390 PyObject *pid = NULL;
3391 int status = 0;
3392
3393 const char persid_op = PERSID;
3394 const char binpersid_op = BINPERSID;
3395
3396 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003397 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003398 if (pid == NULL)
3399 return -1;
3400
3401 if (pid != Py_None) {
3402 if (self->bin) {
3403 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003404 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003405 goto error;
3406 }
3407 else {
3408 PyObject *pid_str = NULL;
3409 char *pid_ascii_bytes;
3410 Py_ssize_t size;
3411
3412 pid_str = PyObject_Str(pid);
3413 if (pid_str == NULL)
3414 goto error;
3415
3416 /* XXX: Should it check whether the persistent id only contains
3417 ASCII characters? And what if the pid contains embedded
3418 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003419 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003420 Py_DECREF(pid_str);
3421 if (pid_ascii_bytes == NULL)
3422 goto error;
3423
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003424 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3425 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3426 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003427 goto error;
3428 }
3429 status = 1;
3430 }
3431
3432 if (0) {
3433 error:
3434 status = -1;
3435 }
3436 Py_XDECREF(pid);
3437
3438 return status;
3439}
3440
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003441static PyObject *
3442get_class(PyObject *obj)
3443{
3444 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003445 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003446
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003447 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003448 if (cls == NULL) {
3449 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3450 PyErr_Clear();
3451 cls = (PyObject *) Py_TYPE(obj);
3452 Py_INCREF(cls);
3453 }
3454 }
3455 return cls;
3456}
3457
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003458/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3459 * appropriate __reduce__ method for obj.
3460 */
3461static int
3462save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3463{
3464 PyObject *callable;
3465 PyObject *argtup;
3466 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003467 PyObject *listitems = Py_None;
3468 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003469 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003470 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003471 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003472
3473 const char reduce_op = REDUCE;
3474 const char build_op = BUILD;
3475 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003476 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003477
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003478 size = PyTuple_Size(args);
3479 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003480 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003481 "__reduce__ must contain 2 through 5 elements");
3482 return -1;
3483 }
3484
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003485 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3486 &callable, &argtup, &state, &listitems, &dictitems))
3487 return -1;
3488
3489 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003490 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003491 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003492 return -1;
3493 }
3494 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003495 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003496 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003497 return -1;
3498 }
3499
3500 if (state == Py_None)
3501 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003502
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003503 if (listitems == Py_None)
3504 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003505 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003506 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003507 "returned by __reduce__ must be an iterator, not %s",
3508 Py_TYPE(listitems)->tp_name);
3509 return -1;
3510 }
3511
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003512 if (dictitems == Py_None)
3513 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003514 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003515 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003516 "returned by __reduce__ must be an iterator, not %s",
3517 Py_TYPE(dictitems)->tp_name);
3518 return -1;
3519 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003520
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003521 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003522 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003523 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524
Victor Stinner804e05e2013-11-14 01:26:17 +01003525 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003526 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003527 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003529 }
3530 PyErr_Clear();
3531 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003532 else if (PyUnicode_Check(name)) {
3533 if (self->proto >= 4) {
3534 _Py_IDENTIFIER(__newobj_ex__);
3535 use_newobj_ex = PyUnicode_Compare(
3536 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
3537 }
3538 if (!use_newobj_ex) {
3539 _Py_IDENTIFIER(__newobj__);
3540 use_newobj = PyUnicode_Compare(
3541 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3542 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003543 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003544 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003546
3547 if (use_newobj_ex) {
3548 PyObject *cls;
3549 PyObject *args;
3550 PyObject *kwargs;
3551
3552 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003553 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003554 "length of the NEWOBJ_EX argument tuple must be "
3555 "exactly 3, not %zd", Py_SIZE(argtup));
3556 return -1;
3557 }
3558
3559 cls = PyTuple_GET_ITEM(argtup, 0);
3560 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003561 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003562 "first item from NEWOBJ_EX argument tuple must "
3563 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3564 return -1;
3565 }
3566 args = PyTuple_GET_ITEM(argtup, 1);
3567 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003568 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003569 "second item from NEWOBJ_EX argument tuple must "
3570 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3571 return -1;
3572 }
3573 kwargs = PyTuple_GET_ITEM(argtup, 2);
3574 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003575 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003576 "third item from NEWOBJ_EX argument tuple must "
3577 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3578 return -1;
3579 }
3580
3581 if (save(self, cls, 0) < 0 ||
3582 save(self, args, 0) < 0 ||
3583 save(self, kwargs, 0) < 0 ||
3584 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3585 return -1;
3586 }
3587 }
3588 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003589 PyObject *cls;
3590 PyObject *newargtup;
3591 PyObject *obj_class;
3592 int p;
3593
3594 /* Sanity checks. */
3595 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003596 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003597 return -1;
3598 }
3599
3600 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003601 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003602 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003603 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003604 return -1;
3605 }
3606
3607 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003608 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003609 p = obj_class != cls; /* true iff a problem */
3610 Py_DECREF(obj_class);
3611 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003612 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003613 "__newobj__ args has the wrong class");
3614 return -1;
3615 }
3616 }
3617 /* XXX: These calls save() are prone to infinite recursion. Imagine
3618 what happen if the value returned by the __reduce__() method of
3619 some extension type contains another object of the same type. Ouch!
3620
3621 Here is a quick example, that I ran into, to illustrate what I
3622 mean:
3623
3624 >>> import pickle, copyreg
3625 >>> copyreg.dispatch_table.pop(complex)
3626 >>> pickle.dumps(1+2j)
3627 Traceback (most recent call last):
3628 ...
3629 RuntimeError: maximum recursion depth exceeded
3630
3631 Removing the complex class from copyreg.dispatch_table made the
3632 __reduce_ex__() method emit another complex object:
3633
3634 >>> (1+1j).__reduce_ex__(2)
3635 (<function __newobj__ at 0xb7b71c3c>,
3636 (<class 'complex'>, (1+1j)), None, None, None)
3637
3638 Thus when save() was called on newargstup (the 2nd item) recursion
3639 ensued. Of course, the bug was in the complex class which had a
3640 broken __getnewargs__() that emitted another complex object. But,
3641 the point, here, is it is quite easy to end up with a broken reduce
3642 function. */
3643
3644 /* Save the class and its __new__ arguments. */
3645 if (save(self, cls, 0) < 0)
3646 return -1;
3647
3648 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3649 if (newargtup == NULL)
3650 return -1;
3651
3652 p = save(self, newargtup, 0);
3653 Py_DECREF(newargtup);
3654 if (p < 0)
3655 return -1;
3656
3657 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003658 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003659 return -1;
3660 }
3661 else { /* Not using NEWOBJ. */
3662 if (save(self, callable, 0) < 0 ||
3663 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003664 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665 return -1;
3666 }
3667
3668 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3669 the caller do not want to memoize the object. Not particularly useful,
3670 but that is to mimic the behavior save_reduce() in pickle.py when
3671 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003672 if (obj != NULL) {
3673 /* If the object is already in the memo, this means it is
3674 recursive. In this case, throw away everything we put on the
3675 stack, and fetch the object back from the memo. */
3676 if (PyMemoTable_Get(self->memo, obj)) {
3677 const char pop_op = POP;
3678
3679 if (_Pickler_Write(self, &pop_op, 1) < 0)
3680 return -1;
3681 if (memo_get(self, obj) < 0)
3682 return -1;
3683
3684 return 0;
3685 }
3686 else if (memo_put(self, obj) < 0)
3687 return -1;
3688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003689
3690 if (listitems && batch_list(self, listitems) < 0)
3691 return -1;
3692
3693 if (dictitems && batch_dict(self, dictitems) < 0)
3694 return -1;
3695
3696 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003697 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003698 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003699 return -1;
3700 }
3701
3702 return 0;
3703}
3704
3705static int
3706save(PicklerObject *self, PyObject *obj, int pers_save)
3707{
3708 PyTypeObject *type;
3709 PyObject *reduce_func = NULL;
3710 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003711 int status = 0;
3712
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003713 if (_Pickler_OpcodeBoundary(self) < 0)
3714 return -1;
3715
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003716 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003717 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003718
3719 /* The extra pers_save argument is necessary to avoid calling save_pers()
3720 on its returned object. */
3721 if (!pers_save && self->pers_func) {
3722 /* save_pers() returns:
3723 -1 to signal an error;
3724 0 if it did nothing successfully;
3725 1 if a persistent id was saved.
3726 */
3727 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3728 goto done;
3729 }
3730
3731 type = Py_TYPE(obj);
3732
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003733 /* The old cPickle had an optimization that used switch-case statement
3734 dispatching on the first letter of the type name. This has was removed
3735 since benchmarks shown that this optimization was actually slowing
3736 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003737
3738 /* Atom types; these aren't memoized, so don't check the memo. */
3739
3740 if (obj == Py_None) {
3741 status = save_none(self, obj);
3742 goto done;
3743 }
3744 else if (obj == Py_False || obj == Py_True) {
3745 status = save_bool(self, obj);
3746 goto done;
3747 }
3748 else if (type == &PyLong_Type) {
3749 status = save_long(self, obj);
3750 goto done;
3751 }
3752 else if (type == &PyFloat_Type) {
3753 status = save_float(self, obj);
3754 goto done;
3755 }
3756
3757 /* Check the memo to see if it has the object. If so, generate
3758 a GET (or BINGET) opcode, instead of pickling the object
3759 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003760 if (PyMemoTable_Get(self->memo, obj)) {
3761 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762 goto error;
3763 goto done;
3764 }
3765
3766 if (type == &PyBytes_Type) {
3767 status = save_bytes(self, obj);
3768 goto done;
3769 }
3770 else if (type == &PyUnicode_Type) {
3771 status = save_unicode(self, obj);
3772 goto done;
3773 }
3774 else if (type == &PyDict_Type) {
3775 status = save_dict(self, obj);
3776 goto done;
3777 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003778 else if (type == &PySet_Type) {
3779 status = save_set(self, obj);
3780 goto done;
3781 }
3782 else if (type == &PyFrozenSet_Type) {
3783 status = save_frozenset(self, obj);
3784 goto done;
3785 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003786 else if (type == &PyList_Type) {
3787 status = save_list(self, obj);
3788 goto done;
3789 }
3790 else if (type == &PyTuple_Type) {
3791 status = save_tuple(self, obj);
3792 goto done;
3793 }
3794 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003795 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003796 goto done;
3797 }
3798 else if (type == &PyFunction_Type) {
3799 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003800 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003801 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802
3803 /* XXX: This part needs some unit tests. */
3804
3805 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003806 * self.dispatch_table, copyreg.dispatch_table, the object's
3807 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003808 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003809 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003810 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003811 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3812 (PyObject *)type);
3813 if (reduce_func == NULL) {
3814 if (PyErr_Occurred()) {
3815 goto error;
3816 }
3817 } else {
3818 /* PyDict_GetItemWithError() returns a borrowed reference.
3819 Increase the reference count to be consistent with
3820 PyObject_GetItem and _PyObject_GetAttrId used below. */
3821 Py_INCREF(reduce_func);
3822 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003823 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003824 reduce_func = PyObject_GetItem(self->dispatch_table,
3825 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003826 if (reduce_func == NULL) {
3827 if (PyErr_ExceptionMatches(PyExc_KeyError))
3828 PyErr_Clear();
3829 else
3830 goto error;
3831 }
3832 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003833 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003834 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003835 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003836 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003837 else if (PyType_IsSubtype(type, &PyType_Type)) {
3838 status = save_global(self, obj, NULL);
3839 goto done;
3840 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003841 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003842 _Py_IDENTIFIER(__reduce__);
3843 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003844
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003845
3846 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3847 automatically defined as __reduce__. While this is convenient, this
3848 make it impossible to know which method was actually called. Of
3849 course, this is not a big deal. But still, it would be nice to let
3850 the user know which method was called when something go
3851 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3852 don't actually have to check for a __reduce__ method. */
3853
3854 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003855 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003856 if (reduce_func != NULL) {
3857 PyObject *proto;
3858 proto = PyLong_FromLong(self->proto);
3859 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003860 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861 }
3862 }
3863 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003864 PickleState *st = _Pickle_GetGlobalState();
3865
3866 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003868 }
3869 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003870 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003871 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003872 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003873 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003875 PyObject *empty_tuple = PyTuple_New(0);
3876 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003877 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003878 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003879 }
3880 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003881 PyErr_Format(st->PicklingError,
3882 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003883 type->tp_name, obj);
3884 goto error;
3885 }
3886 }
3887 }
3888
3889 if (reduce_value == NULL)
3890 goto error;
3891
3892 if (PyUnicode_Check(reduce_value)) {
3893 status = save_global(self, obj, reduce_value);
3894 goto done;
3895 }
3896
3897 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003898 PickleState *st = _Pickle_GetGlobalState();
3899 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 "__reduce__ must return a string or tuple");
3901 goto error;
3902 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003903
3904 status = save_reduce(self, reduce_value, obj);
3905
3906 if (0) {
3907 error:
3908 status = -1;
3909 }
3910 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003911
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003912 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003913 Py_XDECREF(reduce_func);
3914 Py_XDECREF(reduce_value);
3915
3916 return status;
3917}
3918
3919static int
3920dump(PicklerObject *self, PyObject *obj)
3921{
3922 const char stop_op = STOP;
3923
3924 if (self->proto >= 2) {
3925 char header[2];
3926
3927 header[0] = PROTO;
3928 assert(self->proto >= 0 && self->proto < 256);
3929 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003930 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003932 if (self->proto >= 4)
3933 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 }
3935
3936 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003937 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 return -1;
3939
3940 return 0;
3941}
3942
Larry Hastings61272b72014-01-07 12:41:53 -08003943/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003944
3945_pickle.Pickler.clear_memo
3946
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003947Clears the pickler's "memo".
3948
3949The memo is the data structure that remembers which objects the
3950pickler has already seen, so that shared or recursive objects are
3951pickled by reference and not by value. This method is useful when
3952re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08003953[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003954
Larry Hastings3cceb382014-01-04 11:09:09 -08003955static PyObject *
3956_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08003957/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003958{
3959 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003960 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961
3962 Py_RETURN_NONE;
3963}
3964
Larry Hastings61272b72014-01-07 12:41:53 -08003965/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003966
3967_pickle.Pickler.dump
3968
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003969 obj: object
3970 /
3971
3972Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08003973[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003974
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003975static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08003976_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08003977/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003979 /* Check whether the Pickler was initialized correctly (issue3664).
3980 Developers often forget to call __init__() in their subclasses, which
3981 would trigger a segfault without this check. */
3982 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003983 PickleState *st = _Pickle_GetGlobalState();
3984 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00003985 "Pickler.__init__() was not called by %s.__init__()",
3986 Py_TYPE(self)->tp_name);
3987 return NULL;
3988 }
3989
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003990 if (_Pickler_ClearBuffer(self) < 0)
3991 return NULL;
3992
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003993 if (dump(self, obj) < 0)
3994 return NULL;
3995
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003996 if (_Pickler_FlushToFile(self) < 0)
3997 return NULL;
3998
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 Py_RETURN_NONE;
4000}
4001
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004002/*[clinic input]
4003
4004_pickle.Pickler.__sizeof__ -> Py_ssize_t
4005
4006Returns size in memory, in bytes.
4007[clinic start generated code]*/
4008
4009static Py_ssize_t
4010_pickle_Pickler___sizeof___impl(PicklerObject *self)
4011/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4012{
4013 Py_ssize_t res, s;
4014
4015 res = sizeof(PicklerObject);
4016 if (self->memo != NULL) {
4017 res += sizeof(PyMemoTable);
4018 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4019 }
4020 if (self->output_buffer != NULL) {
4021 s = _PySys_GetSizeOf(self->output_buffer);
4022 if (s == -1)
4023 return -1;
4024 res += s;
4025 }
4026 return res;
4027}
4028
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004029static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030 _PICKLE_PICKLER_DUMP_METHODDEF
4031 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004032 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004033 {NULL, NULL} /* sentinel */
4034};
4035
4036static void
4037Pickler_dealloc(PicklerObject *self)
4038{
4039 PyObject_GC_UnTrack(self);
4040
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004041 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004042 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004044 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004045 Py_XDECREF(self->fast_memo);
4046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004047 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004048
4049 Py_TYPE(self)->tp_free((PyObject *)self);
4050}
4051
4052static int
4053Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4054{
4055 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004056 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004057 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058 Py_VISIT(self->fast_memo);
4059 return 0;
4060}
4061
4062static int
4063Pickler_clear(PicklerObject *self)
4064{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004065 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004067 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004068 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004069 Py_CLEAR(self->fast_memo);
4070
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004071 if (self->memo != NULL) {
4072 PyMemoTable *memo = self->memo;
4073 self->memo = NULL;
4074 PyMemoTable_Del(memo);
4075 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004076 return 0;
4077}
4078
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004079
Larry Hastings61272b72014-01-07 12:41:53 -08004080/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004081
4082_pickle.Pickler.__init__
4083
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004084 file: object
4085 protocol: object = NULL
4086 fix_imports: bool = True
4087
4088This takes a binary file for writing a pickle data stream.
4089
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004090The optional *protocol* argument tells the pickler to use the given
4091protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4092protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004093
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004094Specifying a negative protocol version selects the highest protocol
4095version supported. The higher the protocol used, the more recent the
4096version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004097
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004098The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004099bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004100writing, a io.BytesIO instance, or any other custom object that meets
4101this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004103If *fix_imports* is True and protocol is less than 3, pickle will try
4104to map the new Python 3 names to the old module names used in Python
41052, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004106[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004107
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004108static int
Larry Hastings89964c42015-04-14 18:07:59 -04004109_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4110 PyObject *protocol, int fix_imports)
4111/*[clinic end generated code: output=b5f31078dab17fb0 input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004112{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004113 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004114 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004115
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 /* In case of multiple __init__() calls, clear previous content. */
4117 if (self->write != NULL)
4118 (void)Pickler_clear(self);
4119
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004120 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004121 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004122
4123 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004124 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004125
4126 /* memo and output_buffer may have already been created in _Pickler_New */
4127 if (self->memo == NULL) {
4128 self->memo = PyMemoTable_New();
4129 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004130 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131 }
4132 self->output_len = 0;
4133 if (self->output_buffer == NULL) {
4134 self->max_output_len = WRITE_BUF_SIZE;
4135 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4136 self->max_output_len);
4137 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004138 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004139 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004141 self->fast = 0;
4142 self->fast_nesting = 0;
4143 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004144 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004145 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4146 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4147 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004148 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004149 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004150 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004151 self->dispatch_table = NULL;
4152 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4153 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4154 &PyId_dispatch_table);
4155 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004156 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004157 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004158
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004159 return 0;
4160}
4161
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004162
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004163/* Define a proxy object for the Pickler's internal memo object. This is to
4164 * avoid breaking code like:
4165 * pickler.memo.clear()
4166 * and
4167 * pickler.memo = saved_memo
4168 * Is this a good idea? Not really, but we don't want to break code that uses
4169 * it. Note that we don't implement the entire mapping API here. This is
4170 * intentional, as these should be treated as black-box implementation details.
4171 */
4172
Larry Hastings61272b72014-01-07 12:41:53 -08004173/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004174_pickle.PicklerMemoProxy.clear
4175
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004176Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004177[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004178
Larry Hastings3cceb382014-01-04 11:09:09 -08004179static PyObject *
4180_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004181/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004182{
4183 if (self->pickler->memo)
4184 PyMemoTable_Clear(self->pickler->memo);
4185 Py_RETURN_NONE;
4186}
4187
Larry Hastings61272b72014-01-07 12:41:53 -08004188/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004189_pickle.PicklerMemoProxy.copy
4190
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004191Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004192[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193
Larry Hastings3cceb382014-01-04 11:09:09 -08004194static PyObject *
4195_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004196/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004197{
4198 Py_ssize_t i;
4199 PyMemoTable *memo;
4200 PyObject *new_memo = PyDict_New();
4201 if (new_memo == NULL)
4202 return NULL;
4203
4204 memo = self->pickler->memo;
4205 for (i = 0; i < memo->mt_allocated; ++i) {
4206 PyMemoEntry entry = memo->mt_table[i];
4207 if (entry.me_key != NULL) {
4208 int status;
4209 PyObject *key, *value;
4210
4211 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004212 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004213
4214 if (key == NULL || value == NULL) {
4215 Py_XDECREF(key);
4216 Py_XDECREF(value);
4217 goto error;
4218 }
4219 status = PyDict_SetItem(new_memo, key, value);
4220 Py_DECREF(key);
4221 Py_DECREF(value);
4222 if (status < 0)
4223 goto error;
4224 }
4225 }
4226 return new_memo;
4227
4228 error:
4229 Py_XDECREF(new_memo);
4230 return NULL;
4231}
4232
Larry Hastings61272b72014-01-07 12:41:53 -08004233/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004234_pickle.PicklerMemoProxy.__reduce__
4235
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004236Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004237[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004238
Larry Hastings3cceb382014-01-04 11:09:09 -08004239static PyObject *
4240_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004241/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004242{
4243 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004244 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004245 if (contents == NULL)
4246 return NULL;
4247
4248 reduce_value = PyTuple_New(2);
4249 if (reduce_value == NULL) {
4250 Py_DECREF(contents);
4251 return NULL;
4252 }
4253 dict_args = PyTuple_New(1);
4254 if (dict_args == NULL) {
4255 Py_DECREF(contents);
4256 Py_DECREF(reduce_value);
4257 return NULL;
4258 }
4259 PyTuple_SET_ITEM(dict_args, 0, contents);
4260 Py_INCREF((PyObject *)&PyDict_Type);
4261 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4262 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4263 return reduce_value;
4264}
4265
4266static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004267 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4268 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4269 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270 {NULL, NULL} /* sentinel */
4271};
4272
4273static void
4274PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4275{
4276 PyObject_GC_UnTrack(self);
4277 Py_XDECREF(self->pickler);
4278 PyObject_GC_Del((PyObject *)self);
4279}
4280
4281static int
4282PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4283 visitproc visit, void *arg)
4284{
4285 Py_VISIT(self->pickler);
4286 return 0;
4287}
4288
4289static int
4290PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4291{
4292 Py_CLEAR(self->pickler);
4293 return 0;
4294}
4295
4296static PyTypeObject PicklerMemoProxyType = {
4297 PyVarObject_HEAD_INIT(NULL, 0)
4298 "_pickle.PicklerMemoProxy", /*tp_name*/
4299 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4300 0,
4301 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4302 0, /* tp_print */
4303 0, /* tp_getattr */
4304 0, /* tp_setattr */
4305 0, /* tp_compare */
4306 0, /* tp_repr */
4307 0, /* tp_as_number */
4308 0, /* tp_as_sequence */
4309 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004310 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004311 0, /* tp_call */
4312 0, /* tp_str */
4313 PyObject_GenericGetAttr, /* tp_getattro */
4314 PyObject_GenericSetAttr, /* tp_setattro */
4315 0, /* tp_as_buffer */
4316 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4317 0, /* tp_doc */
4318 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4319 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4320 0, /* tp_richcompare */
4321 0, /* tp_weaklistoffset */
4322 0, /* tp_iter */
4323 0, /* tp_iternext */
4324 picklerproxy_methods, /* tp_methods */
4325};
4326
4327static PyObject *
4328PicklerMemoProxy_New(PicklerObject *pickler)
4329{
4330 PicklerMemoProxyObject *self;
4331
4332 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4333 if (self == NULL)
4334 return NULL;
4335 Py_INCREF(pickler);
4336 self->pickler = pickler;
4337 PyObject_GC_Track(self);
4338 return (PyObject *)self;
4339}
4340
4341/*****************************************************************************/
4342
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004343static PyObject *
4344Pickler_get_memo(PicklerObject *self)
4345{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004346 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004347}
4348
4349static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004350Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004351{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004352 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004353
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004354 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004355 PyErr_SetString(PyExc_TypeError,
4356 "attribute deletion is not supported");
4357 return -1;
4358 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004359
4360 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4361 PicklerObject *pickler =
4362 ((PicklerMemoProxyObject *)obj)->pickler;
4363
4364 new_memo = PyMemoTable_Copy(pickler->memo);
4365 if (new_memo == NULL)
4366 return -1;
4367 }
4368 else if (PyDict_Check(obj)) {
4369 Py_ssize_t i = 0;
4370 PyObject *key, *value;
4371
4372 new_memo = PyMemoTable_New();
4373 if (new_memo == NULL)
4374 return -1;
4375
4376 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004377 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004378 PyObject *memo_obj;
4379
4380 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4381 PyErr_SetString(PyExc_TypeError,
4382 "'memo' values must be 2-item tuples");
4383 goto error;
4384 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004385 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004386 if (memo_id == -1 && PyErr_Occurred())
4387 goto error;
4388 memo_obj = PyTuple_GET_ITEM(value, 1);
4389 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4390 goto error;
4391 }
4392 }
4393 else {
4394 PyErr_Format(PyExc_TypeError,
4395 "'memo' attribute must be an PicklerMemoProxy object"
4396 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004397 return -1;
4398 }
4399
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004400 PyMemoTable_Del(self->memo);
4401 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004402
4403 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004404
4405 error:
4406 if (new_memo)
4407 PyMemoTable_Del(new_memo);
4408 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409}
4410
4411static PyObject *
4412Pickler_get_persid(PicklerObject *self)
4413{
4414 if (self->pers_func == NULL)
4415 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4416 else
4417 Py_INCREF(self->pers_func);
4418 return self->pers_func;
4419}
4420
4421static int
4422Pickler_set_persid(PicklerObject *self, PyObject *value)
4423{
4424 PyObject *tmp;
4425
4426 if (value == NULL) {
4427 PyErr_SetString(PyExc_TypeError,
4428 "attribute deletion is not supported");
4429 return -1;
4430 }
4431 if (!PyCallable_Check(value)) {
4432 PyErr_SetString(PyExc_TypeError,
4433 "persistent_id must be a callable taking one argument");
4434 return -1;
4435 }
4436
4437 tmp = self->pers_func;
4438 Py_INCREF(value);
4439 self->pers_func = value;
4440 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4441
4442 return 0;
4443}
4444
4445static PyMemberDef Pickler_members[] = {
4446 {"bin", T_INT, offsetof(PicklerObject, bin)},
4447 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004448 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004449 {NULL}
4450};
4451
4452static PyGetSetDef Pickler_getsets[] = {
4453 {"memo", (getter)Pickler_get_memo,
4454 (setter)Pickler_set_memo},
4455 {"persistent_id", (getter)Pickler_get_persid,
4456 (setter)Pickler_set_persid},
4457 {NULL}
4458};
4459
4460static PyTypeObject Pickler_Type = {
4461 PyVarObject_HEAD_INIT(NULL, 0)
4462 "_pickle.Pickler" , /*tp_name*/
4463 sizeof(PicklerObject), /*tp_basicsize*/
4464 0, /*tp_itemsize*/
4465 (destructor)Pickler_dealloc, /*tp_dealloc*/
4466 0, /*tp_print*/
4467 0, /*tp_getattr*/
4468 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004469 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 0, /*tp_repr*/
4471 0, /*tp_as_number*/
4472 0, /*tp_as_sequence*/
4473 0, /*tp_as_mapping*/
4474 0, /*tp_hash*/
4475 0, /*tp_call*/
4476 0, /*tp_str*/
4477 0, /*tp_getattro*/
4478 0, /*tp_setattro*/
4479 0, /*tp_as_buffer*/
4480 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004481 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004482 (traverseproc)Pickler_traverse, /*tp_traverse*/
4483 (inquiry)Pickler_clear, /*tp_clear*/
4484 0, /*tp_richcompare*/
4485 0, /*tp_weaklistoffset*/
4486 0, /*tp_iter*/
4487 0, /*tp_iternext*/
4488 Pickler_methods, /*tp_methods*/
4489 Pickler_members, /*tp_members*/
4490 Pickler_getsets, /*tp_getset*/
4491 0, /*tp_base*/
4492 0, /*tp_dict*/
4493 0, /*tp_descr_get*/
4494 0, /*tp_descr_set*/
4495 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004496 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497 PyType_GenericAlloc, /*tp_alloc*/
4498 PyType_GenericNew, /*tp_new*/
4499 PyObject_GC_Del, /*tp_free*/
4500 0, /*tp_is_gc*/
4501};
4502
Victor Stinner121aab42011-09-29 23:40:53 +02004503/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004504
4505 XXX: It would be nice to able to avoid Python function call overhead, by
4506 using directly the C version of find_class(), when find_class() is not
4507 overridden by a subclass. Although, this could become rather hackish. A
4508 simpler optimization would be to call the C function when self is not a
4509 subclass instance. */
4510static PyObject *
4511find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4512{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004513 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004514
4515 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4516 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004517}
4518
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004519static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004520marker(UnpicklerObject *self)
4521{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004522 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004523 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004524 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004525 return -1;
4526 }
4527
4528 return self->marks[--self->num_marks];
4529}
4530
4531static int
4532load_none(UnpicklerObject *self)
4533{
4534 PDATA_APPEND(self->stack, Py_None, -1);
4535 return 0;
4536}
4537
4538static int
4539bad_readline(void)
4540{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004541 PickleState *st = _Pickle_GetGlobalState();
4542 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004543 return -1;
4544}
4545
4546static int
4547load_int(UnpicklerObject *self)
4548{
4549 PyObject *value;
4550 char *endptr, *s;
4551 Py_ssize_t len;
4552 long x;
4553
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004554 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004555 return -1;
4556 if (len < 2)
4557 return bad_readline();
4558
4559 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004560 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004561 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004562 x = strtol(s, &endptr, 0);
4563
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004564 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004565 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004566 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004567 errno = 0;
4568 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004569 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570 if (value == NULL) {
4571 PyErr_SetString(PyExc_ValueError,
4572 "could not convert string to int");
4573 return -1;
4574 }
4575 }
4576 else {
4577 if (len == 3 && (x == 0 || x == 1)) {
4578 if ((value = PyBool_FromLong(x)) == NULL)
4579 return -1;
4580 }
4581 else {
4582 if ((value = PyLong_FromLong(x)) == NULL)
4583 return -1;
4584 }
4585 }
4586
4587 PDATA_PUSH(self->stack, value, -1);
4588 return 0;
4589}
4590
4591static int
4592load_bool(UnpicklerObject *self, PyObject *boolean)
4593{
4594 assert(boolean == Py_True || boolean == Py_False);
4595 PDATA_APPEND(self->stack, boolean, -1);
4596 return 0;
4597}
4598
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004599/* s contains x bytes of an unsigned little-endian integer. Return its value
4600 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4601 */
4602static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004603calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004604{
4605 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004606 Py_ssize_t i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004607 size_t x = 0;
4608
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004609 for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004610 x |= (size_t) s[i] << (8 * i);
4611 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004612
4613 if (x > PY_SSIZE_T_MAX)
4614 return -1;
4615 else
4616 return (Py_ssize_t) x;
4617}
4618
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004619/* s contains x bytes of a little-endian integer. Return its value as a
4620 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4621 * int, but when x is 4 it's a signed one. This is an historical source
4622 * of x-platform bugs.
4623 */
4624static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004625calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004626{
4627 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004628 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004629 long x = 0;
4630
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004631 for (i = 0; i < nbytes; i++) {
4632 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633 }
4634
4635 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4636 * is signed, so on a box with longs bigger than 4 bytes we need
4637 * to extend a BININT's sign bit to the full width.
4638 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004639 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 x |= -(x & (1L << 31));
4641 }
4642
4643 return x;
4644}
4645
4646static int
4647load_binintx(UnpicklerObject *self, char *s, int size)
4648{
4649 PyObject *value;
4650 long x;
4651
4652 x = calc_binint(s, size);
4653
4654 if ((value = PyLong_FromLong(x)) == NULL)
4655 return -1;
4656
4657 PDATA_PUSH(self->stack, value, -1);
4658 return 0;
4659}
4660
4661static int
4662load_binint(UnpicklerObject *self)
4663{
4664 char *s;
4665
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004666 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004667 return -1;
4668
4669 return load_binintx(self, s, 4);
4670}
4671
4672static int
4673load_binint1(UnpicklerObject *self)
4674{
4675 char *s;
4676
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004677 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004678 return -1;
4679
4680 return load_binintx(self, s, 1);
4681}
4682
4683static int
4684load_binint2(UnpicklerObject *self)
4685{
4686 char *s;
4687
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004688 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004689 return -1;
4690
4691 return load_binintx(self, s, 2);
4692}
4693
4694static int
4695load_long(UnpicklerObject *self)
4696{
4697 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004698 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004699 Py_ssize_t len;
4700
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004701 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004702 return -1;
4703 if (len < 2)
4704 return bad_readline();
4705
Mark Dickinson8dd05142009-01-20 20:43:58 +00004706 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4707 the 'L' before calling PyLong_FromString. In order to maintain
4708 compatibility with Python 3.0.0, we don't actually *require*
4709 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004710 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004711 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004712 /* XXX: Should the base argument explicitly set to 10? */
4713 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004714 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715 return -1;
4716
4717 PDATA_PUSH(self->stack, value, -1);
4718 return 0;
4719}
4720
4721/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4722 * data following.
4723 */
4724static int
4725load_counted_long(UnpicklerObject *self, int size)
4726{
4727 PyObject *value;
4728 char *nbytes;
4729 char *pdata;
4730
4731 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004732 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004733 return -1;
4734
4735 size = calc_binint(nbytes, size);
4736 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004737 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004739 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004740 "LONG pickle has negative byte count");
4741 return -1;
4742 }
4743
4744 if (size == 0)
4745 value = PyLong_FromLong(0L);
4746 else {
4747 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004748 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004749 return -1;
4750 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4751 1 /* little endian */ , 1 /* signed */ );
4752 }
4753 if (value == NULL)
4754 return -1;
4755 PDATA_PUSH(self->stack, value, -1);
4756 return 0;
4757}
4758
4759static int
4760load_float(UnpicklerObject *self)
4761{
4762 PyObject *value;
4763 char *endptr, *s;
4764 Py_ssize_t len;
4765 double d;
4766
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004767 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004768 return -1;
4769 if (len < 2)
4770 return bad_readline();
4771
4772 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004773 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4774 if (d == -1.0 && PyErr_Occurred())
4775 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004776 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4778 return -1;
4779 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004780 value = PyFloat_FromDouble(d);
4781 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004782 return -1;
4783
4784 PDATA_PUSH(self->stack, value, -1);
4785 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004786}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004787
4788static int
4789load_binfloat(UnpicklerObject *self)
4790{
4791 PyObject *value;
4792 double x;
4793 char *s;
4794
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004795 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004796 return -1;
4797
4798 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4799 if (x == -1.0 && PyErr_Occurred())
4800 return -1;
4801
4802 if ((value = PyFloat_FromDouble(x)) == NULL)
4803 return -1;
4804
4805 PDATA_PUSH(self->stack, value, -1);
4806 return 0;
4807}
4808
4809static int
4810load_string(UnpicklerObject *self)
4811{
4812 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004813 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 Py_ssize_t len;
4815 char *s, *p;
4816
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004817 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004819 /* Strip the newline */
4820 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004821 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004822 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 p = s + 1;
4824 len -= 2;
4825 }
4826 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004827 PickleState *st = _Pickle_GetGlobalState();
4828 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004829 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004830 return -1;
4831 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004832 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004833
4834 /* Use the PyBytes API to decode the string, since that is what is used
4835 to encode, and then coerce the result to Unicode. */
4836 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004837 if (bytes == NULL)
4838 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004839
4840 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4841 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4842 if (strcmp(self->encoding, "bytes") == 0) {
4843 obj = bytes;
4844 }
4845 else {
4846 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4847 Py_DECREF(bytes);
4848 if (obj == NULL) {
4849 return -1;
4850 }
4851 }
4852
4853 PDATA_PUSH(self->stack, obj, -1);
4854 return 0;
4855}
4856
4857static int
4858load_counted_binstring(UnpicklerObject *self, int nbytes)
4859{
4860 PyObject *obj;
4861 Py_ssize_t size;
4862 char *s;
4863
4864 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004865 return -1;
4866
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004867 size = calc_binsize(s, nbytes);
4868 if (size < 0) {
4869 PickleState *st = _Pickle_GetGlobalState();
4870 PyErr_Format(st->UnpicklingError,
4871 "BINSTRING exceeds system's maximum size of %zd bytes",
4872 PY_SSIZE_T_MAX);
4873 return -1;
4874 }
4875
4876 if (_Unpickler_Read(self, &s, size) < 0)
4877 return -1;
4878
4879 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4880 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4881 if (strcmp(self->encoding, "bytes") == 0) {
4882 obj = PyBytes_FromStringAndSize(s, size);
4883 }
4884 else {
4885 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4886 }
4887 if (obj == NULL) {
4888 return -1;
4889 }
4890
4891 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892 return 0;
4893}
4894
4895static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004896load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897{
4898 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004899 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004900 char *s;
4901
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004902 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 return -1;
4904
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004905 size = calc_binsize(s, nbytes);
4906 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004907 PyErr_Format(PyExc_OverflowError,
4908 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004909 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 return -1;
4911 }
4912
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004913 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004914 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004915
4916 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004917 if (bytes == NULL)
4918 return -1;
4919
4920 PDATA_PUSH(self->stack, bytes, -1);
4921 return 0;
4922}
4923
4924static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004925load_unicode(UnpicklerObject *self)
4926{
4927 PyObject *str;
4928 Py_ssize_t len;
4929 char *s;
4930
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004931 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004932 return -1;
4933 if (len < 1)
4934 return bad_readline();
4935
4936 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
4937 if (str == NULL)
4938 return -1;
4939
4940 PDATA_PUSH(self->stack, str, -1);
4941 return 0;
4942}
4943
4944static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004945load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004946{
4947 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004948 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004949 char *s;
4950
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004951 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004952 return -1;
4953
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004954 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004955 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004956 PyErr_Format(PyExc_OverflowError,
4957 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004958 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004959 return -1;
4960 }
4961
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004962 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963 return -1;
4964
Victor Stinner485fb562010-04-13 11:07:24 +00004965 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004966 if (str == NULL)
4967 return -1;
4968
4969 PDATA_PUSH(self->stack, str, -1);
4970 return 0;
4971}
4972
4973static int
4974load_tuple(UnpicklerObject *self)
4975{
4976 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004977 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978
4979 if ((i = marker(self)) < 0)
4980 return -1;
4981
4982 tuple = Pdata_poptuple(self->stack, i);
4983 if (tuple == NULL)
4984 return -1;
4985 PDATA_PUSH(self->stack, tuple, -1);
4986 return 0;
4987}
4988
4989static int
4990load_counted_tuple(UnpicklerObject *self, int len)
4991{
4992 PyObject *tuple;
4993
4994 tuple = PyTuple_New(len);
4995 if (tuple == NULL)
4996 return -1;
4997
4998 while (--len >= 0) {
4999 PyObject *item;
5000
5001 PDATA_POP(self->stack, item);
5002 if (item == NULL)
5003 return -1;
5004 PyTuple_SET_ITEM(tuple, len, item);
5005 }
5006 PDATA_PUSH(self->stack, tuple, -1);
5007 return 0;
5008}
5009
5010static int
5011load_empty_list(UnpicklerObject *self)
5012{
5013 PyObject *list;
5014
5015 if ((list = PyList_New(0)) == NULL)
5016 return -1;
5017 PDATA_PUSH(self->stack, list, -1);
5018 return 0;
5019}
5020
5021static int
5022load_empty_dict(UnpicklerObject *self)
5023{
5024 PyObject *dict;
5025
5026 if ((dict = PyDict_New()) == NULL)
5027 return -1;
5028 PDATA_PUSH(self->stack, dict, -1);
5029 return 0;
5030}
5031
5032static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005033load_empty_set(UnpicklerObject *self)
5034{
5035 PyObject *set;
5036
5037 if ((set = PySet_New(NULL)) == NULL)
5038 return -1;
5039 PDATA_PUSH(self->stack, set, -1);
5040 return 0;
5041}
5042
5043static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044load_list(UnpicklerObject *self)
5045{
5046 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005047 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048
5049 if ((i = marker(self)) < 0)
5050 return -1;
5051
5052 list = Pdata_poplist(self->stack, i);
5053 if (list == NULL)
5054 return -1;
5055 PDATA_PUSH(self->stack, list, -1);
5056 return 0;
5057}
5058
5059static int
5060load_dict(UnpicklerObject *self)
5061{
5062 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005063 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064
5065 if ((i = marker(self)) < 0)
5066 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005067 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005068
5069 if ((dict = PyDict_New()) == NULL)
5070 return -1;
5071
5072 for (k = i + 1; k < j; k += 2) {
5073 key = self->stack->data[k - 1];
5074 value = self->stack->data[k];
5075 if (PyDict_SetItem(dict, key, value) < 0) {
5076 Py_DECREF(dict);
5077 return -1;
5078 }
5079 }
5080 Pdata_clear(self->stack, i);
5081 PDATA_PUSH(self->stack, dict, -1);
5082 return 0;
5083}
5084
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005085static int
5086load_frozenset(UnpicklerObject *self)
5087{
5088 PyObject *items;
5089 PyObject *frozenset;
5090 Py_ssize_t i;
5091
5092 if ((i = marker(self)) < 0)
5093 return -1;
5094
5095 items = Pdata_poptuple(self->stack, i);
5096 if (items == NULL)
5097 return -1;
5098
5099 frozenset = PyFrozenSet_New(items);
5100 Py_DECREF(items);
5101 if (frozenset == NULL)
5102 return -1;
5103
5104 PDATA_PUSH(self->stack, frozenset, -1);
5105 return 0;
5106}
5107
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005108static PyObject *
5109instantiate(PyObject *cls, PyObject *args)
5110{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005111 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005112 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005113 /* Caller must assure args are a tuple. Normally, args come from
5114 Pdata_poptuple which packs objects from the top of the stack
5115 into a newly created tuple. */
5116 assert(PyTuple_Check(args));
5117 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005118 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005119 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005121 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005122 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005123
5124 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005125 }
5126 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005127}
5128
5129static int
5130load_obj(UnpicklerObject *self)
5131{
5132 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005133 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005134
5135 if ((i = marker(self)) < 0)
5136 return -1;
5137
5138 args = Pdata_poptuple(self->stack, i + 1);
5139 if (args == NULL)
5140 return -1;
5141
5142 PDATA_POP(self->stack, cls);
5143 if (cls) {
5144 obj = instantiate(cls, args);
5145 Py_DECREF(cls);
5146 }
5147 Py_DECREF(args);
5148 if (obj == NULL)
5149 return -1;
5150
5151 PDATA_PUSH(self->stack, obj, -1);
5152 return 0;
5153}
5154
5155static int
5156load_inst(UnpicklerObject *self)
5157{
5158 PyObject *cls = NULL;
5159 PyObject *args = NULL;
5160 PyObject *obj = NULL;
5161 PyObject *module_name;
5162 PyObject *class_name;
5163 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005164 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005165 char *s;
5166
5167 if ((i = marker(self)) < 0)
5168 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005169 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005170 return -1;
5171 if (len < 2)
5172 return bad_readline();
5173
5174 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5175 identifiers are permitted in Python 3.0, since the INST opcode is only
5176 supported by older protocols on Python 2.x. */
5177 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5178 if (module_name == NULL)
5179 return -1;
5180
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005181 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005182 if (len < 2)
5183 return bad_readline();
5184 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005185 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005186 cls = find_class(self, module_name, class_name);
5187 Py_DECREF(class_name);
5188 }
5189 }
5190 Py_DECREF(module_name);
5191
5192 if (cls == NULL)
5193 return -1;
5194
5195 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5196 obj = instantiate(cls, args);
5197 Py_DECREF(args);
5198 }
5199 Py_DECREF(cls);
5200
5201 if (obj == NULL)
5202 return -1;
5203
5204 PDATA_PUSH(self->stack, obj, -1);
5205 return 0;
5206}
5207
5208static int
5209load_newobj(UnpicklerObject *self)
5210{
5211 PyObject *args = NULL;
5212 PyObject *clsraw = NULL;
5213 PyTypeObject *cls; /* clsraw cast to its true type */
5214 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005215 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005216
5217 /* Stack is ... cls argtuple, and we want to call
5218 * cls.__new__(cls, *argtuple).
5219 */
5220 PDATA_POP(self->stack, args);
5221 if (args == NULL)
5222 goto error;
5223 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005224 PyErr_SetString(st->UnpicklingError,
5225 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005226 goto error;
5227 }
5228
5229 PDATA_POP(self->stack, clsraw);
5230 cls = (PyTypeObject *)clsraw;
5231 if (cls == NULL)
5232 goto error;
5233 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005234 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005235 "isn't a type object");
5236 goto error;
5237 }
5238 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005239 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005240 "has NULL tp_new");
5241 goto error;
5242 }
5243
5244 /* Call __new__. */
5245 obj = cls->tp_new(cls, args, NULL);
5246 if (obj == NULL)
5247 goto error;
5248
5249 Py_DECREF(args);
5250 Py_DECREF(clsraw);
5251 PDATA_PUSH(self->stack, obj, -1);
5252 return 0;
5253
5254 error:
5255 Py_XDECREF(args);
5256 Py_XDECREF(clsraw);
5257 return -1;
5258}
5259
5260static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005261load_newobj_ex(UnpicklerObject *self)
5262{
5263 PyObject *cls, *args, *kwargs;
5264 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005265 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005266
5267 PDATA_POP(self->stack, kwargs);
5268 if (kwargs == NULL) {
5269 return -1;
5270 }
5271 PDATA_POP(self->stack, args);
5272 if (args == NULL) {
5273 Py_DECREF(kwargs);
5274 return -1;
5275 }
5276 PDATA_POP(self->stack, cls);
5277 if (cls == NULL) {
5278 Py_DECREF(kwargs);
5279 Py_DECREF(args);
5280 return -1;
5281 }
Larry Hastings61272b72014-01-07 12:41:53 -08005282
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005283 if (!PyType_Check(cls)) {
5284 Py_DECREF(kwargs);
5285 Py_DECREF(args);
5286 Py_DECREF(cls);
Larry Hastings61272b72014-01-07 12:41:53 -08005287 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005288 "NEWOBJ_EX class argument must be a type, not %.200s",
5289 Py_TYPE(cls)->tp_name);
5290 return -1;
5291 }
5292
5293 if (((PyTypeObject *)cls)->tp_new == NULL) {
5294 Py_DECREF(kwargs);
5295 Py_DECREF(args);
5296 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005297 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005298 "NEWOBJ_EX class argument doesn't have __new__");
5299 return -1;
5300 }
5301 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5302 Py_DECREF(kwargs);
5303 Py_DECREF(args);
5304 Py_DECREF(cls);
5305 if (obj == NULL) {
5306 return -1;
5307 }
5308 PDATA_PUSH(self->stack, obj, -1);
5309 return 0;
5310}
5311
5312static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005313load_global(UnpicklerObject *self)
5314{
5315 PyObject *global = NULL;
5316 PyObject *module_name;
5317 PyObject *global_name;
5318 Py_ssize_t len;
5319 char *s;
5320
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005321 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005322 return -1;
5323 if (len < 2)
5324 return bad_readline();
5325 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5326 if (!module_name)
5327 return -1;
5328
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005329 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005330 if (len < 2) {
5331 Py_DECREF(module_name);
5332 return bad_readline();
5333 }
5334 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5335 if (global_name) {
5336 global = find_class(self, module_name, global_name);
5337 Py_DECREF(global_name);
5338 }
5339 }
5340 Py_DECREF(module_name);
5341
5342 if (global == NULL)
5343 return -1;
5344 PDATA_PUSH(self->stack, global, -1);
5345 return 0;
5346}
5347
5348static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005349load_stack_global(UnpicklerObject *self)
5350{
5351 PyObject *global;
5352 PyObject *module_name;
5353 PyObject *global_name;
5354
5355 PDATA_POP(self->stack, global_name);
5356 PDATA_POP(self->stack, module_name);
5357 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5358 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005359 PickleState *st = _Pickle_GetGlobalState();
5360 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005361 Py_XDECREF(global_name);
5362 Py_XDECREF(module_name);
5363 return -1;
5364 }
5365 global = find_class(self, module_name, global_name);
5366 Py_DECREF(global_name);
5367 Py_DECREF(module_name);
5368 if (global == NULL)
5369 return -1;
5370 PDATA_PUSH(self->stack, global, -1);
5371 return 0;
5372}
5373
5374static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005375load_persid(UnpicklerObject *self)
5376{
5377 PyObject *pid;
5378 Py_ssize_t len;
5379 char *s;
5380
5381 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005382 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005383 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005384 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005385 return bad_readline();
5386
5387 pid = PyBytes_FromStringAndSize(s, len - 1);
5388 if (pid == NULL)
5389 return -1;
5390
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005391 /* This does not leak since _Pickle_FastCall() steals the reference
5392 to pid first. */
5393 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005394 if (pid == NULL)
5395 return -1;
5396
5397 PDATA_PUSH(self->stack, pid, -1);
5398 return 0;
5399 }
5400 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005401 PickleState *st = _Pickle_GetGlobalState();
5402 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403 "A load persistent id instruction was encountered,\n"
5404 "but no persistent_load function was specified.");
5405 return -1;
5406 }
5407}
5408
5409static int
5410load_binpersid(UnpicklerObject *self)
5411{
5412 PyObject *pid;
5413
5414 if (self->pers_func) {
5415 PDATA_POP(self->stack, pid);
5416 if (pid == NULL)
5417 return -1;
5418
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005419 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005420 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005421 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005422 if (pid == NULL)
5423 return -1;
5424
5425 PDATA_PUSH(self->stack, pid, -1);
5426 return 0;
5427 }
5428 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005429 PickleState *st = _Pickle_GetGlobalState();
5430 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005431 "A load persistent id instruction was encountered,\n"
5432 "but no persistent_load function was specified.");
5433 return -1;
5434 }
5435}
5436
5437static int
5438load_pop(UnpicklerObject *self)
5439{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005440 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005441
5442 /* Note that we split the (pickle.py) stack into two stacks,
5443 * an object stack and a mark stack. We have to be clever and
5444 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005445 * mark stack first, and only signalling a stack underflow if
5446 * the object stack is empty and the mark stack doesn't match
5447 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005448 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005449 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005450 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005451 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005452 len--;
5453 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005454 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005455 } else {
5456 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005457 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005458 return 0;
5459}
5460
5461static int
5462load_pop_mark(UnpicklerObject *self)
5463{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005464 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465
5466 if ((i = marker(self)) < 0)
5467 return -1;
5468
5469 Pdata_clear(self->stack, i);
5470
5471 return 0;
5472}
5473
5474static int
5475load_dup(UnpicklerObject *self)
5476{
5477 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005478 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005479
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005480 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005481 return stack_underflow();
5482 last = self->stack->data[len - 1];
5483 PDATA_APPEND(self->stack, last, -1);
5484 return 0;
5485}
5486
5487static int
5488load_get(UnpicklerObject *self)
5489{
5490 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005491 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005492 Py_ssize_t len;
5493 char *s;
5494
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005495 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496 return -1;
5497 if (len < 2)
5498 return bad_readline();
5499
5500 key = PyLong_FromString(s, NULL, 10);
5501 if (key == NULL)
5502 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005503 idx = PyLong_AsSsize_t(key);
5504 if (idx == -1 && PyErr_Occurred()) {
5505 Py_DECREF(key);
5506 return -1;
5507 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005509 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005510 if (value == NULL) {
5511 if (!PyErr_Occurred())
5512 PyErr_SetObject(PyExc_KeyError, key);
5513 Py_DECREF(key);
5514 return -1;
5515 }
5516 Py_DECREF(key);
5517
5518 PDATA_APPEND(self->stack, value, -1);
5519 return 0;
5520}
5521
5522static int
5523load_binget(UnpicklerObject *self)
5524{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005525 PyObject *value;
5526 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005527 char *s;
5528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005529 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005530 return -1;
5531
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005532 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005534 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005535 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005536 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005537 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005538 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005539 Py_DECREF(key);
5540 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541 return -1;
5542 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543
5544 PDATA_APPEND(self->stack, value, -1);
5545 return 0;
5546}
5547
5548static int
5549load_long_binget(UnpicklerObject *self)
5550{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 PyObject *value;
5552 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005553 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005555 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 return -1;
5557
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005558 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005559
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005560 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005561 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005562 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005563 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005564 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005565 Py_DECREF(key);
5566 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005567 return -1;
5568 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005569
5570 PDATA_APPEND(self->stack, value, -1);
5571 return 0;
5572}
5573
5574/* Push an object from the extension registry (EXT[124]). nbytes is
5575 * the number of bytes following the opcode, holding the index (code) value.
5576 */
5577static int
5578load_extension(UnpicklerObject *self, int nbytes)
5579{
5580 char *codebytes; /* the nbytes bytes after the opcode */
5581 long code; /* calc_binint returns long */
5582 PyObject *py_code; /* code as a Python int */
5583 PyObject *obj; /* the object to push */
5584 PyObject *pair; /* (module_name, class_name) */
5585 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005586 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005587
5588 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005589 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005590 return -1;
5591 code = calc_binint(codebytes, nbytes);
5592 if (code <= 0) { /* note that 0 is forbidden */
5593 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005594 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005595 return -1;
5596 }
5597
5598 /* Look for the code in the cache. */
5599 py_code = PyLong_FromLong(code);
5600 if (py_code == NULL)
5601 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005602 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603 if (obj != NULL) {
5604 /* Bingo. */
5605 Py_DECREF(py_code);
5606 PDATA_APPEND(self->stack, obj, -1);
5607 return 0;
5608 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005609 if (PyErr_Occurred()) {
5610 Py_DECREF(py_code);
5611 return -1;
5612 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613
5614 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005615 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 if (pair == NULL) {
5617 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005618 if (!PyErr_Occurred()) {
5619 PyErr_Format(PyExc_ValueError, "unregistered extension "
5620 "code %ld", code);
5621 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622 return -1;
5623 }
5624 /* Since the extension registry is manipulable via Python code,
5625 * confirm that pair is really a 2-tuple of strings.
5626 */
5627 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5628 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5629 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5630 Py_DECREF(py_code);
5631 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5632 "isn't a 2-tuple of strings", code);
5633 return -1;
5634 }
5635 /* Load the object. */
5636 obj = find_class(self, module_name, class_name);
5637 if (obj == NULL) {
5638 Py_DECREF(py_code);
5639 return -1;
5640 }
5641 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005642 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643 Py_DECREF(py_code);
5644 if (code < 0) {
5645 Py_DECREF(obj);
5646 return -1;
5647 }
5648 PDATA_PUSH(self->stack, obj, -1);
5649 return 0;
5650}
5651
5652static int
5653load_put(UnpicklerObject *self)
5654{
5655 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005656 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 Py_ssize_t len;
5658 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005659
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005660 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return -1;
5662 if (len < 2)
5663 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005664 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005666 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005667
5668 key = PyLong_FromString(s, NULL, 10);
5669 if (key == NULL)
5670 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005671 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005672 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005673 if (idx < 0) {
5674 if (!PyErr_Occurred())
5675 PyErr_SetString(PyExc_ValueError,
5676 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005677 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005678 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005679
5680 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005681}
5682
5683static int
5684load_binput(UnpicklerObject *self)
5685{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005686 PyObject *value;
5687 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005690 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005692
5693 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005694 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005695 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005697 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005699 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005700}
5701
5702static int
5703load_long_binput(UnpicklerObject *self)
5704{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005705 PyObject *value;
5706 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005707 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005708
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005709 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005710 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005711
5712 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005713 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005714 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005716 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005717 if (idx < 0) {
5718 PyErr_SetString(PyExc_ValueError,
5719 "negative LONG_BINPUT argument");
5720 return -1;
5721 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005722
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005723 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005724}
5725
5726static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005727load_memoize(UnpicklerObject *self)
5728{
5729 PyObject *value;
5730
5731 if (Py_SIZE(self->stack) <= 0)
5732 return stack_underflow();
5733 value = self->stack->data[Py_SIZE(self->stack) - 1];
5734
5735 return _Unpickler_MemoPut(self, self->memo_len, value);
5736}
5737
5738static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005739do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740{
5741 PyObject *value;
5742 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005743 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005744
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 if (x > len || x <= 0)
5747 return stack_underflow();
5748 if (len == x) /* nothing to do */
5749 return 0;
5750
5751 list = self->stack->data[x - 1];
5752
5753 if (PyList_Check(list)) {
5754 PyObject *slice;
5755 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005756 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757
5758 slice = Pdata_poplist(self->stack, x);
5759 if (!slice)
5760 return -1;
5761 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005762 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005764 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 }
5766 else {
5767 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005768 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005770 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771 if (append_func == NULL)
5772 return -1;
5773 for (i = x; i < len; i++) {
5774 PyObject *result;
5775
5776 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005777 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 if (result == NULL) {
5779 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005781 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782 return -1;
5783 }
5784 Py_DECREF(result);
5785 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005787 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788 }
5789
5790 return 0;
5791}
5792
5793static int
5794load_append(UnpicklerObject *self)
5795{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005796 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797}
5798
5799static int
5800load_appends(UnpicklerObject *self)
5801{
5802 return do_append(self, marker(self));
5803}
5804
5805static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005806do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005807{
5808 PyObject *value, *key;
5809 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005810 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005811 int status = 0;
5812
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005813 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005814 if (x > len || x <= 0)
5815 return stack_underflow();
5816 if (len == x) /* nothing to do */
5817 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005818 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005819 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005821 PyErr_SetString(st->UnpicklingError,
5822 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005823 return -1;
5824 }
5825
5826 /* Here, dict does not actually need to be a PyDict; it could be anything
5827 that supports the __setitem__ attribute. */
5828 dict = self->stack->data[x - 1];
5829
5830 for (i = x + 1; i < len; i += 2) {
5831 key = self->stack->data[i - 1];
5832 value = self->stack->data[i];
5833 if (PyObject_SetItem(dict, key, value) < 0) {
5834 status = -1;
5835 break;
5836 }
5837 }
5838
5839 Pdata_clear(self->stack, x);
5840 return status;
5841}
5842
5843static int
5844load_setitem(UnpicklerObject *self)
5845{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005846 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005847}
5848
5849static int
5850load_setitems(UnpicklerObject *self)
5851{
5852 return do_setitems(self, marker(self));
5853}
5854
5855static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005856load_additems(UnpicklerObject *self)
5857{
5858 PyObject *set;
5859 Py_ssize_t mark, len, i;
5860
5861 mark = marker(self);
5862 len = Py_SIZE(self->stack);
5863 if (mark > len || mark <= 0)
5864 return stack_underflow();
5865 if (len == mark) /* nothing to do */
5866 return 0;
5867
5868 set = self->stack->data[mark - 1];
5869
5870 if (PySet_Check(set)) {
5871 PyObject *items;
5872 int status;
5873
5874 items = Pdata_poptuple(self->stack, mark);
5875 if (items == NULL)
5876 return -1;
5877
5878 status = _PySet_Update(set, items);
5879 Py_DECREF(items);
5880 return status;
5881 }
5882 else {
5883 PyObject *add_func;
5884 _Py_IDENTIFIER(add);
5885
5886 add_func = _PyObject_GetAttrId(set, &PyId_add);
5887 if (add_func == NULL)
5888 return -1;
5889 for (i = mark; i < len; i++) {
5890 PyObject *result;
5891 PyObject *item;
5892
5893 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005894 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005895 if (result == NULL) {
5896 Pdata_clear(self->stack, i + 1);
5897 Py_SIZE(self->stack) = mark;
5898 return -1;
5899 }
5900 Py_DECREF(result);
5901 }
5902 Py_SIZE(self->stack) = mark;
5903 }
5904
5905 return 0;
5906}
5907
5908static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005909load_build(UnpicklerObject *self)
5910{
5911 PyObject *state, *inst, *slotstate;
5912 PyObject *setstate;
5913 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005914 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005915
5916 /* Stack is ... instance, state. We want to leave instance at
5917 * the stack top, possibly mutated via instance.__setstate__(state).
5918 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005919 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005920 return stack_underflow();
5921
5922 PDATA_POP(self->stack, state);
5923 if (state == NULL)
5924 return -1;
5925
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005926 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005927
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005928 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005929 if (setstate == NULL) {
5930 if (PyErr_ExceptionMatches(PyExc_AttributeError))
5931 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00005932 else {
5933 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00005934 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00005935 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005936 }
5937 else {
5938 PyObject *result;
5939
5940 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005941 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005942 Py_DECREF(setstate);
5943 if (result == NULL)
5944 return -1;
5945 Py_DECREF(result);
5946 return 0;
5947 }
5948
5949 /* A default __setstate__. First see whether state embeds a
5950 * slot state dict too (a proto 2 addition).
5951 */
5952 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
5953 PyObject *tmp = state;
5954
5955 state = PyTuple_GET_ITEM(tmp, 0);
5956 slotstate = PyTuple_GET_ITEM(tmp, 1);
5957 Py_INCREF(state);
5958 Py_INCREF(slotstate);
5959 Py_DECREF(tmp);
5960 }
5961 else
5962 slotstate = NULL;
5963
5964 /* Set inst.__dict__ from the state dict (if any). */
5965 if (state != Py_None) {
5966 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005967 PyObject *d_key, *d_value;
5968 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005969 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005970
5971 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005972 PickleState *st = _Pickle_GetGlobalState();
5973 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005974 goto error;
5975 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005976 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005977 if (dict == NULL)
5978 goto error;
5979
Antoine Pitroua9f48a02009-05-02 21:41:14 +00005980 i = 0;
5981 while (PyDict_Next(state, &i, &d_key, &d_value)) {
5982 /* normally the keys for instance attributes are
5983 interned. we should try to do that here. */
5984 Py_INCREF(d_key);
5985 if (PyUnicode_CheckExact(d_key))
5986 PyUnicode_InternInPlace(&d_key);
5987 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
5988 Py_DECREF(d_key);
5989 goto error;
5990 }
5991 Py_DECREF(d_key);
5992 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005993 Py_DECREF(dict);
5994 }
5995
5996 /* Also set instance attributes from the slotstate dict (if any). */
5997 if (slotstate != NULL) {
5998 PyObject *d_key, *d_value;
5999 Py_ssize_t i;
6000
6001 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006002 PickleState *st = _Pickle_GetGlobalState();
6003 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006004 "slot state is not a dictionary");
6005 goto error;
6006 }
6007 i = 0;
6008 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6009 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6010 goto error;
6011 }
6012 }
6013
6014 if (0) {
6015 error:
6016 status = -1;
6017 }
6018
6019 Py_DECREF(state);
6020 Py_XDECREF(slotstate);
6021 return status;
6022}
6023
6024static int
6025load_mark(UnpicklerObject *self)
6026{
6027
6028 /* Note that we split the (pickle.py) stack into two stacks, an
6029 * object stack and a mark stack. Here we push a mark onto the
6030 * mark stack.
6031 */
6032
6033 if ((self->num_marks + 1) >= self->marks_size) {
6034 size_t alloc;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006035 Py_ssize_t *marks;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006036
6037 /* Use the size_t type to check for overflow. */
6038 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006039 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006040 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041 PyErr_NoMemory();
6042 return -1;
6043 }
6044
6045 if (self->marks == NULL)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006046 marks = (Py_ssize_t *) PyMem_Malloc(alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006047 else
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006048 marks = (Py_ssize_t *) PyMem_Realloc(self->marks,
6049 alloc * sizeof(Py_ssize_t));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006050 if (marks == NULL) {
6051 PyErr_NoMemory();
6052 return -1;
6053 }
6054 self->marks = marks;
6055 self->marks_size = (Py_ssize_t)alloc;
6056 }
6057
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006058 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006059
6060 return 0;
6061}
6062
6063static int
6064load_reduce(UnpicklerObject *self)
6065{
6066 PyObject *callable = NULL;
6067 PyObject *argtup = NULL;
6068 PyObject *obj = NULL;
6069
6070 PDATA_POP(self->stack, argtup);
6071 if (argtup == NULL)
6072 return -1;
6073 PDATA_POP(self->stack, callable);
6074 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006075 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076 Py_DECREF(callable);
6077 }
6078 Py_DECREF(argtup);
6079
6080 if (obj == NULL)
6081 return -1;
6082
6083 PDATA_PUSH(self->stack, obj, -1);
6084 return 0;
6085}
6086
6087/* Just raises an error if we don't know the protocol specified. PROTO
6088 * is the first opcode for protocols >= 2.
6089 */
6090static int
6091load_proto(UnpicklerObject *self)
6092{
6093 char *s;
6094 int i;
6095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006096 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006097 return -1;
6098
6099 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006100 if (i <= HIGHEST_PROTOCOL) {
6101 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006102 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006103 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006104
6105 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6106 return -1;
6107}
6108
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006109static int
6110load_frame(UnpicklerObject *self)
6111{
6112 char *s;
6113 Py_ssize_t frame_len;
6114
6115 if (_Unpickler_Read(self, &s, 8) < 0)
6116 return -1;
6117
6118 frame_len = calc_binsize(s, 8);
6119 if (frame_len < 0) {
6120 PyErr_Format(PyExc_OverflowError,
6121 "FRAME length exceeds system's maximum of %zd bytes",
6122 PY_SSIZE_T_MAX);
6123 return -1;
6124 }
6125
6126 if (_Unpickler_Read(self, &s, frame_len) < 0)
6127 return -1;
6128
6129 /* Rewind to start of frame */
6130 self->next_read_idx -= frame_len;
6131 return 0;
6132}
6133
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006134static PyObject *
6135load(UnpicklerObject *self)
6136{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006137 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006138 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006139
6140 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006141 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006142 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006143 Pdata_clear(self->stack, 0);
6144
6145 /* Convenient macros for the dispatch while-switch loop just below. */
6146#define OP(opcode, load_func) \
6147 case opcode: if (load_func(self) < 0) break; continue;
6148
6149#define OP_ARG(opcode, load_func, arg) \
6150 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6151
6152 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006153 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006154 break;
6155
6156 switch ((enum opcode)s[0]) {
6157 OP(NONE, load_none)
6158 OP(BININT, load_binint)
6159 OP(BININT1, load_binint1)
6160 OP(BININT2, load_binint2)
6161 OP(INT, load_int)
6162 OP(LONG, load_long)
6163 OP_ARG(LONG1, load_counted_long, 1)
6164 OP_ARG(LONG4, load_counted_long, 4)
6165 OP(FLOAT, load_float)
6166 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006167 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6168 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6169 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6170 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6171 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006172 OP(STRING, load_string)
6173 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006174 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6175 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6176 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006177 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6178 OP_ARG(TUPLE1, load_counted_tuple, 1)
6179 OP_ARG(TUPLE2, load_counted_tuple, 2)
6180 OP_ARG(TUPLE3, load_counted_tuple, 3)
6181 OP(TUPLE, load_tuple)
6182 OP(EMPTY_LIST, load_empty_list)
6183 OP(LIST, load_list)
6184 OP(EMPTY_DICT, load_empty_dict)
6185 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006186 OP(EMPTY_SET, load_empty_set)
6187 OP(ADDITEMS, load_additems)
6188 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006189 OP(OBJ, load_obj)
6190 OP(INST, load_inst)
6191 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006192 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006193 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006194 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006195 OP(APPEND, load_append)
6196 OP(APPENDS, load_appends)
6197 OP(BUILD, load_build)
6198 OP(DUP, load_dup)
6199 OP(BINGET, load_binget)
6200 OP(LONG_BINGET, load_long_binget)
6201 OP(GET, load_get)
6202 OP(MARK, load_mark)
6203 OP(BINPUT, load_binput)
6204 OP(LONG_BINPUT, load_long_binput)
6205 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006206 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207 OP(POP, load_pop)
6208 OP(POP_MARK, load_pop_mark)
6209 OP(SETITEM, load_setitem)
6210 OP(SETITEMS, load_setitems)
6211 OP(PERSID, load_persid)
6212 OP(BINPERSID, load_binpersid)
6213 OP(REDUCE, load_reduce)
6214 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006215 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216 OP_ARG(EXT1, load_extension, 1)
6217 OP_ARG(EXT2, load_extension, 2)
6218 OP_ARG(EXT4, load_extension, 4)
6219 OP_ARG(NEWTRUE, load_bool, Py_True)
6220 OP_ARG(NEWFALSE, load_bool, Py_False)
6221
6222 case STOP:
6223 break;
6224
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006225 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006226 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006227 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006228 }
6229 else {
6230 PickleState *st = _Pickle_GetGlobalState();
6231 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006232 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006233 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006234 return NULL;
6235 }
6236
6237 break; /* and we are done! */
6238 }
6239
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006240 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 return NULL;
6242 }
6243
Victor Stinner2ae57e32013-10-31 13:39:23 +01006244 if (_Unpickler_SkipConsumed(self) < 0)
6245 return NULL;
6246
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247 PDATA_POP(self->stack, value);
6248 return value;
6249}
6250
Larry Hastings61272b72014-01-07 12:41:53 -08006251/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006252
6253_pickle.Unpickler.load
6254
6255Load a pickle.
6256
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006257Read a pickled object representation from the open file object given
6258in the constructor, and return the reconstituted object hierarchy
6259specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006260[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006261
Larry Hastings3cceb382014-01-04 11:09:09 -08006262static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006263_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006264/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006265{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006266 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006267
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268 /* Check whether the Unpickler was initialized correctly. This prevents
6269 segfaulting if a subclass overridden __init__ with a function that does
6270 not call Unpickler.__init__(). Here, we simply ensure that self->read
6271 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006272 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006273 PickleState *st = _Pickle_GetGlobalState();
6274 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006275 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006276 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006277 return NULL;
6278 }
6279
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006280 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281}
6282
6283/* The name of find_class() is misleading. In newer pickle protocols, this
6284 function is used for loading any global (i.e., functions), not just
6285 classes. The name is kept only for backward compatibility. */
6286
Larry Hastings61272b72014-01-07 12:41:53 -08006287/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006288
6289_pickle.Unpickler.find_class
6290
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006291 module_name: object
6292 global_name: object
6293 /
6294
6295Return an object from a specified module.
6296
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006297If necessary, the module will be imported. Subclasses may override
6298this method (e.g. to restrict unpickling of arbitrary classes and
6299functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006300
6301This method is called whenever a class or a function object is
6302needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006303[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006304
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006305static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006306_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6307 PyObject *module_name,
6308 PyObject *global_name)
6309/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310{
6311 PyObject *global;
6312 PyObject *modules_dict;
6313 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006314 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006315
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006316 /* Try to map the old names used in Python 2.x to the new ones used in
6317 Python 3.x. We do this only with old pickle protocols and when the
6318 user has not disabled the feature. */
6319 if (self->proto < 3 && self->fix_imports) {
6320 PyObject *key;
6321 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006322 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006323
6324 /* Check if the global (i.e., a function or a class) was renamed
6325 or moved to another module. */
6326 key = PyTuple_Pack(2, module_name, global_name);
6327 if (key == NULL)
6328 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006329 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006330 Py_DECREF(key);
6331 if (item) {
6332 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6333 PyErr_Format(PyExc_RuntimeError,
6334 "_compat_pickle.NAME_MAPPING values should be "
6335 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6336 return NULL;
6337 }
6338 module_name = PyTuple_GET_ITEM(item, 0);
6339 global_name = PyTuple_GET_ITEM(item, 1);
6340 if (!PyUnicode_Check(module_name) ||
6341 !PyUnicode_Check(global_name)) {
6342 PyErr_Format(PyExc_RuntimeError,
6343 "_compat_pickle.NAME_MAPPING values should be "
6344 "pairs of str, not (%.200s, %.200s)",
6345 Py_TYPE(module_name)->tp_name,
6346 Py_TYPE(global_name)->tp_name);
6347 return NULL;
6348 }
6349 }
6350 else if (PyErr_Occurred()) {
6351 return NULL;
6352 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006353 else {
6354 /* Check if the module was renamed. */
6355 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6356 if (item) {
6357 if (!PyUnicode_Check(item)) {
6358 PyErr_Format(PyExc_RuntimeError,
6359 "_compat_pickle.IMPORT_MAPPING values should be "
6360 "strings, not %.200s", Py_TYPE(item)->tp_name);
6361 return NULL;
6362 }
6363 module_name = item;
6364 }
6365 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006366 return NULL;
6367 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006368 }
6369 }
6370
Victor Stinnerbb520202013-11-06 22:40:41 +01006371 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006372 if (modules_dict == NULL) {
6373 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006374 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006375 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006376
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006377 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006378 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006379 if (PyErr_Occurred())
6380 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 module = PyImport_Import(module_name);
6382 if (module == NULL)
6383 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006384 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006385 Py_DECREF(module);
6386 }
Victor Stinner121aab42011-09-29 23:40:53 +02006387 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006388 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006389 }
6390 return global;
6391}
6392
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006393/*[clinic input]
6394
6395_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6396
6397Returns size in memory, in bytes.
6398[clinic start generated code]*/
6399
6400static Py_ssize_t
6401_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6402/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6403{
6404 Py_ssize_t res;
6405
6406 res = sizeof(UnpicklerObject);
6407 if (self->memo != NULL)
6408 res += self->memo_size * sizeof(PyObject *);
6409 if (self->marks != NULL)
6410 res += self->marks_size * sizeof(Py_ssize_t);
6411 if (self->input_line != NULL)
6412 res += strlen(self->input_line) + 1;
6413 if (self->encoding != NULL)
6414 res += strlen(self->encoding) + 1;
6415 if (self->errors != NULL)
6416 res += strlen(self->errors) + 1;
6417 return res;
6418}
6419
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006420static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006421 _PICKLE_UNPICKLER_LOAD_METHODDEF
6422 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006423 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006424 {NULL, NULL} /* sentinel */
6425};
6426
6427static void
6428Unpickler_dealloc(UnpicklerObject *self)
6429{
6430 PyObject_GC_UnTrack((PyObject *)self);
6431 Py_XDECREF(self->readline);
6432 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006433 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006434 Py_XDECREF(self->stack);
6435 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006436 if (self->buffer.buf != NULL) {
6437 PyBuffer_Release(&self->buffer);
6438 self->buffer.buf = NULL;
6439 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006440
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006441 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006442 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006443 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006444 PyMem_Free(self->encoding);
6445 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006446
6447 Py_TYPE(self)->tp_free((PyObject *)self);
6448}
6449
6450static int
6451Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6452{
6453 Py_VISIT(self->readline);
6454 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006455 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006456 Py_VISIT(self->stack);
6457 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006458 return 0;
6459}
6460
6461static int
6462Unpickler_clear(UnpicklerObject *self)
6463{
6464 Py_CLEAR(self->readline);
6465 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006466 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006467 Py_CLEAR(self->stack);
6468 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006469 if (self->buffer.buf != NULL) {
6470 PyBuffer_Release(&self->buffer);
6471 self->buffer.buf = NULL;
6472 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006473
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006474 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006475 PyMem_Free(self->marks);
6476 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006477 PyMem_Free(self->input_line);
6478 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006479 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006480 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006481 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 self->errors = NULL;
6483
6484 return 0;
6485}
6486
Larry Hastings61272b72014-01-07 12:41:53 -08006487/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006488
6489_pickle.Unpickler.__init__
6490
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006491 file: object
6492 *
6493 fix_imports: bool = True
6494 encoding: str = 'ASCII'
6495 errors: str = 'strict'
6496
6497This takes a binary file for reading a pickle data stream.
6498
6499The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006500protocol argument is needed. Bytes past the pickled object's
6501representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006502
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006503The argument *file* must have two methods, a read() method that takes
6504an integer argument, and a readline() method that requires no
6505arguments. Both methods should return bytes. Thus *file* can be a
6506binary file object opened for reading, a io.BytesIO object, or any
6507other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006508
6509Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6510which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006511generated by Python 2. If *fix_imports* is True, pickle will try to
6512map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006513*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006514instances pickled by Python 2; these default to 'ASCII' and 'strict',
6515respectively. The *encoding* can be 'bytes' to read these 8-bit
6516string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006517[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006518
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006519static int
Larry Hastings89964c42015-04-14 18:07:59 -04006520_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6521 int fix_imports, const char *encoding,
6522 const char *errors)
6523/*[clinic end generated code: output=e2c8ce748edc57b0 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006524{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006525 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006526
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006527 /* In case of multiple __init__() calls, clear previous content. */
6528 if (self->read != NULL)
6529 (void)Unpickler_clear(self);
6530
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006531 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006532 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006533
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006534 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006535 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006536
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006537 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006538 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006539 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006541 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006542 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6543 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006545 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 }
6547 else {
6548 self->pers_func = NULL;
6549 }
6550
6551 self->stack = (Pdata *)Pdata_New();
6552 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006553 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006555 self->memo_size = 32;
6556 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006557 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006558 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006559
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006560 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006561
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006562 return 0;
6563}
6564
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006565
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006566/* Define a proxy object for the Unpickler's internal memo object. This is to
6567 * avoid breaking code like:
6568 * unpickler.memo.clear()
6569 * and
6570 * unpickler.memo = saved_memo
6571 * Is this a good idea? Not really, but we don't want to break code that uses
6572 * it. Note that we don't implement the entire mapping API here. This is
6573 * intentional, as these should be treated as black-box implementation details.
6574 *
6575 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006576 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006577 */
6578
Larry Hastings61272b72014-01-07 12:41:53 -08006579/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006580_pickle.UnpicklerMemoProxy.clear
6581
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006582Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006583[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006584
Larry Hastings3cceb382014-01-04 11:09:09 -08006585static PyObject *
6586_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006587/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006588{
6589 _Unpickler_MemoCleanup(self->unpickler);
6590 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6591 if (self->unpickler->memo == NULL)
6592 return NULL;
6593 Py_RETURN_NONE;
6594}
6595
Larry Hastings61272b72014-01-07 12:41:53 -08006596/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597_pickle.UnpicklerMemoProxy.copy
6598
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006599Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006600[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006601
Larry Hastings3cceb382014-01-04 11:09:09 -08006602static PyObject *
6603_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006604/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006605{
6606 Py_ssize_t i;
6607 PyObject *new_memo = PyDict_New();
6608 if (new_memo == NULL)
6609 return NULL;
6610
6611 for (i = 0; i < self->unpickler->memo_size; i++) {
6612 int status;
6613 PyObject *key, *value;
6614
6615 value = self->unpickler->memo[i];
6616 if (value == NULL)
6617 continue;
6618
6619 key = PyLong_FromSsize_t(i);
6620 if (key == NULL)
6621 goto error;
6622 status = PyDict_SetItem(new_memo, key, value);
6623 Py_DECREF(key);
6624 if (status < 0)
6625 goto error;
6626 }
6627 return new_memo;
6628
6629error:
6630 Py_DECREF(new_memo);
6631 return NULL;
6632}
6633
Larry Hastings61272b72014-01-07 12:41:53 -08006634/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006635_pickle.UnpicklerMemoProxy.__reduce__
6636
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006637Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006638[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006639
Larry Hastings3cceb382014-01-04 11:09:09 -08006640static PyObject *
6641_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006642/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006643{
6644 PyObject *reduce_value;
6645 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006646 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006647 if (contents == NULL)
6648 return NULL;
6649
6650 reduce_value = PyTuple_New(2);
6651 if (reduce_value == NULL) {
6652 Py_DECREF(contents);
6653 return NULL;
6654 }
6655 constructor_args = PyTuple_New(1);
6656 if (constructor_args == NULL) {
6657 Py_DECREF(contents);
6658 Py_DECREF(reduce_value);
6659 return NULL;
6660 }
6661 PyTuple_SET_ITEM(constructor_args, 0, contents);
6662 Py_INCREF((PyObject *)&PyDict_Type);
6663 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6664 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6665 return reduce_value;
6666}
6667
6668static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006669 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6670 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6671 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006672 {NULL, NULL} /* sentinel */
6673};
6674
6675static void
6676UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6677{
6678 PyObject_GC_UnTrack(self);
6679 Py_XDECREF(self->unpickler);
6680 PyObject_GC_Del((PyObject *)self);
6681}
6682
6683static int
6684UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6685 visitproc visit, void *arg)
6686{
6687 Py_VISIT(self->unpickler);
6688 return 0;
6689}
6690
6691static int
6692UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6693{
6694 Py_CLEAR(self->unpickler);
6695 return 0;
6696}
6697
6698static PyTypeObject UnpicklerMemoProxyType = {
6699 PyVarObject_HEAD_INIT(NULL, 0)
6700 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6701 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6702 0,
6703 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6704 0, /* tp_print */
6705 0, /* tp_getattr */
6706 0, /* tp_setattr */
6707 0, /* tp_compare */
6708 0, /* tp_repr */
6709 0, /* tp_as_number */
6710 0, /* tp_as_sequence */
6711 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006712 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006713 0, /* tp_call */
6714 0, /* tp_str */
6715 PyObject_GenericGetAttr, /* tp_getattro */
6716 PyObject_GenericSetAttr, /* tp_setattro */
6717 0, /* tp_as_buffer */
6718 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6719 0, /* tp_doc */
6720 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6721 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6722 0, /* tp_richcompare */
6723 0, /* tp_weaklistoffset */
6724 0, /* tp_iter */
6725 0, /* tp_iternext */
6726 unpicklerproxy_methods, /* tp_methods */
6727};
6728
6729static PyObject *
6730UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6731{
6732 UnpicklerMemoProxyObject *self;
6733
6734 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6735 &UnpicklerMemoProxyType);
6736 if (self == NULL)
6737 return NULL;
6738 Py_INCREF(unpickler);
6739 self->unpickler = unpickler;
6740 PyObject_GC_Track(self);
6741 return (PyObject *)self;
6742}
6743
6744/*****************************************************************************/
6745
6746
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006747static PyObject *
6748Unpickler_get_memo(UnpicklerObject *self)
6749{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006750 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006751}
6752
6753static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006754Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006755{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006756 PyObject **new_memo;
6757 Py_ssize_t new_memo_size = 0;
6758 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006759
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006760 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006761 PyErr_SetString(PyExc_TypeError,
6762 "attribute deletion is not supported");
6763 return -1;
6764 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006765
6766 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6767 UnpicklerObject *unpickler =
6768 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6769
6770 new_memo_size = unpickler->memo_size;
6771 new_memo = _Unpickler_NewMemo(new_memo_size);
6772 if (new_memo == NULL)
6773 return -1;
6774
6775 for (i = 0; i < new_memo_size; i++) {
6776 Py_XINCREF(unpickler->memo[i]);
6777 new_memo[i] = unpickler->memo[i];
6778 }
6779 }
6780 else if (PyDict_Check(obj)) {
6781 Py_ssize_t i = 0;
6782 PyObject *key, *value;
6783
6784 new_memo_size = PyDict_Size(obj);
6785 new_memo = _Unpickler_NewMemo(new_memo_size);
6786 if (new_memo == NULL)
6787 return -1;
6788
6789 while (PyDict_Next(obj, &i, &key, &value)) {
6790 Py_ssize_t idx;
6791 if (!PyLong_Check(key)) {
6792 PyErr_SetString(PyExc_TypeError,
6793 "memo key must be integers");
6794 goto error;
6795 }
6796 idx = PyLong_AsSsize_t(key);
6797 if (idx == -1 && PyErr_Occurred())
6798 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006799 if (idx < 0) {
6800 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006801 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006802 goto error;
6803 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006804 if (_Unpickler_MemoPut(self, idx, value) < 0)
6805 goto error;
6806 }
6807 }
6808 else {
6809 PyErr_Format(PyExc_TypeError,
6810 "'memo' attribute must be an UnpicklerMemoProxy object"
6811 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006812 return -1;
6813 }
6814
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006815 _Unpickler_MemoCleanup(self);
6816 self->memo_size = new_memo_size;
6817 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006818
6819 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006820
6821 error:
6822 if (new_memo_size) {
6823 i = new_memo_size;
6824 while (--i >= 0) {
6825 Py_XDECREF(new_memo[i]);
6826 }
6827 PyMem_FREE(new_memo);
6828 }
6829 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006830}
6831
6832static PyObject *
6833Unpickler_get_persload(UnpicklerObject *self)
6834{
6835 if (self->pers_func == NULL)
6836 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6837 else
6838 Py_INCREF(self->pers_func);
6839 return self->pers_func;
6840}
6841
6842static int
6843Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6844{
6845 PyObject *tmp;
6846
6847 if (value == NULL) {
6848 PyErr_SetString(PyExc_TypeError,
6849 "attribute deletion is not supported");
6850 return -1;
6851 }
6852 if (!PyCallable_Check(value)) {
6853 PyErr_SetString(PyExc_TypeError,
6854 "persistent_load must be a callable taking "
6855 "one argument");
6856 return -1;
6857 }
6858
6859 tmp = self->pers_func;
6860 Py_INCREF(value);
6861 self->pers_func = value;
6862 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6863
6864 return 0;
6865}
6866
6867static PyGetSetDef Unpickler_getsets[] = {
6868 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6869 {"persistent_load", (getter)Unpickler_get_persload,
6870 (setter)Unpickler_set_persload},
6871 {NULL}
6872};
6873
6874static PyTypeObject Unpickler_Type = {
6875 PyVarObject_HEAD_INIT(NULL, 0)
6876 "_pickle.Unpickler", /*tp_name*/
6877 sizeof(UnpicklerObject), /*tp_basicsize*/
6878 0, /*tp_itemsize*/
6879 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6880 0, /*tp_print*/
6881 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006882 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006883 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006884 0, /*tp_repr*/
6885 0, /*tp_as_number*/
6886 0, /*tp_as_sequence*/
6887 0, /*tp_as_mapping*/
6888 0, /*tp_hash*/
6889 0, /*tp_call*/
6890 0, /*tp_str*/
6891 0, /*tp_getattro*/
6892 0, /*tp_setattro*/
6893 0, /*tp_as_buffer*/
6894 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006895 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006896 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6897 (inquiry)Unpickler_clear, /*tp_clear*/
6898 0, /*tp_richcompare*/
6899 0, /*tp_weaklistoffset*/
6900 0, /*tp_iter*/
6901 0, /*tp_iternext*/
6902 Unpickler_methods, /*tp_methods*/
6903 0, /*tp_members*/
6904 Unpickler_getsets, /*tp_getset*/
6905 0, /*tp_base*/
6906 0, /*tp_dict*/
6907 0, /*tp_descr_get*/
6908 0, /*tp_descr_set*/
6909 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006910 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006911 PyType_GenericAlloc, /*tp_alloc*/
6912 PyType_GenericNew, /*tp_new*/
6913 PyObject_GC_Del, /*tp_free*/
6914 0, /*tp_is_gc*/
6915};
6916
Larry Hastings61272b72014-01-07 12:41:53 -08006917/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006918
6919_pickle.dump
6920
6921 obj: object
6922 file: object
6923 protocol: object = NULL
6924 *
6925 fix_imports: bool = True
6926
6927Write a pickled representation of obj to the open file object file.
6928
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006929This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6930be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006931
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006932The optional *protocol* argument tells the pickler to use the given
6933protocol supported protocols are 0, 1, 2, 3 and 4. The default
6934protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006935
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006936Specifying a negative protocol version selects the highest protocol
6937version supported. The higher the protocol used, the more recent the
6938version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006939
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006940The *file* argument must have a write() method that accepts a single
6941bytes argument. It can thus be a file object opened for binary
6942writing, a io.BytesIO instance, or any other custom object that meets
6943this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006944
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006945If *fix_imports* is True and protocol is less than 3, pickle will try
6946to map the new Python 3 names to the old module names used in Python
69472, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08006948[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006949
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006950static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006951_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
6952 PyObject *protocol, int fix_imports)
6953/*[clinic end generated code: output=0de7dff89c406816 input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006954{
6955 PicklerObject *pickler = _Pickler_New();
6956
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006957 if (pickler == NULL)
6958 return NULL;
6959
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006960 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006961 goto error;
6962
6963 if (_Pickler_SetOutputStream(pickler, file) < 0)
6964 goto error;
6965
6966 if (dump(pickler, obj) < 0)
6967 goto error;
6968
6969 if (_Pickler_FlushToFile(pickler) < 0)
6970 goto error;
6971
6972 Py_DECREF(pickler);
6973 Py_RETURN_NONE;
6974
6975 error:
6976 Py_XDECREF(pickler);
6977 return NULL;
6978}
6979
Larry Hastings61272b72014-01-07 12:41:53 -08006980/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006981
6982_pickle.dumps
6983
6984 obj: object
6985 protocol: object = NULL
6986 *
6987 fix_imports: bool = True
6988
6989Return the pickled representation of the object as a bytes object.
6990
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006991The optional *protocol* argument tells the pickler to use the given
6992protocol; supported protocols are 0, 1, 2, 3 and 4. The default
6993protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006994
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006995Specifying a negative protocol version selects the highest protocol
6996version supported. The higher the protocol used, the more recent the
6997version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006998
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006999If *fix_imports* is True and *protocol* is less than 3, pickle will
7000try to map the new Python 3 names to the old module names used in
7001Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007002[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007003
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007005_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7006 int fix_imports)
7007/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007009 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007010 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007012 if (pickler == NULL)
7013 return NULL;
7014
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007015 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007016 goto error;
7017
7018 if (dump(pickler, obj) < 0)
7019 goto error;
7020
7021 result = _Pickler_GetString(pickler);
7022 Py_DECREF(pickler);
7023 return result;
7024
7025 error:
7026 Py_XDECREF(pickler);
7027 return NULL;
7028}
7029
Larry Hastings61272b72014-01-07 12:41:53 -08007030/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007031
7032_pickle.load
7033
7034 file: object
7035 *
7036 fix_imports: bool = True
7037 encoding: str = 'ASCII'
7038 errors: str = 'strict'
7039
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007040Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007041
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007042This is equivalent to ``Unpickler(file).load()``, but may be more
7043efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007044
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007045The protocol version of the pickle is detected automatically, so no
7046protocol argument is needed. Bytes past the pickled object's
7047representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007048
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007049The argument *file* must have two methods, a read() method that takes
7050an integer argument, and a readline() method that requires no
7051arguments. Both methods should return bytes. Thus *file* can be a
7052binary file object opened for reading, a io.BytesIO object, or any
7053other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007054
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007055Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7056which are used to control compatiblity support for pickle stream
7057generated by Python 2. If *fix_imports* is True, pickle will try to
7058map the old Python 2 names to the new names used in Python 3. The
7059*encoding* and *errors* tell pickle how to decode 8-bit string
7060instances pickled by Python 2; these default to 'ASCII' and 'strict',
7061respectively. The *encoding* can be 'bytes' to read these 8-bit
7062string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007063[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007064
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007065static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007066_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7067 const char *encoding, const char *errors)
7068/*[clinic end generated code: output=798f1c57cb2b4eb1 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007069{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007070 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007072
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007073 if (unpickler == NULL)
7074 return NULL;
7075
7076 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7077 goto error;
7078
7079 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7080 goto error;
7081
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007083
7084 result = load(unpickler);
7085 Py_DECREF(unpickler);
7086 return result;
7087
7088 error:
7089 Py_XDECREF(unpickler);
7090 return NULL;
7091}
7092
Larry Hastings61272b72014-01-07 12:41:53 -08007093/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094
7095_pickle.loads
7096
7097 data: object
7098 *
7099 fix_imports: bool = True
7100 encoding: str = 'ASCII'
7101 errors: str = 'strict'
7102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007103Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007104
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007105The protocol version of the pickle is detected automatically, so no
7106protocol argument is needed. Bytes past the pickled object's
7107representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007108
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007109Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7110which are used to control compatiblity support for pickle stream
7111generated by Python 2. If *fix_imports* is True, pickle will try to
7112map the old Python 2 names to the new names used in Python 3. The
7113*encoding* and *errors* tell pickle how to decode 8-bit string
7114instances pickled by Python 2; these default to 'ASCII' and 'strict',
7115respectively. The *encoding* can be 'bytes' to read these 8-bit
7116string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007117[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007118
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007119static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007120_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7121 const char *encoding, const char *errors)
7122/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007123{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007124 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007125 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007126
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007127 if (unpickler == NULL)
7128 return NULL;
7129
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007130 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007131 goto error;
7132
7133 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7134 goto error;
7135
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007136 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007137
7138 result = load(unpickler);
7139 Py_DECREF(unpickler);
7140 return result;
7141
7142 error:
7143 Py_XDECREF(unpickler);
7144 return NULL;
7145}
7146
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007147static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007148 _PICKLE_DUMP_METHODDEF
7149 _PICKLE_DUMPS_METHODDEF
7150 _PICKLE_LOAD_METHODDEF
7151 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007152 {NULL, NULL} /* sentinel */
7153};
7154
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007155static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007156pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007157{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007158 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007159 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007160}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007161
Stefan Krahf483b0f2013-12-14 13:43:10 +01007162static void
7163pickle_free(PyObject *m)
7164{
7165 _Pickle_ClearState(_Pickle_GetState(m));
7166}
7167
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007168static int
7169pickle_traverse(PyObject *m, visitproc visit, void *arg)
7170{
7171 PickleState *st = _Pickle_GetState(m);
7172 Py_VISIT(st->PickleError);
7173 Py_VISIT(st->PicklingError);
7174 Py_VISIT(st->UnpicklingError);
7175 Py_VISIT(st->dispatch_table);
7176 Py_VISIT(st->extension_registry);
7177 Py_VISIT(st->extension_cache);
7178 Py_VISIT(st->inverted_registry);
7179 Py_VISIT(st->name_mapping_2to3);
7180 Py_VISIT(st->import_mapping_2to3);
7181 Py_VISIT(st->name_mapping_3to2);
7182 Py_VISIT(st->import_mapping_3to2);
7183 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007184 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007185 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007186}
7187
7188static struct PyModuleDef _picklemodule = {
7189 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007190 "_pickle", /* m_name */
7191 pickle_module_doc, /* m_doc */
7192 sizeof(PickleState), /* m_size */
7193 pickle_methods, /* m_methods */
7194 NULL, /* m_reload */
7195 pickle_traverse, /* m_traverse */
7196 pickle_clear, /* m_clear */
7197 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007198};
7199
7200PyMODINIT_FUNC
7201PyInit__pickle(void)
7202{
7203 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007204 PickleState *st;
7205
7206 m = PyState_FindModule(&_picklemodule);
7207 if (m) {
7208 Py_INCREF(m);
7209 return m;
7210 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007211
7212 if (PyType_Ready(&Unpickler_Type) < 0)
7213 return NULL;
7214 if (PyType_Ready(&Pickler_Type) < 0)
7215 return NULL;
7216 if (PyType_Ready(&Pdata_Type) < 0)
7217 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007218 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7219 return NULL;
7220 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7221 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007222
7223 /* Create the module and add the functions. */
7224 m = PyModule_Create(&_picklemodule);
7225 if (m == NULL)
7226 return NULL;
7227
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007228 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007229 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7230 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007231 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007232 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7233 return NULL;
7234
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007235 st = _Pickle_GetState(m);
7236
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007237 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007238 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7239 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007240 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007241 st->PicklingError = \
7242 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7243 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007244 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007245 st->UnpicklingError = \
7246 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7247 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007248 return NULL;
7249
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007250 Py_INCREF(st->PickleError);
7251 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007252 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007253 Py_INCREF(st->PicklingError);
7254 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007255 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007256 Py_INCREF(st->UnpicklingError);
7257 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007258 return NULL;
7259
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007260 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007261 return NULL;
7262
7263 return m;
7264}