blob: a6f414c8c384df7ea85a15ece5bbb185c360692f [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;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300156 /* functools.partial, used for implementing __newobj_ex__ with protocols
157 2 and 3 */
158 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800159} PickleState;
160
161/* Forward declaration of the _pickle module definition. */
162static struct PyModuleDef _picklemodule;
163
164/* Given a module object, get its per-module state. */
165static PickleState *
166_Pickle_GetState(PyObject *module)
167{
168 return (PickleState *)PyModule_GetState(module);
169}
170
171/* Find the module instance imported in the currently running sub-interpreter
172 and get its state. */
173static PickleState *
174_Pickle_GetGlobalState(void)
175{
176 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
177}
178
179/* Clear the given pickle module state. */
180static void
181_Pickle_ClearState(PickleState *st)
182{
183 Py_CLEAR(st->PickleError);
184 Py_CLEAR(st->PicklingError);
185 Py_CLEAR(st->UnpicklingError);
186 Py_CLEAR(st->dispatch_table);
187 Py_CLEAR(st->extension_registry);
188 Py_CLEAR(st->extension_cache);
189 Py_CLEAR(st->inverted_registry);
190 Py_CLEAR(st->name_mapping_2to3);
191 Py_CLEAR(st->import_mapping_2to3);
192 Py_CLEAR(st->name_mapping_3to2);
193 Py_CLEAR(st->import_mapping_3to2);
194 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300195 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100196 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800197}
198
199/* Initialize the given pickle module state. */
200static int
201_Pickle_InitState(PickleState *st)
202{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300203 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204 PyObject *copyreg = NULL;
205 PyObject *compat_pickle = NULL;
206 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300207 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800208
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300209 builtins = PyEval_GetBuiltins();
210 if (builtins == NULL)
211 goto error;
212 st->getattr = PyDict_GetItemString(builtins, "getattr");
213 if (st->getattr == NULL)
214 goto error;
215 Py_INCREF(st->getattr);
216
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217 copyreg = PyImport_ImportModule("copyreg");
218 if (!copyreg)
219 goto error;
220 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
221 if (!st->dispatch_table)
222 goto error;
223 if (!PyDict_CheckExact(st->dispatch_table)) {
224 PyErr_Format(PyExc_RuntimeError,
225 "copyreg.dispatch_table should be a dict, not %.200s",
226 Py_TYPE(st->dispatch_table)->tp_name);
227 goto error;
228 }
229 st->extension_registry = \
230 PyObject_GetAttrString(copyreg, "_extension_registry");
231 if (!st->extension_registry)
232 goto error;
233 if (!PyDict_CheckExact(st->extension_registry)) {
234 PyErr_Format(PyExc_RuntimeError,
235 "copyreg._extension_registry should be a dict, "
236 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
237 goto error;
238 }
239 st->inverted_registry = \
240 PyObject_GetAttrString(copyreg, "_inverted_registry");
241 if (!st->inverted_registry)
242 goto error;
243 if (!PyDict_CheckExact(st->inverted_registry)) {
244 PyErr_Format(PyExc_RuntimeError,
245 "copyreg._inverted_registry should be a dict, "
246 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
247 goto error;
248 }
249 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
250 if (!st->extension_cache)
251 goto error;
252 if (!PyDict_CheckExact(st->extension_cache)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._extension_cache should be a dict, "
255 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
256 goto error;
257 }
258 Py_CLEAR(copyreg);
259
260 /* Load the 2.x -> 3.x stdlib module mapping tables */
261 compat_pickle = PyImport_ImportModule("_compat_pickle");
262 if (!compat_pickle)
263 goto error;
264 st->name_mapping_2to3 = \
265 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
266 if (!st->name_mapping_2to3)
267 goto error;
268 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
269 PyErr_Format(PyExc_RuntimeError,
270 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
271 Py_TYPE(st->name_mapping_2to3)->tp_name);
272 goto error;
273 }
274 st->import_mapping_2to3 = \
275 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
276 if (!st->import_mapping_2to3)
277 goto error;
278 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
279 PyErr_Format(PyExc_RuntimeError,
280 "_compat_pickle.IMPORT_MAPPING should be a dict, "
281 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
282 goto error;
283 }
284 /* ... and the 3.x -> 2.x mapping tables */
285 st->name_mapping_3to2 = \
286 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
287 if (!st->name_mapping_3to2)
288 goto error;
289 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
290 PyErr_Format(PyExc_RuntimeError,
291 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
292 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
293 goto error;
294 }
295 st->import_mapping_3to2 = \
296 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
297 if (!st->import_mapping_3to2)
298 goto error;
299 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
300 PyErr_Format(PyExc_RuntimeError,
301 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
302 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
303 goto error;
304 }
305 Py_CLEAR(compat_pickle);
306
307 codecs = PyImport_ImportModule("codecs");
308 if (codecs == NULL)
309 goto error;
310 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
311 if (st->codecs_encode == NULL) {
312 goto error;
313 }
314 if (!PyCallable_Check(st->codecs_encode)) {
315 PyErr_Format(PyExc_RuntimeError,
316 "codecs.encode should be a callable, not %.200s",
317 Py_TYPE(st->codecs_encode)->tp_name);
318 goto error;
319 }
320 Py_CLEAR(codecs);
321
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300322 functools = PyImport_ImportModule("functools");
323 if (!functools)
324 goto error;
325 st->partial = PyObject_GetAttrString(functools, "partial");
326 if (!st->partial)
327 goto error;
328 Py_CLEAR(functools);
329
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800330 return 0;
331
332 error:
333 Py_CLEAR(copyreg);
334 Py_CLEAR(compat_pickle);
335 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300336 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 _Pickle_ClearState(st);
338 return -1;
339}
340
341/* Helper for calling a function with a single argument quickly.
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 This function steals the reference of the given argument. */
344static PyObject *
345_Pickle_FastCall(PyObject *func, PyObject *obj)
346{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800349
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800350 /* Note: this function used to reuse the argument tuple. This used to give
351 a slight performance boost with older pickle implementations where many
352 unbuffered reads occurred (thus needing many function calls).
353
354 However, this optimization was removed because it was too complicated
355 to get right. It abused the C API for tuples to mutate them which led
356 to subtle reference counting and concurrency bugs. Furthermore, the
357 introduction of protocol 4 and the prefetching optimization via peek()
358 significantly reduced the number of function calls we do. Thus, the
359 benefits became marginal at best. */
360
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800361 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800362 Py_DECREF(obj);
363 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PyTuple_SET_ITEM(arg_tuple, 0, obj);
366 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800367 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800368 return result;
369}
370
371/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372
373static int
374stack_underflow(void)
375{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800376 PickleState *st = _Pickle_GetGlobalState();
377 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000378 return -1;
379}
380
381/* Internal data type used as the unpickling stack. */
382typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000383 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000384 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000385 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000386} Pdata;
387
388static void
389Pdata_dealloc(Pdata *self)
390{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200391 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000392 while (--i >= 0) {
393 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000394 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000395 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000396 PyObject_Del(self);
397}
398
399static PyTypeObject Pdata_Type = {
400 PyVarObject_HEAD_INIT(NULL, 0)
401 "_pickle.Pdata", /*tp_name*/
402 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200403 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000404 (destructor)Pdata_dealloc, /*tp_dealloc*/
405};
406
407static PyObject *
408Pdata_New(void)
409{
410 Pdata *self;
411
412 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
413 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000414 Py_SIZE(self) = 0;
415 self->allocated = 8;
416 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000417 if (self->data)
418 return (PyObject *)self;
419 Py_DECREF(self);
420 return PyErr_NoMemory();
421}
422
423
424/* Retain only the initial clearto items. If clearto >= the current
425 * number of items, this is a (non-erroneous) NOP.
426 */
427static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200428Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000429{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200430 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000431
432 if (clearto < 0)
433 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000434 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000435 return 0;
436
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000437 while (--i >= clearto) {
438 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000439 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000440 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000441 return 0;
442}
443
444static int
445Pdata_grow(Pdata *self)
446{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000447 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200448 size_t allocated = (size_t)self->allocated;
449 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000450
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000451 new_allocated = (allocated >> 3) + 6;
452 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200453 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000454 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000455 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500456 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000457 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000458 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000459
460 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200461 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000462 return 0;
463
464 nomemory:
465 PyErr_NoMemory();
466 return -1;
467}
468
469/* D is a Pdata*. Pop the topmost element and store it into V, which
470 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
471 * is raised and V is set to NULL.
472 */
473static PyObject *
474Pdata_pop(Pdata *self)
475{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800476 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000477 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800478 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000479 return NULL;
480 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000481 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000482}
483#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
484
485static int
486Pdata_push(Pdata *self, PyObject *obj)
487{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000488 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000489 return -1;
490 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000491 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000492 return 0;
493}
494
495/* Push an object on stack, transferring its ownership to the stack. */
496#define PDATA_PUSH(D, O, ER) do { \
497 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
498
499/* Push an object on stack, adding a new reference to the object. */
500#define PDATA_APPEND(D, O, ER) do { \
501 Py_INCREF((O)); \
502 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
503
504static PyObject *
505Pdata_poptuple(Pdata *self, Py_ssize_t start)
506{
507 PyObject *tuple;
508 Py_ssize_t len, i, j;
509
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000510 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000511 tuple = PyTuple_New(len);
512 if (tuple == NULL)
513 return NULL;
514 for (i = start, j = 0; j < len; i++, j++)
515 PyTuple_SET_ITEM(tuple, j, self->data[i]);
516
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000517 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000518 return tuple;
519}
520
521static PyObject *
522Pdata_poplist(Pdata *self, Py_ssize_t start)
523{
524 PyObject *list;
525 Py_ssize_t len, i, j;
526
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000527 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000528 list = PyList_New(len);
529 if (list == NULL)
530 return NULL;
531 for (i = start, j = 0; j < len; i++, j++)
532 PyList_SET_ITEM(list, j, self->data[i]);
533
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000534 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000535 return list;
536}
537
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000538typedef struct {
539 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200540 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000541} PyMemoEntry;
542
543typedef struct {
544 Py_ssize_t mt_mask;
545 Py_ssize_t mt_used;
546 Py_ssize_t mt_allocated;
547 PyMemoEntry *mt_table;
548} PyMemoTable;
549
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000550typedef struct PicklerObject {
551 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000552 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000553 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000554 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000555 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100556 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000557
558 PyObject *write; /* write() method of the output stream. */
559 PyObject *output_buffer; /* Write into a local bytearray buffer before
560 flushing to the stream. */
561 Py_ssize_t output_len; /* Length of output_buffer. */
562 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000563 int proto; /* Pickle protocol number, >= 0 */
564 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100565 int framing; /* True when framing is enabled, proto >= 4 */
566 Py_ssize_t frame_start; /* Position in output_buffer where the
567 where the current frame begins. -1 if there
568 is no frame currently open. */
569
570 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000571 int fast; /* Enable fast mode if set to a true value.
572 The fast mode disable the usage of memo,
573 therefore speeding the pickling process by
574 not generating superfluous PUT opcodes. It
575 should not be used if with self-referential
576 objects. */
577 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000578 int fix_imports; /* Indicate whether Pickler should fix
579 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000580 PyObject *fast_memo;
581} PicklerObject;
582
583typedef struct UnpicklerObject {
584 PyObject_HEAD
585 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000586
587 /* The unpickler memo is just an array of PyObject *s. Using a dict
588 is unnecessary, since the keys are contiguous ints. */
589 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100590 Py_ssize_t memo_size; /* Capacity of the memo array */
591 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000592
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000593 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000594
595 Py_buffer buffer;
596 char *input_buffer;
597 char *input_line;
598 Py_ssize_t input_len;
599 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000600 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100601
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000602 PyObject *read; /* read() method of the input stream. */
603 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000604 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000605
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000606 char *encoding; /* Name of the encoding to be used for
607 decoding strings pickled using Python
608 2.x. The default value is "ASCII" */
609 char *errors; /* Name of errors handling scheme to used when
610 decoding strings. The default value is
611 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500612 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000613 objects. */
614 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
615 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000616 int proto; /* Protocol of the pickle loaded. */
617 int fix_imports; /* Indicate whether Unpickler should fix
618 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000619} UnpicklerObject;
620
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200621typedef struct {
622 PyObject_HEAD
623 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
624} PicklerMemoProxyObject;
625
626typedef struct {
627 PyObject_HEAD
628 UnpicklerObject *unpickler;
629} UnpicklerMemoProxyObject;
630
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000631/* Forward declarations */
632static int save(PicklerObject *, PyObject *, int);
633static int save_reduce(PicklerObject *, PyObject *, PyObject *);
634static PyTypeObject Pickler_Type;
635static PyTypeObject Unpickler_Type;
636
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200637#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000638
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000639/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300640 A custom hashtable mapping void* to Python ints. This is used by the pickler
641 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000642 a bunch of unnecessary object creation. This makes a huge performance
643 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000644
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000645#define MT_MINSIZE 8
646#define PERTURB_SHIFT 5
647
648
649static PyMemoTable *
650PyMemoTable_New(void)
651{
652 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
653 if (memo == NULL) {
654 PyErr_NoMemory();
655 return NULL;
656 }
657
658 memo->mt_used = 0;
659 memo->mt_allocated = MT_MINSIZE;
660 memo->mt_mask = MT_MINSIZE - 1;
661 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
662 if (memo->mt_table == NULL) {
663 PyMem_FREE(memo);
664 PyErr_NoMemory();
665 return NULL;
666 }
667 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
668
669 return memo;
670}
671
672static PyMemoTable *
673PyMemoTable_Copy(PyMemoTable *self)
674{
675 Py_ssize_t i;
676 PyMemoTable *new = PyMemoTable_New();
677 if (new == NULL)
678 return NULL;
679
680 new->mt_used = self->mt_used;
681 new->mt_allocated = self->mt_allocated;
682 new->mt_mask = self->mt_mask;
683 /* The table we get from _New() is probably smaller than we wanted.
684 Free it and allocate one that's the right size. */
685 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500686 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000687 if (new->mt_table == NULL) {
688 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200689 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000690 return NULL;
691 }
692 for (i = 0; i < self->mt_allocated; i++) {
693 Py_XINCREF(self->mt_table[i].me_key);
694 }
695 memcpy(new->mt_table, self->mt_table,
696 sizeof(PyMemoEntry) * self->mt_allocated);
697
698 return new;
699}
700
701static Py_ssize_t
702PyMemoTable_Size(PyMemoTable *self)
703{
704 return self->mt_used;
705}
706
707static int
708PyMemoTable_Clear(PyMemoTable *self)
709{
710 Py_ssize_t i = self->mt_allocated;
711
712 while (--i >= 0) {
713 Py_XDECREF(self->mt_table[i].me_key);
714 }
715 self->mt_used = 0;
716 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
717 return 0;
718}
719
720static void
721PyMemoTable_Del(PyMemoTable *self)
722{
723 if (self == NULL)
724 return;
725 PyMemoTable_Clear(self);
726
727 PyMem_FREE(self->mt_table);
728 PyMem_FREE(self);
729}
730
731/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
732 can be considerably simpler than dictobject.c's lookdict(). */
733static PyMemoEntry *
734_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
735{
736 size_t i;
737 size_t perturb;
738 size_t mask = (size_t)self->mt_mask;
739 PyMemoEntry *table = self->mt_table;
740 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000741 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000742
743 i = hash & mask;
744 entry = &table[i];
745 if (entry->me_key == NULL || entry->me_key == key)
746 return entry;
747
748 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
749 i = (i << 2) + i + perturb + 1;
750 entry = &table[i & mask];
751 if (entry->me_key == NULL || entry->me_key == key)
752 return entry;
753 }
754 assert(0); /* Never reached */
755 return NULL;
756}
757
758/* Returns -1 on failure, 0 on success. */
759static int
760_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
761{
762 PyMemoEntry *oldtable = NULL;
763 PyMemoEntry *oldentry, *newentry;
764 Py_ssize_t new_size = MT_MINSIZE;
765 Py_ssize_t to_process;
766
767 assert(min_size > 0);
768
769 /* Find the smallest valid table size >= min_size. */
770 while (new_size < min_size && new_size > 0)
771 new_size <<= 1;
772 if (new_size <= 0) {
773 PyErr_NoMemory();
774 return -1;
775 }
776 /* new_size needs to be a power of two. */
777 assert((new_size & (new_size - 1)) == 0);
778
779 /* Allocate new table. */
780 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500781 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000782 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200783 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000784 PyErr_NoMemory();
785 return -1;
786 }
787 self->mt_allocated = new_size;
788 self->mt_mask = new_size - 1;
789 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
790
791 /* Copy entries from the old table. */
792 to_process = self->mt_used;
793 for (oldentry = oldtable; to_process > 0; oldentry++) {
794 if (oldentry->me_key != NULL) {
795 to_process--;
796 /* newentry is a pointer to a chunk of the new
797 mt_table, so we're setting the key:value pair
798 in-place. */
799 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
800 newentry->me_key = oldentry->me_key;
801 newentry->me_value = oldentry->me_value;
802 }
803 }
804
805 /* Deallocate the old table. */
806 PyMem_FREE(oldtable);
807 return 0;
808}
809
810/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200811static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000812PyMemoTable_Get(PyMemoTable *self, PyObject *key)
813{
814 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
815 if (entry->me_key == NULL)
816 return NULL;
817 return &entry->me_value;
818}
819
820/* Returns -1 on failure, 0 on success. */
821static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200822PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000823{
824 PyMemoEntry *entry;
825
826 assert(key != NULL);
827
828 entry = _PyMemoTable_Lookup(self, key);
829 if (entry->me_key != NULL) {
830 entry->me_value = value;
831 return 0;
832 }
833 Py_INCREF(key);
834 entry->me_key = key;
835 entry->me_value = value;
836 self->mt_used++;
837
838 /* If we added a key, we can safely resize. Otherwise just return!
839 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
840 *
841 * Quadrupling the size improves average table sparseness
842 * (reducing collisions) at the cost of some memory. It also halves
843 * the number of expensive resize operations in a growing memo table.
844 *
845 * Very large memo tables (over 50K items) use doubling instead.
846 * This may help applications with severe memory constraints.
847 */
848 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
849 return 0;
850 return _PyMemoTable_ResizeTable(self,
851 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
852}
853
854#undef MT_MINSIZE
855#undef PERTURB_SHIFT
856
857/*************************************************************************/
858
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000859
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000860static int
861_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000862{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000863 Py_CLEAR(self->output_buffer);
864 self->output_buffer =
865 PyBytes_FromStringAndSize(NULL, self->max_output_len);
866 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000867 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000868 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100869 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000870 return 0;
871}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000872
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100873static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100874_write_size64(char *out, size_t value)
875{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200876 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800877
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200878 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800879
880 for (i = 0; i < sizeof(size_t); i++) {
881 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
882 }
883 for (i = sizeof(size_t); i < 8; i++) {
884 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800885 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100886}
887
888static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100889_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
890{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100891 qdata[0] = FRAME;
892 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100893}
894
895static int
896_Pickler_CommitFrame(PicklerObject *self)
897{
898 size_t frame_len;
899 char *qdata;
900
901 if (!self->framing || self->frame_start == -1)
902 return 0;
903 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
904 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
905 _Pickler_WriteFrameHeader(self, qdata, frame_len);
906 self->frame_start = -1;
907 return 0;
908}
909
910static int
911_Pickler_OpcodeBoundary(PicklerObject *self)
912{
913 Py_ssize_t frame_len;
914
915 if (!self->framing || self->frame_start == -1)
916 return 0;
917 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
918 if (frame_len >= FRAME_SIZE_TARGET)
919 return _Pickler_CommitFrame(self);
920 else
921 return 0;
922}
923
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000924static PyObject *
925_Pickler_GetString(PicklerObject *self)
926{
927 PyObject *output_buffer = self->output_buffer;
928
929 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100930
931 if (_Pickler_CommitFrame(self))
932 return NULL;
933
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000934 self->output_buffer = NULL;
935 /* Resize down to exact size */
936 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
937 return NULL;
938 return output_buffer;
939}
940
941static int
942_Pickler_FlushToFile(PicklerObject *self)
943{
944 PyObject *output, *result;
945
946 assert(self->write != NULL);
947
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100948 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000949 output = _Pickler_GetString(self);
950 if (output == NULL)
951 return -1;
952
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800953 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000954 Py_XDECREF(result);
955 return (result == NULL) ? -1 : 0;
956}
957
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200958static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100959_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000960{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100961 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000962 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100963 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000964
965 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100966 need_new_frame = (self->framing && self->frame_start == -1);
967
968 if (need_new_frame)
969 n = data_len + FRAME_HEADER_SIZE;
970 else
971 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972
973 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100974 if (required > self->max_output_len) {
975 /* Make place in buffer for the pickle chunk */
976 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
977 PyErr_NoMemory();
978 return -1;
979 }
980 self->max_output_len = (self->output_len + n) / 2 * 3;
981 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
982 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000983 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000984 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100985 if (need_new_frame) {
986 /* Setup new frame */
987 Py_ssize_t frame_start = self->output_len;
988 self->frame_start = frame_start;
989 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
990 /* Write an invalid value, for debugging */
991 buffer[frame_start + i] = 0xFE;
992 }
993 self->output_len += FRAME_HEADER_SIZE;
994 }
995 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000996 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100997 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000998 buffer[self->output_len + i] = s[i];
999 }
1000 }
1001 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001002 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001003 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001004 self->output_len += data_len;
1005 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001006}
1007
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001008static PicklerObject *
1009_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001010{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001011 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001012
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001013 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1014 if (self == NULL)
1015 return NULL;
1016
1017 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001018 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019 self->write = NULL;
1020 self->proto = 0;
1021 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001022 self->framing = 0;
1023 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001024 self->fast = 0;
1025 self->fast_nesting = 0;
1026 self->fix_imports = 0;
1027 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001028 self->max_output_len = WRITE_BUF_SIZE;
1029 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001030
1031 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1033 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001034
1035 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001036 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001037 return NULL;
1038 }
1039 return self;
1040}
1041
1042static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001043_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001044{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001045 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001046
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001047 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001048 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001049 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001050 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051 proto = PyLong_AsLong(protocol);
1052 if (proto < 0) {
1053 if (proto == -1 && PyErr_Occurred())
1054 return -1;
1055 proto = HIGHEST_PROTOCOL;
1056 }
1057 else if (proto > HIGHEST_PROTOCOL) {
1058 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1059 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001060 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001061 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001062 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001063 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001064 self->bin = proto > 0;
1065 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001066 return 0;
1067}
1068
1069/* Returns -1 (with an exception set) on failure, 0 on success. This may
1070 be called once on a freshly created Pickler. */
1071static int
1072_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1073{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001074 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001075 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001076 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001077 if (self->write == NULL) {
1078 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1079 PyErr_SetString(PyExc_TypeError,
1080 "file must have a 'write' attribute");
1081 return -1;
1082 }
1083
1084 return 0;
1085}
1086
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001087/* Returns the size of the input on success, -1 on failure. This takes its
1088 own reference to `input`. */
1089static Py_ssize_t
1090_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1091{
1092 if (self->buffer.buf != NULL)
1093 PyBuffer_Release(&self->buffer);
1094 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1095 return -1;
1096 self->input_buffer = self->buffer.buf;
1097 self->input_len = self->buffer.len;
1098 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001099 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001100 return self->input_len;
1101}
1102
Antoine Pitrou04248a82010-10-12 20:51:21 +00001103static int
1104_Unpickler_SkipConsumed(UnpicklerObject *self)
1105{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001106 Py_ssize_t consumed;
1107 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001108
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001109 consumed = self->next_read_idx - self->prefetched_idx;
1110 if (consumed <= 0)
1111 return 0;
1112
1113 assert(self->peek); /* otherwise we did something wrong */
1114 /* This makes an useless copy... */
1115 r = PyObject_CallFunction(self->read, "n", consumed);
1116 if (r == NULL)
1117 return -1;
1118 Py_DECREF(r);
1119
1120 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001121 return 0;
1122}
1123
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001124static const Py_ssize_t READ_WHOLE_LINE = -1;
1125
1126/* If reading from a file, we need to only pull the bytes we need, since there
1127 may be multiple pickle objects arranged contiguously in the same input
1128 buffer.
1129
1130 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1131 bytes from the input stream/buffer.
1132
1133 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1134 failure; on success, returns the number of bytes read from the file.
1135
1136 On success, self->input_len will be 0; this is intentional so that when
1137 unpickling from a file, the "we've run out of data" code paths will trigger,
1138 causing the Unpickler to go back to the file for more data. Use the returned
1139 size to tell you how much data you can process. */
1140static Py_ssize_t
1141_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1142{
1143 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001144 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001145
1146 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001147
Antoine Pitrou04248a82010-10-12 20:51:21 +00001148 if (_Unpickler_SkipConsumed(self) < 0)
1149 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001150
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001151 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001152 PyObject *empty_tuple = PyTuple_New(0);
1153 data = PyObject_Call(self->readline, empty_tuple, NULL);
1154 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001155 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001156 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001157 PyObject *len;
1158 /* Prefetch some data without advancing the file pointer, if possible */
1159 if (self->peek && n < PREFETCH) {
1160 len = PyLong_FromSsize_t(PREFETCH);
1161 if (len == NULL)
1162 return -1;
1163 data = _Pickle_FastCall(self->peek, len);
1164 if (data == NULL) {
1165 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1166 return -1;
1167 /* peek() is probably not supported by the given file object */
1168 PyErr_Clear();
1169 Py_CLEAR(self->peek);
1170 }
1171 else {
1172 read_size = _Unpickler_SetStringInput(self, data);
1173 Py_DECREF(data);
1174 self->prefetched_idx = 0;
1175 if (n <= read_size)
1176 return n;
1177 }
1178 }
1179 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001180 if (len == NULL)
1181 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001182 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001183 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001184 if (data == NULL)
1185 return -1;
1186
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001187 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001188 Py_DECREF(data);
1189 return read_size;
1190}
1191
1192/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1193
1194 This should be used for all data reads, rather than accessing the unpickler's
1195 input buffer directly. This method deals correctly with reading from input
1196 streams, which the input buffer doesn't deal with.
1197
1198 Note that when reading from a file-like object, self->next_read_idx won't
1199 be updated (it should remain at 0 for the entire unpickling process). You
1200 should use this function's return value to know how many bytes you can
1201 consume.
1202
1203 Returns -1 (with an exception set) on failure. On success, return the
1204 number of chars read. */
1205static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001206_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001207{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001208 Py_ssize_t num_read;
1209
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001210 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001211 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1212 PickleState *st = _Pickle_GetGlobalState();
1213 PyErr_SetString(st->UnpicklingError,
1214 "read would overflow (invalid bytecode)");
1215 return -1;
1216 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001217 if (self->next_read_idx + n <= self->input_len) {
1218 *s = self->input_buffer + self->next_read_idx;
1219 self->next_read_idx += n;
1220 return n;
1221 }
1222 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001223 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001224 return -1;
1225 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001226 num_read = _Unpickler_ReadFromFile(self, n);
1227 if (num_read < 0)
1228 return -1;
1229 if (num_read < n) {
1230 PyErr_Format(PyExc_EOFError, "Ran out of input");
1231 return -1;
1232 }
1233 *s = self->input_buffer;
1234 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001235 return n;
1236}
1237
1238static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001239_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1240 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001241{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001242 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001243 if (input_line == NULL) {
1244 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001245 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001246 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001247
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001248 memcpy(input_line, line, len);
1249 input_line[len] = '\0';
1250 self->input_line = input_line;
1251 *result = self->input_line;
1252 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001253}
1254
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001255/* Read a line from the input stream/buffer. If we run off the end of the input
1256 before hitting \n, return the data we found.
1257
1258 Returns the number of chars read, or -1 on failure. */
1259static Py_ssize_t
1260_Unpickler_Readline(UnpicklerObject *self, char **result)
1261{
1262 Py_ssize_t i, num_read;
1263
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001265 if (self->input_buffer[i] == '\n') {
1266 char *line_start = self->input_buffer + self->next_read_idx;
1267 num_read = i - self->next_read_idx + 1;
1268 self->next_read_idx = i + 1;
1269 return _Unpickler_CopyLine(self, line_start, num_read, result);
1270 }
1271 }
1272 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1274 if (num_read < 0)
1275 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001276 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001277 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001278 }
Victor Stinner121aab42011-09-29 23:40:53 +02001279
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001280 /* If we get here, we've run off the end of the input string. Return the
1281 remaining string and let the caller figure it out. */
1282 *result = self->input_buffer + self->next_read_idx;
1283 num_read = i - self->next_read_idx;
1284 self->next_read_idx = i;
1285 return num_read;
1286}
1287
1288/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1289 will be modified in place. */
1290static int
1291_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1292{
1293 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001294
1295 assert(new_size > self->memo_size);
1296
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001297 PyMem_RESIZE(self->memo, PyObject *, new_size);
1298 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001299 PyErr_NoMemory();
1300 return -1;
1301 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001302 for (i = self->memo_size; i < new_size; i++)
1303 self->memo[i] = NULL;
1304 self->memo_size = new_size;
1305 return 0;
1306}
1307
1308/* Returns NULL if idx is out of bounds. */
1309static PyObject *
1310_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1311{
1312 if (idx < 0 || idx >= self->memo_size)
1313 return NULL;
1314
1315 return self->memo[idx];
1316}
1317
1318/* Returns -1 (with an exception set) on failure, 0 on success.
1319 This takes its own reference to `value`. */
1320static int
1321_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1322{
1323 PyObject *old_item;
1324
1325 if (idx >= self->memo_size) {
1326 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1327 return -1;
1328 assert(idx < self->memo_size);
1329 }
1330 Py_INCREF(value);
1331 old_item = self->memo[idx];
1332 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001333 if (old_item != NULL) {
1334 Py_DECREF(old_item);
1335 }
1336 else {
1337 self->memo_len++;
1338 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001339 return 0;
1340}
1341
1342static PyObject **
1343_Unpickler_NewMemo(Py_ssize_t new_size)
1344{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001345 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001346 if (memo == NULL) {
1347 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001348 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001349 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001350 memset(memo, 0, new_size * sizeof(PyObject *));
1351 return memo;
1352}
1353
1354/* Free the unpickler's memo, taking care to decref any items left in it. */
1355static void
1356_Unpickler_MemoCleanup(UnpicklerObject *self)
1357{
1358 Py_ssize_t i;
1359 PyObject **memo = self->memo;
1360
1361 if (self->memo == NULL)
1362 return;
1363 self->memo = NULL;
1364 i = self->memo_size;
1365 while (--i >= 0) {
1366 Py_XDECREF(memo[i]);
1367 }
1368 PyMem_FREE(memo);
1369}
1370
1371static UnpicklerObject *
1372_Unpickler_New(void)
1373{
1374 UnpicklerObject *self;
1375
1376 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1377 if (self == NULL)
1378 return NULL;
1379
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001380 self->pers_func = NULL;
1381 self->input_buffer = NULL;
1382 self->input_line = NULL;
1383 self->input_len = 0;
1384 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001385 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001386 self->read = NULL;
1387 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001388 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001389 self->encoding = NULL;
1390 self->errors = NULL;
1391 self->marks = NULL;
1392 self->num_marks = 0;
1393 self->marks_size = 0;
1394 self->proto = 0;
1395 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001396 memset(&self->buffer, 0, sizeof(Py_buffer));
1397 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001398 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001399 self->memo = _Unpickler_NewMemo(self->memo_size);
1400 self->stack = (Pdata *)Pdata_New();
1401
1402 if (self->memo == NULL || self->stack == NULL) {
1403 Py_DECREF(self);
1404 return NULL;
1405 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001406
1407 return self;
1408}
1409
1410/* Returns -1 (with an exception set) on failure, 0 on success. This may
1411 be called once on a freshly created Pickler. */
1412static int
1413_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1414{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001415 _Py_IDENTIFIER(peek);
1416 _Py_IDENTIFIER(read);
1417 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001418
1419 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001420 if (self->peek == NULL) {
1421 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1422 PyErr_Clear();
1423 else
1424 return -1;
1425 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001426 self->read = _PyObject_GetAttrId(file, &PyId_read);
1427 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001428 if (self->readline == NULL || self->read == NULL) {
1429 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1430 PyErr_SetString(PyExc_TypeError,
1431 "file must have 'read' and 'readline' attributes");
1432 Py_CLEAR(self->read);
1433 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001434 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001435 return -1;
1436 }
1437 return 0;
1438}
1439
1440/* Returns -1 (with an exception set) on failure, 0 on success. This may
1441 be called once on a freshly created Pickler. */
1442static int
1443_Unpickler_SetInputEncoding(UnpicklerObject *self,
1444 const char *encoding,
1445 const char *errors)
1446{
1447 if (encoding == NULL)
1448 encoding = "ASCII";
1449 if (errors == NULL)
1450 errors = "strict";
1451
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001452 self->encoding = _PyMem_Strdup(encoding);
1453 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001454 if (self->encoding == NULL || self->errors == NULL) {
1455 PyErr_NoMemory();
1456 return -1;
1457 }
1458 return 0;
1459}
1460
1461/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001462static int
1463memo_get(PicklerObject *self, PyObject *key)
1464{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001465 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001466 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001467 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001468
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001469 value = PyMemoTable_Get(self->memo, key);
1470 if (value == NULL) {
1471 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001472 return -1;
1473 }
1474
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001475 if (!self->bin) {
1476 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001477 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1478 "%" PY_FORMAT_SIZE_T "d\n", *value);
1479 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001480 }
1481 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001482 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001484 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001485 len = 2;
1486 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001487 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001489 pdata[1] = (unsigned char)(*value & 0xff);
1490 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1491 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1492 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001493 len = 5;
1494 }
1495 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001496 PickleState *st = _Pickle_GetGlobalState();
1497 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001498 "memo id too large for LONG_BINGET");
1499 return -1;
1500 }
1501 }
1502
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001503 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001504 return -1;
1505
1506 return 0;
1507}
1508
1509/* Store an object in the memo, assign it a new unique ID based on the number
1510 of objects currently stored in the memo and generate a PUT opcode. */
1511static int
1512memo_put(PicklerObject *self, PyObject *obj)
1513{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001514 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001515 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001516 Py_ssize_t idx;
1517
1518 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001519
1520 if (self->fast)
1521 return 0;
1522
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001523 idx = PyMemoTable_Size(self->memo);
1524 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1525 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001526
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001527 if (self->proto >= 4) {
1528 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1529 return -1;
1530 return 0;
1531 }
1532 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001533 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001534 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001535 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001536 len = strlen(pdata);
1537 }
1538 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001539 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001540 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001541 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001542 len = 2;
1543 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001544 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001545 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001546 pdata[1] = (unsigned char)(idx & 0xff);
1547 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1548 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1549 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550 len = 5;
1551 }
1552 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001553 PickleState *st = _Pickle_GetGlobalState();
1554 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001555 "memo id too large for LONG_BINPUT");
1556 return -1;
1557 }
1558 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001559 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001560 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001561
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001562 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563}
1564
1565static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001566get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001567 _Py_static_string(PyId_dot, ".");
1568 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001569 PyObject *dotted_path;
1570 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001571
1572 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001573 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001574 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001575 n = PyList_GET_SIZE(dotted_path);
1576 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001577 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001578 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001579 PyObject *result = PyUnicode_RichCompare(
1580 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1581 int is_equal = (result == Py_True);
1582 assert(PyBool_Check(result));
1583 Py_DECREF(result);
1584 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001585 if (obj == NULL)
1586 PyErr_Format(PyExc_AttributeError,
1587 "Can't pickle local object %R", name);
1588 else
1589 PyErr_Format(PyExc_AttributeError,
1590 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001591 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001592 return NULL;
1593 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001594 }
1595 return dotted_path;
1596}
1597
1598static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001599get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001600{
1601 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001602 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001603
1604 assert(PyList_CheckExact(names));
1605 Py_INCREF(obj);
1606 n = PyList_GET_SIZE(names);
1607 for (i = 0; i < n; i++) {
1608 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001609 Py_XDECREF(parent);
1610 parent = obj;
1611 obj = PyObject_GetAttr(parent, name);
1612 if (obj == NULL) {
1613 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001614 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001615 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001616 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001617 if (pparent != NULL)
1618 *pparent = parent;
1619 else
1620 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001621 return obj;
1622}
1623
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001624static void
1625reformat_attribute_error(PyObject *obj, PyObject *name)
1626{
1627 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1628 PyErr_Clear();
1629 PyErr_Format(PyExc_AttributeError,
1630 "Can't get attribute %R on %R", name, obj);
1631 }
1632}
1633
1634
1635static PyObject *
1636getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1637{
1638 PyObject *dotted_path, *attr;
1639
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001640 if (allow_qualname) {
1641 dotted_path = get_dotted_path(obj, name);
1642 if (dotted_path == NULL)
1643 return NULL;
1644 attr = get_deep_attribute(obj, dotted_path, NULL);
1645 Py_DECREF(dotted_path);
1646 }
1647 else
1648 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001649 if (attr == NULL)
1650 reformat_attribute_error(obj, name);
1651 return attr;
1652}
1653
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001654static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001655whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001656{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001657 PyObject *module_name;
1658 PyObject *modules_dict;
1659 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001660 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001661 _Py_IDENTIFIER(__module__);
1662 _Py_IDENTIFIER(modules);
1663 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001664
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001665 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1666
1667 if (module_name == NULL) {
1668 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001669 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001670 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001671 }
1672 else {
1673 /* In some rare cases (e.g., bound methods of extension types),
1674 __module__ can be None. If it is so, then search sys.modules for
1675 the module of global. */
1676 if (module_name != Py_None)
1677 return module_name;
1678 Py_CLEAR(module_name);
1679 }
1680 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001681
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001682 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001683 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001684 if (modules_dict == NULL) {
1685 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001686 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001688
1689 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001690 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1691 PyObject *candidate;
1692 if (PyUnicode_Check(module_name) &&
1693 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001694 continue;
1695 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001696 continue;
1697
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001698 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001699 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001700 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001701 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001702 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001703 continue;
1704 }
1705
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001706 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001707 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001708 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001709 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001710 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001711 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001712 }
1713
1714 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001715 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001716 Py_INCREF(module_name);
1717 return module_name;
1718}
1719
1720/* fast_save_enter() and fast_save_leave() are guards against recursive
1721 objects when Pickler is used with the "fast mode" (i.e., with object
1722 memoization disabled). If the nesting of a list or dict object exceed
1723 FAST_NESTING_LIMIT, these guards will start keeping an internal
1724 reference to the seen list or dict objects and check whether these objects
1725 are recursive. These are not strictly necessary, since save() has a
1726 hard-coded recursion limit, but they give a nicer error message than the
1727 typical RuntimeError. */
1728static int
1729fast_save_enter(PicklerObject *self, PyObject *obj)
1730{
1731 /* if fast_nesting < 0, we're doing an error exit. */
1732 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1733 PyObject *key = NULL;
1734 if (self->fast_memo == NULL) {
1735 self->fast_memo = PyDict_New();
1736 if (self->fast_memo == NULL) {
1737 self->fast_nesting = -1;
1738 return 0;
1739 }
1740 }
1741 key = PyLong_FromVoidPtr(obj);
1742 if (key == NULL)
1743 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001744 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001745 Py_DECREF(key);
1746 PyErr_Format(PyExc_ValueError,
1747 "fast mode: can't pickle cyclic objects "
1748 "including object type %.200s at %p",
1749 obj->ob_type->tp_name, obj);
1750 self->fast_nesting = -1;
1751 return 0;
1752 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001753 if (PyErr_Occurred()) {
1754 return 0;
1755 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001756 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1757 Py_DECREF(key);
1758 self->fast_nesting = -1;
1759 return 0;
1760 }
1761 Py_DECREF(key);
1762 }
1763 return 1;
1764}
1765
1766static int
1767fast_save_leave(PicklerObject *self, PyObject *obj)
1768{
1769 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1770 PyObject *key = PyLong_FromVoidPtr(obj);
1771 if (key == NULL)
1772 return 0;
1773 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1774 Py_DECREF(key);
1775 return 0;
1776 }
1777 Py_DECREF(key);
1778 }
1779 return 1;
1780}
1781
1782static int
1783save_none(PicklerObject *self, PyObject *obj)
1784{
1785 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001786 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001787 return -1;
1788
1789 return 0;
1790}
1791
1792static int
1793save_bool(PicklerObject *self, PyObject *obj)
1794{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001796 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001797 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001798 return -1;
1799 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001800 else {
1801 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1802 * so that unpicklers written before bools were introduced unpickle them
1803 * as ints, but unpicklers after can recognize that bools were intended.
1804 * Note that protocol 2 added direct ways to pickle bools.
1805 */
1806 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1807 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1808 return -1;
1809 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001810 return 0;
1811}
1812
1813static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001814save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001815{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001816 PyObject *repr = NULL;
1817 Py_ssize_t size;
1818 long val;
1819 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001820
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001821 const char long_op = LONG;
1822
1823 val= PyLong_AsLong(obj);
1824 if (val == -1 && PyErr_Occurred()) {
1825 /* out of range for int pickling */
1826 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001827 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001828 else if (self->bin &&
1829 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001830 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001831 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001832
1833 Note: we can't use -0x80000000L in the above condition because some
1834 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1835 before applying the unary minus when sizeof(long) <= 4. The
1836 resulting value stays unsigned which is commonly not what we want,
1837 so MSVC happily warns us about it. However, that result would have
1838 been fine because we guard for sizeof(long) <= 4 which turns the
1839 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001840 char pdata[32];
1841 Py_ssize_t len = 0;
1842
1843 pdata[1] = (unsigned char)(val & 0xff);
1844 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1845 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1846 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001847
1848 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1849 if (pdata[2] == 0) {
1850 pdata[0] = BININT1;
1851 len = 2;
1852 }
1853 else {
1854 pdata[0] = BININT2;
1855 len = 3;
1856 }
1857 }
1858 else {
1859 pdata[0] = BININT;
1860 len = 5;
1861 }
1862
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001863 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001864 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001865
1866 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001867 }
1868
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001869 if (self->proto >= 2) {
1870 /* Linear-time pickling. */
1871 size_t nbits;
1872 size_t nbytes;
1873 unsigned char *pdata;
1874 char header[5];
1875 int i;
1876 int sign = _PyLong_Sign(obj);
1877
1878 if (sign == 0) {
1879 header[0] = LONG1;
1880 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001881 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001882 goto error;
1883 return 0;
1884 }
1885 nbits = _PyLong_NumBits(obj);
1886 if (nbits == (size_t)-1 && PyErr_Occurred())
1887 goto error;
1888 /* How many bytes do we need? There are nbits >> 3 full
1889 * bytes of data, and nbits & 7 leftover bits. If there
1890 * are any leftover bits, then we clearly need another
1891 * byte. Wnat's not so obvious is that we *probably*
1892 * need another byte even if there aren't any leftovers:
1893 * the most-significant bit of the most-significant byte
1894 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001895 * opposite of the one we need. The exception is ints
1896 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001897 * its own 256's-complement, so has the right sign bit
1898 * even without the extra byte. That's a pain to check
1899 * for in advance, though, so we always grab an extra
1900 * byte at the start, and cut it back later if possible.
1901 */
1902 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001903 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001904 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001905 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001906 goto error;
1907 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001908 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001909 if (repr == NULL)
1910 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001911 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001912 i = _PyLong_AsByteArray((PyLongObject *)obj,
1913 pdata, nbytes,
1914 1 /* little endian */ , 1 /* signed */ );
1915 if (i < 0)
1916 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001917 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001918 * needed. This is so iff the MSB is all redundant sign
1919 * bits.
1920 */
1921 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001922 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001923 pdata[nbytes - 1] == 0xff &&
1924 (pdata[nbytes - 2] & 0x80) != 0) {
1925 nbytes--;
1926 }
1927
1928 if (nbytes < 256) {
1929 header[0] = LONG1;
1930 header[1] = (unsigned char)nbytes;
1931 size = 2;
1932 }
1933 else {
1934 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001935 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001936 for (i = 1; i < 5; i++) {
1937 header[i] = (unsigned char)(size & 0xff);
1938 size >>= 8;
1939 }
1940 size = 5;
1941 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001942 if (_Pickler_Write(self, header, size) < 0 ||
1943 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 goto error;
1945 }
1946 else {
1947 char *string;
1948
Mark Dickinson8dd05142009-01-20 20:43:58 +00001949 /* proto < 2: write the repr and newline. This is quadratic-time (in
1950 the number of digits), in both directions. We add a trailing 'L'
1951 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001952
1953 repr = PyObject_Repr(obj);
1954 if (repr == NULL)
1955 goto error;
1956
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001957 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001958 if (string == NULL)
1959 goto error;
1960
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001961 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1962 _Pickler_Write(self, string, size) < 0 ||
1963 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001964 goto error;
1965 }
1966
1967 if (0) {
1968 error:
1969 status = -1;
1970 }
1971 Py_XDECREF(repr);
1972
1973 return status;
1974}
1975
1976static int
1977save_float(PicklerObject *self, PyObject *obj)
1978{
1979 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1980
1981 if (self->bin) {
1982 char pdata[9];
1983 pdata[0] = BINFLOAT;
1984 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1985 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001986 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001987 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001988 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001989 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001990 int result = -1;
1991 char *buf = NULL;
1992 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001993
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001994 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001995 goto done;
1996
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001997 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001998 if (!buf) {
1999 PyErr_NoMemory();
2000 goto done;
2001 }
2002
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002003 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002004 goto done;
2005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002006 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002007 goto done;
2008
2009 result = 0;
2010done:
2011 PyMem_Free(buf);
2012 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002013 }
2014
2015 return 0;
2016}
2017
2018static int
2019save_bytes(PicklerObject *self, PyObject *obj)
2020{
2021 if (self->proto < 3) {
2022 /* Older pickle protocols do not have an opcode for pickling bytes
2023 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002024 the __reduce__ method) to permit bytes object unpickling.
2025
2026 Here we use a hack to be compatible with Python 2. Since in Python
2027 2 'bytes' is just an alias for 'str' (which has different
2028 parameters than the actual bytes object), we use codecs.encode
2029 to create the appropriate 'str' object when unpickled using
2030 Python 2 *and* the appropriate 'bytes' object when unpickled
2031 using Python 3. Again this is a hack and we don't need to do this
2032 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002033 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002034 int status;
2035
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002036 if (PyBytes_GET_SIZE(obj) == 0) {
2037 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2038 }
2039 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002040 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002041 PyObject *unicode_str =
2042 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2043 PyBytes_GET_SIZE(obj),
2044 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002045 _Py_IDENTIFIER(latin1);
2046
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002047 if (unicode_str == NULL)
2048 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002049 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002050 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002051 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002052 Py_DECREF(unicode_str);
2053 }
2054
2055 if (reduce_value == NULL)
2056 return -1;
2057
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002058 /* save_reduce() will memoize the object automatically. */
2059 status = save_reduce(self, reduce_value, obj);
2060 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002061 return status;
2062 }
2063 else {
2064 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002065 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002066 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002067
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002068 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069 if (size < 0)
2070 return -1;
2071
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002072 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002073 header[0] = SHORT_BINBYTES;
2074 header[1] = (unsigned char)size;
2075 len = 2;
2076 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002077 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002078 header[0] = BINBYTES;
2079 header[1] = (unsigned char)(size & 0xff);
2080 header[2] = (unsigned char)((size >> 8) & 0xff);
2081 header[3] = (unsigned char)((size >> 16) & 0xff);
2082 header[4] = (unsigned char)((size >> 24) & 0xff);
2083 len = 5;
2084 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002085 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002086 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002087 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002088 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002089 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002090 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002091 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002092 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002093 return -1; /* string too large */
2094 }
2095
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002096 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002097 return -1;
2098
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002099 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002100 return -1;
2101
2102 if (memo_put(self, obj) < 0)
2103 return -1;
2104
2105 return 0;
2106 }
2107}
2108
2109/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2110 backslash and newline characters to \uXXXX escapes. */
2111static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002112raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002115 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002116 void *data;
2117 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002118 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002119
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002120 if (PyUnicode_READY(obj))
2121 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122
Victor Stinner358af132015-10-12 22:36:57 +02002123 _PyBytesWriter_Init(&writer);
2124
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002125 size = PyUnicode_GET_LENGTH(obj);
2126 data = PyUnicode_DATA(obj);
2127 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002128
Victor Stinner358af132015-10-12 22:36:57 +02002129 p = _PyBytesWriter_Alloc(&writer, size);
2130 if (p == NULL)
2131 goto error;
2132 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002133
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002134 for (i=0; i < size; i++) {
2135 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002136 /* Map 32-bit characters to '\Uxxxxxxxx' */
2137 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002138 /* -1: substract 1 preallocated byte */
2139 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2140 if (p == NULL)
2141 goto error;
2142
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002143 *p++ = '\\';
2144 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002145 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2147 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2149 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2150 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2151 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2152 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002153 }
Victor Stinner358af132015-10-12 22:36:57 +02002154 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002155 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002156 /* -1: substract 1 preallocated byte */
2157 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2158 if (p == NULL)
2159 goto error;
2160
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002161 *p++ = '\\';
2162 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002163 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2164 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2165 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2166 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002167 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002168 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002169 else
2170 *p++ = (char) ch;
2171 }
Victor Stinner358af132015-10-12 22:36:57 +02002172
2173 return _PyBytesWriter_Finish(&writer, p);
2174
2175error:
2176 _PyBytesWriter_Dealloc(&writer);
2177 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002178}
2179
2180static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002181write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2182{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002183 char header[9];
2184 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002185
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002186 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002187 if (size <= 0xff && self->proto >= 4) {
2188 header[0] = SHORT_BINUNICODE;
2189 header[1] = (unsigned char)(size & 0xff);
2190 len = 2;
2191 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002192 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002193 header[0] = BINUNICODE;
2194 header[1] = (unsigned char)(size & 0xff);
2195 header[2] = (unsigned char)((size >> 8) & 0xff);
2196 header[3] = (unsigned char)((size >> 16) & 0xff);
2197 header[4] = (unsigned char)((size >> 24) & 0xff);
2198 len = 5;
2199 }
2200 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002201 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002202 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002203 len = 9;
2204 }
2205 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002206 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002207 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002208 return -1;
2209 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002210
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002211 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002212 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002213 if (_Pickler_Write(self, data, size) < 0)
2214 return -1;
2215
2216 return 0;
2217}
2218
2219static int
2220write_unicode_binary(PicklerObject *self, PyObject *obj)
2221{
2222 PyObject *encoded = NULL;
2223 Py_ssize_t size;
2224 char *data;
2225 int r;
2226
2227 if (PyUnicode_READY(obj))
2228 return -1;
2229
2230 data = PyUnicode_AsUTF8AndSize(obj, &size);
2231 if (data != NULL)
2232 return write_utf8(self, data, size);
2233
2234 /* Issue #8383: for strings with lone surrogates, fallback on the
2235 "surrogatepass" error handler. */
2236 PyErr_Clear();
2237 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2238 if (encoded == NULL)
2239 return -1;
2240
2241 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2242 PyBytes_GET_SIZE(encoded));
2243 Py_DECREF(encoded);
2244 return r;
2245}
2246
2247static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002248save_unicode(PicklerObject *self, PyObject *obj)
2249{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002250 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002251 if (write_unicode_binary(self, obj) < 0)
2252 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002253 }
2254 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002255 PyObject *encoded;
2256 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257 const char unicode_op = UNICODE;
2258
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002259 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002260 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002261 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262
Antoine Pitrou299978d2013-04-07 17:38:11 +02002263 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2264 Py_DECREF(encoded);
2265 return -1;
2266 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002267
2268 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002269 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2270 Py_DECREF(encoded);
2271 return -1;
2272 }
2273 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002274
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002275 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002276 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277 }
2278 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002279 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282}
2283
2284/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2285static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002286store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002287{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002288 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289
2290 assert(PyTuple_Size(t) == len);
2291
2292 for (i = 0; i < len; i++) {
2293 PyObject *element = PyTuple_GET_ITEM(t, i);
2294
2295 if (element == NULL)
2296 return -1;
2297 if (save(self, element, 0) < 0)
2298 return -1;
2299 }
2300
2301 return 0;
2302}
2303
2304/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2305 * used across protocols to minimize the space needed to pickle them.
2306 * Tuples are also the only builtin immutable type that can be recursive
2307 * (a tuple can be reached from itself), and that requires some subtle
2308 * magic so that it works in all cases. IOW, this is a long routine.
2309 */
2310static int
2311save_tuple(PicklerObject *self, PyObject *obj)
2312{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002313 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002314
2315 const char mark_op = MARK;
2316 const char tuple_op = TUPLE;
2317 const char pop_op = POP;
2318 const char pop_mark_op = POP_MARK;
2319 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2320
2321 if ((len = PyTuple_Size(obj)) < 0)
2322 return -1;
2323
2324 if (len == 0) {
2325 char pdata[2];
2326
2327 if (self->proto) {
2328 pdata[0] = EMPTY_TUPLE;
2329 len = 1;
2330 }
2331 else {
2332 pdata[0] = MARK;
2333 pdata[1] = TUPLE;
2334 len = 2;
2335 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002337 return -1;
2338 return 0;
2339 }
2340
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002341 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002342 * saving the tuple elements, the tuple must be recursive, in
2343 * which case we'll pop everything we put on the stack, and fetch
2344 * its value from the memo.
2345 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002346 if (len <= 3 && self->proto >= 2) {
2347 /* Use TUPLE{1,2,3} opcodes. */
2348 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002349 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002351 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002352 /* pop the len elements */
2353 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002354 if (_Pickler_Write(self, &pop_op, 1) < 0)
2355 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002356 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002357 if (memo_get(self, obj) < 0)
2358 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002360 return 0;
2361 }
2362 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002363 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2364 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002365 }
2366 goto memoize;
2367 }
2368
2369 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2370 * Generate MARK e1 e2 ... TUPLE
2371 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002372 if (_Pickler_Write(self, &mark_op, 1) < 0)
2373 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374
2375 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002376 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002377
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002378 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002379 /* pop the stack stuff we pushed */
2380 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002381 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2382 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002383 }
2384 else {
2385 /* Note that we pop one more than len, to remove
2386 * the MARK too.
2387 */
2388 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002389 if (_Pickler_Write(self, &pop_op, 1) < 0)
2390 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002391 }
2392 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002393 if (memo_get(self, obj) < 0)
2394 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002395
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002396 return 0;
2397 }
2398 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002399 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2400 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002401 }
2402
2403 memoize:
2404 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002405 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002406
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002407 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002408}
2409
2410/* iter is an iterator giving items, and we batch up chunks of
2411 * MARK item item ... item APPENDS
2412 * opcode sequences. Calling code should have arranged to first create an
2413 * empty list, or list-like object, for the APPENDS to operate on.
2414 * Returns 0 on success, <0 on error.
2415 */
2416static int
2417batch_list(PicklerObject *self, PyObject *iter)
2418{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002419 PyObject *obj = NULL;
2420 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002421 int i, n;
2422
2423 const char mark_op = MARK;
2424 const char append_op = APPEND;
2425 const char appends_op = APPENDS;
2426
2427 assert(iter != NULL);
2428
2429 /* XXX: I think this function could be made faster by avoiding the
2430 iterator interface and fetching objects directly from list using
2431 PyList_GET_ITEM.
2432 */
2433
2434 if (self->proto == 0) {
2435 /* APPENDS isn't available; do one at a time. */
2436 for (;;) {
2437 obj = PyIter_Next(iter);
2438 if (obj == NULL) {
2439 if (PyErr_Occurred())
2440 return -1;
2441 break;
2442 }
2443 i = save(self, obj, 0);
2444 Py_DECREF(obj);
2445 if (i < 0)
2446 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002447 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002448 return -1;
2449 }
2450 return 0;
2451 }
2452
2453 /* proto > 0: write in batches of BATCHSIZE. */
2454 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002455 /* Get first item */
2456 firstitem = PyIter_Next(iter);
2457 if (firstitem == NULL) {
2458 if (PyErr_Occurred())
2459 goto error;
2460
2461 /* nothing more to add */
2462 break;
2463 }
2464
2465 /* Try to get a second item */
2466 obj = PyIter_Next(iter);
2467 if (obj == NULL) {
2468 if (PyErr_Occurred())
2469 goto error;
2470
2471 /* Only one item to write */
2472 if (save(self, firstitem, 0) < 0)
2473 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002474 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002475 goto error;
2476 Py_CLEAR(firstitem);
2477 break;
2478 }
2479
2480 /* More than one item to write */
2481
2482 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002483 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002484 goto error;
2485
2486 if (save(self, firstitem, 0) < 0)
2487 goto error;
2488 Py_CLEAR(firstitem);
2489 n = 1;
2490
2491 /* Fetch and save up to BATCHSIZE items */
2492 while (obj) {
2493 if (save(self, obj, 0) < 0)
2494 goto error;
2495 Py_CLEAR(obj);
2496 n += 1;
2497
2498 if (n == BATCHSIZE)
2499 break;
2500
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002501 obj = PyIter_Next(iter);
2502 if (obj == NULL) {
2503 if (PyErr_Occurred())
2504 goto error;
2505 break;
2506 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002507 }
2508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002509 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002510 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002511
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002512 } while (n == BATCHSIZE);
2513 return 0;
2514
2515 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002516 Py_XDECREF(firstitem);
2517 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002518 return -1;
2519}
2520
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002521/* This is a variant of batch_list() above, specialized for lists (with no
2522 * support for list subclasses). Like batch_list(), we batch up chunks of
2523 * MARK item item ... item APPENDS
2524 * opcode sequences. Calling code should have arranged to first create an
2525 * empty list, or list-like object, for the APPENDS to operate on.
2526 * Returns 0 on success, -1 on error.
2527 *
2528 * This version is considerably faster than batch_list(), if less general.
2529 *
2530 * Note that this only works for protocols > 0.
2531 */
2532static int
2533batch_list_exact(PicklerObject *self, PyObject *obj)
2534{
2535 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002536 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002537
2538 const char append_op = APPEND;
2539 const char appends_op = APPENDS;
2540 const char mark_op = MARK;
2541
2542 assert(obj != NULL);
2543 assert(self->proto > 0);
2544 assert(PyList_CheckExact(obj));
2545
2546 if (PyList_GET_SIZE(obj) == 1) {
2547 item = PyList_GET_ITEM(obj, 0);
2548 if (save(self, item, 0) < 0)
2549 return -1;
2550 if (_Pickler_Write(self, &append_op, 1) < 0)
2551 return -1;
2552 return 0;
2553 }
2554
2555 /* Write in batches of BATCHSIZE. */
2556 total = 0;
2557 do {
2558 this_batch = 0;
2559 if (_Pickler_Write(self, &mark_op, 1) < 0)
2560 return -1;
2561 while (total < PyList_GET_SIZE(obj)) {
2562 item = PyList_GET_ITEM(obj, total);
2563 if (save(self, item, 0) < 0)
2564 return -1;
2565 total++;
2566 if (++this_batch == BATCHSIZE)
2567 break;
2568 }
2569 if (_Pickler_Write(self, &appends_op, 1) < 0)
2570 return -1;
2571
2572 } while (total < PyList_GET_SIZE(obj));
2573
2574 return 0;
2575}
2576
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002577static int
2578save_list(PicklerObject *self, PyObject *obj)
2579{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002580 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002581 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002582 int status = 0;
2583
2584 if (self->fast && !fast_save_enter(self, obj))
2585 goto error;
2586
2587 /* Create an empty list. */
2588 if (self->bin) {
2589 header[0] = EMPTY_LIST;
2590 len = 1;
2591 }
2592 else {
2593 header[0] = MARK;
2594 header[1] = LIST;
2595 len = 2;
2596 }
2597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002598 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002599 goto error;
2600
2601 /* Get list length, and bow out early if empty. */
2602 if ((len = PyList_Size(obj)) < 0)
2603 goto error;
2604
2605 if (memo_put(self, obj) < 0)
2606 goto error;
2607
2608 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002609 /* Materialize the list elements. */
2610 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002611 if (Py_EnterRecursiveCall(" while pickling an object"))
2612 goto error;
2613 status = batch_list_exact(self, obj);
2614 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002615 } else {
2616 PyObject *iter = PyObject_GetIter(obj);
2617 if (iter == NULL)
2618 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002619
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002620 if (Py_EnterRecursiveCall(" while pickling an object")) {
2621 Py_DECREF(iter);
2622 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002623 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002624 status = batch_list(self, iter);
2625 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002626 Py_DECREF(iter);
2627 }
2628 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002629 if (0) {
2630 error:
2631 status = -1;
2632 }
2633
2634 if (self->fast && !fast_save_leave(self, obj))
2635 status = -1;
2636
2637 return status;
2638}
2639
2640/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2641 * MARK key value ... key value SETITEMS
2642 * opcode sequences. Calling code should have arranged to first create an
2643 * empty dict, or dict-like object, for the SETITEMS to operate on.
2644 * Returns 0 on success, <0 on error.
2645 *
2646 * This is very much like batch_list(). The difference between saving
2647 * elements directly, and picking apart two-tuples, is so long-winded at
2648 * the C level, though, that attempts to combine these routines were too
2649 * ugly to bear.
2650 */
2651static int
2652batch_dict(PicklerObject *self, PyObject *iter)
2653{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002654 PyObject *obj = NULL;
2655 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002656 int i, n;
2657
2658 const char mark_op = MARK;
2659 const char setitem_op = SETITEM;
2660 const char setitems_op = SETITEMS;
2661
2662 assert(iter != NULL);
2663
2664 if (self->proto == 0) {
2665 /* SETITEMS isn't available; do one at a time. */
2666 for (;;) {
2667 obj = PyIter_Next(iter);
2668 if (obj == NULL) {
2669 if (PyErr_Occurred())
2670 return -1;
2671 break;
2672 }
2673 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2674 PyErr_SetString(PyExc_TypeError, "dict items "
2675 "iterator must return 2-tuples");
2676 return -1;
2677 }
2678 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2679 if (i >= 0)
2680 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2681 Py_DECREF(obj);
2682 if (i < 0)
2683 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002684 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002685 return -1;
2686 }
2687 return 0;
2688 }
2689
2690 /* proto > 0: write in batches of BATCHSIZE. */
2691 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002692 /* Get first item */
2693 firstitem = PyIter_Next(iter);
2694 if (firstitem == NULL) {
2695 if (PyErr_Occurred())
2696 goto error;
2697
2698 /* nothing more to add */
2699 break;
2700 }
2701 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2702 PyErr_SetString(PyExc_TypeError, "dict items "
2703 "iterator must return 2-tuples");
2704 goto error;
2705 }
2706
2707 /* Try to get a second item */
2708 obj = PyIter_Next(iter);
2709 if (obj == NULL) {
2710 if (PyErr_Occurred())
2711 goto error;
2712
2713 /* Only one item to write */
2714 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2715 goto error;
2716 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2717 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002718 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002719 goto error;
2720 Py_CLEAR(firstitem);
2721 break;
2722 }
2723
2724 /* More than one item to write */
2725
2726 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002727 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002728 goto error;
2729
2730 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2731 goto error;
2732 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2733 goto error;
2734 Py_CLEAR(firstitem);
2735 n = 1;
2736
2737 /* Fetch and save up to BATCHSIZE items */
2738 while (obj) {
2739 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2740 PyErr_SetString(PyExc_TypeError, "dict items "
2741 "iterator must return 2-tuples");
2742 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002743 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002744 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2745 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2746 goto error;
2747 Py_CLEAR(obj);
2748 n += 1;
2749
2750 if (n == BATCHSIZE)
2751 break;
2752
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002753 obj = PyIter_Next(iter);
2754 if (obj == NULL) {
2755 if (PyErr_Occurred())
2756 goto error;
2757 break;
2758 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002759 }
2760
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002761 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002762 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002763
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002764 } while (n == BATCHSIZE);
2765 return 0;
2766
2767 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002768 Py_XDECREF(firstitem);
2769 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002770 return -1;
2771}
2772
Collin Winter5c9b02d2009-05-25 05:43:30 +00002773/* This is a variant of batch_dict() above that specializes for dicts, with no
2774 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2775 * MARK key value ... key value SETITEMS
2776 * opcode sequences. Calling code should have arranged to first create an
2777 * empty dict, or dict-like object, for the SETITEMS to operate on.
2778 * Returns 0 on success, -1 on error.
2779 *
2780 * Note that this currently doesn't work for protocol 0.
2781 */
2782static int
2783batch_dict_exact(PicklerObject *self, PyObject *obj)
2784{
2785 PyObject *key = NULL, *value = NULL;
2786 int i;
2787 Py_ssize_t dict_size, ppos = 0;
2788
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002789 const char mark_op = MARK;
2790 const char setitem_op = SETITEM;
2791 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002792
2793 assert(obj != NULL);
2794 assert(self->proto > 0);
2795
2796 dict_size = PyDict_Size(obj);
2797
2798 /* Special-case len(d) == 1 to save space. */
2799 if (dict_size == 1) {
2800 PyDict_Next(obj, &ppos, &key, &value);
2801 if (save(self, key, 0) < 0)
2802 return -1;
2803 if (save(self, value, 0) < 0)
2804 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002805 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002806 return -1;
2807 return 0;
2808 }
2809
2810 /* Write in batches of BATCHSIZE. */
2811 do {
2812 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002813 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002814 return -1;
2815 while (PyDict_Next(obj, &ppos, &key, &value)) {
2816 if (save(self, key, 0) < 0)
2817 return -1;
2818 if (save(self, value, 0) < 0)
2819 return -1;
2820 if (++i == BATCHSIZE)
2821 break;
2822 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002823 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002824 return -1;
2825 if (PyDict_Size(obj) != dict_size) {
2826 PyErr_Format(
2827 PyExc_RuntimeError,
2828 "dictionary changed size during iteration");
2829 return -1;
2830 }
2831
2832 } while (i == BATCHSIZE);
2833 return 0;
2834}
2835
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002836static int
2837save_dict(PicklerObject *self, PyObject *obj)
2838{
2839 PyObject *items, *iter;
2840 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002841 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002842 int status = 0;
2843
2844 if (self->fast && !fast_save_enter(self, obj))
2845 goto error;
2846
2847 /* Create an empty dict. */
2848 if (self->bin) {
2849 header[0] = EMPTY_DICT;
2850 len = 1;
2851 }
2852 else {
2853 header[0] = MARK;
2854 header[1] = DICT;
2855 len = 2;
2856 }
2857
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002858 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002859 goto error;
2860
2861 /* Get dict size, and bow out early if empty. */
2862 if ((len = PyDict_Size(obj)) < 0)
2863 goto error;
2864
2865 if (memo_put(self, obj) < 0)
2866 goto error;
2867
2868 if (len != 0) {
2869 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002870 if (PyDict_CheckExact(obj) && self->proto > 0) {
2871 /* We can take certain shortcuts if we know this is a dict and
2872 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002873 if (Py_EnterRecursiveCall(" while pickling an object"))
2874 goto error;
2875 status = batch_dict_exact(self, obj);
2876 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002877 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002878 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002879
2880 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002881 if (items == NULL)
2882 goto error;
2883 iter = PyObject_GetIter(items);
2884 Py_DECREF(items);
2885 if (iter == NULL)
2886 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002887 if (Py_EnterRecursiveCall(" while pickling an object")) {
2888 Py_DECREF(iter);
2889 goto error;
2890 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002891 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002892 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002893 Py_DECREF(iter);
2894 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002895 }
2896
2897 if (0) {
2898 error:
2899 status = -1;
2900 }
2901
2902 if (self->fast && !fast_save_leave(self, obj))
2903 status = -1;
2904
2905 return status;
2906}
2907
2908static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002909save_set(PicklerObject *self, PyObject *obj)
2910{
2911 PyObject *item;
2912 int i;
2913 Py_ssize_t set_size, ppos = 0;
2914 Py_hash_t hash;
2915
2916 const char empty_set_op = EMPTY_SET;
2917 const char mark_op = MARK;
2918 const char additems_op = ADDITEMS;
2919
2920 if (self->proto < 4) {
2921 PyObject *items;
2922 PyObject *reduce_value;
2923 int status;
2924
2925 items = PySequence_List(obj);
2926 if (items == NULL) {
2927 return -1;
2928 }
2929 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2930 Py_DECREF(items);
2931 if (reduce_value == NULL) {
2932 return -1;
2933 }
2934 /* save_reduce() will memoize the object automatically. */
2935 status = save_reduce(self, reduce_value, obj);
2936 Py_DECREF(reduce_value);
2937 return status;
2938 }
2939
2940 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2941 return -1;
2942
2943 if (memo_put(self, obj) < 0)
2944 return -1;
2945
2946 set_size = PySet_GET_SIZE(obj);
2947 if (set_size == 0)
2948 return 0; /* nothing to do */
2949
2950 /* Write in batches of BATCHSIZE. */
2951 do {
2952 i = 0;
2953 if (_Pickler_Write(self, &mark_op, 1) < 0)
2954 return -1;
2955 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2956 if (save(self, item, 0) < 0)
2957 return -1;
2958 if (++i == BATCHSIZE)
2959 break;
2960 }
2961 if (_Pickler_Write(self, &additems_op, 1) < 0)
2962 return -1;
2963 if (PySet_GET_SIZE(obj) != set_size) {
2964 PyErr_Format(
2965 PyExc_RuntimeError,
2966 "set changed size during iteration");
2967 return -1;
2968 }
2969 } while (i == BATCHSIZE);
2970
2971 return 0;
2972}
2973
2974static int
2975save_frozenset(PicklerObject *self, PyObject *obj)
2976{
2977 PyObject *iter;
2978
2979 const char mark_op = MARK;
2980 const char frozenset_op = FROZENSET;
2981
2982 if (self->fast && !fast_save_enter(self, obj))
2983 return -1;
2984
2985 if (self->proto < 4) {
2986 PyObject *items;
2987 PyObject *reduce_value;
2988 int status;
2989
2990 items = PySequence_List(obj);
2991 if (items == NULL) {
2992 return -1;
2993 }
2994 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2995 items);
2996 Py_DECREF(items);
2997 if (reduce_value == NULL) {
2998 return -1;
2999 }
3000 /* save_reduce() will memoize the object automatically. */
3001 status = save_reduce(self, reduce_value, obj);
3002 Py_DECREF(reduce_value);
3003 return status;
3004 }
3005
3006 if (_Pickler_Write(self, &mark_op, 1) < 0)
3007 return -1;
3008
3009 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003010 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003011 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003012 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003013 for (;;) {
3014 PyObject *item;
3015
3016 item = PyIter_Next(iter);
3017 if (item == NULL) {
3018 if (PyErr_Occurred()) {
3019 Py_DECREF(iter);
3020 return -1;
3021 }
3022 break;
3023 }
3024 if (save(self, item, 0) < 0) {
3025 Py_DECREF(item);
3026 Py_DECREF(iter);
3027 return -1;
3028 }
3029 Py_DECREF(item);
3030 }
3031 Py_DECREF(iter);
3032
3033 /* If the object is already in the memo, this means it is
3034 recursive. In this case, throw away everything we put on the
3035 stack, and fetch the object back from the memo. */
3036 if (PyMemoTable_Get(self->memo, obj)) {
3037 const char pop_mark_op = POP_MARK;
3038
3039 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3040 return -1;
3041 if (memo_get(self, obj) < 0)
3042 return -1;
3043 return 0;
3044 }
3045
3046 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3047 return -1;
3048 if (memo_put(self, obj) < 0)
3049 return -1;
3050
3051 return 0;
3052}
3053
3054static int
3055fix_imports(PyObject **module_name, PyObject **global_name)
3056{
3057 PyObject *key;
3058 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003059 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003060
3061 key = PyTuple_Pack(2, *module_name, *global_name);
3062 if (key == NULL)
3063 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003064 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003065 Py_DECREF(key);
3066 if (item) {
3067 PyObject *fixed_module_name;
3068 PyObject *fixed_global_name;
3069
3070 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3071 PyErr_Format(PyExc_RuntimeError,
3072 "_compat_pickle.REVERSE_NAME_MAPPING values "
3073 "should be 2-tuples, not %.200s",
3074 Py_TYPE(item)->tp_name);
3075 return -1;
3076 }
3077 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3078 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3079 if (!PyUnicode_Check(fixed_module_name) ||
3080 !PyUnicode_Check(fixed_global_name)) {
3081 PyErr_Format(PyExc_RuntimeError,
3082 "_compat_pickle.REVERSE_NAME_MAPPING values "
3083 "should be pairs of str, not (%.200s, %.200s)",
3084 Py_TYPE(fixed_module_name)->tp_name,
3085 Py_TYPE(fixed_global_name)->tp_name);
3086 return -1;
3087 }
3088
3089 Py_CLEAR(*module_name);
3090 Py_CLEAR(*global_name);
3091 Py_INCREF(fixed_module_name);
3092 Py_INCREF(fixed_global_name);
3093 *module_name = fixed_module_name;
3094 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003095 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003096 }
3097 else if (PyErr_Occurred()) {
3098 return -1;
3099 }
3100
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003101 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003102 if (item) {
3103 if (!PyUnicode_Check(item)) {
3104 PyErr_Format(PyExc_RuntimeError,
3105 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3106 "should be strings, not %.200s",
3107 Py_TYPE(item)->tp_name);
3108 return -1;
3109 }
3110 Py_CLEAR(*module_name);
3111 Py_INCREF(item);
3112 *module_name = item;
3113 }
3114 else if (PyErr_Occurred()) {
3115 return -1;
3116 }
3117
3118 return 0;
3119}
3120
3121static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003122save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3123{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003124 PyObject *global_name = NULL;
3125 PyObject *module_name = NULL;
3126 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003127 PyObject *parent = NULL;
3128 PyObject *dotted_path = NULL;
3129 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003130 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003131 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003132 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003133 _Py_IDENTIFIER(__name__);
3134 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135
3136 const char global_op = GLOBAL;
3137
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003138 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003139 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003140 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003141 }
3142 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003143 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3144 if (global_name == NULL) {
3145 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3146 goto error;
3147 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003148 }
3149 if (global_name == NULL) {
3150 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3151 if (global_name == NULL)
3152 goto error;
3153 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003154 }
3155
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003156 dotted_path = get_dotted_path(module, global_name);
3157 if (dotted_path == NULL)
3158 goto error;
3159 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003160 if (module_name == NULL)
3161 goto error;
3162
3163 /* XXX: Change to use the import C API directly with level=0 to disallow
3164 relative imports.
3165
3166 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3167 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3168 custom import functions (IMHO, this would be a nice security
3169 feature). The import C API would need to be extended to support the
3170 extra parameters of __import__ to fix that. */
3171 module = PyImport_Import(module_name);
3172 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003173 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003174 "Can't pickle %R: import of module %R failed",
3175 obj, module_name);
3176 goto error;
3177 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003178 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3179 Py_INCREF(lastname);
3180 cls = get_deep_attribute(module, dotted_path, &parent);
3181 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003182 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003183 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003184 "Can't pickle %R: attribute lookup %S on %S failed",
3185 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003186 goto error;
3187 }
3188 if (cls != obj) {
3189 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003190 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003191 "Can't pickle %R: it's not the same object as %S.%S",
3192 obj, module_name, global_name);
3193 goto error;
3194 }
3195 Py_DECREF(cls);
3196
3197 if (self->proto >= 2) {
3198 /* See whether this is in the extension registry, and if
3199 * so generate an EXT opcode.
3200 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003201 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003202 PyObject *code_obj; /* extension code as Python object */
3203 long code; /* extension code as C value */
3204 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003205 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003206
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003207 extension_key = PyTuple_Pack(2, module_name, global_name);
3208 if (extension_key == NULL) {
3209 goto error;
3210 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003211 code_obj = PyDict_GetItemWithError(st->extension_registry,
3212 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003213 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003214 /* The object is not registered in the extension registry.
3215 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003216 if (code_obj == NULL) {
3217 if (PyErr_Occurred()) {
3218 goto error;
3219 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003220 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003221 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003222
3223 /* XXX: pickle.py doesn't check neither the type, nor the range
3224 of the value returned by the extension_registry. It should for
3225 consistency. */
3226
3227 /* Verify code_obj has the right type and value. */
3228 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003229 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003230 "Can't pickle %R: extension code %R isn't an integer",
3231 obj, code_obj);
3232 goto error;
3233 }
3234 code = PyLong_AS_LONG(code_obj);
3235 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003236 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003237 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3238 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003239 goto error;
3240 }
3241
3242 /* Generate an EXT opcode. */
3243 if (code <= 0xff) {
3244 pdata[0] = EXT1;
3245 pdata[1] = (unsigned char)code;
3246 n = 2;
3247 }
3248 else if (code <= 0xffff) {
3249 pdata[0] = EXT2;
3250 pdata[1] = (unsigned char)(code & 0xff);
3251 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3252 n = 3;
3253 }
3254 else {
3255 pdata[0] = EXT4;
3256 pdata[1] = (unsigned char)(code & 0xff);
3257 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3258 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3259 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3260 n = 5;
3261 }
3262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003263 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003264 goto error;
3265 }
3266 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003267 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003268 if (parent == module) {
3269 Py_INCREF(lastname);
3270 Py_DECREF(global_name);
3271 global_name = lastname;
3272 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003273 if (self->proto >= 4) {
3274 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003275
Christian Heimese8b1ba12013-11-23 21:13:39 +01003276 if (save(self, module_name, 0) < 0)
3277 goto error;
3278 if (save(self, global_name, 0) < 0)
3279 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003280
3281 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3282 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003283 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003284 else if (parent != module) {
3285 PickleState *st = _Pickle_GetGlobalState();
3286 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3287 st->getattr, parent, lastname);
3288 status = save_reduce(self, reduce_value, NULL);
3289 Py_DECREF(reduce_value);
3290 if (status < 0)
3291 goto error;
3292 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003293 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003294 /* Generate a normal global opcode if we are using a pickle
3295 protocol < 4, or if the object is not registered in the
3296 extension registry. */
3297 PyObject *encoded;
3298 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003299
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003300 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003301 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003302
3303 /* For protocol < 3 and if the user didn't request against doing
3304 so, we convert module names to the old 2.x module names. */
3305 if (self->proto < 3 && self->fix_imports) {
3306 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003307 goto error;
3308 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003309 }
3310
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003311 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3312 both the module name and the global name using UTF-8. We do so
3313 only when we are using the pickle protocol newer than version
3314 3. This is to ensure compatibility with older Unpickler running
3315 on Python 2.x. */
3316 if (self->proto == 3) {
3317 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003318 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003319 else {
3320 unicode_encoder = PyUnicode_AsASCIIString;
3321 }
3322 encoded = unicode_encoder(module_name);
3323 if (encoded == NULL) {
3324 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003325 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003326 "can't pickle module identifier '%S' using "
3327 "pickle protocol %i",
3328 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003329 goto error;
3330 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003331 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3332 PyBytes_GET_SIZE(encoded)) < 0) {
3333 Py_DECREF(encoded);
3334 goto error;
3335 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003336 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003337 if(_Pickler_Write(self, "\n", 1) < 0)
3338 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003339
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340 /* Save the name of the module. */
3341 encoded = unicode_encoder(global_name);
3342 if (encoded == NULL) {
3343 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003344 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003345 "can't pickle global identifier '%S' using "
3346 "pickle protocol %i",
3347 global_name, self->proto);
3348 goto error;
3349 }
3350 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3351 PyBytes_GET_SIZE(encoded)) < 0) {
3352 Py_DECREF(encoded);
3353 goto error;
3354 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003355 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003356 if (_Pickler_Write(self, "\n", 1) < 0)
3357 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003359 /* Memoize the object. */
3360 if (memo_put(self, obj) < 0)
3361 goto error;
3362 }
3363
3364 if (0) {
3365 error:
3366 status = -1;
3367 }
3368 Py_XDECREF(module_name);
3369 Py_XDECREF(global_name);
3370 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003371 Py_XDECREF(parent);
3372 Py_XDECREF(dotted_path);
3373 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003374
3375 return status;
3376}
3377
3378static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003379save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3380{
3381 PyObject *reduce_value;
3382 int status;
3383
3384 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3385 if (reduce_value == NULL) {
3386 return -1;
3387 }
3388 status = save_reduce(self, reduce_value, obj);
3389 Py_DECREF(reduce_value);
3390 return status;
3391}
3392
3393static int
3394save_type(PicklerObject *self, PyObject *obj)
3395{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003396 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003397 return save_singleton_type(self, obj, Py_None);
3398 }
3399 else if (obj == (PyObject *)&PyEllipsis_Type) {
3400 return save_singleton_type(self, obj, Py_Ellipsis);
3401 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003402 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003403 return save_singleton_type(self, obj, Py_NotImplemented);
3404 }
3405 return save_global(self, obj, NULL);
3406}
3407
3408static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003409save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3410{
3411 PyObject *pid = NULL;
3412 int status = 0;
3413
3414 const char persid_op = PERSID;
3415 const char binpersid_op = BINPERSID;
3416
3417 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003418 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003419 if (pid == NULL)
3420 return -1;
3421
3422 if (pid != Py_None) {
3423 if (self->bin) {
3424 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003425 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003426 goto error;
3427 }
3428 else {
3429 PyObject *pid_str = NULL;
3430 char *pid_ascii_bytes;
3431 Py_ssize_t size;
3432
3433 pid_str = PyObject_Str(pid);
3434 if (pid_str == NULL)
3435 goto error;
3436
3437 /* XXX: Should it check whether the persistent id only contains
3438 ASCII characters? And what if the pid contains embedded
3439 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003440 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003441 Py_DECREF(pid_str);
3442 if (pid_ascii_bytes == NULL)
3443 goto error;
3444
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003445 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3446 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3447 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003448 goto error;
3449 }
3450 status = 1;
3451 }
3452
3453 if (0) {
3454 error:
3455 status = -1;
3456 }
3457 Py_XDECREF(pid);
3458
3459 return status;
3460}
3461
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003462static PyObject *
3463get_class(PyObject *obj)
3464{
3465 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003466 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003467
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003468 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003469 if (cls == NULL) {
3470 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3471 PyErr_Clear();
3472 cls = (PyObject *) Py_TYPE(obj);
3473 Py_INCREF(cls);
3474 }
3475 }
3476 return cls;
3477}
3478
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003479/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3480 * appropriate __reduce__ method for obj.
3481 */
3482static int
3483save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3484{
3485 PyObject *callable;
3486 PyObject *argtup;
3487 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003488 PyObject *listitems = Py_None;
3489 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003490 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003491 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003492 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003493
3494 const char reduce_op = REDUCE;
3495 const char build_op = BUILD;
3496 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003497 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003498
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003499 size = PyTuple_Size(args);
3500 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003501 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003502 "__reduce__ must contain 2 through 5 elements");
3503 return -1;
3504 }
3505
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003506 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3507 &callable, &argtup, &state, &listitems, &dictitems))
3508 return -1;
3509
3510 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003512 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003513 return -1;
3514 }
3515 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003516 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003517 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003518 return -1;
3519 }
3520
3521 if (state == Py_None)
3522 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003523
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003524 if (listitems == Py_None)
3525 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003526 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003527 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003528 "returned by __reduce__ must be an iterator, not %s",
3529 Py_TYPE(listitems)->tp_name);
3530 return -1;
3531 }
3532
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003533 if (dictitems == Py_None)
3534 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003536 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003537 "returned by __reduce__ must be an iterator, not %s",
3538 Py_TYPE(dictitems)->tp_name);
3539 return -1;
3540 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003541
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003542 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003543 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003544 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003545
Victor Stinner804e05e2013-11-14 01:26:17 +01003546 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003547 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003548 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003549 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003550 }
3551 PyErr_Clear();
3552 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003553 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003554 _Py_IDENTIFIER(__newobj_ex__);
3555 use_newobj_ex = PyUnicode_Compare(
3556 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003557 if (!use_newobj_ex) {
3558 _Py_IDENTIFIER(__newobj__);
3559 use_newobj = PyUnicode_Compare(
3560 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3561 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003562 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003563 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003564 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003565
3566 if (use_newobj_ex) {
3567 PyObject *cls;
3568 PyObject *args;
3569 PyObject *kwargs;
3570
3571 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003572 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003573 "length of the NEWOBJ_EX argument tuple must be "
3574 "exactly 3, not %zd", Py_SIZE(argtup));
3575 return -1;
3576 }
3577
3578 cls = PyTuple_GET_ITEM(argtup, 0);
3579 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003580 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003581 "first item from NEWOBJ_EX argument tuple must "
3582 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3583 return -1;
3584 }
3585 args = PyTuple_GET_ITEM(argtup, 1);
3586 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003587 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003588 "second item from NEWOBJ_EX argument tuple must "
3589 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3590 return -1;
3591 }
3592 kwargs = PyTuple_GET_ITEM(argtup, 2);
3593 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003594 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003595 "third item from NEWOBJ_EX argument tuple must "
3596 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3597 return -1;
3598 }
3599
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003600 if (self->proto >= 4) {
3601 if (save(self, cls, 0) < 0 ||
3602 save(self, args, 0) < 0 ||
3603 save(self, kwargs, 0) < 0 ||
3604 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3605 return -1;
3606 }
3607 }
3608 else {
3609 PyObject *newargs;
3610 PyObject *cls_new;
3611 Py_ssize_t i;
3612 _Py_IDENTIFIER(__new__);
3613
3614 newargs = PyTuple_New(Py_SIZE(args) + 2);
3615 if (newargs == NULL)
3616 return -1;
3617
3618 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3619 if (cls_new == NULL) {
3620 Py_DECREF(newargs);
3621 return -1;
3622 }
3623 PyTuple_SET_ITEM(newargs, 0, cls_new);
3624 Py_INCREF(cls);
3625 PyTuple_SET_ITEM(newargs, 1, cls);
3626 for (i = 0; i < Py_SIZE(args); i++) {
3627 PyObject *item = PyTuple_GET_ITEM(args, i);
3628 Py_INCREF(item);
3629 PyTuple_SET_ITEM(newargs, i + 2, item);
3630 }
3631
3632 callable = PyObject_Call(st->partial, newargs, kwargs);
3633 Py_DECREF(newargs);
3634 if (callable == NULL)
3635 return -1;
3636
3637 newargs = PyTuple_New(0);
3638 if (newargs == NULL) {
3639 Py_DECREF(callable);
3640 return -1;
3641 }
3642
3643 if (save(self, callable, 0) < 0 ||
3644 save(self, newargs, 0) < 0 ||
3645 _Pickler_Write(self, &reduce_op, 1) < 0) {
3646 Py_DECREF(newargs);
3647 Py_DECREF(callable);
3648 return -1;
3649 }
3650 Py_DECREF(newargs);
3651 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003652 }
3653 }
3654 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003655 PyObject *cls;
3656 PyObject *newargtup;
3657 PyObject *obj_class;
3658 int p;
3659
3660 /* Sanity checks. */
3661 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003662 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003663 return -1;
3664 }
3665
3666 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003667 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003668 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003669 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 return -1;
3671 }
3672
3673 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003674 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003675 p = obj_class != cls; /* true iff a problem */
3676 Py_DECREF(obj_class);
3677 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003678 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003679 "__newobj__ args has the wrong class");
3680 return -1;
3681 }
3682 }
3683 /* XXX: These calls save() are prone to infinite recursion. Imagine
3684 what happen if the value returned by the __reduce__() method of
3685 some extension type contains another object of the same type. Ouch!
3686
3687 Here is a quick example, that I ran into, to illustrate what I
3688 mean:
3689
3690 >>> import pickle, copyreg
3691 >>> copyreg.dispatch_table.pop(complex)
3692 >>> pickle.dumps(1+2j)
3693 Traceback (most recent call last):
3694 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003695 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003696
3697 Removing the complex class from copyreg.dispatch_table made the
3698 __reduce_ex__() method emit another complex object:
3699
3700 >>> (1+1j).__reduce_ex__(2)
3701 (<function __newobj__ at 0xb7b71c3c>,
3702 (<class 'complex'>, (1+1j)), None, None, None)
3703
3704 Thus when save() was called on newargstup (the 2nd item) recursion
3705 ensued. Of course, the bug was in the complex class which had a
3706 broken __getnewargs__() that emitted another complex object. But,
3707 the point, here, is it is quite easy to end up with a broken reduce
3708 function. */
3709
3710 /* Save the class and its __new__ arguments. */
3711 if (save(self, cls, 0) < 0)
3712 return -1;
3713
3714 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3715 if (newargtup == NULL)
3716 return -1;
3717
3718 p = save(self, newargtup, 0);
3719 Py_DECREF(newargtup);
3720 if (p < 0)
3721 return -1;
3722
3723 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003724 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003725 return -1;
3726 }
3727 else { /* Not using NEWOBJ. */
3728 if (save(self, callable, 0) < 0 ||
3729 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003730 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003731 return -1;
3732 }
3733
3734 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3735 the caller do not want to memoize the object. Not particularly useful,
3736 but that is to mimic the behavior save_reduce() in pickle.py when
3737 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003738 if (obj != NULL) {
3739 /* If the object is already in the memo, this means it is
3740 recursive. In this case, throw away everything we put on the
3741 stack, and fetch the object back from the memo. */
3742 if (PyMemoTable_Get(self->memo, obj)) {
3743 const char pop_op = POP;
3744
3745 if (_Pickler_Write(self, &pop_op, 1) < 0)
3746 return -1;
3747 if (memo_get(self, obj) < 0)
3748 return -1;
3749
3750 return 0;
3751 }
3752 else if (memo_put(self, obj) < 0)
3753 return -1;
3754 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003755
3756 if (listitems && batch_list(self, listitems) < 0)
3757 return -1;
3758
3759 if (dictitems && batch_dict(self, dictitems) < 0)
3760 return -1;
3761
3762 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003763 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003764 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003765 return -1;
3766 }
3767
3768 return 0;
3769}
3770
3771static int
3772save(PicklerObject *self, PyObject *obj, int pers_save)
3773{
3774 PyTypeObject *type;
3775 PyObject *reduce_func = NULL;
3776 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003777 int status = 0;
3778
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003779 if (_Pickler_OpcodeBoundary(self) < 0)
3780 return -1;
3781
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003782 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003783 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003784
3785 /* The extra pers_save argument is necessary to avoid calling save_pers()
3786 on its returned object. */
3787 if (!pers_save && self->pers_func) {
3788 /* save_pers() returns:
3789 -1 to signal an error;
3790 0 if it did nothing successfully;
3791 1 if a persistent id was saved.
3792 */
3793 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3794 goto done;
3795 }
3796
3797 type = Py_TYPE(obj);
3798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003799 /* The old cPickle had an optimization that used switch-case statement
3800 dispatching on the first letter of the type name. This has was removed
3801 since benchmarks shown that this optimization was actually slowing
3802 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003803
3804 /* Atom types; these aren't memoized, so don't check the memo. */
3805
3806 if (obj == Py_None) {
3807 status = save_none(self, obj);
3808 goto done;
3809 }
3810 else if (obj == Py_False || obj == Py_True) {
3811 status = save_bool(self, obj);
3812 goto done;
3813 }
3814 else if (type == &PyLong_Type) {
3815 status = save_long(self, obj);
3816 goto done;
3817 }
3818 else if (type == &PyFloat_Type) {
3819 status = save_float(self, obj);
3820 goto done;
3821 }
3822
3823 /* Check the memo to see if it has the object. If so, generate
3824 a GET (or BINGET) opcode, instead of pickling the object
3825 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003826 if (PyMemoTable_Get(self->memo, obj)) {
3827 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003828 goto error;
3829 goto done;
3830 }
3831
3832 if (type == &PyBytes_Type) {
3833 status = save_bytes(self, obj);
3834 goto done;
3835 }
3836 else if (type == &PyUnicode_Type) {
3837 status = save_unicode(self, obj);
3838 goto done;
3839 }
3840 else if (type == &PyDict_Type) {
3841 status = save_dict(self, obj);
3842 goto done;
3843 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003844 else if (type == &PySet_Type) {
3845 status = save_set(self, obj);
3846 goto done;
3847 }
3848 else if (type == &PyFrozenSet_Type) {
3849 status = save_frozenset(self, obj);
3850 goto done;
3851 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003852 else if (type == &PyList_Type) {
3853 status = save_list(self, obj);
3854 goto done;
3855 }
3856 else if (type == &PyTuple_Type) {
3857 status = save_tuple(self, obj);
3858 goto done;
3859 }
3860 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003861 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862 goto done;
3863 }
3864 else if (type == &PyFunction_Type) {
3865 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003866 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003868
3869 /* XXX: This part needs some unit tests. */
3870
3871 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003872 * self.dispatch_table, copyreg.dispatch_table, the object's
3873 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003875 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003876 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003877 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3878 (PyObject *)type);
3879 if (reduce_func == NULL) {
3880 if (PyErr_Occurred()) {
3881 goto error;
3882 }
3883 } else {
3884 /* PyDict_GetItemWithError() returns a borrowed reference.
3885 Increase the reference count to be consistent with
3886 PyObject_GetItem and _PyObject_GetAttrId used below. */
3887 Py_INCREF(reduce_func);
3888 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003889 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003890 reduce_func = PyObject_GetItem(self->dispatch_table,
3891 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003892 if (reduce_func == NULL) {
3893 if (PyErr_ExceptionMatches(PyExc_KeyError))
3894 PyErr_Clear();
3895 else
3896 goto error;
3897 }
3898 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003900 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003901 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003903 else if (PyType_IsSubtype(type, &PyType_Type)) {
3904 status = save_global(self, obj, NULL);
3905 goto done;
3906 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003908 _Py_IDENTIFIER(__reduce__);
3909 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003911
3912 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3913 automatically defined as __reduce__. While this is convenient, this
3914 make it impossible to know which method was actually called. Of
3915 course, this is not a big deal. But still, it would be nice to let
3916 the user know which method was called when something go
3917 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3918 don't actually have to check for a __reduce__ method. */
3919
3920 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003921 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922 if (reduce_func != NULL) {
3923 PyObject *proto;
3924 proto = PyLong_FromLong(self->proto);
3925 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003926 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003927 }
3928 }
3929 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003930 PickleState *st = _Pickle_GetGlobalState();
3931
3932 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003934 }
3935 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003936 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003937 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003938 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003939 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003941 PyObject *empty_tuple = PyTuple_New(0);
3942 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003943 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003944 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 }
3946 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003947 PyErr_Format(st->PicklingError,
3948 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003949 type->tp_name, obj);
3950 goto error;
3951 }
3952 }
3953 }
3954
3955 if (reduce_value == NULL)
3956 goto error;
3957
3958 if (PyUnicode_Check(reduce_value)) {
3959 status = save_global(self, obj, reduce_value);
3960 goto done;
3961 }
3962
3963 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003964 PickleState *st = _Pickle_GetGlobalState();
3965 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003966 "__reduce__ must return a string or tuple");
3967 goto error;
3968 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003969
3970 status = save_reduce(self, reduce_value, obj);
3971
3972 if (0) {
3973 error:
3974 status = -1;
3975 }
3976 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003977
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003978 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003979 Py_XDECREF(reduce_func);
3980 Py_XDECREF(reduce_value);
3981
3982 return status;
3983}
3984
3985static int
3986dump(PicklerObject *self, PyObject *obj)
3987{
3988 const char stop_op = STOP;
3989
3990 if (self->proto >= 2) {
3991 char header[2];
3992
3993 header[0] = PROTO;
3994 assert(self->proto >= 0 && self->proto < 256);
3995 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003996 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003997 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003998 if (self->proto >= 4)
3999 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004000 }
4001
4002 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004003 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 return -1;
4005
4006 return 0;
4007}
4008
Larry Hastings61272b72014-01-07 12:41:53 -08004009/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004010
4011_pickle.Pickler.clear_memo
4012
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004013Clears the pickler's "memo".
4014
4015The memo is the data structure that remembers which objects the
4016pickler has already seen, so that shared or recursive objects are
4017pickled by reference and not by value. This method is useful when
4018re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004019[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004020
Larry Hastings3cceb382014-01-04 11:09:09 -08004021static PyObject *
4022_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004023/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004024{
4025 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004026 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004027
4028 Py_RETURN_NONE;
4029}
4030
Larry Hastings61272b72014-01-07 12:41:53 -08004031/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004032
4033_pickle.Pickler.dump
4034
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004035 obj: object
4036 /
4037
4038Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004039[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004040
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004041static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004042_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004043/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004044{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004045 /* Check whether the Pickler was initialized correctly (issue3664).
4046 Developers often forget to call __init__() in their subclasses, which
4047 would trigger a segfault without this check. */
4048 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004049 PickleState *st = _Pickle_GetGlobalState();
4050 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004051 "Pickler.__init__() was not called by %s.__init__()",
4052 Py_TYPE(self)->tp_name);
4053 return NULL;
4054 }
4055
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004056 if (_Pickler_ClearBuffer(self) < 0)
4057 return NULL;
4058
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004059 if (dump(self, obj) < 0)
4060 return NULL;
4061
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004062 if (_Pickler_FlushToFile(self) < 0)
4063 return NULL;
4064
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004065 Py_RETURN_NONE;
4066}
4067
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004068/*[clinic input]
4069
4070_pickle.Pickler.__sizeof__ -> Py_ssize_t
4071
4072Returns size in memory, in bytes.
4073[clinic start generated code]*/
4074
4075static Py_ssize_t
4076_pickle_Pickler___sizeof___impl(PicklerObject *self)
4077/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4078{
4079 Py_ssize_t res, s;
4080
4081 res = sizeof(PicklerObject);
4082 if (self->memo != NULL) {
4083 res += sizeof(PyMemoTable);
4084 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4085 }
4086 if (self->output_buffer != NULL) {
4087 s = _PySys_GetSizeOf(self->output_buffer);
4088 if (s == -1)
4089 return -1;
4090 res += s;
4091 }
4092 return res;
4093}
4094
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004095static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004096 _PICKLE_PICKLER_DUMP_METHODDEF
4097 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004098 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004099 {NULL, NULL} /* sentinel */
4100};
4101
4102static void
4103Pickler_dealloc(PicklerObject *self)
4104{
4105 PyObject_GC_UnTrack(self);
4106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004107 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004109 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004110 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004111 Py_XDECREF(self->fast_memo);
4112
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004113 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004114
4115 Py_TYPE(self)->tp_free((PyObject *)self);
4116}
4117
4118static int
4119Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4120{
4121 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004122 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004123 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004124 Py_VISIT(self->fast_memo);
4125 return 0;
4126}
4127
4128static int
4129Pickler_clear(PicklerObject *self)
4130{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004131 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004133 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004134 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004135 Py_CLEAR(self->fast_memo);
4136
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004137 if (self->memo != NULL) {
4138 PyMemoTable *memo = self->memo;
4139 self->memo = NULL;
4140 PyMemoTable_Del(memo);
4141 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 return 0;
4143}
4144
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004145
Larry Hastings61272b72014-01-07 12:41:53 -08004146/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004147
4148_pickle.Pickler.__init__
4149
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004150 file: object
4151 protocol: object = NULL
4152 fix_imports: bool = True
4153
4154This takes a binary file for writing a pickle data stream.
4155
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004156The optional *protocol* argument tells the pickler to use the given
4157protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4158protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004159
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004160Specifying a negative protocol version selects the highest protocol
4161version supported. The higher the protocol used, the more recent the
4162version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004163
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004164The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004165bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004166writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004167this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004169If *fix_imports* is True and protocol is less than 3, pickle will try
4170to map the new Python 3 names to the old module names used in Python
41712, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004172[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004174static int
Larry Hastings89964c42015-04-14 18:07:59 -04004175_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4176 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004177/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004178{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004179 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004180 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004181
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004182 /* In case of multiple __init__() calls, clear previous content. */
4183 if (self->write != NULL)
4184 (void)Pickler_clear(self);
4185
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004186 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004187 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004188
4189 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004190 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004191
4192 /* memo and output_buffer may have already been created in _Pickler_New */
4193 if (self->memo == NULL) {
4194 self->memo = PyMemoTable_New();
4195 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004196 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004197 }
4198 self->output_len = 0;
4199 if (self->output_buffer == NULL) {
4200 self->max_output_len = WRITE_BUF_SIZE;
4201 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4202 self->max_output_len);
4203 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004204 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004205 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004206
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004207 self->fast = 0;
4208 self->fast_nesting = 0;
4209 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004210 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004211 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4212 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4213 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004214 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004215 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004216 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004217 self->dispatch_table = NULL;
4218 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4219 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4220 &PyId_dispatch_table);
4221 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004223 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004224
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004225 return 0;
4226}
4227
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004228
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004229/* Define a proxy object for the Pickler's internal memo object. This is to
4230 * avoid breaking code like:
4231 * pickler.memo.clear()
4232 * and
4233 * pickler.memo = saved_memo
4234 * Is this a good idea? Not really, but we don't want to break code that uses
4235 * it. Note that we don't implement the entire mapping API here. This is
4236 * intentional, as these should be treated as black-box implementation details.
4237 */
4238
Larry Hastings61272b72014-01-07 12:41:53 -08004239/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004240_pickle.PicklerMemoProxy.clear
4241
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004242Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004243[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004244
Larry Hastings3cceb382014-01-04 11:09:09 -08004245static PyObject *
4246_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004247/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004248{
4249 if (self->pickler->memo)
4250 PyMemoTable_Clear(self->pickler->memo);
4251 Py_RETURN_NONE;
4252}
4253
Larry Hastings61272b72014-01-07 12:41:53 -08004254/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004255_pickle.PicklerMemoProxy.copy
4256
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004257Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004258[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004259
Larry Hastings3cceb382014-01-04 11:09:09 -08004260static PyObject *
4261_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004262/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004263{
4264 Py_ssize_t i;
4265 PyMemoTable *memo;
4266 PyObject *new_memo = PyDict_New();
4267 if (new_memo == NULL)
4268 return NULL;
4269
4270 memo = self->pickler->memo;
4271 for (i = 0; i < memo->mt_allocated; ++i) {
4272 PyMemoEntry entry = memo->mt_table[i];
4273 if (entry.me_key != NULL) {
4274 int status;
4275 PyObject *key, *value;
4276
4277 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004278 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004279
4280 if (key == NULL || value == NULL) {
4281 Py_XDECREF(key);
4282 Py_XDECREF(value);
4283 goto error;
4284 }
4285 status = PyDict_SetItem(new_memo, key, value);
4286 Py_DECREF(key);
4287 Py_DECREF(value);
4288 if (status < 0)
4289 goto error;
4290 }
4291 }
4292 return new_memo;
4293
4294 error:
4295 Py_XDECREF(new_memo);
4296 return NULL;
4297}
4298
Larry Hastings61272b72014-01-07 12:41:53 -08004299/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004300_pickle.PicklerMemoProxy.__reduce__
4301
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004302Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004303[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004304
Larry Hastings3cceb382014-01-04 11:09:09 -08004305static PyObject *
4306_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004307/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004308{
4309 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004310 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004311 if (contents == NULL)
4312 return NULL;
4313
4314 reduce_value = PyTuple_New(2);
4315 if (reduce_value == NULL) {
4316 Py_DECREF(contents);
4317 return NULL;
4318 }
4319 dict_args = PyTuple_New(1);
4320 if (dict_args == NULL) {
4321 Py_DECREF(contents);
4322 Py_DECREF(reduce_value);
4323 return NULL;
4324 }
4325 PyTuple_SET_ITEM(dict_args, 0, contents);
4326 Py_INCREF((PyObject *)&PyDict_Type);
4327 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4328 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4329 return reduce_value;
4330}
4331
4332static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004333 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4334 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4335 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004336 {NULL, NULL} /* sentinel */
4337};
4338
4339static void
4340PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4341{
4342 PyObject_GC_UnTrack(self);
4343 Py_XDECREF(self->pickler);
4344 PyObject_GC_Del((PyObject *)self);
4345}
4346
4347static int
4348PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4349 visitproc visit, void *arg)
4350{
4351 Py_VISIT(self->pickler);
4352 return 0;
4353}
4354
4355static int
4356PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4357{
4358 Py_CLEAR(self->pickler);
4359 return 0;
4360}
4361
4362static PyTypeObject PicklerMemoProxyType = {
4363 PyVarObject_HEAD_INIT(NULL, 0)
4364 "_pickle.PicklerMemoProxy", /*tp_name*/
4365 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4366 0,
4367 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4368 0, /* tp_print */
4369 0, /* tp_getattr */
4370 0, /* tp_setattr */
4371 0, /* tp_compare */
4372 0, /* tp_repr */
4373 0, /* tp_as_number */
4374 0, /* tp_as_sequence */
4375 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004376 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004377 0, /* tp_call */
4378 0, /* tp_str */
4379 PyObject_GenericGetAttr, /* tp_getattro */
4380 PyObject_GenericSetAttr, /* tp_setattro */
4381 0, /* tp_as_buffer */
4382 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4383 0, /* tp_doc */
4384 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4385 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4386 0, /* tp_richcompare */
4387 0, /* tp_weaklistoffset */
4388 0, /* tp_iter */
4389 0, /* tp_iternext */
4390 picklerproxy_methods, /* tp_methods */
4391};
4392
4393static PyObject *
4394PicklerMemoProxy_New(PicklerObject *pickler)
4395{
4396 PicklerMemoProxyObject *self;
4397
4398 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4399 if (self == NULL)
4400 return NULL;
4401 Py_INCREF(pickler);
4402 self->pickler = pickler;
4403 PyObject_GC_Track(self);
4404 return (PyObject *)self;
4405}
4406
4407/*****************************************************************************/
4408
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004409static PyObject *
4410Pickler_get_memo(PicklerObject *self)
4411{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004412 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004413}
4414
4415static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004416Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004417{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004418 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004419
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004421 PyErr_SetString(PyExc_TypeError,
4422 "attribute deletion is not supported");
4423 return -1;
4424 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425
4426 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4427 PicklerObject *pickler =
4428 ((PicklerMemoProxyObject *)obj)->pickler;
4429
4430 new_memo = PyMemoTable_Copy(pickler->memo);
4431 if (new_memo == NULL)
4432 return -1;
4433 }
4434 else if (PyDict_Check(obj)) {
4435 Py_ssize_t i = 0;
4436 PyObject *key, *value;
4437
4438 new_memo = PyMemoTable_New();
4439 if (new_memo == NULL)
4440 return -1;
4441
4442 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004443 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004444 PyObject *memo_obj;
4445
4446 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4447 PyErr_SetString(PyExc_TypeError,
4448 "'memo' values must be 2-item tuples");
4449 goto error;
4450 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004451 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004452 if (memo_id == -1 && PyErr_Occurred())
4453 goto error;
4454 memo_obj = PyTuple_GET_ITEM(value, 1);
4455 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4456 goto error;
4457 }
4458 }
4459 else {
4460 PyErr_Format(PyExc_TypeError,
4461 "'memo' attribute must be an PicklerMemoProxy object"
4462 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463 return -1;
4464 }
4465
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004466 PyMemoTable_Del(self->memo);
4467 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004468
4469 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004470
4471 error:
4472 if (new_memo)
4473 PyMemoTable_Del(new_memo);
4474 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004475}
4476
4477static PyObject *
4478Pickler_get_persid(PicklerObject *self)
4479{
4480 if (self->pers_func == NULL)
4481 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4482 else
4483 Py_INCREF(self->pers_func);
4484 return self->pers_func;
4485}
4486
4487static int
4488Pickler_set_persid(PicklerObject *self, PyObject *value)
4489{
4490 PyObject *tmp;
4491
4492 if (value == NULL) {
4493 PyErr_SetString(PyExc_TypeError,
4494 "attribute deletion is not supported");
4495 return -1;
4496 }
4497 if (!PyCallable_Check(value)) {
4498 PyErr_SetString(PyExc_TypeError,
4499 "persistent_id must be a callable taking one argument");
4500 return -1;
4501 }
4502
4503 tmp = self->pers_func;
4504 Py_INCREF(value);
4505 self->pers_func = value;
4506 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4507
4508 return 0;
4509}
4510
4511static PyMemberDef Pickler_members[] = {
4512 {"bin", T_INT, offsetof(PicklerObject, bin)},
4513 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004514 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004515 {NULL}
4516};
4517
4518static PyGetSetDef Pickler_getsets[] = {
4519 {"memo", (getter)Pickler_get_memo,
4520 (setter)Pickler_set_memo},
4521 {"persistent_id", (getter)Pickler_get_persid,
4522 (setter)Pickler_set_persid},
4523 {NULL}
4524};
4525
4526static PyTypeObject Pickler_Type = {
4527 PyVarObject_HEAD_INIT(NULL, 0)
4528 "_pickle.Pickler" , /*tp_name*/
4529 sizeof(PicklerObject), /*tp_basicsize*/
4530 0, /*tp_itemsize*/
4531 (destructor)Pickler_dealloc, /*tp_dealloc*/
4532 0, /*tp_print*/
4533 0, /*tp_getattr*/
4534 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004535 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004536 0, /*tp_repr*/
4537 0, /*tp_as_number*/
4538 0, /*tp_as_sequence*/
4539 0, /*tp_as_mapping*/
4540 0, /*tp_hash*/
4541 0, /*tp_call*/
4542 0, /*tp_str*/
4543 0, /*tp_getattro*/
4544 0, /*tp_setattro*/
4545 0, /*tp_as_buffer*/
4546 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004547 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004548 (traverseproc)Pickler_traverse, /*tp_traverse*/
4549 (inquiry)Pickler_clear, /*tp_clear*/
4550 0, /*tp_richcompare*/
4551 0, /*tp_weaklistoffset*/
4552 0, /*tp_iter*/
4553 0, /*tp_iternext*/
4554 Pickler_methods, /*tp_methods*/
4555 Pickler_members, /*tp_members*/
4556 Pickler_getsets, /*tp_getset*/
4557 0, /*tp_base*/
4558 0, /*tp_dict*/
4559 0, /*tp_descr_get*/
4560 0, /*tp_descr_set*/
4561 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004562 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004563 PyType_GenericAlloc, /*tp_alloc*/
4564 PyType_GenericNew, /*tp_new*/
4565 PyObject_GC_Del, /*tp_free*/
4566 0, /*tp_is_gc*/
4567};
4568
Victor Stinner121aab42011-09-29 23:40:53 +02004569/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570
4571 XXX: It would be nice to able to avoid Python function call overhead, by
4572 using directly the C version of find_class(), when find_class() is not
4573 overridden by a subclass. Although, this could become rather hackish. A
4574 simpler optimization would be to call the C function when self is not a
4575 subclass instance. */
4576static PyObject *
4577find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4578{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004579 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004580
4581 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4582 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004583}
4584
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004585static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586marker(UnpicklerObject *self)
4587{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004588 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004589 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004590 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004591 return -1;
4592 }
4593
4594 return self->marks[--self->num_marks];
4595}
4596
4597static int
4598load_none(UnpicklerObject *self)
4599{
4600 PDATA_APPEND(self->stack, Py_None, -1);
4601 return 0;
4602}
4603
4604static int
4605bad_readline(void)
4606{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004607 PickleState *st = _Pickle_GetGlobalState();
4608 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004609 return -1;
4610}
4611
4612static int
4613load_int(UnpicklerObject *self)
4614{
4615 PyObject *value;
4616 char *endptr, *s;
4617 Py_ssize_t len;
4618 long x;
4619
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004620 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004621 return -1;
4622 if (len < 2)
4623 return bad_readline();
4624
4625 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004626 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004627 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628 x = strtol(s, &endptr, 0);
4629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004630 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004632 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004633 errno = 0;
4634 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004635 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004636 if (value == NULL) {
4637 PyErr_SetString(PyExc_ValueError,
4638 "could not convert string to int");
4639 return -1;
4640 }
4641 }
4642 else {
4643 if (len == 3 && (x == 0 || x == 1)) {
4644 if ((value = PyBool_FromLong(x)) == NULL)
4645 return -1;
4646 }
4647 else {
4648 if ((value = PyLong_FromLong(x)) == NULL)
4649 return -1;
4650 }
4651 }
4652
4653 PDATA_PUSH(self->stack, value, -1);
4654 return 0;
4655}
4656
4657static int
4658load_bool(UnpicklerObject *self, PyObject *boolean)
4659{
4660 assert(boolean == Py_True || boolean == Py_False);
4661 PDATA_APPEND(self->stack, boolean, -1);
4662 return 0;
4663}
4664
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004665/* s contains x bytes of an unsigned little-endian integer. Return its value
4666 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4667 */
4668static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004669calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004670{
4671 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004672 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004673 size_t x = 0;
4674
Serhiy Storchakae0606192015-09-29 22:10:07 +03004675 if (nbytes > (int)sizeof(size_t)) {
4676 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4677 * have 64-bit size that can't be represented on 32-bit platform.
4678 */
4679 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4680 if (s[i])
4681 return -1;
4682 }
4683 nbytes = (int)sizeof(size_t);
4684 }
4685 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004686 x |= (size_t) s[i] << (8 * i);
4687 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004688
4689 if (x > PY_SSIZE_T_MAX)
4690 return -1;
4691 else
4692 return (Py_ssize_t) x;
4693}
4694
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004695/* s contains x bytes of a little-endian integer. Return its value as a
4696 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4697 * int, but when x is 4 it's a signed one. This is an historical source
4698 * of x-platform bugs.
4699 */
4700static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004701calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004702{
4703 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004704 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004705 long x = 0;
4706
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004707 for (i = 0; i < nbytes; i++) {
4708 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004709 }
4710
4711 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4712 * is signed, so on a box with longs bigger than 4 bytes we need
4713 * to extend a BININT's sign bit to the full width.
4714 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004715 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004716 x |= -(x & (1L << 31));
4717 }
4718
4719 return x;
4720}
4721
4722static int
4723load_binintx(UnpicklerObject *self, char *s, int size)
4724{
4725 PyObject *value;
4726 long x;
4727
4728 x = calc_binint(s, size);
4729
4730 if ((value = PyLong_FromLong(x)) == NULL)
4731 return -1;
4732
4733 PDATA_PUSH(self->stack, value, -1);
4734 return 0;
4735}
4736
4737static int
4738load_binint(UnpicklerObject *self)
4739{
4740 char *s;
4741
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004742 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004743 return -1;
4744
4745 return load_binintx(self, s, 4);
4746}
4747
4748static int
4749load_binint1(UnpicklerObject *self)
4750{
4751 char *s;
4752
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004753 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004754 return -1;
4755
4756 return load_binintx(self, s, 1);
4757}
4758
4759static int
4760load_binint2(UnpicklerObject *self)
4761{
4762 char *s;
4763
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004764 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004765 return -1;
4766
4767 return load_binintx(self, s, 2);
4768}
4769
4770static int
4771load_long(UnpicklerObject *self)
4772{
4773 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004774 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004775 Py_ssize_t len;
4776
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004777 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778 return -1;
4779 if (len < 2)
4780 return bad_readline();
4781
Mark Dickinson8dd05142009-01-20 20:43:58 +00004782 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4783 the 'L' before calling PyLong_FromString. In order to maintain
4784 compatibility with Python 3.0.0, we don't actually *require*
4785 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004786 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004787 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004788 /* XXX: Should the base argument explicitly set to 10? */
4789 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004790 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 return -1;
4792
4793 PDATA_PUSH(self->stack, value, -1);
4794 return 0;
4795}
4796
4797/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4798 * data following.
4799 */
4800static int
4801load_counted_long(UnpicklerObject *self, int size)
4802{
4803 PyObject *value;
4804 char *nbytes;
4805 char *pdata;
4806
4807 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004808 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004809 return -1;
4810
4811 size = calc_binint(nbytes, size);
4812 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004813 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004814 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004815 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004816 "LONG pickle has negative byte count");
4817 return -1;
4818 }
4819
4820 if (size == 0)
4821 value = PyLong_FromLong(0L);
4822 else {
4823 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004824 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004825 return -1;
4826 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4827 1 /* little endian */ , 1 /* signed */ );
4828 }
4829 if (value == NULL)
4830 return -1;
4831 PDATA_PUSH(self->stack, value, -1);
4832 return 0;
4833}
4834
4835static int
4836load_float(UnpicklerObject *self)
4837{
4838 PyObject *value;
4839 char *endptr, *s;
4840 Py_ssize_t len;
4841 double d;
4842
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004843 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004844 return -1;
4845 if (len < 2)
4846 return bad_readline();
4847
4848 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004849 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4850 if (d == -1.0 && PyErr_Occurred())
4851 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4854 return -1;
4855 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004856 value = PyFloat_FromDouble(d);
4857 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858 return -1;
4859
4860 PDATA_PUSH(self->stack, value, -1);
4861 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004862}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004863
4864static int
4865load_binfloat(UnpicklerObject *self)
4866{
4867 PyObject *value;
4868 double x;
4869 char *s;
4870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004871 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004872 return -1;
4873
4874 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4875 if (x == -1.0 && PyErr_Occurred())
4876 return -1;
4877
4878 if ((value = PyFloat_FromDouble(x)) == NULL)
4879 return -1;
4880
4881 PDATA_PUSH(self->stack, value, -1);
4882 return 0;
4883}
4884
4885static int
4886load_string(UnpicklerObject *self)
4887{
4888 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004889 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004890 Py_ssize_t len;
4891 char *s, *p;
4892
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004893 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004895 /* Strip the newline */
4896 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004897 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004898 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 p = s + 1;
4900 len -= 2;
4901 }
4902 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004903 PickleState *st = _Pickle_GetGlobalState();
4904 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004905 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 return -1;
4907 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004908 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004909
4910 /* Use the PyBytes API to decode the string, since that is what is used
4911 to encode, and then coerce the result to Unicode. */
4912 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004913 if (bytes == NULL)
4914 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004915
4916 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4917 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4918 if (strcmp(self->encoding, "bytes") == 0) {
4919 obj = bytes;
4920 }
4921 else {
4922 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4923 Py_DECREF(bytes);
4924 if (obj == NULL) {
4925 return -1;
4926 }
4927 }
4928
4929 PDATA_PUSH(self->stack, obj, -1);
4930 return 0;
4931}
4932
4933static int
4934load_counted_binstring(UnpicklerObject *self, int nbytes)
4935{
4936 PyObject *obj;
4937 Py_ssize_t size;
4938 char *s;
4939
4940 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004941 return -1;
4942
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004943 size = calc_binsize(s, nbytes);
4944 if (size < 0) {
4945 PickleState *st = _Pickle_GetGlobalState();
4946 PyErr_Format(st->UnpicklingError,
4947 "BINSTRING exceeds system's maximum size of %zd bytes",
4948 PY_SSIZE_T_MAX);
4949 return -1;
4950 }
4951
4952 if (_Unpickler_Read(self, &s, size) < 0)
4953 return -1;
4954
4955 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4956 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4957 if (strcmp(self->encoding, "bytes") == 0) {
4958 obj = PyBytes_FromStringAndSize(s, size);
4959 }
4960 else {
4961 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4962 }
4963 if (obj == NULL) {
4964 return -1;
4965 }
4966
4967 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968 return 0;
4969}
4970
4971static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004972load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004973{
4974 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004975 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004976 char *s;
4977
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004978 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004979 return -1;
4980
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004981 size = calc_binsize(s, nbytes);
4982 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004983 PyErr_Format(PyExc_OverflowError,
4984 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004985 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004986 return -1;
4987 }
4988
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004989 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004990 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004991
4992 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004993 if (bytes == NULL)
4994 return -1;
4995
4996 PDATA_PUSH(self->stack, bytes, -1);
4997 return 0;
4998}
4999
5000static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005001load_unicode(UnpicklerObject *self)
5002{
5003 PyObject *str;
5004 Py_ssize_t len;
5005 char *s;
5006
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005007 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005008 return -1;
5009 if (len < 1)
5010 return bad_readline();
5011
5012 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5013 if (str == NULL)
5014 return -1;
5015
5016 PDATA_PUSH(self->stack, str, -1);
5017 return 0;
5018}
5019
5020static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005021load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005022{
5023 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005024 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005025 char *s;
5026
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005027 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005028 return -1;
5029
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005030 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005031 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005032 PyErr_Format(PyExc_OverflowError,
5033 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005034 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035 return -1;
5036 }
5037
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005038 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005039 return -1;
5040
Victor Stinner485fb562010-04-13 11:07:24 +00005041 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005042 if (str == NULL)
5043 return -1;
5044
5045 PDATA_PUSH(self->stack, str, -1);
5046 return 0;
5047}
5048
5049static int
5050load_tuple(UnpicklerObject *self)
5051{
5052 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005053 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005054
5055 if ((i = marker(self)) < 0)
5056 return -1;
5057
5058 tuple = Pdata_poptuple(self->stack, i);
5059 if (tuple == NULL)
5060 return -1;
5061 PDATA_PUSH(self->stack, tuple, -1);
5062 return 0;
5063}
5064
5065static int
5066load_counted_tuple(UnpicklerObject *self, int len)
5067{
5068 PyObject *tuple;
5069
5070 tuple = PyTuple_New(len);
5071 if (tuple == NULL)
5072 return -1;
5073
5074 while (--len >= 0) {
5075 PyObject *item;
5076
5077 PDATA_POP(self->stack, item);
5078 if (item == NULL)
5079 return -1;
5080 PyTuple_SET_ITEM(tuple, len, item);
5081 }
5082 PDATA_PUSH(self->stack, tuple, -1);
5083 return 0;
5084}
5085
5086static int
5087load_empty_list(UnpicklerObject *self)
5088{
5089 PyObject *list;
5090
5091 if ((list = PyList_New(0)) == NULL)
5092 return -1;
5093 PDATA_PUSH(self->stack, list, -1);
5094 return 0;
5095}
5096
5097static int
5098load_empty_dict(UnpicklerObject *self)
5099{
5100 PyObject *dict;
5101
5102 if ((dict = PyDict_New()) == NULL)
5103 return -1;
5104 PDATA_PUSH(self->stack, dict, -1);
5105 return 0;
5106}
5107
5108static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005109load_empty_set(UnpicklerObject *self)
5110{
5111 PyObject *set;
5112
5113 if ((set = PySet_New(NULL)) == NULL)
5114 return -1;
5115 PDATA_PUSH(self->stack, set, -1);
5116 return 0;
5117}
5118
5119static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005120load_list(UnpicklerObject *self)
5121{
5122 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005123 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005124
5125 if ((i = marker(self)) < 0)
5126 return -1;
5127
5128 list = Pdata_poplist(self->stack, i);
5129 if (list == NULL)
5130 return -1;
5131 PDATA_PUSH(self->stack, list, -1);
5132 return 0;
5133}
5134
5135static int
5136load_dict(UnpicklerObject *self)
5137{
5138 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005139 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005140
5141 if ((i = marker(self)) < 0)
5142 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005143 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005144
5145 if ((dict = PyDict_New()) == NULL)
5146 return -1;
5147
5148 for (k = i + 1; k < j; k += 2) {
5149 key = self->stack->data[k - 1];
5150 value = self->stack->data[k];
5151 if (PyDict_SetItem(dict, key, value) < 0) {
5152 Py_DECREF(dict);
5153 return -1;
5154 }
5155 }
5156 Pdata_clear(self->stack, i);
5157 PDATA_PUSH(self->stack, dict, -1);
5158 return 0;
5159}
5160
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005161static int
5162load_frozenset(UnpicklerObject *self)
5163{
5164 PyObject *items;
5165 PyObject *frozenset;
5166 Py_ssize_t i;
5167
5168 if ((i = marker(self)) < 0)
5169 return -1;
5170
5171 items = Pdata_poptuple(self->stack, i);
5172 if (items == NULL)
5173 return -1;
5174
5175 frozenset = PyFrozenSet_New(items);
5176 Py_DECREF(items);
5177 if (frozenset == NULL)
5178 return -1;
5179
5180 PDATA_PUSH(self->stack, frozenset, -1);
5181 return 0;
5182}
5183
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005184static PyObject *
5185instantiate(PyObject *cls, PyObject *args)
5186{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005187 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005188 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005189 /* Caller must assure args are a tuple. Normally, args come from
5190 Pdata_poptuple which packs objects from the top of the stack
5191 into a newly created tuple. */
5192 assert(PyTuple_Check(args));
5193 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005194 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005195 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005196 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005197 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005198 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005199
5200 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005201 }
5202 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005203}
5204
5205static int
5206load_obj(UnpicklerObject *self)
5207{
5208 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005209 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005210
5211 if ((i = marker(self)) < 0)
5212 return -1;
5213
5214 args = Pdata_poptuple(self->stack, i + 1);
5215 if (args == NULL)
5216 return -1;
5217
5218 PDATA_POP(self->stack, cls);
5219 if (cls) {
5220 obj = instantiate(cls, args);
5221 Py_DECREF(cls);
5222 }
5223 Py_DECREF(args);
5224 if (obj == NULL)
5225 return -1;
5226
5227 PDATA_PUSH(self->stack, obj, -1);
5228 return 0;
5229}
5230
5231static int
5232load_inst(UnpicklerObject *self)
5233{
5234 PyObject *cls = NULL;
5235 PyObject *args = NULL;
5236 PyObject *obj = NULL;
5237 PyObject *module_name;
5238 PyObject *class_name;
5239 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005240 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241 char *s;
5242
5243 if ((i = marker(self)) < 0)
5244 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005245 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005246 return -1;
5247 if (len < 2)
5248 return bad_readline();
5249
5250 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5251 identifiers are permitted in Python 3.0, since the INST opcode is only
5252 supported by older protocols on Python 2.x. */
5253 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5254 if (module_name == NULL)
5255 return -1;
5256
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005257 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005258 if (len < 2)
5259 return bad_readline();
5260 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005261 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005262 cls = find_class(self, module_name, class_name);
5263 Py_DECREF(class_name);
5264 }
5265 }
5266 Py_DECREF(module_name);
5267
5268 if (cls == NULL)
5269 return -1;
5270
5271 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5272 obj = instantiate(cls, args);
5273 Py_DECREF(args);
5274 }
5275 Py_DECREF(cls);
5276
5277 if (obj == NULL)
5278 return -1;
5279
5280 PDATA_PUSH(self->stack, obj, -1);
5281 return 0;
5282}
5283
5284static int
5285load_newobj(UnpicklerObject *self)
5286{
5287 PyObject *args = NULL;
5288 PyObject *clsraw = NULL;
5289 PyTypeObject *cls; /* clsraw cast to its true type */
5290 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005291 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005292
5293 /* Stack is ... cls argtuple, and we want to call
5294 * cls.__new__(cls, *argtuple).
5295 */
5296 PDATA_POP(self->stack, args);
5297 if (args == NULL)
5298 goto error;
5299 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005300 PyErr_SetString(st->UnpicklingError,
5301 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005302 goto error;
5303 }
5304
5305 PDATA_POP(self->stack, clsraw);
5306 cls = (PyTypeObject *)clsraw;
5307 if (cls == NULL)
5308 goto error;
5309 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005310 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005311 "isn't a type object");
5312 goto error;
5313 }
5314 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005315 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005316 "has NULL tp_new");
5317 goto error;
5318 }
5319
5320 /* Call __new__. */
5321 obj = cls->tp_new(cls, args, NULL);
5322 if (obj == NULL)
5323 goto error;
5324
5325 Py_DECREF(args);
5326 Py_DECREF(clsraw);
5327 PDATA_PUSH(self->stack, obj, -1);
5328 return 0;
5329
5330 error:
5331 Py_XDECREF(args);
5332 Py_XDECREF(clsraw);
5333 return -1;
5334}
5335
5336static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005337load_newobj_ex(UnpicklerObject *self)
5338{
5339 PyObject *cls, *args, *kwargs;
5340 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005341 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005342
5343 PDATA_POP(self->stack, kwargs);
5344 if (kwargs == NULL) {
5345 return -1;
5346 }
5347 PDATA_POP(self->stack, args);
5348 if (args == NULL) {
5349 Py_DECREF(kwargs);
5350 return -1;
5351 }
5352 PDATA_POP(self->stack, cls);
5353 if (cls == NULL) {
5354 Py_DECREF(kwargs);
5355 Py_DECREF(args);
5356 return -1;
5357 }
Larry Hastings61272b72014-01-07 12:41:53 -08005358
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005359 if (!PyType_Check(cls)) {
5360 Py_DECREF(kwargs);
5361 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005362 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005363 "NEWOBJ_EX class argument must be a type, not %.200s",
5364 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005365 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005366 return -1;
5367 }
5368
5369 if (((PyTypeObject *)cls)->tp_new == NULL) {
5370 Py_DECREF(kwargs);
5371 Py_DECREF(args);
5372 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005373 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005374 "NEWOBJ_EX class argument doesn't have __new__");
5375 return -1;
5376 }
5377 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5378 Py_DECREF(kwargs);
5379 Py_DECREF(args);
5380 Py_DECREF(cls);
5381 if (obj == NULL) {
5382 return -1;
5383 }
5384 PDATA_PUSH(self->stack, obj, -1);
5385 return 0;
5386}
5387
5388static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005389load_global(UnpicklerObject *self)
5390{
5391 PyObject *global = NULL;
5392 PyObject *module_name;
5393 PyObject *global_name;
5394 Py_ssize_t len;
5395 char *s;
5396
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005397 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005398 return -1;
5399 if (len < 2)
5400 return bad_readline();
5401 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5402 if (!module_name)
5403 return -1;
5404
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005405 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005406 if (len < 2) {
5407 Py_DECREF(module_name);
5408 return bad_readline();
5409 }
5410 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5411 if (global_name) {
5412 global = find_class(self, module_name, global_name);
5413 Py_DECREF(global_name);
5414 }
5415 }
5416 Py_DECREF(module_name);
5417
5418 if (global == NULL)
5419 return -1;
5420 PDATA_PUSH(self->stack, global, -1);
5421 return 0;
5422}
5423
5424static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005425load_stack_global(UnpicklerObject *self)
5426{
5427 PyObject *global;
5428 PyObject *module_name;
5429 PyObject *global_name;
5430
5431 PDATA_POP(self->stack, global_name);
5432 PDATA_POP(self->stack, module_name);
5433 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5434 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005435 PickleState *st = _Pickle_GetGlobalState();
5436 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005437 Py_XDECREF(global_name);
5438 Py_XDECREF(module_name);
5439 return -1;
5440 }
5441 global = find_class(self, module_name, global_name);
5442 Py_DECREF(global_name);
5443 Py_DECREF(module_name);
5444 if (global == NULL)
5445 return -1;
5446 PDATA_PUSH(self->stack, global, -1);
5447 return 0;
5448}
5449
5450static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005451load_persid(UnpicklerObject *self)
5452{
5453 PyObject *pid;
5454 Py_ssize_t len;
5455 char *s;
5456
5457 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005458 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005459 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005460 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461 return bad_readline();
5462
5463 pid = PyBytes_FromStringAndSize(s, len - 1);
5464 if (pid == NULL)
5465 return -1;
5466
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005467 /* This does not leak since _Pickle_FastCall() steals the reference
5468 to pid first. */
5469 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005470 if (pid == NULL)
5471 return -1;
5472
5473 PDATA_PUSH(self->stack, pid, -1);
5474 return 0;
5475 }
5476 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005477 PickleState *st = _Pickle_GetGlobalState();
5478 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005479 "A load persistent id instruction was encountered,\n"
5480 "but no persistent_load function was specified.");
5481 return -1;
5482 }
5483}
5484
5485static int
5486load_binpersid(UnpicklerObject *self)
5487{
5488 PyObject *pid;
5489
5490 if (self->pers_func) {
5491 PDATA_POP(self->stack, pid);
5492 if (pid == NULL)
5493 return -1;
5494
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005495 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005496 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005497 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005498 if (pid == NULL)
5499 return -1;
5500
5501 PDATA_PUSH(self->stack, pid, -1);
5502 return 0;
5503 }
5504 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005505 PickleState *st = _Pickle_GetGlobalState();
5506 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005507 "A load persistent id instruction was encountered,\n"
5508 "but no persistent_load function was specified.");
5509 return -1;
5510 }
5511}
5512
5513static int
5514load_pop(UnpicklerObject *self)
5515{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005516 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005517
5518 /* Note that we split the (pickle.py) stack into two stacks,
5519 * an object stack and a mark stack. We have to be clever and
5520 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005521 * mark stack first, and only signalling a stack underflow if
5522 * the object stack is empty and the mark stack doesn't match
5523 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005524 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005525 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005526 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005527 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005528 len--;
5529 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005530 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005531 } else {
5532 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 return 0;
5535}
5536
5537static int
5538load_pop_mark(UnpicklerObject *self)
5539{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005540 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005541
5542 if ((i = marker(self)) < 0)
5543 return -1;
5544
5545 Pdata_clear(self->stack, i);
5546
5547 return 0;
5548}
5549
5550static int
5551load_dup(UnpicklerObject *self)
5552{
5553 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005554 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005555
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005556 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005557 return stack_underflow();
5558 last = self->stack->data[len - 1];
5559 PDATA_APPEND(self->stack, last, -1);
5560 return 0;
5561}
5562
5563static int
5564load_get(UnpicklerObject *self)
5565{
5566 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005567 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568 Py_ssize_t len;
5569 char *s;
5570
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005571 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005572 return -1;
5573 if (len < 2)
5574 return bad_readline();
5575
5576 key = PyLong_FromString(s, NULL, 10);
5577 if (key == NULL)
5578 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005579 idx = PyLong_AsSsize_t(key);
5580 if (idx == -1 && PyErr_Occurred()) {
5581 Py_DECREF(key);
5582 return -1;
5583 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005584
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005585 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005586 if (value == NULL) {
5587 if (!PyErr_Occurred())
5588 PyErr_SetObject(PyExc_KeyError, key);
5589 Py_DECREF(key);
5590 return -1;
5591 }
5592 Py_DECREF(key);
5593
5594 PDATA_APPEND(self->stack, value, -1);
5595 return 0;
5596}
5597
5598static int
5599load_binget(UnpicklerObject *self)
5600{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005601 PyObject *value;
5602 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603 char *s;
5604
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005605 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 return -1;
5607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005608 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005610 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005611 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005612 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005613 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005615 Py_DECREF(key);
5616 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005617 return -1;
5618 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619
5620 PDATA_APPEND(self->stack, value, -1);
5621 return 0;
5622}
5623
5624static int
5625load_long_binget(UnpicklerObject *self)
5626{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005627 PyObject *value;
5628 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 return -1;
5633
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005634 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005636 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005637 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005638 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005639 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005641 Py_DECREF(key);
5642 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643 return -1;
5644 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005645
5646 PDATA_APPEND(self->stack, value, -1);
5647 return 0;
5648}
5649
5650/* Push an object from the extension registry (EXT[124]). nbytes is
5651 * the number of bytes following the opcode, holding the index (code) value.
5652 */
5653static int
5654load_extension(UnpicklerObject *self, int nbytes)
5655{
5656 char *codebytes; /* the nbytes bytes after the opcode */
5657 long code; /* calc_binint returns long */
5658 PyObject *py_code; /* code as a Python int */
5659 PyObject *obj; /* the object to push */
5660 PyObject *pair; /* (module_name, class_name) */
5661 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005662 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005663
5664 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005665 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666 return -1;
5667 code = calc_binint(codebytes, nbytes);
5668 if (code <= 0) { /* note that 0 is forbidden */
5669 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005670 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005671 return -1;
5672 }
5673
5674 /* Look for the code in the cache. */
5675 py_code = PyLong_FromLong(code);
5676 if (py_code == NULL)
5677 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005678 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679 if (obj != NULL) {
5680 /* Bingo. */
5681 Py_DECREF(py_code);
5682 PDATA_APPEND(self->stack, obj, -1);
5683 return 0;
5684 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005685 if (PyErr_Occurred()) {
5686 Py_DECREF(py_code);
5687 return -1;
5688 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005689
5690 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005691 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 if (pair == NULL) {
5693 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005694 if (!PyErr_Occurred()) {
5695 PyErr_Format(PyExc_ValueError, "unregistered extension "
5696 "code %ld", code);
5697 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005698 return -1;
5699 }
5700 /* Since the extension registry is manipulable via Python code,
5701 * confirm that pair is really a 2-tuple of strings.
5702 */
5703 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5704 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5705 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5706 Py_DECREF(py_code);
5707 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5708 "isn't a 2-tuple of strings", code);
5709 return -1;
5710 }
5711 /* Load the object. */
5712 obj = find_class(self, module_name, class_name);
5713 if (obj == NULL) {
5714 Py_DECREF(py_code);
5715 return -1;
5716 }
5717 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005718 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005719 Py_DECREF(py_code);
5720 if (code < 0) {
5721 Py_DECREF(obj);
5722 return -1;
5723 }
5724 PDATA_PUSH(self->stack, obj, -1);
5725 return 0;
5726}
5727
5728static int
5729load_put(UnpicklerObject *self)
5730{
5731 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005732 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005733 Py_ssize_t len;
5734 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005735
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005736 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005737 return -1;
5738 if (len < 2)
5739 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005740 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005741 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005742 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005743
5744 key = PyLong_FromString(s, NULL, 10);
5745 if (key == NULL)
5746 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005747 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005748 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005749 if (idx < 0) {
5750 if (!PyErr_Occurred())
5751 PyErr_SetString(PyExc_ValueError,
5752 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005753 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005754 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005755
5756 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005757}
5758
5759static int
5760load_binput(UnpicklerObject *self)
5761{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005762 PyObject *value;
5763 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768
5769 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005771 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005773 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005775 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005776}
5777
5778static int
5779load_long_binput(UnpicklerObject *self)
5780{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005781 PyObject *value;
5782 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005787
5788 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005792 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005793 if (idx < 0) {
5794 PyErr_SetString(PyExc_ValueError,
5795 "negative LONG_BINPUT argument");
5796 return -1;
5797 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005798
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005799 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800}
5801
5802static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005803load_memoize(UnpicklerObject *self)
5804{
5805 PyObject *value;
5806
5807 if (Py_SIZE(self->stack) <= 0)
5808 return stack_underflow();
5809 value = self->stack->data[Py_SIZE(self->stack) - 1];
5810
5811 return _Unpickler_MemoPut(self, self->memo_len, value);
5812}
5813
5814static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005815do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005816{
5817 PyObject *value;
5818 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005819 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005820
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005821 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005822 if (x > len || x <= 0)
5823 return stack_underflow();
5824 if (len == x) /* nothing to do */
5825 return 0;
5826
5827 list = self->stack->data[x - 1];
5828
5829 if (PyList_Check(list)) {
5830 PyObject *slice;
5831 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005832 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833
5834 slice = Pdata_poplist(self->stack, x);
5835 if (!slice)
5836 return -1;
5837 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005838 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005839 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005840 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841 }
5842 else {
5843 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005844 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005845
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005846 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005847 if (append_func == NULL)
5848 return -1;
5849 for (i = x; i < len; i++) {
5850 PyObject *result;
5851
5852 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005853 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 if (result == NULL) {
5855 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005856 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005857 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 return -1;
5859 }
5860 Py_DECREF(result);
5861 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005862 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005863 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864 }
5865
5866 return 0;
5867}
5868
5869static int
5870load_append(UnpicklerObject *self)
5871{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005872 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005873}
5874
5875static int
5876load_appends(UnpicklerObject *self)
5877{
5878 return do_append(self, marker(self));
5879}
5880
5881static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005882do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005883{
5884 PyObject *value, *key;
5885 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005886 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005887 int status = 0;
5888
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005889 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005890 if (x > len || x <= 0)
5891 return stack_underflow();
5892 if (len == x) /* nothing to do */
5893 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005894 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005895 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005896 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005897 PyErr_SetString(st->UnpicklingError,
5898 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005899 return -1;
5900 }
5901
5902 /* Here, dict does not actually need to be a PyDict; it could be anything
5903 that supports the __setitem__ attribute. */
5904 dict = self->stack->data[x - 1];
5905
5906 for (i = x + 1; i < len; i += 2) {
5907 key = self->stack->data[i - 1];
5908 value = self->stack->data[i];
5909 if (PyObject_SetItem(dict, key, value) < 0) {
5910 status = -1;
5911 break;
5912 }
5913 }
5914
5915 Pdata_clear(self->stack, x);
5916 return status;
5917}
5918
5919static int
5920load_setitem(UnpicklerObject *self)
5921{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005922 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005923}
5924
5925static int
5926load_setitems(UnpicklerObject *self)
5927{
5928 return do_setitems(self, marker(self));
5929}
5930
5931static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005932load_additems(UnpicklerObject *self)
5933{
5934 PyObject *set;
5935 Py_ssize_t mark, len, i;
5936
5937 mark = marker(self);
5938 len = Py_SIZE(self->stack);
5939 if (mark > len || mark <= 0)
5940 return stack_underflow();
5941 if (len == mark) /* nothing to do */
5942 return 0;
5943
5944 set = self->stack->data[mark - 1];
5945
5946 if (PySet_Check(set)) {
5947 PyObject *items;
5948 int status;
5949
5950 items = Pdata_poptuple(self->stack, mark);
5951 if (items == NULL)
5952 return -1;
5953
5954 status = _PySet_Update(set, items);
5955 Py_DECREF(items);
5956 return status;
5957 }
5958 else {
5959 PyObject *add_func;
5960 _Py_IDENTIFIER(add);
5961
5962 add_func = _PyObject_GetAttrId(set, &PyId_add);
5963 if (add_func == NULL)
5964 return -1;
5965 for (i = mark; i < len; i++) {
5966 PyObject *result;
5967 PyObject *item;
5968
5969 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005970 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005971 if (result == NULL) {
5972 Pdata_clear(self->stack, i + 1);
5973 Py_SIZE(self->stack) = mark;
5974 return -1;
5975 }
5976 Py_DECREF(result);
5977 }
5978 Py_SIZE(self->stack) = mark;
5979 }
5980
5981 return 0;
5982}
5983
5984static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005985load_build(UnpicklerObject *self)
5986{
5987 PyObject *state, *inst, *slotstate;
5988 PyObject *setstate;
5989 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005990 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991
5992 /* Stack is ... instance, state. We want to leave instance at
5993 * the stack top, possibly mutated via instance.__setstate__(state).
5994 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005995 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005996 return stack_underflow();
5997
5998 PDATA_POP(self->stack, state);
5999 if (state == NULL)
6000 return -1;
6001
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006002 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006003
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006004 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006005 if (setstate == NULL) {
6006 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6007 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006008 else {
6009 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006010 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006011 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012 }
6013 else {
6014 PyObject *result;
6015
6016 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006017 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006018 Py_DECREF(setstate);
6019 if (result == NULL)
6020 return -1;
6021 Py_DECREF(result);
6022 return 0;
6023 }
6024
6025 /* A default __setstate__. First see whether state embeds a
6026 * slot state dict too (a proto 2 addition).
6027 */
6028 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6029 PyObject *tmp = state;
6030
6031 state = PyTuple_GET_ITEM(tmp, 0);
6032 slotstate = PyTuple_GET_ITEM(tmp, 1);
6033 Py_INCREF(state);
6034 Py_INCREF(slotstate);
6035 Py_DECREF(tmp);
6036 }
6037 else
6038 slotstate = NULL;
6039
6040 /* Set inst.__dict__ from the state dict (if any). */
6041 if (state != Py_None) {
6042 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006043 PyObject *d_key, *d_value;
6044 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006045 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006046
6047 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006048 PickleState *st = _Pickle_GetGlobalState();
6049 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006050 goto error;
6051 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006052 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006053 if (dict == NULL)
6054 goto error;
6055
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006056 i = 0;
6057 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6058 /* normally the keys for instance attributes are
6059 interned. we should try to do that here. */
6060 Py_INCREF(d_key);
6061 if (PyUnicode_CheckExact(d_key))
6062 PyUnicode_InternInPlace(&d_key);
6063 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6064 Py_DECREF(d_key);
6065 goto error;
6066 }
6067 Py_DECREF(d_key);
6068 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069 Py_DECREF(dict);
6070 }
6071
6072 /* Also set instance attributes from the slotstate dict (if any). */
6073 if (slotstate != NULL) {
6074 PyObject *d_key, *d_value;
6075 Py_ssize_t i;
6076
6077 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006078 PickleState *st = _Pickle_GetGlobalState();
6079 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 "slot state is not a dictionary");
6081 goto error;
6082 }
6083 i = 0;
6084 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6085 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6086 goto error;
6087 }
6088 }
6089
6090 if (0) {
6091 error:
6092 status = -1;
6093 }
6094
6095 Py_DECREF(state);
6096 Py_XDECREF(slotstate);
6097 return status;
6098}
6099
6100static int
6101load_mark(UnpicklerObject *self)
6102{
6103
6104 /* Note that we split the (pickle.py) stack into two stacks, an
6105 * object stack and a mark stack. Here we push a mark onto the
6106 * mark stack.
6107 */
6108
6109 if ((self->num_marks + 1) >= self->marks_size) {
6110 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006111
6112 /* Use the size_t type to check for overflow. */
6113 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006114 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006115 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006116 PyErr_NoMemory();
6117 return -1;
6118 }
6119
6120 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006121 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006122 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006123 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6124 if (self->marks == NULL) {
6125 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006126 PyErr_NoMemory();
6127 return -1;
6128 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006129 self->marks_size = (Py_ssize_t)alloc;
6130 }
6131
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006132 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006133
6134 return 0;
6135}
6136
6137static int
6138load_reduce(UnpicklerObject *self)
6139{
6140 PyObject *callable = NULL;
6141 PyObject *argtup = NULL;
6142 PyObject *obj = NULL;
6143
6144 PDATA_POP(self->stack, argtup);
6145 if (argtup == NULL)
6146 return -1;
6147 PDATA_POP(self->stack, callable);
6148 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006149 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006150 Py_DECREF(callable);
6151 }
6152 Py_DECREF(argtup);
6153
6154 if (obj == NULL)
6155 return -1;
6156
6157 PDATA_PUSH(self->stack, obj, -1);
6158 return 0;
6159}
6160
6161/* Just raises an error if we don't know the protocol specified. PROTO
6162 * is the first opcode for protocols >= 2.
6163 */
6164static int
6165load_proto(UnpicklerObject *self)
6166{
6167 char *s;
6168 int i;
6169
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006170 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 return -1;
6172
6173 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006174 if (i <= HIGHEST_PROTOCOL) {
6175 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006176 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006177 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006178
6179 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6180 return -1;
6181}
6182
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006183static int
6184load_frame(UnpicklerObject *self)
6185{
6186 char *s;
6187 Py_ssize_t frame_len;
6188
6189 if (_Unpickler_Read(self, &s, 8) < 0)
6190 return -1;
6191
6192 frame_len = calc_binsize(s, 8);
6193 if (frame_len < 0) {
6194 PyErr_Format(PyExc_OverflowError,
6195 "FRAME length exceeds system's maximum of %zd bytes",
6196 PY_SSIZE_T_MAX);
6197 return -1;
6198 }
6199
6200 if (_Unpickler_Read(self, &s, frame_len) < 0)
6201 return -1;
6202
6203 /* Rewind to start of frame */
6204 self->next_read_idx -= frame_len;
6205 return 0;
6206}
6207
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208static PyObject *
6209load(UnpicklerObject *self)
6210{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006211 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006212 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006213
6214 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006215 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006216 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006217 Pdata_clear(self->stack, 0);
6218
6219 /* Convenient macros for the dispatch while-switch loop just below. */
6220#define OP(opcode, load_func) \
6221 case opcode: if (load_func(self) < 0) break; continue;
6222
6223#define OP_ARG(opcode, load_func, arg) \
6224 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6225
6226 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006227 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006228 break;
6229
6230 switch ((enum opcode)s[0]) {
6231 OP(NONE, load_none)
6232 OP(BININT, load_binint)
6233 OP(BININT1, load_binint1)
6234 OP(BININT2, load_binint2)
6235 OP(INT, load_int)
6236 OP(LONG, load_long)
6237 OP_ARG(LONG1, load_counted_long, 1)
6238 OP_ARG(LONG4, load_counted_long, 4)
6239 OP(FLOAT, load_float)
6240 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006241 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6242 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6243 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6244 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6245 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006246 OP(STRING, load_string)
6247 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006248 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6249 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6250 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006251 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6252 OP_ARG(TUPLE1, load_counted_tuple, 1)
6253 OP_ARG(TUPLE2, load_counted_tuple, 2)
6254 OP_ARG(TUPLE3, load_counted_tuple, 3)
6255 OP(TUPLE, load_tuple)
6256 OP(EMPTY_LIST, load_empty_list)
6257 OP(LIST, load_list)
6258 OP(EMPTY_DICT, load_empty_dict)
6259 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006260 OP(EMPTY_SET, load_empty_set)
6261 OP(ADDITEMS, load_additems)
6262 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006263 OP(OBJ, load_obj)
6264 OP(INST, load_inst)
6265 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006266 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006267 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006268 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006269 OP(APPEND, load_append)
6270 OP(APPENDS, load_appends)
6271 OP(BUILD, load_build)
6272 OP(DUP, load_dup)
6273 OP(BINGET, load_binget)
6274 OP(LONG_BINGET, load_long_binget)
6275 OP(GET, load_get)
6276 OP(MARK, load_mark)
6277 OP(BINPUT, load_binput)
6278 OP(LONG_BINPUT, load_long_binput)
6279 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006280 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 OP(POP, load_pop)
6282 OP(POP_MARK, load_pop_mark)
6283 OP(SETITEM, load_setitem)
6284 OP(SETITEMS, load_setitems)
6285 OP(PERSID, load_persid)
6286 OP(BINPERSID, load_binpersid)
6287 OP(REDUCE, load_reduce)
6288 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006289 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006290 OP_ARG(EXT1, load_extension, 1)
6291 OP_ARG(EXT2, load_extension, 2)
6292 OP_ARG(EXT4, load_extension, 4)
6293 OP_ARG(NEWTRUE, load_bool, Py_True)
6294 OP_ARG(NEWFALSE, load_bool, Py_False)
6295
6296 case STOP:
6297 break;
6298
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006300 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006301 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006302 }
6303 else {
6304 PickleState *st = _Pickle_GetGlobalState();
6305 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006306 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006307 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006308 return NULL;
6309 }
6310
6311 break; /* and we are done! */
6312 }
6313
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006314 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006315 return NULL;
6316 }
6317
Victor Stinner2ae57e32013-10-31 13:39:23 +01006318 if (_Unpickler_SkipConsumed(self) < 0)
6319 return NULL;
6320
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006321 PDATA_POP(self->stack, value);
6322 return value;
6323}
6324
Larry Hastings61272b72014-01-07 12:41:53 -08006325/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006326
6327_pickle.Unpickler.load
6328
6329Load a pickle.
6330
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006331Read a pickled object representation from the open file object given
6332in the constructor, and return the reconstituted object hierarchy
6333specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006334[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006335
Larry Hastings3cceb382014-01-04 11:09:09 -08006336static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006337_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006338/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006339{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006340 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006341
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006342 /* Check whether the Unpickler was initialized correctly. This prevents
6343 segfaulting if a subclass overridden __init__ with a function that does
6344 not call Unpickler.__init__(). Here, we simply ensure that self->read
6345 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006346 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006347 PickleState *st = _Pickle_GetGlobalState();
6348 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006349 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006350 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006351 return NULL;
6352 }
6353
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006354 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006355}
6356
6357/* The name of find_class() is misleading. In newer pickle protocols, this
6358 function is used for loading any global (i.e., functions), not just
6359 classes. The name is kept only for backward compatibility. */
6360
Larry Hastings61272b72014-01-07 12:41:53 -08006361/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006362
6363_pickle.Unpickler.find_class
6364
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006365 module_name: object
6366 global_name: object
6367 /
6368
6369Return an object from a specified module.
6370
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006371If necessary, the module will be imported. Subclasses may override
6372this method (e.g. to restrict unpickling of arbitrary classes and
6373functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006374
6375This method is called whenever a class or a function object is
6376needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006377[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006379static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006380_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6381 PyObject *module_name,
6382 PyObject *global_name)
6383/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006384{
6385 PyObject *global;
6386 PyObject *modules_dict;
6387 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006388 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006389
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006390 /* Try to map the old names used in Python 2.x to the new ones used in
6391 Python 3.x. We do this only with old pickle protocols and when the
6392 user has not disabled the feature. */
6393 if (self->proto < 3 && self->fix_imports) {
6394 PyObject *key;
6395 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006396 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006397
6398 /* Check if the global (i.e., a function or a class) was renamed
6399 or moved to another module. */
6400 key = PyTuple_Pack(2, module_name, global_name);
6401 if (key == NULL)
6402 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006403 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006404 Py_DECREF(key);
6405 if (item) {
6406 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6407 PyErr_Format(PyExc_RuntimeError,
6408 "_compat_pickle.NAME_MAPPING values should be "
6409 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6410 return NULL;
6411 }
6412 module_name = PyTuple_GET_ITEM(item, 0);
6413 global_name = PyTuple_GET_ITEM(item, 1);
6414 if (!PyUnicode_Check(module_name) ||
6415 !PyUnicode_Check(global_name)) {
6416 PyErr_Format(PyExc_RuntimeError,
6417 "_compat_pickle.NAME_MAPPING values should be "
6418 "pairs of str, not (%.200s, %.200s)",
6419 Py_TYPE(module_name)->tp_name,
6420 Py_TYPE(global_name)->tp_name);
6421 return NULL;
6422 }
6423 }
6424 else if (PyErr_Occurred()) {
6425 return NULL;
6426 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006427 else {
6428 /* Check if the module was renamed. */
6429 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6430 if (item) {
6431 if (!PyUnicode_Check(item)) {
6432 PyErr_Format(PyExc_RuntimeError,
6433 "_compat_pickle.IMPORT_MAPPING values should be "
6434 "strings, not %.200s", Py_TYPE(item)->tp_name);
6435 return NULL;
6436 }
6437 module_name = item;
6438 }
6439 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006440 return NULL;
6441 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006442 }
6443 }
6444
Victor Stinnerbb520202013-11-06 22:40:41 +01006445 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006446 if (modules_dict == NULL) {
6447 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006448 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006449 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006450
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006451 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006452 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006453 if (PyErr_Occurred())
6454 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006455 module = PyImport_Import(module_name);
6456 if (module == NULL)
6457 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006458 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006459 Py_DECREF(module);
6460 }
Victor Stinner121aab42011-09-29 23:40:53 +02006461 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006462 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006463 }
6464 return global;
6465}
6466
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006467/*[clinic input]
6468
6469_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6470
6471Returns size in memory, in bytes.
6472[clinic start generated code]*/
6473
6474static Py_ssize_t
6475_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6476/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6477{
6478 Py_ssize_t res;
6479
6480 res = sizeof(UnpicklerObject);
6481 if (self->memo != NULL)
6482 res += self->memo_size * sizeof(PyObject *);
6483 if (self->marks != NULL)
6484 res += self->marks_size * sizeof(Py_ssize_t);
6485 if (self->input_line != NULL)
6486 res += strlen(self->input_line) + 1;
6487 if (self->encoding != NULL)
6488 res += strlen(self->encoding) + 1;
6489 if (self->errors != NULL)
6490 res += strlen(self->errors) + 1;
6491 return res;
6492}
6493
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006494static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006495 _PICKLE_UNPICKLER_LOAD_METHODDEF
6496 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006497 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006498 {NULL, NULL} /* sentinel */
6499};
6500
6501static void
6502Unpickler_dealloc(UnpicklerObject *self)
6503{
6504 PyObject_GC_UnTrack((PyObject *)self);
6505 Py_XDECREF(self->readline);
6506 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006507 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006508 Py_XDECREF(self->stack);
6509 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006510 if (self->buffer.buf != NULL) {
6511 PyBuffer_Release(&self->buffer);
6512 self->buffer.buf = NULL;
6513 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006514
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006515 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006516 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006517 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006518 PyMem_Free(self->encoding);
6519 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006520
6521 Py_TYPE(self)->tp_free((PyObject *)self);
6522}
6523
6524static int
6525Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6526{
6527 Py_VISIT(self->readline);
6528 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006529 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006530 Py_VISIT(self->stack);
6531 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006532 return 0;
6533}
6534
6535static int
6536Unpickler_clear(UnpicklerObject *self)
6537{
6538 Py_CLEAR(self->readline);
6539 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006540 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006541 Py_CLEAR(self->stack);
6542 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006543 if (self->buffer.buf != NULL) {
6544 PyBuffer_Release(&self->buffer);
6545 self->buffer.buf = NULL;
6546 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006547
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006548 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549 PyMem_Free(self->marks);
6550 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006551 PyMem_Free(self->input_line);
6552 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006553 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006554 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006555 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006556 self->errors = NULL;
6557
6558 return 0;
6559}
6560
Larry Hastings61272b72014-01-07 12:41:53 -08006561/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006562
6563_pickle.Unpickler.__init__
6564
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006565 file: object
6566 *
6567 fix_imports: bool = True
6568 encoding: str = 'ASCII'
6569 errors: str = 'strict'
6570
6571This takes a binary file for reading a pickle data stream.
6572
6573The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006574protocol argument is needed. Bytes past the pickled object's
6575representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006576
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006577The argument *file* must have two methods, a read() method that takes
6578an integer argument, and a readline() method that requires no
6579arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006580binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006581other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006582
6583Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6584which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006585generated by Python 2. If *fix_imports* is True, pickle will try to
6586map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006587*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006588instances pickled by Python 2; these default to 'ASCII' and 'strict',
6589respectively. The *encoding* can be 'bytes' to read these 8-bit
6590string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006591[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006592
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006593static int
Larry Hastings89964c42015-04-14 18:07:59 -04006594_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6595 int fix_imports, const char *encoding,
6596 const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00006597/*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006598{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006599 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006601 /* In case of multiple __init__() calls, clear previous content. */
6602 if (self->read != NULL)
6603 (void)Unpickler_clear(self);
6604
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006605 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006606 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006607
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006608 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006609 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006610
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006611 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006612 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006613 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006614
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006615 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006616 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6617 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006618 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006619 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006620 }
6621 else {
6622 self->pers_func = NULL;
6623 }
6624
6625 self->stack = (Pdata *)Pdata_New();
6626 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006627 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006629 self->memo_size = 32;
6630 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006631 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006632 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006633
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006634 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006635
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006636 return 0;
6637}
6638
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006639
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006640/* Define a proxy object for the Unpickler's internal memo object. This is to
6641 * avoid breaking code like:
6642 * unpickler.memo.clear()
6643 * and
6644 * unpickler.memo = saved_memo
6645 * Is this a good idea? Not really, but we don't want to break code that uses
6646 * it. Note that we don't implement the entire mapping API here. This is
6647 * intentional, as these should be treated as black-box implementation details.
6648 *
6649 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006650 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006651 */
6652
Larry Hastings61272b72014-01-07 12:41:53 -08006653/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006654_pickle.UnpicklerMemoProxy.clear
6655
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006656Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006657[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006658
Larry Hastings3cceb382014-01-04 11:09:09 -08006659static PyObject *
6660_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006661/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006662{
6663 _Unpickler_MemoCleanup(self->unpickler);
6664 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6665 if (self->unpickler->memo == NULL)
6666 return NULL;
6667 Py_RETURN_NONE;
6668}
6669
Larry Hastings61272b72014-01-07 12:41:53 -08006670/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006671_pickle.UnpicklerMemoProxy.copy
6672
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006673Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006674[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006675
Larry Hastings3cceb382014-01-04 11:09:09 -08006676static PyObject *
6677_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006678/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006679{
6680 Py_ssize_t i;
6681 PyObject *new_memo = PyDict_New();
6682 if (new_memo == NULL)
6683 return NULL;
6684
6685 for (i = 0; i < self->unpickler->memo_size; i++) {
6686 int status;
6687 PyObject *key, *value;
6688
6689 value = self->unpickler->memo[i];
6690 if (value == NULL)
6691 continue;
6692
6693 key = PyLong_FromSsize_t(i);
6694 if (key == NULL)
6695 goto error;
6696 status = PyDict_SetItem(new_memo, key, value);
6697 Py_DECREF(key);
6698 if (status < 0)
6699 goto error;
6700 }
6701 return new_memo;
6702
6703error:
6704 Py_DECREF(new_memo);
6705 return NULL;
6706}
6707
Larry Hastings61272b72014-01-07 12:41:53 -08006708/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006709_pickle.UnpicklerMemoProxy.__reduce__
6710
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006711Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006712[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006713
Larry Hastings3cceb382014-01-04 11:09:09 -08006714static PyObject *
6715_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006716/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006717{
6718 PyObject *reduce_value;
6719 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006720 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006721 if (contents == NULL)
6722 return NULL;
6723
6724 reduce_value = PyTuple_New(2);
6725 if (reduce_value == NULL) {
6726 Py_DECREF(contents);
6727 return NULL;
6728 }
6729 constructor_args = PyTuple_New(1);
6730 if (constructor_args == NULL) {
6731 Py_DECREF(contents);
6732 Py_DECREF(reduce_value);
6733 return NULL;
6734 }
6735 PyTuple_SET_ITEM(constructor_args, 0, contents);
6736 Py_INCREF((PyObject *)&PyDict_Type);
6737 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6738 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6739 return reduce_value;
6740}
6741
6742static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6744 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6745 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006746 {NULL, NULL} /* sentinel */
6747};
6748
6749static void
6750UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6751{
6752 PyObject_GC_UnTrack(self);
6753 Py_XDECREF(self->unpickler);
6754 PyObject_GC_Del((PyObject *)self);
6755}
6756
6757static int
6758UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6759 visitproc visit, void *arg)
6760{
6761 Py_VISIT(self->unpickler);
6762 return 0;
6763}
6764
6765static int
6766UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6767{
6768 Py_CLEAR(self->unpickler);
6769 return 0;
6770}
6771
6772static PyTypeObject UnpicklerMemoProxyType = {
6773 PyVarObject_HEAD_INIT(NULL, 0)
6774 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6775 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6776 0,
6777 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6778 0, /* tp_print */
6779 0, /* tp_getattr */
6780 0, /* tp_setattr */
6781 0, /* tp_compare */
6782 0, /* tp_repr */
6783 0, /* tp_as_number */
6784 0, /* tp_as_sequence */
6785 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006786 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006787 0, /* tp_call */
6788 0, /* tp_str */
6789 PyObject_GenericGetAttr, /* tp_getattro */
6790 PyObject_GenericSetAttr, /* tp_setattro */
6791 0, /* tp_as_buffer */
6792 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6793 0, /* tp_doc */
6794 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6795 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6796 0, /* tp_richcompare */
6797 0, /* tp_weaklistoffset */
6798 0, /* tp_iter */
6799 0, /* tp_iternext */
6800 unpicklerproxy_methods, /* tp_methods */
6801};
6802
6803static PyObject *
6804UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6805{
6806 UnpicklerMemoProxyObject *self;
6807
6808 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6809 &UnpicklerMemoProxyType);
6810 if (self == NULL)
6811 return NULL;
6812 Py_INCREF(unpickler);
6813 self->unpickler = unpickler;
6814 PyObject_GC_Track(self);
6815 return (PyObject *)self;
6816}
6817
6818/*****************************************************************************/
6819
6820
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006821static PyObject *
6822Unpickler_get_memo(UnpicklerObject *self)
6823{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006824 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006825}
6826
6827static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006828Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006829{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006830 PyObject **new_memo;
6831 Py_ssize_t new_memo_size = 0;
6832 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006833
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006834 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006835 PyErr_SetString(PyExc_TypeError,
6836 "attribute deletion is not supported");
6837 return -1;
6838 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006839
6840 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6841 UnpicklerObject *unpickler =
6842 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6843
6844 new_memo_size = unpickler->memo_size;
6845 new_memo = _Unpickler_NewMemo(new_memo_size);
6846 if (new_memo == NULL)
6847 return -1;
6848
6849 for (i = 0; i < new_memo_size; i++) {
6850 Py_XINCREF(unpickler->memo[i]);
6851 new_memo[i] = unpickler->memo[i];
6852 }
6853 }
6854 else if (PyDict_Check(obj)) {
6855 Py_ssize_t i = 0;
6856 PyObject *key, *value;
6857
6858 new_memo_size = PyDict_Size(obj);
6859 new_memo = _Unpickler_NewMemo(new_memo_size);
6860 if (new_memo == NULL)
6861 return -1;
6862
6863 while (PyDict_Next(obj, &i, &key, &value)) {
6864 Py_ssize_t idx;
6865 if (!PyLong_Check(key)) {
6866 PyErr_SetString(PyExc_TypeError,
6867 "memo key must be integers");
6868 goto error;
6869 }
6870 idx = PyLong_AsSsize_t(key);
6871 if (idx == -1 && PyErr_Occurred())
6872 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006873 if (idx < 0) {
6874 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006875 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006876 goto error;
6877 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006878 if (_Unpickler_MemoPut(self, idx, value) < 0)
6879 goto error;
6880 }
6881 }
6882 else {
6883 PyErr_Format(PyExc_TypeError,
6884 "'memo' attribute must be an UnpicklerMemoProxy object"
6885 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006886 return -1;
6887 }
6888
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006889 _Unpickler_MemoCleanup(self);
6890 self->memo_size = new_memo_size;
6891 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006892
6893 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006894
6895 error:
6896 if (new_memo_size) {
6897 i = new_memo_size;
6898 while (--i >= 0) {
6899 Py_XDECREF(new_memo[i]);
6900 }
6901 PyMem_FREE(new_memo);
6902 }
6903 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006904}
6905
6906static PyObject *
6907Unpickler_get_persload(UnpicklerObject *self)
6908{
6909 if (self->pers_func == NULL)
6910 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6911 else
6912 Py_INCREF(self->pers_func);
6913 return self->pers_func;
6914}
6915
6916static int
6917Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6918{
6919 PyObject *tmp;
6920
6921 if (value == NULL) {
6922 PyErr_SetString(PyExc_TypeError,
6923 "attribute deletion is not supported");
6924 return -1;
6925 }
6926 if (!PyCallable_Check(value)) {
6927 PyErr_SetString(PyExc_TypeError,
6928 "persistent_load must be a callable taking "
6929 "one argument");
6930 return -1;
6931 }
6932
6933 tmp = self->pers_func;
6934 Py_INCREF(value);
6935 self->pers_func = value;
6936 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6937
6938 return 0;
6939}
6940
6941static PyGetSetDef Unpickler_getsets[] = {
6942 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6943 {"persistent_load", (getter)Unpickler_get_persload,
6944 (setter)Unpickler_set_persload},
6945 {NULL}
6946};
6947
6948static PyTypeObject Unpickler_Type = {
6949 PyVarObject_HEAD_INIT(NULL, 0)
6950 "_pickle.Unpickler", /*tp_name*/
6951 sizeof(UnpicklerObject), /*tp_basicsize*/
6952 0, /*tp_itemsize*/
6953 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6954 0, /*tp_print*/
6955 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006956 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006957 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006958 0, /*tp_repr*/
6959 0, /*tp_as_number*/
6960 0, /*tp_as_sequence*/
6961 0, /*tp_as_mapping*/
6962 0, /*tp_hash*/
6963 0, /*tp_call*/
6964 0, /*tp_str*/
6965 0, /*tp_getattro*/
6966 0, /*tp_setattro*/
6967 0, /*tp_as_buffer*/
6968 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006969 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006970 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6971 (inquiry)Unpickler_clear, /*tp_clear*/
6972 0, /*tp_richcompare*/
6973 0, /*tp_weaklistoffset*/
6974 0, /*tp_iter*/
6975 0, /*tp_iternext*/
6976 Unpickler_methods, /*tp_methods*/
6977 0, /*tp_members*/
6978 Unpickler_getsets, /*tp_getset*/
6979 0, /*tp_base*/
6980 0, /*tp_dict*/
6981 0, /*tp_descr_get*/
6982 0, /*tp_descr_set*/
6983 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006984 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006985 PyType_GenericAlloc, /*tp_alloc*/
6986 PyType_GenericNew, /*tp_new*/
6987 PyObject_GC_Del, /*tp_free*/
6988 0, /*tp_is_gc*/
6989};
6990
Larry Hastings61272b72014-01-07 12:41:53 -08006991/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006992
6993_pickle.dump
6994
6995 obj: object
6996 file: object
6997 protocol: object = NULL
6998 *
6999 fix_imports: bool = True
7000
7001Write a pickled representation of obj to the open file object file.
7002
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007003This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7004be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007005
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007006The optional *protocol* argument tells the pickler to use the given
7007protocol supported protocols are 0, 1, 2, 3 and 4. The default
7008protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007009
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007010Specifying a negative protocol version selects the highest protocol
7011version supported. The higher the protocol used, the more recent the
7012version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007013
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007014The *file* argument must have a write() method that accepts a single
7015bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007016writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007017this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007018
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007019If *fix_imports* is True and protocol is less than 3, pickle will try
7020to map the new Python 3 names to the old module names used in Python
70212, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007022[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007023
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007024static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007025_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7026 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00007027/*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007028{
7029 PicklerObject *pickler = _Pickler_New();
7030
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007031 if (pickler == NULL)
7032 return NULL;
7033
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007034 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007035 goto error;
7036
7037 if (_Pickler_SetOutputStream(pickler, file) < 0)
7038 goto error;
7039
7040 if (dump(pickler, obj) < 0)
7041 goto error;
7042
7043 if (_Pickler_FlushToFile(pickler) < 0)
7044 goto error;
7045
7046 Py_DECREF(pickler);
7047 Py_RETURN_NONE;
7048
7049 error:
7050 Py_XDECREF(pickler);
7051 return NULL;
7052}
7053
Larry Hastings61272b72014-01-07 12:41:53 -08007054/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007055
7056_pickle.dumps
7057
7058 obj: object
7059 protocol: object = NULL
7060 *
7061 fix_imports: bool = True
7062
7063Return the pickled representation of the object as a bytes object.
7064
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007065The optional *protocol* argument tells the pickler to use the given
7066protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7067protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007068
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007069Specifying a negative protocol version selects the highest protocol
7070version supported. The higher the protocol used, the more recent the
7071version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007072
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007073If *fix_imports* is True and *protocol* is less than 3, pickle will
7074try to map the new Python 3 names to the old module names used in
7075Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007076[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007077
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007078static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007079_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7080 int fix_imports)
7081/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007082{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007083 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007084 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007085
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007086 if (pickler == NULL)
7087 return NULL;
7088
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007089 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007090 goto error;
7091
7092 if (dump(pickler, obj) < 0)
7093 goto error;
7094
7095 result = _Pickler_GetString(pickler);
7096 Py_DECREF(pickler);
7097 return result;
7098
7099 error:
7100 Py_XDECREF(pickler);
7101 return NULL;
7102}
7103
Larry Hastings61272b72014-01-07 12:41:53 -08007104/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007105
7106_pickle.load
7107
7108 file: object
7109 *
7110 fix_imports: bool = True
7111 encoding: str = 'ASCII'
7112 errors: str = 'strict'
7113
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007114Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007115
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007116This is equivalent to ``Unpickler(file).load()``, but may be more
7117efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007118
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007119The protocol version of the pickle is detected automatically, so no
7120protocol argument is needed. Bytes past the pickled object's
7121representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007122
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007123The argument *file* must have two methods, a read() method that takes
7124an integer argument, and a readline() method that requires no
7125arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007126binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007127other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007128
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007129Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7130which are used to control compatiblity support for pickle stream
7131generated by Python 2. If *fix_imports* is True, pickle will try to
7132map the old Python 2 names to the new names used in Python 3. The
7133*encoding* and *errors* tell pickle how to decode 8-bit string
7134instances pickled by Python 2; these default to 'ASCII' and 'strict',
7135respectively. The *encoding* can be 'bytes' to read these 8-bit
7136string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007137[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007139static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007140_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7141 const char *encoding, const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00007142/*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007143{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007144 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007145 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007146
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007147 if (unpickler == NULL)
7148 return NULL;
7149
7150 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7151 goto error;
7152
7153 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7154 goto error;
7155
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007156 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007157
7158 result = load(unpickler);
7159 Py_DECREF(unpickler);
7160 return result;
7161
7162 error:
7163 Py_XDECREF(unpickler);
7164 return NULL;
7165}
7166
Larry Hastings61272b72014-01-07 12:41:53 -08007167/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007168
7169_pickle.loads
7170
7171 data: object
7172 *
7173 fix_imports: bool = True
7174 encoding: str = 'ASCII'
7175 errors: str = 'strict'
7176
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007177Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007178
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007179The protocol version of the pickle is detected automatically, so no
7180protocol argument is needed. Bytes past the pickled object's
7181representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007182
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007183Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7184which are used to control compatiblity support for pickle stream
7185generated by Python 2. If *fix_imports* is True, pickle will try to
7186map the old Python 2 names to the new names used in Python 3. The
7187*encoding* and *errors* tell pickle how to decode 8-bit string
7188instances pickled by Python 2; these default to 'ASCII' and 'strict',
7189respectively. The *encoding* can be 'bytes' to read these 8-bit
7190string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007191[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007193static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007194_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7195 const char *encoding, const char *errors)
7196/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007197{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007198 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007200
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007201 if (unpickler == NULL)
7202 return NULL;
7203
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007204 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007205 goto error;
7206
7207 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7208 goto error;
7209
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007210 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007211
7212 result = load(unpickler);
7213 Py_DECREF(unpickler);
7214 return result;
7215
7216 error:
7217 Py_XDECREF(unpickler);
7218 return NULL;
7219}
7220
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007221static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007222 _PICKLE_DUMP_METHODDEF
7223 _PICKLE_DUMPS_METHODDEF
7224 _PICKLE_LOAD_METHODDEF
7225 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007226 {NULL, NULL} /* sentinel */
7227};
7228
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007229static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007230pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007231{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007232 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007233 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007234}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007235
Stefan Krahf483b0f2013-12-14 13:43:10 +01007236static void
7237pickle_free(PyObject *m)
7238{
7239 _Pickle_ClearState(_Pickle_GetState(m));
7240}
7241
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007242static int
7243pickle_traverse(PyObject *m, visitproc visit, void *arg)
7244{
7245 PickleState *st = _Pickle_GetState(m);
7246 Py_VISIT(st->PickleError);
7247 Py_VISIT(st->PicklingError);
7248 Py_VISIT(st->UnpicklingError);
7249 Py_VISIT(st->dispatch_table);
7250 Py_VISIT(st->extension_registry);
7251 Py_VISIT(st->extension_cache);
7252 Py_VISIT(st->inverted_registry);
7253 Py_VISIT(st->name_mapping_2to3);
7254 Py_VISIT(st->import_mapping_2to3);
7255 Py_VISIT(st->name_mapping_3to2);
7256 Py_VISIT(st->import_mapping_3to2);
7257 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007258 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007259 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007260}
7261
7262static struct PyModuleDef _picklemodule = {
7263 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007264 "_pickle", /* m_name */
7265 pickle_module_doc, /* m_doc */
7266 sizeof(PickleState), /* m_size */
7267 pickle_methods, /* m_methods */
7268 NULL, /* m_reload */
7269 pickle_traverse, /* m_traverse */
7270 pickle_clear, /* m_clear */
7271 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007272};
7273
7274PyMODINIT_FUNC
7275PyInit__pickle(void)
7276{
7277 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007278 PickleState *st;
7279
7280 m = PyState_FindModule(&_picklemodule);
7281 if (m) {
7282 Py_INCREF(m);
7283 return m;
7284 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007285
7286 if (PyType_Ready(&Unpickler_Type) < 0)
7287 return NULL;
7288 if (PyType_Ready(&Pickler_Type) < 0)
7289 return NULL;
7290 if (PyType_Ready(&Pdata_Type) < 0)
7291 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007292 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7293 return NULL;
7294 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7295 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007296
7297 /* Create the module and add the functions. */
7298 m = PyModule_Create(&_picklemodule);
7299 if (m == NULL)
7300 return NULL;
7301
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007302 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007303 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7304 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007305 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007306 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7307 return NULL;
7308
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007309 st = _Pickle_GetState(m);
7310
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007311 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007312 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7313 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007314 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007315 st->PicklingError = \
7316 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7317 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007318 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007319 st->UnpicklingError = \
7320 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7321 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007322 return NULL;
7323
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007324 Py_INCREF(st->PickleError);
7325 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007326 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007327 Py_INCREF(st->PicklingError);
7328 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007329 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007330 Py_INCREF(st->UnpicklingError);
7331 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007332 return NULL;
7333
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007334 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007335 return NULL;
7336
7337 return m;
7338}