blob: abaf4e522980c2f189eac4eaa0d0cd84ccdc5e65 [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);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800196}
197
198/* Initialize the given pickle module state. */
199static int
200_Pickle_InitState(PickleState *st)
201{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300202 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800203 PyObject *copyreg = NULL;
204 PyObject *compat_pickle = NULL;
205 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300206 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800207
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300208 builtins = PyEval_GetBuiltins();
209 if (builtins == NULL)
210 goto error;
211 st->getattr = PyDict_GetItemString(builtins, "getattr");
212 if (st->getattr == NULL)
213 goto error;
214 Py_INCREF(st->getattr);
215
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800216 copyreg = PyImport_ImportModule("copyreg");
217 if (!copyreg)
218 goto error;
219 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
220 if (!st->dispatch_table)
221 goto error;
222 if (!PyDict_CheckExact(st->dispatch_table)) {
223 PyErr_Format(PyExc_RuntimeError,
224 "copyreg.dispatch_table should be a dict, not %.200s",
225 Py_TYPE(st->dispatch_table)->tp_name);
226 goto error;
227 }
228 st->extension_registry = \
229 PyObject_GetAttrString(copyreg, "_extension_registry");
230 if (!st->extension_registry)
231 goto error;
232 if (!PyDict_CheckExact(st->extension_registry)) {
233 PyErr_Format(PyExc_RuntimeError,
234 "copyreg._extension_registry should be a dict, "
235 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
236 goto error;
237 }
238 st->inverted_registry = \
239 PyObject_GetAttrString(copyreg, "_inverted_registry");
240 if (!st->inverted_registry)
241 goto error;
242 if (!PyDict_CheckExact(st->inverted_registry)) {
243 PyErr_Format(PyExc_RuntimeError,
244 "copyreg._inverted_registry should be a dict, "
245 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
246 goto error;
247 }
248 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
249 if (!st->extension_cache)
250 goto error;
251 if (!PyDict_CheckExact(st->extension_cache)) {
252 PyErr_Format(PyExc_RuntimeError,
253 "copyreg._extension_cache should be a dict, "
254 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
255 goto error;
256 }
257 Py_CLEAR(copyreg);
258
259 /* Load the 2.x -> 3.x stdlib module mapping tables */
260 compat_pickle = PyImport_ImportModule("_compat_pickle");
261 if (!compat_pickle)
262 goto error;
263 st->name_mapping_2to3 = \
264 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
265 if (!st->name_mapping_2to3)
266 goto error;
267 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
268 PyErr_Format(PyExc_RuntimeError,
269 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
270 Py_TYPE(st->name_mapping_2to3)->tp_name);
271 goto error;
272 }
273 st->import_mapping_2to3 = \
274 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
275 if (!st->import_mapping_2to3)
276 goto error;
277 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
278 PyErr_Format(PyExc_RuntimeError,
279 "_compat_pickle.IMPORT_MAPPING should be a dict, "
280 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
281 goto error;
282 }
283 /* ... and the 3.x -> 2.x mapping tables */
284 st->name_mapping_3to2 = \
285 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
286 if (!st->name_mapping_3to2)
287 goto error;
288 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
289 PyErr_Format(PyExc_RuntimeError,
290 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
291 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
292 goto error;
293 }
294 st->import_mapping_3to2 = \
295 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
296 if (!st->import_mapping_3to2)
297 goto error;
298 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
299 PyErr_Format(PyExc_RuntimeError,
300 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
301 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
302 goto error;
303 }
304 Py_CLEAR(compat_pickle);
305
306 codecs = PyImport_ImportModule("codecs");
307 if (codecs == NULL)
308 goto error;
309 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
310 if (st->codecs_encode == NULL) {
311 goto error;
312 }
313 if (!PyCallable_Check(st->codecs_encode)) {
314 PyErr_Format(PyExc_RuntimeError,
315 "codecs.encode should be a callable, not %.200s",
316 Py_TYPE(st->codecs_encode)->tp_name);
317 goto error;
318 }
319 Py_CLEAR(codecs);
320
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300321 functools = PyImport_ImportModule("functools");
322 if (!functools)
323 goto error;
324 st->partial = PyObject_GetAttrString(functools, "partial");
325 if (!st->partial)
326 goto error;
327 Py_CLEAR(functools);
328
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800329 return 0;
330
331 error:
332 Py_CLEAR(copyreg);
333 Py_CLEAR(compat_pickle);
334 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300335 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800336 _Pickle_ClearState(st);
337 return -1;
338}
339
340/* Helper for calling a function with a single argument quickly.
341
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800342 This function steals the reference of the given argument. */
343static PyObject *
344_Pickle_FastCall(PyObject *func, PyObject *obj)
345{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800346 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800347 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800348
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800349 /* Note: this function used to reuse the argument tuple. This used to give
350 a slight performance boost with older pickle implementations where many
351 unbuffered reads occurred (thus needing many function calls).
352
353 However, this optimization was removed because it was too complicated
354 to get right. It abused the C API for tuples to mutate them which led
355 to subtle reference counting and concurrency bugs. Furthermore, the
356 introduction of protocol 4 and the prefetching optimization via peek()
357 significantly reduced the number of function calls we do. Thus, the
358 benefits became marginal at best. */
359
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800360 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800361 Py_DECREF(obj);
362 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800363 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 PyTuple_SET_ITEM(arg_tuple, 0, obj);
365 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800366 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800367 return result;
368}
369
370/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000371
372static int
373stack_underflow(void)
374{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800375 PickleState *st = _Pickle_GetGlobalState();
376 PyErr_SetString(st->UnpicklingError, "unpickling stack underflow");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000377 return -1;
378}
379
380/* Internal data type used as the unpickling stack. */
381typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000382 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000383 PyObject **data;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000384 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000385} Pdata;
386
387static void
388Pdata_dealloc(Pdata *self)
389{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200390 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000391 while (--i >= 0) {
392 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000393 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000394 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000395 PyObject_Del(self);
396}
397
398static PyTypeObject Pdata_Type = {
399 PyVarObject_HEAD_INIT(NULL, 0)
400 "_pickle.Pdata", /*tp_name*/
401 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200402 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000403 (destructor)Pdata_dealloc, /*tp_dealloc*/
404};
405
406static PyObject *
407Pdata_New(void)
408{
409 Pdata *self;
410
411 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
412 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000413 Py_SIZE(self) = 0;
414 self->allocated = 8;
415 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000416 if (self->data)
417 return (PyObject *)self;
418 Py_DECREF(self);
419 return PyErr_NoMemory();
420}
421
422
423/* Retain only the initial clearto items. If clearto >= the current
424 * number of items, this is a (non-erroneous) NOP.
425 */
426static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200427Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000428{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200429 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430
431 if (clearto < 0)
432 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000433 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 return 0;
435
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000436 while (--i >= clearto) {
437 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000438 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000439 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000440 return 0;
441}
442
443static int
444Pdata_grow(Pdata *self)
445{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000446 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200447 size_t allocated = (size_t)self->allocated;
448 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450 new_allocated = (allocated >> 3) + 6;
451 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200452 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500455 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000456 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000458
459 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200460 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000461 return 0;
462
463 nomemory:
464 PyErr_NoMemory();
465 return -1;
466}
467
468/* D is a Pdata*. Pop the topmost element and store it into V, which
469 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
470 * is raised and V is set to NULL.
471 */
472static PyObject *
473Pdata_pop(Pdata *self)
474{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800475 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000476 if (Py_SIZE(self) == 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800477 PyErr_SetString(st->UnpicklingError, "bad pickle data");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000478 return NULL;
479 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000480 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000481}
482#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
483
484static int
485Pdata_push(Pdata *self, PyObject *obj)
486{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000487 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000488 return -1;
489 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000490 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000491 return 0;
492}
493
494/* Push an object on stack, transferring its ownership to the stack. */
495#define PDATA_PUSH(D, O, ER) do { \
496 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
497
498/* Push an object on stack, adding a new reference to the object. */
499#define PDATA_APPEND(D, O, ER) do { \
500 Py_INCREF((O)); \
501 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
502
503static PyObject *
504Pdata_poptuple(Pdata *self, Py_ssize_t start)
505{
506 PyObject *tuple;
507 Py_ssize_t len, i, j;
508
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000509 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000510 tuple = PyTuple_New(len);
511 if (tuple == NULL)
512 return NULL;
513 for (i = start, j = 0; j < len; i++, j++)
514 PyTuple_SET_ITEM(tuple, j, self->data[i]);
515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000516 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000517 return tuple;
518}
519
520static PyObject *
521Pdata_poplist(Pdata *self, Py_ssize_t start)
522{
523 PyObject *list;
524 Py_ssize_t len, i, j;
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 list = PyList_New(len);
528 if (list == NULL)
529 return NULL;
530 for (i = start, j = 0; j < len; i++, j++)
531 PyList_SET_ITEM(list, j, self->data[i]);
532
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000533 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000534 return list;
535}
536
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000537typedef struct {
538 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200539 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000540} PyMemoEntry;
541
542typedef struct {
543 Py_ssize_t mt_mask;
544 Py_ssize_t mt_used;
545 Py_ssize_t mt_allocated;
546 PyMemoEntry *mt_table;
547} PyMemoTable;
548
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000549typedef struct PicklerObject {
550 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000551 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000552 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000553 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000554 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100555 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000556
557 PyObject *write; /* write() method of the output stream. */
558 PyObject *output_buffer; /* Write into a local bytearray buffer before
559 flushing to the stream. */
560 Py_ssize_t output_len; /* Length of output_buffer. */
561 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 int proto; /* Pickle protocol number, >= 0 */
563 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100564 int framing; /* True when framing is enabled, proto >= 4 */
565 Py_ssize_t frame_start; /* Position in output_buffer where the
566 where the current frame begins. -1 if there
567 is no frame currently open. */
568
569 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000570 int fast; /* Enable fast mode if set to a true value.
571 The fast mode disable the usage of memo,
572 therefore speeding the pickling process by
573 not generating superfluous PUT opcodes. It
574 should not be used if with self-referential
575 objects. */
576 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000577 int fix_imports; /* Indicate whether Pickler should fix
578 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000579 PyObject *fast_memo;
580} PicklerObject;
581
582typedef struct UnpicklerObject {
583 PyObject_HEAD
584 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000585
586 /* The unpickler memo is just an array of PyObject *s. Using a dict
587 is unnecessary, since the keys are contiguous ints. */
588 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100589 Py_ssize_t memo_size; /* Capacity of the memo array */
590 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000591
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000592 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000593
594 Py_buffer buffer;
595 char *input_buffer;
596 char *input_line;
597 Py_ssize_t input_len;
598 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000599 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100600
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601 PyObject *read; /* read() method of the input stream. */
602 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000603 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000604
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000605 char *encoding; /* Name of the encoding to be used for
606 decoding strings pickled using Python
607 2.x. The default value is "ASCII" */
608 char *errors; /* Name of errors handling scheme to used when
609 decoding strings. The default value is
610 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500611 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000612 objects. */
613 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
614 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000615 int proto; /* Protocol of the pickle loaded. */
616 int fix_imports; /* Indicate whether Unpickler should fix
617 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000618} UnpicklerObject;
619
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200620typedef struct {
621 PyObject_HEAD
622 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
623} PicklerMemoProxyObject;
624
625typedef struct {
626 PyObject_HEAD
627 UnpicklerObject *unpickler;
628} UnpicklerMemoProxyObject;
629
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000630/* Forward declarations */
631static int save(PicklerObject *, PyObject *, int);
632static int save_reduce(PicklerObject *, PyObject *, PyObject *);
633static PyTypeObject Pickler_Type;
634static PyTypeObject Unpickler_Type;
635
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200636#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000638/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300639 A custom hashtable mapping void* to Python ints. This is used by the pickler
640 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000641 a bunch of unnecessary object creation. This makes a huge performance
642 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000644#define MT_MINSIZE 8
645#define PERTURB_SHIFT 5
646
647
648static PyMemoTable *
649PyMemoTable_New(void)
650{
651 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
652 if (memo == NULL) {
653 PyErr_NoMemory();
654 return NULL;
655 }
656
657 memo->mt_used = 0;
658 memo->mt_allocated = MT_MINSIZE;
659 memo->mt_mask = MT_MINSIZE - 1;
660 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
661 if (memo->mt_table == NULL) {
662 PyMem_FREE(memo);
663 PyErr_NoMemory();
664 return NULL;
665 }
666 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
667
668 return memo;
669}
670
671static PyMemoTable *
672PyMemoTable_Copy(PyMemoTable *self)
673{
674 Py_ssize_t i;
675 PyMemoTable *new = PyMemoTable_New();
676 if (new == NULL)
677 return NULL;
678
679 new->mt_used = self->mt_used;
680 new->mt_allocated = self->mt_allocated;
681 new->mt_mask = self->mt_mask;
682 /* The table we get from _New() is probably smaller than we wanted.
683 Free it and allocate one that's the right size. */
684 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500685 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000686 if (new->mt_table == NULL) {
687 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200688 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000689 return NULL;
690 }
691 for (i = 0; i < self->mt_allocated; i++) {
692 Py_XINCREF(self->mt_table[i].me_key);
693 }
694 memcpy(new->mt_table, self->mt_table,
695 sizeof(PyMemoEntry) * self->mt_allocated);
696
697 return new;
698}
699
700static Py_ssize_t
701PyMemoTable_Size(PyMemoTable *self)
702{
703 return self->mt_used;
704}
705
706static int
707PyMemoTable_Clear(PyMemoTable *self)
708{
709 Py_ssize_t i = self->mt_allocated;
710
711 while (--i >= 0) {
712 Py_XDECREF(self->mt_table[i].me_key);
713 }
714 self->mt_used = 0;
715 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
716 return 0;
717}
718
719static void
720PyMemoTable_Del(PyMemoTable *self)
721{
722 if (self == NULL)
723 return;
724 PyMemoTable_Clear(self);
725
726 PyMem_FREE(self->mt_table);
727 PyMem_FREE(self);
728}
729
730/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
731 can be considerably simpler than dictobject.c's lookdict(). */
732static PyMemoEntry *
733_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
734{
735 size_t i;
736 size_t perturb;
737 size_t mask = (size_t)self->mt_mask;
738 PyMemoEntry *table = self->mt_table;
739 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000740 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000741
742 i = hash & mask;
743 entry = &table[i];
744 if (entry->me_key == NULL || entry->me_key == key)
745 return entry;
746
747 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
748 i = (i << 2) + i + perturb + 1;
749 entry = &table[i & mask];
750 if (entry->me_key == NULL || entry->me_key == key)
751 return entry;
752 }
753 assert(0); /* Never reached */
754 return NULL;
755}
756
757/* Returns -1 on failure, 0 on success. */
758static int
759_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
760{
761 PyMemoEntry *oldtable = NULL;
762 PyMemoEntry *oldentry, *newentry;
763 Py_ssize_t new_size = MT_MINSIZE;
764 Py_ssize_t to_process;
765
766 assert(min_size > 0);
767
768 /* Find the smallest valid table size >= min_size. */
769 while (new_size < min_size && new_size > 0)
770 new_size <<= 1;
771 if (new_size <= 0) {
772 PyErr_NoMemory();
773 return -1;
774 }
775 /* new_size needs to be a power of two. */
776 assert((new_size & (new_size - 1)) == 0);
777
778 /* Allocate new table. */
779 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500780 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000781 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200782 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000783 PyErr_NoMemory();
784 return -1;
785 }
786 self->mt_allocated = new_size;
787 self->mt_mask = new_size - 1;
788 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
789
790 /* Copy entries from the old table. */
791 to_process = self->mt_used;
792 for (oldentry = oldtable; to_process > 0; oldentry++) {
793 if (oldentry->me_key != NULL) {
794 to_process--;
795 /* newentry is a pointer to a chunk of the new
796 mt_table, so we're setting the key:value pair
797 in-place. */
798 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
799 newentry->me_key = oldentry->me_key;
800 newentry->me_value = oldentry->me_value;
801 }
802 }
803
804 /* Deallocate the old table. */
805 PyMem_FREE(oldtable);
806 return 0;
807}
808
809/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200810static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000811PyMemoTable_Get(PyMemoTable *self, PyObject *key)
812{
813 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
814 if (entry->me_key == NULL)
815 return NULL;
816 return &entry->me_value;
817}
818
819/* Returns -1 on failure, 0 on success. */
820static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200821PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000822{
823 PyMemoEntry *entry;
824
825 assert(key != NULL);
826
827 entry = _PyMemoTable_Lookup(self, key);
828 if (entry->me_key != NULL) {
829 entry->me_value = value;
830 return 0;
831 }
832 Py_INCREF(key);
833 entry->me_key = key;
834 entry->me_value = value;
835 self->mt_used++;
836
837 /* If we added a key, we can safely resize. Otherwise just return!
838 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
839 *
840 * Quadrupling the size improves average table sparseness
841 * (reducing collisions) at the cost of some memory. It also halves
842 * the number of expensive resize operations in a growing memo table.
843 *
844 * Very large memo tables (over 50K items) use doubling instead.
845 * This may help applications with severe memory constraints.
846 */
847 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
848 return 0;
849 return _PyMemoTable_ResizeTable(self,
850 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
851}
852
853#undef MT_MINSIZE
854#undef PERTURB_SHIFT
855
856/*************************************************************************/
857
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000858
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000859static int
860_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000861{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000862 Py_CLEAR(self->output_buffer);
863 self->output_buffer =
864 PyBytes_FromStringAndSize(NULL, self->max_output_len);
865 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000866 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000867 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100868 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000869 return 0;
870}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000871
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100872static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100873_write_size64(char *out, size_t value)
874{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200875 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800876
877 assert(sizeof(size_t) <= 8);
878
879 for (i = 0; i < sizeof(size_t); i++) {
880 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
881 }
882 for (i = sizeof(size_t); i < 8; i++) {
883 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800884 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100885}
886
887static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100888_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
889{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100890 qdata[0] = FRAME;
891 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100892}
893
894static int
895_Pickler_CommitFrame(PicklerObject *self)
896{
897 size_t frame_len;
898 char *qdata;
899
900 if (!self->framing || self->frame_start == -1)
901 return 0;
902 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
903 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
904 _Pickler_WriteFrameHeader(self, qdata, frame_len);
905 self->frame_start = -1;
906 return 0;
907}
908
909static int
910_Pickler_OpcodeBoundary(PicklerObject *self)
911{
912 Py_ssize_t frame_len;
913
914 if (!self->framing || self->frame_start == -1)
915 return 0;
916 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
917 if (frame_len >= FRAME_SIZE_TARGET)
918 return _Pickler_CommitFrame(self);
919 else
920 return 0;
921}
922
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000923static PyObject *
924_Pickler_GetString(PicklerObject *self)
925{
926 PyObject *output_buffer = self->output_buffer;
927
928 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100929
930 if (_Pickler_CommitFrame(self))
931 return NULL;
932
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000933 self->output_buffer = NULL;
934 /* Resize down to exact size */
935 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
936 return NULL;
937 return output_buffer;
938}
939
940static int
941_Pickler_FlushToFile(PicklerObject *self)
942{
943 PyObject *output, *result;
944
945 assert(self->write != NULL);
946
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100947 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000948 output = _Pickler_GetString(self);
949 if (output == NULL)
950 return -1;
951
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800952 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000953 Py_XDECREF(result);
954 return (result == NULL) ? -1 : 0;
955}
956
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200957static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100958_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000959{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100960 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000961 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100962 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000963
964 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100965 need_new_frame = (self->framing && self->frame_start == -1);
966
967 if (need_new_frame)
968 n = data_len + FRAME_HEADER_SIZE;
969 else
970 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000971
972 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100973 if (required > self->max_output_len) {
974 /* Make place in buffer for the pickle chunk */
975 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
976 PyErr_NoMemory();
977 return -1;
978 }
979 self->max_output_len = (self->output_len + n) / 2 * 3;
980 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
981 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000982 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000983 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100984 if (need_new_frame) {
985 /* Setup new frame */
986 Py_ssize_t frame_start = self->output_len;
987 self->frame_start = frame_start;
988 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
989 /* Write an invalid value, for debugging */
990 buffer[frame_start + i] = 0xFE;
991 }
992 self->output_len += FRAME_HEADER_SIZE;
993 }
994 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000995 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100996 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000997 buffer[self->output_len + i] = s[i];
998 }
999 }
1000 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001001 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001002 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001003 self->output_len += data_len;
1004 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001005}
1006
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001007static PicklerObject *
1008_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001009{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001010 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001011
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001012 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1013 if (self == NULL)
1014 return NULL;
1015
1016 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001017 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001018 self->write = NULL;
1019 self->proto = 0;
1020 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001021 self->framing = 0;
1022 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001023 self->fast = 0;
1024 self->fast_nesting = 0;
1025 self->fix_imports = 0;
1026 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001027 self->max_output_len = WRITE_BUF_SIZE;
1028 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001029
1030 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001031 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1032 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001033
1034 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001035 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 return NULL;
1037 }
1038 return self;
1039}
1040
1041static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001042_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001043{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001044 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001046 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001047 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001048 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001049 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001050 proto = PyLong_AsLong(protocol);
1051 if (proto < 0) {
1052 if (proto == -1 && PyErr_Occurred())
1053 return -1;
1054 proto = HIGHEST_PROTOCOL;
1055 }
1056 else if (proto > HIGHEST_PROTOCOL) {
1057 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1058 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001059 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001060 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001061 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001062 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001063 self->bin = proto > 0;
1064 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001065 return 0;
1066}
1067
1068/* Returns -1 (with an exception set) on failure, 0 on success. This may
1069 be called once on a freshly created Pickler. */
1070static int
1071_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1072{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001073 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001075 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001076 if (self->write == NULL) {
1077 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1078 PyErr_SetString(PyExc_TypeError,
1079 "file must have a 'write' attribute");
1080 return -1;
1081 }
1082
1083 return 0;
1084}
1085
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001086/* Returns the size of the input on success, -1 on failure. This takes its
1087 own reference to `input`. */
1088static Py_ssize_t
1089_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1090{
1091 if (self->buffer.buf != NULL)
1092 PyBuffer_Release(&self->buffer);
1093 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1094 return -1;
1095 self->input_buffer = self->buffer.buf;
1096 self->input_len = self->buffer.len;
1097 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001098 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001099 return self->input_len;
1100}
1101
Antoine Pitrou04248a82010-10-12 20:51:21 +00001102static int
1103_Unpickler_SkipConsumed(UnpicklerObject *self)
1104{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001105 Py_ssize_t consumed;
1106 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001108 consumed = self->next_read_idx - self->prefetched_idx;
1109 if (consumed <= 0)
1110 return 0;
1111
1112 assert(self->peek); /* otherwise we did something wrong */
1113 /* This makes an useless copy... */
1114 r = PyObject_CallFunction(self->read, "n", consumed);
1115 if (r == NULL)
1116 return -1;
1117 Py_DECREF(r);
1118
1119 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001120 return 0;
1121}
1122
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001123static const Py_ssize_t READ_WHOLE_LINE = -1;
1124
1125/* If reading from a file, we need to only pull the bytes we need, since there
1126 may be multiple pickle objects arranged contiguously in the same input
1127 buffer.
1128
1129 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1130 bytes from the input stream/buffer.
1131
1132 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1133 failure; on success, returns the number of bytes read from the file.
1134
1135 On success, self->input_len will be 0; this is intentional so that when
1136 unpickling from a file, the "we've run out of data" code paths will trigger,
1137 causing the Unpickler to go back to the file for more data. Use the returned
1138 size to tell you how much data you can process. */
1139static Py_ssize_t
1140_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1141{
1142 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001143 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001144
1145 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001146
Antoine Pitrou04248a82010-10-12 20:51:21 +00001147 if (_Unpickler_SkipConsumed(self) < 0)
1148 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001149
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001150 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001151 PyObject *empty_tuple = PyTuple_New(0);
1152 data = PyObject_Call(self->readline, empty_tuple, NULL);
1153 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001154 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001155 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001156 PyObject *len;
1157 /* Prefetch some data without advancing the file pointer, if possible */
1158 if (self->peek && n < PREFETCH) {
1159 len = PyLong_FromSsize_t(PREFETCH);
1160 if (len == NULL)
1161 return -1;
1162 data = _Pickle_FastCall(self->peek, len);
1163 if (data == NULL) {
1164 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1165 return -1;
1166 /* peek() is probably not supported by the given file object */
1167 PyErr_Clear();
1168 Py_CLEAR(self->peek);
1169 }
1170 else {
1171 read_size = _Unpickler_SetStringInput(self, data);
1172 Py_DECREF(data);
1173 self->prefetched_idx = 0;
1174 if (n <= read_size)
1175 return n;
1176 }
1177 }
1178 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001179 if (len == NULL)
1180 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001181 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001182 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001183 if (data == NULL)
1184 return -1;
1185
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001186 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001187 Py_DECREF(data);
1188 return read_size;
1189}
1190
1191/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1192
1193 This should be used for all data reads, rather than accessing the unpickler's
1194 input buffer directly. This method deals correctly with reading from input
1195 streams, which the input buffer doesn't deal with.
1196
1197 Note that when reading from a file-like object, self->next_read_idx won't
1198 be updated (it should remain at 0 for the entire unpickling process). You
1199 should use this function's return value to know how many bytes you can
1200 consume.
1201
1202 Returns -1 (with an exception set) on failure. On success, return the
1203 number of chars read. */
1204static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001205_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001206{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001207 Py_ssize_t num_read;
1208
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001209 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001210 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1211 PickleState *st = _Pickle_GetGlobalState();
1212 PyErr_SetString(st->UnpicklingError,
1213 "read would overflow (invalid bytecode)");
1214 return -1;
1215 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001216 if (self->next_read_idx + n <= self->input_len) {
1217 *s = self->input_buffer + self->next_read_idx;
1218 self->next_read_idx += n;
1219 return n;
1220 }
1221 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001222 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001223 return -1;
1224 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001225 num_read = _Unpickler_ReadFromFile(self, n);
1226 if (num_read < 0)
1227 return -1;
1228 if (num_read < n) {
1229 PyErr_Format(PyExc_EOFError, "Ran out of input");
1230 return -1;
1231 }
1232 *s = self->input_buffer;
1233 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001234 return n;
1235}
1236
1237static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001238_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1239 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001240{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001241 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001242 if (input_line == NULL) {
1243 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001244 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001245 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001246
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001247 memcpy(input_line, line, len);
1248 input_line[len] = '\0';
1249 self->input_line = input_line;
1250 *result = self->input_line;
1251 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001252}
1253
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001254/* Read a line from the input stream/buffer. If we run off the end of the input
1255 before hitting \n, return the data we found.
1256
1257 Returns the number of chars read, or -1 on failure. */
1258static Py_ssize_t
1259_Unpickler_Readline(UnpicklerObject *self, char **result)
1260{
1261 Py_ssize_t i, num_read;
1262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001264 if (self->input_buffer[i] == '\n') {
1265 char *line_start = self->input_buffer + self->next_read_idx;
1266 num_read = i - self->next_read_idx + 1;
1267 self->next_read_idx = i + 1;
1268 return _Unpickler_CopyLine(self, line_start, num_read, result);
1269 }
1270 }
1271 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1273 if (num_read < 0)
1274 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001275 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001276 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001277 }
Victor Stinner121aab42011-09-29 23:40:53 +02001278
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001279 /* If we get here, we've run off the end of the input string. Return the
1280 remaining string and let the caller figure it out. */
1281 *result = self->input_buffer + self->next_read_idx;
1282 num_read = i - self->next_read_idx;
1283 self->next_read_idx = i;
1284 return num_read;
1285}
1286
1287/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1288 will be modified in place. */
1289static int
1290_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1291{
1292 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001293
1294 assert(new_size > self->memo_size);
1295
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001296 PyMem_RESIZE(self->memo, PyObject *, new_size);
1297 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001298 PyErr_NoMemory();
1299 return -1;
1300 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001301 for (i = self->memo_size; i < new_size; i++)
1302 self->memo[i] = NULL;
1303 self->memo_size = new_size;
1304 return 0;
1305}
1306
1307/* Returns NULL if idx is out of bounds. */
1308static PyObject *
1309_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1310{
1311 if (idx < 0 || idx >= self->memo_size)
1312 return NULL;
1313
1314 return self->memo[idx];
1315}
1316
1317/* Returns -1 (with an exception set) on failure, 0 on success.
1318 This takes its own reference to `value`. */
1319static int
1320_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1321{
1322 PyObject *old_item;
1323
1324 if (idx >= self->memo_size) {
1325 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1326 return -1;
1327 assert(idx < self->memo_size);
1328 }
1329 Py_INCREF(value);
1330 old_item = self->memo[idx];
1331 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001332 if (old_item != NULL) {
1333 Py_DECREF(old_item);
1334 }
1335 else {
1336 self->memo_len++;
1337 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001338 return 0;
1339}
1340
1341static PyObject **
1342_Unpickler_NewMemo(Py_ssize_t new_size)
1343{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001344 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001345 if (memo == NULL) {
1346 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001348 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001349 memset(memo, 0, new_size * sizeof(PyObject *));
1350 return memo;
1351}
1352
1353/* Free the unpickler's memo, taking care to decref any items left in it. */
1354static void
1355_Unpickler_MemoCleanup(UnpicklerObject *self)
1356{
1357 Py_ssize_t i;
1358 PyObject **memo = self->memo;
1359
1360 if (self->memo == NULL)
1361 return;
1362 self->memo = NULL;
1363 i = self->memo_size;
1364 while (--i >= 0) {
1365 Py_XDECREF(memo[i]);
1366 }
1367 PyMem_FREE(memo);
1368}
1369
1370static UnpicklerObject *
1371_Unpickler_New(void)
1372{
1373 UnpicklerObject *self;
1374
1375 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1376 if (self == NULL)
1377 return NULL;
1378
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001379 self->pers_func = NULL;
1380 self->input_buffer = NULL;
1381 self->input_line = NULL;
1382 self->input_len = 0;
1383 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001384 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001385 self->read = NULL;
1386 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001387 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001388 self->encoding = NULL;
1389 self->errors = NULL;
1390 self->marks = NULL;
1391 self->num_marks = 0;
1392 self->marks_size = 0;
1393 self->proto = 0;
1394 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001395 memset(&self->buffer, 0, sizeof(Py_buffer));
1396 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001397 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001398 self->memo = _Unpickler_NewMemo(self->memo_size);
1399 self->stack = (Pdata *)Pdata_New();
1400
1401 if (self->memo == NULL || self->stack == NULL) {
1402 Py_DECREF(self);
1403 return NULL;
1404 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001405
1406 return self;
1407}
1408
1409/* Returns -1 (with an exception set) on failure, 0 on success. This may
1410 be called once on a freshly created Pickler. */
1411static int
1412_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1413{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001414 _Py_IDENTIFIER(peek);
1415 _Py_IDENTIFIER(read);
1416 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001417
1418 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001419 if (self->peek == NULL) {
1420 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1421 PyErr_Clear();
1422 else
1423 return -1;
1424 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001425 self->read = _PyObject_GetAttrId(file, &PyId_read);
1426 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001427 if (self->readline == NULL || self->read == NULL) {
1428 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1429 PyErr_SetString(PyExc_TypeError,
1430 "file must have 'read' and 'readline' attributes");
1431 Py_CLEAR(self->read);
1432 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001433 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001434 return -1;
1435 }
1436 return 0;
1437}
1438
1439/* Returns -1 (with an exception set) on failure, 0 on success. This may
1440 be called once on a freshly created Pickler. */
1441static int
1442_Unpickler_SetInputEncoding(UnpicklerObject *self,
1443 const char *encoding,
1444 const char *errors)
1445{
1446 if (encoding == NULL)
1447 encoding = "ASCII";
1448 if (errors == NULL)
1449 errors = "strict";
1450
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001451 self->encoding = _PyMem_Strdup(encoding);
1452 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001453 if (self->encoding == NULL || self->errors == NULL) {
1454 PyErr_NoMemory();
1455 return -1;
1456 }
1457 return 0;
1458}
1459
1460/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001461static int
1462memo_get(PicklerObject *self, PyObject *key)
1463{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001464 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001465 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001466 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001467
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001468 value = PyMemoTable_Get(self->memo, key);
1469 if (value == NULL) {
1470 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001471 return -1;
1472 }
1473
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001474 if (!self->bin) {
1475 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001476 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1477 "%" PY_FORMAT_SIZE_T "d\n", *value);
1478 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001479 }
1480 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001481 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001482 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001483 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001484 len = 2;
1485 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001486 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001487 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001488 pdata[1] = (unsigned char)(*value & 0xff);
1489 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1490 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1491 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001492 len = 5;
1493 }
1494 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001495 PickleState *st = _Pickle_GetGlobalState();
1496 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001497 "memo id too large for LONG_BINGET");
1498 return -1;
1499 }
1500 }
1501
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001502 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001503 return -1;
1504
1505 return 0;
1506}
1507
1508/* Store an object in the memo, assign it a new unique ID based on the number
1509 of objects currently stored in the memo and generate a PUT opcode. */
1510static int
1511memo_put(PicklerObject *self, PyObject *obj)
1512{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001513 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001514 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001515 Py_ssize_t idx;
1516
1517 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001518
1519 if (self->fast)
1520 return 0;
1521
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001522 idx = PyMemoTable_Size(self->memo);
1523 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1524 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001525
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001526 if (self->proto >= 4) {
1527 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1528 return -1;
1529 return 0;
1530 }
1531 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001532 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001533 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001534 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001535 len = strlen(pdata);
1536 }
1537 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001538 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001539 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001540 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001541 len = 2;
1542 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001543 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001544 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001545 pdata[1] = (unsigned char)(idx & 0xff);
1546 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1547 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1548 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001549 len = 5;
1550 }
1551 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001552 PickleState *st = _Pickle_GetGlobalState();
1553 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001554 "memo id too large for LONG_BINPUT");
1555 return -1;
1556 }
1557 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001558 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001559 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001560
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001561 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001562}
1563
1564static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001565get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001566 _Py_static_string(PyId_dot, ".");
1567 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001568 PyObject *dotted_path;
1569 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001570
1571 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001572 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001573 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001574 n = PyList_GET_SIZE(dotted_path);
1575 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001576 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001577 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001578 PyObject *result = PyUnicode_RichCompare(
1579 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1580 int is_equal = (result == Py_True);
1581 assert(PyBool_Check(result));
1582 Py_DECREF(result);
1583 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001584 if (obj == NULL)
1585 PyErr_Format(PyExc_AttributeError,
1586 "Can't pickle local object %R", name);
1587 else
1588 PyErr_Format(PyExc_AttributeError,
1589 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001590 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001591 return NULL;
1592 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001593 }
1594 return dotted_path;
1595}
1596
1597static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001598get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001599{
1600 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001601 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001602
1603 assert(PyList_CheckExact(names));
1604 Py_INCREF(obj);
1605 n = PyList_GET_SIZE(names);
1606 for (i = 0; i < n; i++) {
1607 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001608 Py_XDECREF(parent);
1609 parent = obj;
1610 obj = PyObject_GetAttr(parent, name);
1611 if (obj == NULL) {
1612 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001613 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001614 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001615 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001616 if (pparent != NULL)
1617 *pparent = parent;
1618 else
1619 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001620 return obj;
1621}
1622
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001623static void
1624reformat_attribute_error(PyObject *obj, PyObject *name)
1625{
1626 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1627 PyErr_Clear();
1628 PyErr_Format(PyExc_AttributeError,
1629 "Can't get attribute %R on %R", name, obj);
1630 }
1631}
1632
1633
1634static PyObject *
1635getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1636{
1637 PyObject *dotted_path, *attr;
1638
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001639 if (allow_qualname) {
1640 dotted_path = get_dotted_path(obj, name);
1641 if (dotted_path == NULL)
1642 return NULL;
1643 attr = get_deep_attribute(obj, dotted_path, NULL);
1644 Py_DECREF(dotted_path);
1645 }
1646 else
1647 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001648 if (attr == NULL)
1649 reformat_attribute_error(obj, name);
1650 return attr;
1651}
1652
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001653static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001654whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001655{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001656 PyObject *module_name;
1657 PyObject *modules_dict;
1658 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001659 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001660 _Py_IDENTIFIER(__module__);
1661 _Py_IDENTIFIER(modules);
1662 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001663
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001664 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1665
1666 if (module_name == NULL) {
1667 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001668 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001669 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001670 }
1671 else {
1672 /* In some rare cases (e.g., bound methods of extension types),
1673 __module__ can be None. If it is so, then search sys.modules for
1674 the module of global. */
1675 if (module_name != Py_None)
1676 return module_name;
1677 Py_CLEAR(module_name);
1678 }
1679 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001680
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001681 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001682 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001683 if (modules_dict == NULL) {
1684 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001685 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001686 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001687
1688 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001689 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1690 PyObject *candidate;
1691 if (PyUnicode_Check(module_name) &&
1692 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001693 continue;
1694 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001695 continue;
1696
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001697 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001698 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001699 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001700 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001701 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001702 continue;
1703 }
1704
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001705 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001706 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001708 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001709 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001710 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 }
1712
1713 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001714 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001715 Py_INCREF(module_name);
1716 return module_name;
1717}
1718
1719/* fast_save_enter() and fast_save_leave() are guards against recursive
1720 objects when Pickler is used with the "fast mode" (i.e., with object
1721 memoization disabled). If the nesting of a list or dict object exceed
1722 FAST_NESTING_LIMIT, these guards will start keeping an internal
1723 reference to the seen list or dict objects and check whether these objects
1724 are recursive. These are not strictly necessary, since save() has a
1725 hard-coded recursion limit, but they give a nicer error message than the
1726 typical RuntimeError. */
1727static int
1728fast_save_enter(PicklerObject *self, PyObject *obj)
1729{
1730 /* if fast_nesting < 0, we're doing an error exit. */
1731 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1732 PyObject *key = NULL;
1733 if (self->fast_memo == NULL) {
1734 self->fast_memo = PyDict_New();
1735 if (self->fast_memo == NULL) {
1736 self->fast_nesting = -1;
1737 return 0;
1738 }
1739 }
1740 key = PyLong_FromVoidPtr(obj);
1741 if (key == NULL)
1742 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001743 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001744 Py_DECREF(key);
1745 PyErr_Format(PyExc_ValueError,
1746 "fast mode: can't pickle cyclic objects "
1747 "including object type %.200s at %p",
1748 obj->ob_type->tp_name, obj);
1749 self->fast_nesting = -1;
1750 return 0;
1751 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001752 if (PyErr_Occurred()) {
1753 return 0;
1754 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001755 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1756 Py_DECREF(key);
1757 self->fast_nesting = -1;
1758 return 0;
1759 }
1760 Py_DECREF(key);
1761 }
1762 return 1;
1763}
1764
1765static int
1766fast_save_leave(PicklerObject *self, PyObject *obj)
1767{
1768 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1769 PyObject *key = PyLong_FromVoidPtr(obj);
1770 if (key == NULL)
1771 return 0;
1772 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1773 Py_DECREF(key);
1774 return 0;
1775 }
1776 Py_DECREF(key);
1777 }
1778 return 1;
1779}
1780
1781static int
1782save_none(PicklerObject *self, PyObject *obj)
1783{
1784 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001785 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001786 return -1;
1787
1788 return 0;
1789}
1790
1791static int
1792save_bool(PicklerObject *self, PyObject *obj)
1793{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001794 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001795 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001796 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001797 return -1;
1798 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001799 else {
1800 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1801 * so that unpicklers written before bools were introduced unpickle them
1802 * as ints, but unpicklers after can recognize that bools were intended.
1803 * Note that protocol 2 added direct ways to pickle bools.
1804 */
1805 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1806 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1807 return -1;
1808 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001809 return 0;
1810}
1811
1812static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001813save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001814{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001815 PyObject *repr = NULL;
1816 Py_ssize_t size;
1817 long val;
1818 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001819
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001820 const char long_op = LONG;
1821
1822 val= PyLong_AsLong(obj);
1823 if (val == -1 && PyErr_Occurred()) {
1824 /* out of range for int pickling */
1825 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001826 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001827 else if (self->bin &&
1828 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001829 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001830 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001831
1832 Note: we can't use -0x80000000L in the above condition because some
1833 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1834 before applying the unary minus when sizeof(long) <= 4. The
1835 resulting value stays unsigned which is commonly not what we want,
1836 so MSVC happily warns us about it. However, that result would have
1837 been fine because we guard for sizeof(long) <= 4 which turns the
1838 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001839 char pdata[32];
1840 Py_ssize_t len = 0;
1841
1842 pdata[1] = (unsigned char)(val & 0xff);
1843 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1844 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1845 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001846
1847 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1848 if (pdata[2] == 0) {
1849 pdata[0] = BININT1;
1850 len = 2;
1851 }
1852 else {
1853 pdata[0] = BININT2;
1854 len = 3;
1855 }
1856 }
1857 else {
1858 pdata[0] = BININT;
1859 len = 5;
1860 }
1861
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001862 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001863 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001864
1865 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001866 }
1867
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001868 if (self->proto >= 2) {
1869 /* Linear-time pickling. */
1870 size_t nbits;
1871 size_t nbytes;
1872 unsigned char *pdata;
1873 char header[5];
1874 int i;
1875 int sign = _PyLong_Sign(obj);
1876
1877 if (sign == 0) {
1878 header[0] = LONG1;
1879 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001880 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001881 goto error;
1882 return 0;
1883 }
1884 nbits = _PyLong_NumBits(obj);
1885 if (nbits == (size_t)-1 && PyErr_Occurred())
1886 goto error;
1887 /* How many bytes do we need? There are nbits >> 3 full
1888 * bytes of data, and nbits & 7 leftover bits. If there
1889 * are any leftover bits, then we clearly need another
1890 * byte. Wnat's not so obvious is that we *probably*
1891 * need another byte even if there aren't any leftovers:
1892 * the most-significant bit of the most-significant byte
1893 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001894 * opposite of the one we need. The exception is ints
1895 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001896 * its own 256's-complement, so has the right sign bit
1897 * even without the extra byte. That's a pain to check
1898 * for in advance, though, so we always grab an extra
1899 * byte at the start, and cut it back later if possible.
1900 */
1901 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001902 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001903 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001904 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 goto error;
1906 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001907 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001908 if (repr == NULL)
1909 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001910 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001911 i = _PyLong_AsByteArray((PyLongObject *)obj,
1912 pdata, nbytes,
1913 1 /* little endian */ , 1 /* signed */ );
1914 if (i < 0)
1915 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001916 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001917 * needed. This is so iff the MSB is all redundant sign
1918 * bits.
1919 */
1920 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001921 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001922 pdata[nbytes - 1] == 0xff &&
1923 (pdata[nbytes - 2] & 0x80) != 0) {
1924 nbytes--;
1925 }
1926
1927 if (nbytes < 256) {
1928 header[0] = LONG1;
1929 header[1] = (unsigned char)nbytes;
1930 size = 2;
1931 }
1932 else {
1933 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001934 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001935 for (i = 1; i < 5; i++) {
1936 header[i] = (unsigned char)(size & 0xff);
1937 size >>= 8;
1938 }
1939 size = 5;
1940 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001941 if (_Pickler_Write(self, header, size) < 0 ||
1942 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001943 goto error;
1944 }
1945 else {
1946 char *string;
1947
Mark Dickinson8dd05142009-01-20 20:43:58 +00001948 /* proto < 2: write the repr and newline. This is quadratic-time (in
1949 the number of digits), in both directions. We add a trailing 'L'
1950 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001951
1952 repr = PyObject_Repr(obj);
1953 if (repr == NULL)
1954 goto error;
1955
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001956 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001957 if (string == NULL)
1958 goto error;
1959
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001960 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1961 _Pickler_Write(self, string, size) < 0 ||
1962 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001963 goto error;
1964 }
1965
1966 if (0) {
1967 error:
1968 status = -1;
1969 }
1970 Py_XDECREF(repr);
1971
1972 return status;
1973}
1974
1975static int
1976save_float(PicklerObject *self, PyObject *obj)
1977{
1978 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1979
1980 if (self->bin) {
1981 char pdata[9];
1982 pdata[0] = BINFLOAT;
1983 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1984 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001985 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001986 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001987 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001988 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001989 int result = -1;
1990 char *buf = NULL;
1991 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001992
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001993 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00001994 goto done;
1995
Serhiy Storchakac86ca262015-02-15 14:18:32 +02001996 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00001997 if (!buf) {
1998 PyErr_NoMemory();
1999 goto done;
2000 }
2001
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002002 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002003 goto done;
2004
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002005 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002006 goto done;
2007
2008 result = 0;
2009done:
2010 PyMem_Free(buf);
2011 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002012 }
2013
2014 return 0;
2015}
2016
2017static int
2018save_bytes(PicklerObject *self, PyObject *obj)
2019{
2020 if (self->proto < 3) {
2021 /* Older pickle protocols do not have an opcode for pickling bytes
2022 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002023 the __reduce__ method) to permit bytes object unpickling.
2024
2025 Here we use a hack to be compatible with Python 2. Since in Python
2026 2 'bytes' is just an alias for 'str' (which has different
2027 parameters than the actual bytes object), we use codecs.encode
2028 to create the appropriate 'str' object when unpickled using
2029 Python 2 *and* the appropriate 'bytes' object when unpickled
2030 using Python 3. Again this is a hack and we don't need to do this
2031 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002032 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002033 int status;
2034
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002035 if (PyBytes_GET_SIZE(obj) == 0) {
2036 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2037 }
2038 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002039 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002040 PyObject *unicode_str =
2041 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2042 PyBytes_GET_SIZE(obj),
2043 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002044 _Py_IDENTIFIER(latin1);
2045
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002046 if (unicode_str == NULL)
2047 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002048 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002049 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002050 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002051 Py_DECREF(unicode_str);
2052 }
2053
2054 if (reduce_value == NULL)
2055 return -1;
2056
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002057 /* save_reduce() will memoize the object automatically. */
2058 status = save_reduce(self, reduce_value, obj);
2059 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002060 return status;
2061 }
2062 else {
2063 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002064 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002065 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002067 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002068 if (size < 0)
2069 return -1;
2070
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002071 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002072 header[0] = SHORT_BINBYTES;
2073 header[1] = (unsigned char)size;
2074 len = 2;
2075 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002076 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 header[0] = BINBYTES;
2078 header[1] = (unsigned char)(size & 0xff);
2079 header[2] = (unsigned char)((size >> 8) & 0xff);
2080 header[3] = (unsigned char)((size >> 16) & 0xff);
2081 header[4] = (unsigned char)((size >> 24) & 0xff);
2082 len = 5;
2083 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002084 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002085 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002086 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002087 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002088 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002089 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002090 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002091 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002092 return -1; /* string too large */
2093 }
2094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002095 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002096 return -1;
2097
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002098 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002099 return -1;
2100
2101 if (memo_put(self, obj) < 0)
2102 return -1;
2103
2104 return 0;
2105 }
2106}
2107
2108/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2109 backslash and newline characters to \uXXXX escapes. */
2110static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002111raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002112{
Victor Stinner7270b7f2014-08-17 21:14:46 +02002113 PyObject *repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002114 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002115 Py_ssize_t i, size;
2116 size_t expandsize;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002117 void *data;
2118 unsigned int kind;
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 Stinnerc806fdc2011-09-29 23:50:23 +02002123 size = PyUnicode_GET_LENGTH(obj);
2124 data = PyUnicode_DATA(obj);
2125 kind = PyUnicode_KIND(obj);
2126 if (kind == PyUnicode_4BYTE_KIND)
2127 expandsize = 10;
2128 else
2129 expandsize = 6;
Victor Stinner121aab42011-09-29 23:40:53 +02002130
Victor Stinner049e5092014-08-17 22:20:00 +02002131 if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize)
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002132 return PyErr_NoMemory();
Victor Stinner7270b7f2014-08-17 21:14:46 +02002133 repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002134 if (repr == NULL)
2135 return NULL;
2136 if (size == 0)
Victor Stinner7270b7f2014-08-17 21:14:46 +02002137 return repr;
2138 assert(Py_REFCNT(repr) == 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002139
Victor Stinner7270b7f2014-08-17 21:14:46 +02002140 p = PyBytes_AS_STRING(repr);
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002141 for (i=0; i < size; i++) {
2142 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002143 /* Map 32-bit characters to '\Uxxxxxxxx' */
2144 if (ch >= 0x10000) {
2145 *p++ = '\\';
2146 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002147 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2149 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2150 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2151 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2152 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2153 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2154 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002155 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002156 /* Map 16-bit characters to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002157 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002158 *p++ = '\\';
2159 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002160 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2161 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2162 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2163 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002164 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002165 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002166 else
2167 *p++ = (char) ch;
2168 }
Victor Stinner7270b7f2014-08-17 21:14:46 +02002169 size = p - PyBytes_AS_STRING(repr);
2170 if (_PyBytes_Resize(&repr, size) < 0)
2171 return NULL;
2172 return repr;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002173}
2174
2175static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002176write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2177{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002178 char header[9];
2179 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002180
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002181 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002182 if (size <= 0xff && self->proto >= 4) {
2183 header[0] = SHORT_BINUNICODE;
2184 header[1] = (unsigned char)(size & 0xff);
2185 len = 2;
2186 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002187 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002188 header[0] = BINUNICODE;
2189 header[1] = (unsigned char)(size & 0xff);
2190 header[2] = (unsigned char)((size >> 8) & 0xff);
2191 header[3] = (unsigned char)((size >> 16) & 0xff);
2192 header[4] = (unsigned char)((size >> 24) & 0xff);
2193 len = 5;
2194 }
2195 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002196 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002197 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002198 len = 9;
2199 }
2200 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002201 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002202 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002203 return -1;
2204 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002205
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002206 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002207 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002208 if (_Pickler_Write(self, data, size) < 0)
2209 return -1;
2210
2211 return 0;
2212}
2213
2214static int
2215write_unicode_binary(PicklerObject *self, PyObject *obj)
2216{
2217 PyObject *encoded = NULL;
2218 Py_ssize_t size;
2219 char *data;
2220 int r;
2221
2222 if (PyUnicode_READY(obj))
2223 return -1;
2224
2225 data = PyUnicode_AsUTF8AndSize(obj, &size);
2226 if (data != NULL)
2227 return write_utf8(self, data, size);
2228
2229 /* Issue #8383: for strings with lone surrogates, fallback on the
2230 "surrogatepass" error handler. */
2231 PyErr_Clear();
2232 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2233 if (encoded == NULL)
2234 return -1;
2235
2236 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2237 PyBytes_GET_SIZE(encoded));
2238 Py_DECREF(encoded);
2239 return r;
2240}
2241
2242static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002243save_unicode(PicklerObject *self, PyObject *obj)
2244{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002245 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002246 if (write_unicode_binary(self, obj) < 0)
2247 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002248 }
2249 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 PyObject *encoded;
2251 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 const char unicode_op = UNICODE;
2253
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002254 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002255 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002256 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002257
Antoine Pitrou299978d2013-04-07 17:38:11 +02002258 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2259 Py_DECREF(encoded);
2260 return -1;
2261 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002262
2263 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002264 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2265 Py_DECREF(encoded);
2266 return -1;
2267 }
2268 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002270 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002271 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002272 }
2273 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002274 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002277}
2278
2279/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2280static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002281store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002283 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002284
2285 assert(PyTuple_Size(t) == len);
2286
2287 for (i = 0; i < len; i++) {
2288 PyObject *element = PyTuple_GET_ITEM(t, i);
2289
2290 if (element == NULL)
2291 return -1;
2292 if (save(self, element, 0) < 0)
2293 return -1;
2294 }
2295
2296 return 0;
2297}
2298
2299/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2300 * used across protocols to minimize the space needed to pickle them.
2301 * Tuples are also the only builtin immutable type that can be recursive
2302 * (a tuple can be reached from itself), and that requires some subtle
2303 * magic so that it works in all cases. IOW, this is a long routine.
2304 */
2305static int
2306save_tuple(PicklerObject *self, PyObject *obj)
2307{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002308 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002309
2310 const char mark_op = MARK;
2311 const char tuple_op = TUPLE;
2312 const char pop_op = POP;
2313 const char pop_mark_op = POP_MARK;
2314 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2315
2316 if ((len = PyTuple_Size(obj)) < 0)
2317 return -1;
2318
2319 if (len == 0) {
2320 char pdata[2];
2321
2322 if (self->proto) {
2323 pdata[0] = EMPTY_TUPLE;
2324 len = 1;
2325 }
2326 else {
2327 pdata[0] = MARK;
2328 pdata[1] = TUPLE;
2329 len = 2;
2330 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002331 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002332 return -1;
2333 return 0;
2334 }
2335
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002336 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002337 * saving the tuple elements, the tuple must be recursive, in
2338 * which case we'll pop everything we put on the stack, and fetch
2339 * its value from the memo.
2340 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 if (len <= 3 && self->proto >= 2) {
2342 /* Use TUPLE{1,2,3} opcodes. */
2343 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002346 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002347 /* pop the len elements */
2348 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002349 if (_Pickler_Write(self, &pop_op, 1) < 0)
2350 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002352 if (memo_get(self, obj) < 0)
2353 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 return 0;
2356 }
2357 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002358 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2359 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002360 }
2361 goto memoize;
2362 }
2363
2364 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2365 * Generate MARK e1 e2 ... TUPLE
2366 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002367 if (_Pickler_Write(self, &mark_op, 1) < 0)
2368 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002369
2370 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002371 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002372
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002373 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002374 /* pop the stack stuff we pushed */
2375 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002376 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2377 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002378 }
2379 else {
2380 /* Note that we pop one more than len, to remove
2381 * the MARK too.
2382 */
2383 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 if (_Pickler_Write(self, &pop_op, 1) < 0)
2385 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002386 }
2387 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002388 if (memo_get(self, obj) < 0)
2389 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002391 return 0;
2392 }
2393 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002394 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2395 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002396 }
2397
2398 memoize:
2399 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002400 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002401
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002402 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403}
2404
2405/* iter is an iterator giving items, and we batch up chunks of
2406 * MARK item item ... item APPENDS
2407 * opcode sequences. Calling code should have arranged to first create an
2408 * empty list, or list-like object, for the APPENDS to operate on.
2409 * Returns 0 on success, <0 on error.
2410 */
2411static int
2412batch_list(PicklerObject *self, PyObject *iter)
2413{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002414 PyObject *obj = NULL;
2415 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002416 int i, n;
2417
2418 const char mark_op = MARK;
2419 const char append_op = APPEND;
2420 const char appends_op = APPENDS;
2421
2422 assert(iter != NULL);
2423
2424 /* XXX: I think this function could be made faster by avoiding the
2425 iterator interface and fetching objects directly from list using
2426 PyList_GET_ITEM.
2427 */
2428
2429 if (self->proto == 0) {
2430 /* APPENDS isn't available; do one at a time. */
2431 for (;;) {
2432 obj = PyIter_Next(iter);
2433 if (obj == NULL) {
2434 if (PyErr_Occurred())
2435 return -1;
2436 break;
2437 }
2438 i = save(self, obj, 0);
2439 Py_DECREF(obj);
2440 if (i < 0)
2441 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002442 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002443 return -1;
2444 }
2445 return 0;
2446 }
2447
2448 /* proto > 0: write in batches of BATCHSIZE. */
2449 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002450 /* Get first item */
2451 firstitem = PyIter_Next(iter);
2452 if (firstitem == NULL) {
2453 if (PyErr_Occurred())
2454 goto error;
2455
2456 /* nothing more to add */
2457 break;
2458 }
2459
2460 /* Try to get a second item */
2461 obj = PyIter_Next(iter);
2462 if (obj == NULL) {
2463 if (PyErr_Occurred())
2464 goto error;
2465
2466 /* Only one item to write */
2467 if (save(self, firstitem, 0) < 0)
2468 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002469 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002470 goto error;
2471 Py_CLEAR(firstitem);
2472 break;
2473 }
2474
2475 /* More than one item to write */
2476
2477 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002478 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002479 goto error;
2480
2481 if (save(self, firstitem, 0) < 0)
2482 goto error;
2483 Py_CLEAR(firstitem);
2484 n = 1;
2485
2486 /* Fetch and save up to BATCHSIZE items */
2487 while (obj) {
2488 if (save(self, obj, 0) < 0)
2489 goto error;
2490 Py_CLEAR(obj);
2491 n += 1;
2492
2493 if (n == BATCHSIZE)
2494 break;
2495
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002496 obj = PyIter_Next(iter);
2497 if (obj == NULL) {
2498 if (PyErr_Occurred())
2499 goto error;
2500 break;
2501 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002502 }
2503
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002504 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002505 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002506
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002507 } while (n == BATCHSIZE);
2508 return 0;
2509
2510 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002511 Py_XDECREF(firstitem);
2512 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002513 return -1;
2514}
2515
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002516/* This is a variant of batch_list() above, specialized for lists (with no
2517 * support for list subclasses). Like batch_list(), we batch up chunks of
2518 * MARK item item ... item APPENDS
2519 * opcode sequences. Calling code should have arranged to first create an
2520 * empty list, or list-like object, for the APPENDS to operate on.
2521 * Returns 0 on success, -1 on error.
2522 *
2523 * This version is considerably faster than batch_list(), if less general.
2524 *
2525 * Note that this only works for protocols > 0.
2526 */
2527static int
2528batch_list_exact(PicklerObject *self, PyObject *obj)
2529{
2530 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002531 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002532
2533 const char append_op = APPEND;
2534 const char appends_op = APPENDS;
2535 const char mark_op = MARK;
2536
2537 assert(obj != NULL);
2538 assert(self->proto > 0);
2539 assert(PyList_CheckExact(obj));
2540
2541 if (PyList_GET_SIZE(obj) == 1) {
2542 item = PyList_GET_ITEM(obj, 0);
2543 if (save(self, item, 0) < 0)
2544 return -1;
2545 if (_Pickler_Write(self, &append_op, 1) < 0)
2546 return -1;
2547 return 0;
2548 }
2549
2550 /* Write in batches of BATCHSIZE. */
2551 total = 0;
2552 do {
2553 this_batch = 0;
2554 if (_Pickler_Write(self, &mark_op, 1) < 0)
2555 return -1;
2556 while (total < PyList_GET_SIZE(obj)) {
2557 item = PyList_GET_ITEM(obj, total);
2558 if (save(self, item, 0) < 0)
2559 return -1;
2560 total++;
2561 if (++this_batch == BATCHSIZE)
2562 break;
2563 }
2564 if (_Pickler_Write(self, &appends_op, 1) < 0)
2565 return -1;
2566
2567 } while (total < PyList_GET_SIZE(obj));
2568
2569 return 0;
2570}
2571
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002572static int
2573save_list(PicklerObject *self, PyObject *obj)
2574{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002575 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002576 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002577 int status = 0;
2578
2579 if (self->fast && !fast_save_enter(self, obj))
2580 goto error;
2581
2582 /* Create an empty list. */
2583 if (self->bin) {
2584 header[0] = EMPTY_LIST;
2585 len = 1;
2586 }
2587 else {
2588 header[0] = MARK;
2589 header[1] = LIST;
2590 len = 2;
2591 }
2592
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002593 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002594 goto error;
2595
2596 /* Get list length, and bow out early if empty. */
2597 if ((len = PyList_Size(obj)) < 0)
2598 goto error;
2599
2600 if (memo_put(self, obj) < 0)
2601 goto error;
2602
2603 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002604 /* Materialize the list elements. */
2605 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002606 if (Py_EnterRecursiveCall(" while pickling an object"))
2607 goto error;
2608 status = batch_list_exact(self, obj);
2609 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002610 } else {
2611 PyObject *iter = PyObject_GetIter(obj);
2612 if (iter == NULL)
2613 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002614
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002615 if (Py_EnterRecursiveCall(" while pickling an object")) {
2616 Py_DECREF(iter);
2617 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002618 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002619 status = batch_list(self, iter);
2620 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002621 Py_DECREF(iter);
2622 }
2623 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002624 if (0) {
2625 error:
2626 status = -1;
2627 }
2628
2629 if (self->fast && !fast_save_leave(self, obj))
2630 status = -1;
2631
2632 return status;
2633}
2634
2635/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2636 * MARK key value ... key value SETITEMS
2637 * opcode sequences. Calling code should have arranged to first create an
2638 * empty dict, or dict-like object, for the SETITEMS to operate on.
2639 * Returns 0 on success, <0 on error.
2640 *
2641 * This is very much like batch_list(). The difference between saving
2642 * elements directly, and picking apart two-tuples, is so long-winded at
2643 * the C level, though, that attempts to combine these routines were too
2644 * ugly to bear.
2645 */
2646static int
2647batch_dict(PicklerObject *self, PyObject *iter)
2648{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002649 PyObject *obj = NULL;
2650 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002651 int i, n;
2652
2653 const char mark_op = MARK;
2654 const char setitem_op = SETITEM;
2655 const char setitems_op = SETITEMS;
2656
2657 assert(iter != NULL);
2658
2659 if (self->proto == 0) {
2660 /* SETITEMS isn't available; do one at a time. */
2661 for (;;) {
2662 obj = PyIter_Next(iter);
2663 if (obj == NULL) {
2664 if (PyErr_Occurred())
2665 return -1;
2666 break;
2667 }
2668 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2669 PyErr_SetString(PyExc_TypeError, "dict items "
2670 "iterator must return 2-tuples");
2671 return -1;
2672 }
2673 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2674 if (i >= 0)
2675 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2676 Py_DECREF(obj);
2677 if (i < 0)
2678 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002679 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002680 return -1;
2681 }
2682 return 0;
2683 }
2684
2685 /* proto > 0: write in batches of BATCHSIZE. */
2686 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002687 /* Get first item */
2688 firstitem = PyIter_Next(iter);
2689 if (firstitem == NULL) {
2690 if (PyErr_Occurred())
2691 goto error;
2692
2693 /* nothing more to add */
2694 break;
2695 }
2696 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2697 PyErr_SetString(PyExc_TypeError, "dict items "
2698 "iterator must return 2-tuples");
2699 goto error;
2700 }
2701
2702 /* Try to get a second item */
2703 obj = PyIter_Next(iter);
2704 if (obj == NULL) {
2705 if (PyErr_Occurred())
2706 goto error;
2707
2708 /* Only one item to write */
2709 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2710 goto error;
2711 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2712 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002713 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002714 goto error;
2715 Py_CLEAR(firstitem);
2716 break;
2717 }
2718
2719 /* More than one item to write */
2720
2721 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002722 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002723 goto error;
2724
2725 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2726 goto error;
2727 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2728 goto error;
2729 Py_CLEAR(firstitem);
2730 n = 1;
2731
2732 /* Fetch and save up to BATCHSIZE items */
2733 while (obj) {
2734 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2735 PyErr_SetString(PyExc_TypeError, "dict items "
2736 "iterator must return 2-tuples");
2737 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002738 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002739 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2740 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2741 goto error;
2742 Py_CLEAR(obj);
2743 n += 1;
2744
2745 if (n == BATCHSIZE)
2746 break;
2747
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002748 obj = PyIter_Next(iter);
2749 if (obj == NULL) {
2750 if (PyErr_Occurred())
2751 goto error;
2752 break;
2753 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002754 }
2755
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002756 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002757 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002759 } while (n == BATCHSIZE);
2760 return 0;
2761
2762 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002763 Py_XDECREF(firstitem);
2764 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002765 return -1;
2766}
2767
Collin Winter5c9b02d2009-05-25 05:43:30 +00002768/* This is a variant of batch_dict() above that specializes for dicts, with no
2769 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2770 * MARK key value ... key value SETITEMS
2771 * opcode sequences. Calling code should have arranged to first create an
2772 * empty dict, or dict-like object, for the SETITEMS to operate on.
2773 * Returns 0 on success, -1 on error.
2774 *
2775 * Note that this currently doesn't work for protocol 0.
2776 */
2777static int
2778batch_dict_exact(PicklerObject *self, PyObject *obj)
2779{
2780 PyObject *key = NULL, *value = NULL;
2781 int i;
2782 Py_ssize_t dict_size, ppos = 0;
2783
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002784 const char mark_op = MARK;
2785 const char setitem_op = SETITEM;
2786 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002787
2788 assert(obj != NULL);
2789 assert(self->proto > 0);
2790
2791 dict_size = PyDict_Size(obj);
2792
2793 /* Special-case len(d) == 1 to save space. */
2794 if (dict_size == 1) {
2795 PyDict_Next(obj, &ppos, &key, &value);
2796 if (save(self, key, 0) < 0)
2797 return -1;
2798 if (save(self, value, 0) < 0)
2799 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002800 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002801 return -1;
2802 return 0;
2803 }
2804
2805 /* Write in batches of BATCHSIZE. */
2806 do {
2807 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002808 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002809 return -1;
2810 while (PyDict_Next(obj, &ppos, &key, &value)) {
2811 if (save(self, key, 0) < 0)
2812 return -1;
2813 if (save(self, value, 0) < 0)
2814 return -1;
2815 if (++i == BATCHSIZE)
2816 break;
2817 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002818 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002819 return -1;
2820 if (PyDict_Size(obj) != dict_size) {
2821 PyErr_Format(
2822 PyExc_RuntimeError,
2823 "dictionary changed size during iteration");
2824 return -1;
2825 }
2826
2827 } while (i == BATCHSIZE);
2828 return 0;
2829}
2830
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002831static int
2832save_dict(PicklerObject *self, PyObject *obj)
2833{
2834 PyObject *items, *iter;
2835 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002836 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002837 int status = 0;
2838
2839 if (self->fast && !fast_save_enter(self, obj))
2840 goto error;
2841
2842 /* Create an empty dict. */
2843 if (self->bin) {
2844 header[0] = EMPTY_DICT;
2845 len = 1;
2846 }
2847 else {
2848 header[0] = MARK;
2849 header[1] = DICT;
2850 len = 2;
2851 }
2852
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002853 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002854 goto error;
2855
2856 /* Get dict size, and bow out early if empty. */
2857 if ((len = PyDict_Size(obj)) < 0)
2858 goto error;
2859
2860 if (memo_put(self, obj) < 0)
2861 goto error;
2862
2863 if (len != 0) {
2864 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002865 if (PyDict_CheckExact(obj) && self->proto > 0) {
2866 /* We can take certain shortcuts if we know this is a dict and
2867 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002868 if (Py_EnterRecursiveCall(" while pickling an object"))
2869 goto error;
2870 status = batch_dict_exact(self, obj);
2871 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002872 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002873 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002874
2875 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002876 if (items == NULL)
2877 goto error;
2878 iter = PyObject_GetIter(items);
2879 Py_DECREF(items);
2880 if (iter == NULL)
2881 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002882 if (Py_EnterRecursiveCall(" while pickling an object")) {
2883 Py_DECREF(iter);
2884 goto error;
2885 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002886 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002887 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002888 Py_DECREF(iter);
2889 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002890 }
2891
2892 if (0) {
2893 error:
2894 status = -1;
2895 }
2896
2897 if (self->fast && !fast_save_leave(self, obj))
2898 status = -1;
2899
2900 return status;
2901}
2902
2903static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002904save_set(PicklerObject *self, PyObject *obj)
2905{
2906 PyObject *item;
2907 int i;
2908 Py_ssize_t set_size, ppos = 0;
2909 Py_hash_t hash;
2910
2911 const char empty_set_op = EMPTY_SET;
2912 const char mark_op = MARK;
2913 const char additems_op = ADDITEMS;
2914
2915 if (self->proto < 4) {
2916 PyObject *items;
2917 PyObject *reduce_value;
2918 int status;
2919
2920 items = PySequence_List(obj);
2921 if (items == NULL) {
2922 return -1;
2923 }
2924 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2925 Py_DECREF(items);
2926 if (reduce_value == NULL) {
2927 return -1;
2928 }
2929 /* save_reduce() will memoize the object automatically. */
2930 status = save_reduce(self, reduce_value, obj);
2931 Py_DECREF(reduce_value);
2932 return status;
2933 }
2934
2935 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2936 return -1;
2937
2938 if (memo_put(self, obj) < 0)
2939 return -1;
2940
2941 set_size = PySet_GET_SIZE(obj);
2942 if (set_size == 0)
2943 return 0; /* nothing to do */
2944
2945 /* Write in batches of BATCHSIZE. */
2946 do {
2947 i = 0;
2948 if (_Pickler_Write(self, &mark_op, 1) < 0)
2949 return -1;
2950 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2951 if (save(self, item, 0) < 0)
2952 return -1;
2953 if (++i == BATCHSIZE)
2954 break;
2955 }
2956 if (_Pickler_Write(self, &additems_op, 1) < 0)
2957 return -1;
2958 if (PySet_GET_SIZE(obj) != set_size) {
2959 PyErr_Format(
2960 PyExc_RuntimeError,
2961 "set changed size during iteration");
2962 return -1;
2963 }
2964 } while (i == BATCHSIZE);
2965
2966 return 0;
2967}
2968
2969static int
2970save_frozenset(PicklerObject *self, PyObject *obj)
2971{
2972 PyObject *iter;
2973
2974 const char mark_op = MARK;
2975 const char frozenset_op = FROZENSET;
2976
2977 if (self->fast && !fast_save_enter(self, obj))
2978 return -1;
2979
2980 if (self->proto < 4) {
2981 PyObject *items;
2982 PyObject *reduce_value;
2983 int status;
2984
2985 items = PySequence_List(obj);
2986 if (items == NULL) {
2987 return -1;
2988 }
2989 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2990 items);
2991 Py_DECREF(items);
2992 if (reduce_value == NULL) {
2993 return -1;
2994 }
2995 /* save_reduce() will memoize the object automatically. */
2996 status = save_reduce(self, reduce_value, obj);
2997 Py_DECREF(reduce_value);
2998 return status;
2999 }
3000
3001 if (_Pickler_Write(self, &mark_op, 1) < 0)
3002 return -1;
3003
3004 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003005 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003006 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003007 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003008 for (;;) {
3009 PyObject *item;
3010
3011 item = PyIter_Next(iter);
3012 if (item == NULL) {
3013 if (PyErr_Occurred()) {
3014 Py_DECREF(iter);
3015 return -1;
3016 }
3017 break;
3018 }
3019 if (save(self, item, 0) < 0) {
3020 Py_DECREF(item);
3021 Py_DECREF(iter);
3022 return -1;
3023 }
3024 Py_DECREF(item);
3025 }
3026 Py_DECREF(iter);
3027
3028 /* If the object is already in the memo, this means it is
3029 recursive. In this case, throw away everything we put on the
3030 stack, and fetch the object back from the memo. */
3031 if (PyMemoTable_Get(self->memo, obj)) {
3032 const char pop_mark_op = POP_MARK;
3033
3034 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3035 return -1;
3036 if (memo_get(self, obj) < 0)
3037 return -1;
3038 return 0;
3039 }
3040
3041 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3042 return -1;
3043 if (memo_put(self, obj) < 0)
3044 return -1;
3045
3046 return 0;
3047}
3048
3049static int
3050fix_imports(PyObject **module_name, PyObject **global_name)
3051{
3052 PyObject *key;
3053 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003054 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003055
3056 key = PyTuple_Pack(2, *module_name, *global_name);
3057 if (key == NULL)
3058 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003059 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003060 Py_DECREF(key);
3061 if (item) {
3062 PyObject *fixed_module_name;
3063 PyObject *fixed_global_name;
3064
3065 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3066 PyErr_Format(PyExc_RuntimeError,
3067 "_compat_pickle.REVERSE_NAME_MAPPING values "
3068 "should be 2-tuples, not %.200s",
3069 Py_TYPE(item)->tp_name);
3070 return -1;
3071 }
3072 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3073 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3074 if (!PyUnicode_Check(fixed_module_name) ||
3075 !PyUnicode_Check(fixed_global_name)) {
3076 PyErr_Format(PyExc_RuntimeError,
3077 "_compat_pickle.REVERSE_NAME_MAPPING values "
3078 "should be pairs of str, not (%.200s, %.200s)",
3079 Py_TYPE(fixed_module_name)->tp_name,
3080 Py_TYPE(fixed_global_name)->tp_name);
3081 return -1;
3082 }
3083
3084 Py_CLEAR(*module_name);
3085 Py_CLEAR(*global_name);
3086 Py_INCREF(fixed_module_name);
3087 Py_INCREF(fixed_global_name);
3088 *module_name = fixed_module_name;
3089 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003090 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003091 }
3092 else if (PyErr_Occurred()) {
3093 return -1;
3094 }
3095
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003096 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003097 if (item) {
3098 if (!PyUnicode_Check(item)) {
3099 PyErr_Format(PyExc_RuntimeError,
3100 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3101 "should be strings, not %.200s",
3102 Py_TYPE(item)->tp_name);
3103 return -1;
3104 }
3105 Py_CLEAR(*module_name);
3106 Py_INCREF(item);
3107 *module_name = item;
3108 }
3109 else if (PyErr_Occurred()) {
3110 return -1;
3111 }
3112
3113 return 0;
3114}
3115
3116static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003117save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3118{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003119 PyObject *global_name = NULL;
3120 PyObject *module_name = NULL;
3121 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003122 PyObject *parent = NULL;
3123 PyObject *dotted_path = NULL;
3124 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003125 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003126 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003127 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003128 _Py_IDENTIFIER(__name__);
3129 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003130
3131 const char global_op = GLOBAL;
3132
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003133 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003134 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003135 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003136 }
3137 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003138 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3139 if (global_name == NULL) {
3140 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3141 goto error;
3142 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003143 }
3144 if (global_name == NULL) {
3145 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3146 if (global_name == NULL)
3147 goto error;
3148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003149 }
3150
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003151 dotted_path = get_dotted_path(module, global_name);
3152 if (dotted_path == NULL)
3153 goto error;
3154 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003155 if (module_name == NULL)
3156 goto error;
3157
3158 /* XXX: Change to use the import C API directly with level=0 to disallow
3159 relative imports.
3160
3161 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3162 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3163 custom import functions (IMHO, this would be a nice security
3164 feature). The import C API would need to be extended to support the
3165 extra parameters of __import__ to fix that. */
3166 module = PyImport_Import(module_name);
3167 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003168 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003169 "Can't pickle %R: import of module %R failed",
3170 obj, module_name);
3171 goto error;
3172 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003173 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3174 Py_INCREF(lastname);
3175 cls = get_deep_attribute(module, dotted_path, &parent);
3176 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003177 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003178 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003179 "Can't pickle %R: attribute lookup %S on %S failed",
3180 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 goto error;
3182 }
3183 if (cls != obj) {
3184 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003185 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003186 "Can't pickle %R: it's not the same object as %S.%S",
3187 obj, module_name, global_name);
3188 goto error;
3189 }
3190 Py_DECREF(cls);
3191
3192 if (self->proto >= 2) {
3193 /* See whether this is in the extension registry, and if
3194 * so generate an EXT opcode.
3195 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003196 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003197 PyObject *code_obj; /* extension code as Python object */
3198 long code; /* extension code as C value */
3199 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003200 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003202 extension_key = PyTuple_Pack(2, module_name, global_name);
3203 if (extension_key == NULL) {
3204 goto error;
3205 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003206 code_obj = PyDict_GetItemWithError(st->extension_registry,
3207 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003208 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003209 /* The object is not registered in the extension registry.
3210 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003211 if (code_obj == NULL) {
3212 if (PyErr_Occurred()) {
3213 goto error;
3214 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003215 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003216 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003217
3218 /* XXX: pickle.py doesn't check neither the type, nor the range
3219 of the value returned by the extension_registry. It should for
3220 consistency. */
3221
3222 /* Verify code_obj has the right type and value. */
3223 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003224 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003225 "Can't pickle %R: extension code %R isn't an integer",
3226 obj, code_obj);
3227 goto error;
3228 }
3229 code = PyLong_AS_LONG(code_obj);
3230 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003231 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003232 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3233 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003234 goto error;
3235 }
3236
3237 /* Generate an EXT opcode. */
3238 if (code <= 0xff) {
3239 pdata[0] = EXT1;
3240 pdata[1] = (unsigned char)code;
3241 n = 2;
3242 }
3243 else if (code <= 0xffff) {
3244 pdata[0] = EXT2;
3245 pdata[1] = (unsigned char)(code & 0xff);
3246 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3247 n = 3;
3248 }
3249 else {
3250 pdata[0] = EXT4;
3251 pdata[1] = (unsigned char)(code & 0xff);
3252 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3253 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3254 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3255 n = 5;
3256 }
3257
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003258 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003259 goto error;
3260 }
3261 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003262 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003263 if (parent == module) {
3264 Py_INCREF(lastname);
3265 Py_DECREF(global_name);
3266 global_name = lastname;
3267 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003268 if (self->proto >= 4) {
3269 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003270
Christian Heimese8b1ba12013-11-23 21:13:39 +01003271 if (save(self, module_name, 0) < 0)
3272 goto error;
3273 if (save(self, global_name, 0) < 0)
3274 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003275
3276 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3277 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003278 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003279 else if (parent != module) {
3280 PickleState *st = _Pickle_GetGlobalState();
3281 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3282 st->getattr, parent, lastname);
3283 status = save_reduce(self, reduce_value, NULL);
3284 Py_DECREF(reduce_value);
3285 if (status < 0)
3286 goto error;
3287 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003288 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003289 /* Generate a normal global opcode if we are using a pickle
3290 protocol < 4, or if the object is not registered in the
3291 extension registry. */
3292 PyObject *encoded;
3293 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003294
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003295 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003296 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003297
3298 /* For protocol < 3 and if the user didn't request against doing
3299 so, we convert module names to the old 2.x module names. */
3300 if (self->proto < 3 && self->fix_imports) {
3301 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003302 goto error;
3303 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003304 }
3305
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003306 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3307 both the module name and the global name using UTF-8. We do so
3308 only when we are using the pickle protocol newer than version
3309 3. This is to ensure compatibility with older Unpickler running
3310 on Python 2.x. */
3311 if (self->proto == 3) {
3312 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003313 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003314 else {
3315 unicode_encoder = PyUnicode_AsASCIIString;
3316 }
3317 encoded = unicode_encoder(module_name);
3318 if (encoded == NULL) {
3319 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003320 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003321 "can't pickle module identifier '%S' using "
3322 "pickle protocol %i",
3323 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003324 goto error;
3325 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003326 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3327 PyBytes_GET_SIZE(encoded)) < 0) {
3328 Py_DECREF(encoded);
3329 goto error;
3330 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003331 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003332 if(_Pickler_Write(self, "\n", 1) < 0)
3333 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003334
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003335 /* Save the name of the module. */
3336 encoded = unicode_encoder(global_name);
3337 if (encoded == NULL) {
3338 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003339 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003340 "can't pickle global identifier '%S' using "
3341 "pickle protocol %i",
3342 global_name, self->proto);
3343 goto error;
3344 }
3345 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3346 PyBytes_GET_SIZE(encoded)) < 0) {
3347 Py_DECREF(encoded);
3348 goto error;
3349 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003350 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003351 if (_Pickler_Write(self, "\n", 1) < 0)
3352 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003353 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 /* Memoize the object. */
3355 if (memo_put(self, obj) < 0)
3356 goto error;
3357 }
3358
3359 if (0) {
3360 error:
3361 status = -1;
3362 }
3363 Py_XDECREF(module_name);
3364 Py_XDECREF(global_name);
3365 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003366 Py_XDECREF(parent);
3367 Py_XDECREF(dotted_path);
3368 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003369
3370 return status;
3371}
3372
3373static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003374save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3375{
3376 PyObject *reduce_value;
3377 int status;
3378
3379 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3380 if (reduce_value == NULL) {
3381 return -1;
3382 }
3383 status = save_reduce(self, reduce_value, obj);
3384 Py_DECREF(reduce_value);
3385 return status;
3386}
3387
3388static int
3389save_type(PicklerObject *self, PyObject *obj)
3390{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003391 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003392 return save_singleton_type(self, obj, Py_None);
3393 }
3394 else if (obj == (PyObject *)&PyEllipsis_Type) {
3395 return save_singleton_type(self, obj, Py_Ellipsis);
3396 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003397 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003398 return save_singleton_type(self, obj, Py_NotImplemented);
3399 }
3400 return save_global(self, obj, NULL);
3401}
3402
3403static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003404save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3405{
3406 PyObject *pid = NULL;
3407 int status = 0;
3408
3409 const char persid_op = PERSID;
3410 const char binpersid_op = BINPERSID;
3411
3412 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003413 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003414 if (pid == NULL)
3415 return -1;
3416
3417 if (pid != Py_None) {
3418 if (self->bin) {
3419 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003420 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003421 goto error;
3422 }
3423 else {
3424 PyObject *pid_str = NULL;
3425 char *pid_ascii_bytes;
3426 Py_ssize_t size;
3427
3428 pid_str = PyObject_Str(pid);
3429 if (pid_str == NULL)
3430 goto error;
3431
3432 /* XXX: Should it check whether the persistent id only contains
3433 ASCII characters? And what if the pid contains embedded
3434 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003435 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003436 Py_DECREF(pid_str);
3437 if (pid_ascii_bytes == NULL)
3438 goto error;
3439
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003440 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3441 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3442 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003443 goto error;
3444 }
3445 status = 1;
3446 }
3447
3448 if (0) {
3449 error:
3450 status = -1;
3451 }
3452 Py_XDECREF(pid);
3453
3454 return status;
3455}
3456
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003457static PyObject *
3458get_class(PyObject *obj)
3459{
3460 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003461 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003462
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003463 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003464 if (cls == NULL) {
3465 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3466 PyErr_Clear();
3467 cls = (PyObject *) Py_TYPE(obj);
3468 Py_INCREF(cls);
3469 }
3470 }
3471 return cls;
3472}
3473
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003474/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3475 * appropriate __reduce__ method for obj.
3476 */
3477static int
3478save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3479{
3480 PyObject *callable;
3481 PyObject *argtup;
3482 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003483 PyObject *listitems = Py_None;
3484 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003485 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003486 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003487 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003488
3489 const char reduce_op = REDUCE;
3490 const char build_op = BUILD;
3491 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003492 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003493
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003494 size = PyTuple_Size(args);
3495 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003496 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003497 "__reduce__ must contain 2 through 5 elements");
3498 return -1;
3499 }
3500
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003501 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3502 &callable, &argtup, &state, &listitems, &dictitems))
3503 return -1;
3504
3505 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003506 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003507 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003508 return -1;
3509 }
3510 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003511 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003512 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003513 return -1;
3514 }
3515
3516 if (state == Py_None)
3517 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003518
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003519 if (listitems == Py_None)
3520 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003521 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003522 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003523 "returned by __reduce__ must be an iterator, not %s",
3524 Py_TYPE(listitems)->tp_name);
3525 return -1;
3526 }
3527
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003528 if (dictitems == Py_None)
3529 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003530 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003531 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003532 "returned by __reduce__ must be an iterator, not %s",
3533 Py_TYPE(dictitems)->tp_name);
3534 return -1;
3535 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003536
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003537 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003538 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003539 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540
Victor Stinner804e05e2013-11-14 01:26:17 +01003541 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003542 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003543 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003545 }
3546 PyErr_Clear();
3547 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003548 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003549 _Py_IDENTIFIER(__newobj_ex__);
3550 use_newobj_ex = PyUnicode_Compare(
3551 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003552 if (!use_newobj_ex) {
3553 _Py_IDENTIFIER(__newobj__);
3554 use_newobj = PyUnicode_Compare(
3555 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3556 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003557 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003558 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003559 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003560
3561 if (use_newobj_ex) {
3562 PyObject *cls;
3563 PyObject *args;
3564 PyObject *kwargs;
3565
3566 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003567 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003568 "length of the NEWOBJ_EX argument tuple must be "
3569 "exactly 3, not %zd", Py_SIZE(argtup));
3570 return -1;
3571 }
3572
3573 cls = PyTuple_GET_ITEM(argtup, 0);
3574 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003575 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003576 "first item from NEWOBJ_EX argument tuple must "
3577 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3578 return -1;
3579 }
3580 args = PyTuple_GET_ITEM(argtup, 1);
3581 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003582 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003583 "second item from NEWOBJ_EX argument tuple must "
3584 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3585 return -1;
3586 }
3587 kwargs = PyTuple_GET_ITEM(argtup, 2);
3588 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003589 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003590 "third item from NEWOBJ_EX argument tuple must "
3591 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3592 return -1;
3593 }
3594
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003595 if (self->proto >= 4) {
3596 if (save(self, cls, 0) < 0 ||
3597 save(self, args, 0) < 0 ||
3598 save(self, kwargs, 0) < 0 ||
3599 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3600 return -1;
3601 }
3602 }
3603 else {
3604 PyObject *newargs;
3605 PyObject *cls_new;
3606 Py_ssize_t i;
3607 _Py_IDENTIFIER(__new__);
3608
3609 newargs = PyTuple_New(Py_SIZE(args) + 2);
3610 if (newargs == NULL)
3611 return -1;
3612
3613 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3614 if (cls_new == NULL) {
3615 Py_DECREF(newargs);
3616 return -1;
3617 }
3618 PyTuple_SET_ITEM(newargs, 0, cls_new);
3619 Py_INCREF(cls);
3620 PyTuple_SET_ITEM(newargs, 1, cls);
3621 for (i = 0; i < Py_SIZE(args); i++) {
3622 PyObject *item = PyTuple_GET_ITEM(args, i);
3623 Py_INCREF(item);
3624 PyTuple_SET_ITEM(newargs, i + 2, item);
3625 }
3626
3627 callable = PyObject_Call(st->partial, newargs, kwargs);
3628 Py_DECREF(newargs);
3629 if (callable == NULL)
3630 return -1;
3631
3632 newargs = PyTuple_New(0);
3633 if (newargs == NULL) {
3634 Py_DECREF(callable);
3635 return -1;
3636 }
3637
3638 if (save(self, callable, 0) < 0 ||
3639 save(self, newargs, 0) < 0 ||
3640 _Pickler_Write(self, &reduce_op, 1) < 0) {
3641 Py_DECREF(newargs);
3642 Py_DECREF(callable);
3643 return -1;
3644 }
3645 Py_DECREF(newargs);
3646 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003647 }
3648 }
3649 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003650 PyObject *cls;
3651 PyObject *newargtup;
3652 PyObject *obj_class;
3653 int p;
3654
3655 /* Sanity checks. */
3656 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003657 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003658 return -1;
3659 }
3660
3661 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003662 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003663 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003664 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003665 return -1;
3666 }
3667
3668 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003669 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 p = obj_class != cls; /* true iff a problem */
3671 Py_DECREF(obj_class);
3672 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003673 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003674 "__newobj__ args has the wrong class");
3675 return -1;
3676 }
3677 }
3678 /* XXX: These calls save() are prone to infinite recursion. Imagine
3679 what happen if the value returned by the __reduce__() method of
3680 some extension type contains another object of the same type. Ouch!
3681
3682 Here is a quick example, that I ran into, to illustrate what I
3683 mean:
3684
3685 >>> import pickle, copyreg
3686 >>> copyreg.dispatch_table.pop(complex)
3687 >>> pickle.dumps(1+2j)
3688 Traceback (most recent call last):
3689 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003690 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003691
3692 Removing the complex class from copyreg.dispatch_table made the
3693 __reduce_ex__() method emit another complex object:
3694
3695 >>> (1+1j).__reduce_ex__(2)
3696 (<function __newobj__ at 0xb7b71c3c>,
3697 (<class 'complex'>, (1+1j)), None, None, None)
3698
3699 Thus when save() was called on newargstup (the 2nd item) recursion
3700 ensued. Of course, the bug was in the complex class which had a
3701 broken __getnewargs__() that emitted another complex object. But,
3702 the point, here, is it is quite easy to end up with a broken reduce
3703 function. */
3704
3705 /* Save the class and its __new__ arguments. */
3706 if (save(self, cls, 0) < 0)
3707 return -1;
3708
3709 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3710 if (newargtup == NULL)
3711 return -1;
3712
3713 p = save(self, newargtup, 0);
3714 Py_DECREF(newargtup);
3715 if (p < 0)
3716 return -1;
3717
3718 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003719 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003720 return -1;
3721 }
3722 else { /* Not using NEWOBJ. */
3723 if (save(self, callable, 0) < 0 ||
3724 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003725 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003726 return -1;
3727 }
3728
3729 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3730 the caller do not want to memoize the object. Not particularly useful,
3731 but that is to mimic the behavior save_reduce() in pickle.py when
3732 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003733 if (obj != NULL) {
3734 /* If the object is already in the memo, this means it is
3735 recursive. In this case, throw away everything we put on the
3736 stack, and fetch the object back from the memo. */
3737 if (PyMemoTable_Get(self->memo, obj)) {
3738 const char pop_op = POP;
3739
3740 if (_Pickler_Write(self, &pop_op, 1) < 0)
3741 return -1;
3742 if (memo_get(self, obj) < 0)
3743 return -1;
3744
3745 return 0;
3746 }
3747 else if (memo_put(self, obj) < 0)
3748 return -1;
3749 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003750
3751 if (listitems && batch_list(self, listitems) < 0)
3752 return -1;
3753
3754 if (dictitems && batch_dict(self, dictitems) < 0)
3755 return -1;
3756
3757 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003758 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003759 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003760 return -1;
3761 }
3762
3763 return 0;
3764}
3765
3766static int
3767save(PicklerObject *self, PyObject *obj, int pers_save)
3768{
3769 PyTypeObject *type;
3770 PyObject *reduce_func = NULL;
3771 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003772 int status = 0;
3773
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003774 if (_Pickler_OpcodeBoundary(self) < 0)
3775 return -1;
3776
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003777 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003778 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003779
3780 /* The extra pers_save argument is necessary to avoid calling save_pers()
3781 on its returned object. */
3782 if (!pers_save && self->pers_func) {
3783 /* save_pers() returns:
3784 -1 to signal an error;
3785 0 if it did nothing successfully;
3786 1 if a persistent id was saved.
3787 */
3788 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3789 goto done;
3790 }
3791
3792 type = Py_TYPE(obj);
3793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003794 /* The old cPickle had an optimization that used switch-case statement
3795 dispatching on the first letter of the type name. This has was removed
3796 since benchmarks shown that this optimization was actually slowing
3797 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003798
3799 /* Atom types; these aren't memoized, so don't check the memo. */
3800
3801 if (obj == Py_None) {
3802 status = save_none(self, obj);
3803 goto done;
3804 }
3805 else if (obj == Py_False || obj == Py_True) {
3806 status = save_bool(self, obj);
3807 goto done;
3808 }
3809 else if (type == &PyLong_Type) {
3810 status = save_long(self, obj);
3811 goto done;
3812 }
3813 else if (type == &PyFloat_Type) {
3814 status = save_float(self, obj);
3815 goto done;
3816 }
3817
3818 /* Check the memo to see if it has the object. If so, generate
3819 a GET (or BINGET) opcode, instead of pickling the object
3820 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003821 if (PyMemoTable_Get(self->memo, obj)) {
3822 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003823 goto error;
3824 goto done;
3825 }
3826
3827 if (type == &PyBytes_Type) {
3828 status = save_bytes(self, obj);
3829 goto done;
3830 }
3831 else if (type == &PyUnicode_Type) {
3832 status = save_unicode(self, obj);
3833 goto done;
3834 }
3835 else if (type == &PyDict_Type) {
3836 status = save_dict(self, obj);
3837 goto done;
3838 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003839 else if (type == &PySet_Type) {
3840 status = save_set(self, obj);
3841 goto done;
3842 }
3843 else if (type == &PyFrozenSet_Type) {
3844 status = save_frozenset(self, obj);
3845 goto done;
3846 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003847 else if (type == &PyList_Type) {
3848 status = save_list(self, obj);
3849 goto done;
3850 }
3851 else if (type == &PyTuple_Type) {
3852 status = save_tuple(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003856 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003857 goto done;
3858 }
3859 else if (type == &PyFunction_Type) {
3860 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003861 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003862 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003863
3864 /* XXX: This part needs some unit tests. */
3865
3866 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003867 * self.dispatch_table, copyreg.dispatch_table, the object's
3868 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003870 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003871 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003872 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3873 (PyObject *)type);
3874 if (reduce_func == NULL) {
3875 if (PyErr_Occurred()) {
3876 goto error;
3877 }
3878 } else {
3879 /* PyDict_GetItemWithError() returns a borrowed reference.
3880 Increase the reference count to be consistent with
3881 PyObject_GetItem and _PyObject_GetAttrId used below. */
3882 Py_INCREF(reduce_func);
3883 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003884 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003885 reduce_func = PyObject_GetItem(self->dispatch_table,
3886 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003887 if (reduce_func == NULL) {
3888 if (PyErr_ExceptionMatches(PyExc_KeyError))
3889 PyErr_Clear();
3890 else
3891 goto error;
3892 }
3893 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003894 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003895 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003896 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003897 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003898 else if (PyType_IsSubtype(type, &PyType_Type)) {
3899 status = save_global(self, obj, NULL);
3900 goto done;
3901 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003902 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003903 _Py_IDENTIFIER(__reduce__);
3904 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003905
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003906
3907 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3908 automatically defined as __reduce__. While this is convenient, this
3909 make it impossible to know which method was actually called. Of
3910 course, this is not a big deal. But still, it would be nice to let
3911 the user know which method was called when something go
3912 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3913 don't actually have to check for a __reduce__ method. */
3914
3915 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003916 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003917 if (reduce_func != NULL) {
3918 PyObject *proto;
3919 proto = PyLong_FromLong(self->proto);
3920 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003921 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003922 }
3923 }
3924 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003925 PickleState *st = _Pickle_GetGlobalState();
3926
3927 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003928 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003929 }
3930 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003931 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003932 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003933 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003934 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003936 PyObject *empty_tuple = PyTuple_New(0);
3937 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003938 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003939 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 }
3941 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003942 PyErr_Format(st->PicklingError,
3943 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 type->tp_name, obj);
3945 goto error;
3946 }
3947 }
3948 }
3949
3950 if (reduce_value == NULL)
3951 goto error;
3952
3953 if (PyUnicode_Check(reduce_value)) {
3954 status = save_global(self, obj, reduce_value);
3955 goto done;
3956 }
3957
3958 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003959 PickleState *st = _Pickle_GetGlobalState();
3960 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003961 "__reduce__ must return a string or tuple");
3962 goto error;
3963 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003964
3965 status = save_reduce(self, reduce_value, obj);
3966
3967 if (0) {
3968 error:
3969 status = -1;
3970 }
3971 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003972
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003973 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003974 Py_XDECREF(reduce_func);
3975 Py_XDECREF(reduce_value);
3976
3977 return status;
3978}
3979
3980static int
3981dump(PicklerObject *self, PyObject *obj)
3982{
3983 const char stop_op = STOP;
3984
3985 if (self->proto >= 2) {
3986 char header[2];
3987
3988 header[0] = PROTO;
3989 assert(self->proto >= 0 && self->proto < 256);
3990 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003991 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003992 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003993 if (self->proto >= 4)
3994 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003995 }
3996
3997 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003998 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 return -1;
4000
4001 return 0;
4002}
4003
Larry Hastings61272b72014-01-07 12:41:53 -08004004/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004005
4006_pickle.Pickler.clear_memo
4007
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004008Clears the pickler's "memo".
4009
4010The memo is the data structure that remembers which objects the
4011pickler has already seen, so that shared or recursive objects are
4012pickled by reference and not by value. This method is useful when
4013re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004014[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004015
Larry Hastings3cceb382014-01-04 11:09:09 -08004016static PyObject *
4017_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004018/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004019{
4020 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004021 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004022
4023 Py_RETURN_NONE;
4024}
4025
Larry Hastings61272b72014-01-07 12:41:53 -08004026/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004027
4028_pickle.Pickler.dump
4029
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004030 obj: object
4031 /
4032
4033Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004034[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004035
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004036static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004037_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004038/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004039{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004040 /* Check whether the Pickler was initialized correctly (issue3664).
4041 Developers often forget to call __init__() in their subclasses, which
4042 would trigger a segfault without this check. */
4043 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004044 PickleState *st = _Pickle_GetGlobalState();
4045 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004046 "Pickler.__init__() was not called by %s.__init__()",
4047 Py_TYPE(self)->tp_name);
4048 return NULL;
4049 }
4050
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004051 if (_Pickler_ClearBuffer(self) < 0)
4052 return NULL;
4053
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004054 if (dump(self, obj) < 0)
4055 return NULL;
4056
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004057 if (_Pickler_FlushToFile(self) < 0)
4058 return NULL;
4059
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004060 Py_RETURN_NONE;
4061}
4062
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004063/*[clinic input]
4064
4065_pickle.Pickler.__sizeof__ -> Py_ssize_t
4066
4067Returns size in memory, in bytes.
4068[clinic start generated code]*/
4069
4070static Py_ssize_t
4071_pickle_Pickler___sizeof___impl(PicklerObject *self)
4072/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4073{
4074 Py_ssize_t res, s;
4075
4076 res = sizeof(PicklerObject);
4077 if (self->memo != NULL) {
4078 res += sizeof(PyMemoTable);
4079 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4080 }
4081 if (self->output_buffer != NULL) {
4082 s = _PySys_GetSizeOf(self->output_buffer);
4083 if (s == -1)
4084 return -1;
4085 res += s;
4086 }
4087 return res;
4088}
4089
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004090static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004091 _PICKLE_PICKLER_DUMP_METHODDEF
4092 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004093 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004094 {NULL, NULL} /* sentinel */
4095};
4096
4097static void
4098Pickler_dealloc(PicklerObject *self)
4099{
4100 PyObject_GC_UnTrack(self);
4101
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004102 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004103 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004104 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004105 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 Py_XDECREF(self->fast_memo);
4107
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004108 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004109
4110 Py_TYPE(self)->tp_free((PyObject *)self);
4111}
4112
4113static int
4114Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4115{
4116 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004117 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004118 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004119 Py_VISIT(self->fast_memo);
4120 return 0;
4121}
4122
4123static int
4124Pickler_clear(PicklerObject *self)
4125{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004126 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004127 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004128 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004129 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004130 Py_CLEAR(self->fast_memo);
4131
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004132 if (self->memo != NULL) {
4133 PyMemoTable *memo = self->memo;
4134 self->memo = NULL;
4135 PyMemoTable_Del(memo);
4136 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004137 return 0;
4138}
4139
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004140
Larry Hastings61272b72014-01-07 12:41:53 -08004141/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004142
4143_pickle.Pickler.__init__
4144
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004145 file: object
4146 protocol: object = NULL
4147 fix_imports: bool = True
4148
4149This takes a binary file for writing a pickle data stream.
4150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004151The optional *protocol* argument tells the pickler to use the given
4152protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4153protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004154
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004155Specifying a negative protocol version selects the highest protocol
4156version supported. The higher the protocol used, the more recent the
4157version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004158
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004159The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004160bytes argument. It can thus be a file object opened for binary
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004161writing, a io.BytesIO instance, or any other custom object that meets
4162this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004163
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004164If *fix_imports* is True and protocol is less than 3, pickle will try
4165to map the new Python 3 names to the old module names used in Python
41662, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004167[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004168
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004169static int
Larry Hastings89964c42015-04-14 18:07:59 -04004170_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4171 PyObject *protocol, int fix_imports)
4172/*[clinic end generated code: output=b5f31078dab17fb0 input=b8cdeb7e3f5ee674]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004173{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004174 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004175 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004176
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004177 /* In case of multiple __init__() calls, clear previous content. */
4178 if (self->write != NULL)
4179 (void)Pickler_clear(self);
4180
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004181 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004182 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004183
4184 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004185 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004186
4187 /* memo and output_buffer may have already been created in _Pickler_New */
4188 if (self->memo == NULL) {
4189 self->memo = PyMemoTable_New();
4190 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004191 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004192 }
4193 self->output_len = 0;
4194 if (self->output_buffer == NULL) {
4195 self->max_output_len = WRITE_BUF_SIZE;
4196 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4197 self->max_output_len);
4198 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004199 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004200 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004201
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004202 self->fast = 0;
4203 self->fast_nesting = 0;
4204 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004205 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004206 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4207 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4208 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004209 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004210 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004211 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004212 self->dispatch_table = NULL;
4213 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4214 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4215 &PyId_dispatch_table);
4216 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004217 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004218 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004219
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004220 return 0;
4221}
4222
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004223
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004224/* Define a proxy object for the Pickler's internal memo object. This is to
4225 * avoid breaking code like:
4226 * pickler.memo.clear()
4227 * and
4228 * pickler.memo = saved_memo
4229 * Is this a good idea? Not really, but we don't want to break code that uses
4230 * it. Note that we don't implement the entire mapping API here. This is
4231 * intentional, as these should be treated as black-box implementation details.
4232 */
4233
Larry Hastings61272b72014-01-07 12:41:53 -08004234/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004235_pickle.PicklerMemoProxy.clear
4236
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004237Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004238[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004239
Larry Hastings3cceb382014-01-04 11:09:09 -08004240static PyObject *
4241_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004242/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004243{
4244 if (self->pickler->memo)
4245 PyMemoTable_Clear(self->pickler->memo);
4246 Py_RETURN_NONE;
4247}
4248
Larry Hastings61272b72014-01-07 12:41:53 -08004249/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004250_pickle.PicklerMemoProxy.copy
4251
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004252Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004253[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254
Larry Hastings3cceb382014-01-04 11:09:09 -08004255static PyObject *
4256_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004257/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004258{
4259 Py_ssize_t i;
4260 PyMemoTable *memo;
4261 PyObject *new_memo = PyDict_New();
4262 if (new_memo == NULL)
4263 return NULL;
4264
4265 memo = self->pickler->memo;
4266 for (i = 0; i < memo->mt_allocated; ++i) {
4267 PyMemoEntry entry = memo->mt_table[i];
4268 if (entry.me_key != NULL) {
4269 int status;
4270 PyObject *key, *value;
4271
4272 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004273 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004274
4275 if (key == NULL || value == NULL) {
4276 Py_XDECREF(key);
4277 Py_XDECREF(value);
4278 goto error;
4279 }
4280 status = PyDict_SetItem(new_memo, key, value);
4281 Py_DECREF(key);
4282 Py_DECREF(value);
4283 if (status < 0)
4284 goto error;
4285 }
4286 }
4287 return new_memo;
4288
4289 error:
4290 Py_XDECREF(new_memo);
4291 return NULL;
4292}
4293
Larry Hastings61272b72014-01-07 12:41:53 -08004294/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004295_pickle.PicklerMemoProxy.__reduce__
4296
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004297Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004298[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004299
Larry Hastings3cceb382014-01-04 11:09:09 -08004300static PyObject *
4301_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004302/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004303{
4304 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004305 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004306 if (contents == NULL)
4307 return NULL;
4308
4309 reduce_value = PyTuple_New(2);
4310 if (reduce_value == NULL) {
4311 Py_DECREF(contents);
4312 return NULL;
4313 }
4314 dict_args = PyTuple_New(1);
4315 if (dict_args == NULL) {
4316 Py_DECREF(contents);
4317 Py_DECREF(reduce_value);
4318 return NULL;
4319 }
4320 PyTuple_SET_ITEM(dict_args, 0, contents);
4321 Py_INCREF((PyObject *)&PyDict_Type);
4322 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4323 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4324 return reduce_value;
4325}
4326
4327static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004328 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4329 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4330 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004331 {NULL, NULL} /* sentinel */
4332};
4333
4334static void
4335PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4336{
4337 PyObject_GC_UnTrack(self);
4338 Py_XDECREF(self->pickler);
4339 PyObject_GC_Del((PyObject *)self);
4340}
4341
4342static int
4343PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4344 visitproc visit, void *arg)
4345{
4346 Py_VISIT(self->pickler);
4347 return 0;
4348}
4349
4350static int
4351PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4352{
4353 Py_CLEAR(self->pickler);
4354 return 0;
4355}
4356
4357static PyTypeObject PicklerMemoProxyType = {
4358 PyVarObject_HEAD_INIT(NULL, 0)
4359 "_pickle.PicklerMemoProxy", /*tp_name*/
4360 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4361 0,
4362 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4363 0, /* tp_print */
4364 0, /* tp_getattr */
4365 0, /* tp_setattr */
4366 0, /* tp_compare */
4367 0, /* tp_repr */
4368 0, /* tp_as_number */
4369 0, /* tp_as_sequence */
4370 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004371 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004372 0, /* tp_call */
4373 0, /* tp_str */
4374 PyObject_GenericGetAttr, /* tp_getattro */
4375 PyObject_GenericSetAttr, /* tp_setattro */
4376 0, /* tp_as_buffer */
4377 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4378 0, /* tp_doc */
4379 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4380 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4381 0, /* tp_richcompare */
4382 0, /* tp_weaklistoffset */
4383 0, /* tp_iter */
4384 0, /* tp_iternext */
4385 picklerproxy_methods, /* tp_methods */
4386};
4387
4388static PyObject *
4389PicklerMemoProxy_New(PicklerObject *pickler)
4390{
4391 PicklerMemoProxyObject *self;
4392
4393 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4394 if (self == NULL)
4395 return NULL;
4396 Py_INCREF(pickler);
4397 self->pickler = pickler;
4398 PyObject_GC_Track(self);
4399 return (PyObject *)self;
4400}
4401
4402/*****************************************************************************/
4403
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004404static PyObject *
4405Pickler_get_memo(PicklerObject *self)
4406{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004407 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004408}
4409
4410static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004411Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004412{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004413 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004415 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416 PyErr_SetString(PyExc_TypeError,
4417 "attribute deletion is not supported");
4418 return -1;
4419 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004420
4421 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4422 PicklerObject *pickler =
4423 ((PicklerMemoProxyObject *)obj)->pickler;
4424
4425 new_memo = PyMemoTable_Copy(pickler->memo);
4426 if (new_memo == NULL)
4427 return -1;
4428 }
4429 else if (PyDict_Check(obj)) {
4430 Py_ssize_t i = 0;
4431 PyObject *key, *value;
4432
4433 new_memo = PyMemoTable_New();
4434 if (new_memo == NULL)
4435 return -1;
4436
4437 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004438 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004439 PyObject *memo_obj;
4440
4441 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4442 PyErr_SetString(PyExc_TypeError,
4443 "'memo' values must be 2-item tuples");
4444 goto error;
4445 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004446 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004447 if (memo_id == -1 && PyErr_Occurred())
4448 goto error;
4449 memo_obj = PyTuple_GET_ITEM(value, 1);
4450 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4451 goto error;
4452 }
4453 }
4454 else {
4455 PyErr_Format(PyExc_TypeError,
4456 "'memo' attribute must be an PicklerMemoProxy object"
4457 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004458 return -1;
4459 }
4460
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004461 PyMemoTable_Del(self->memo);
4462 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004463
4464 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004465
4466 error:
4467 if (new_memo)
4468 PyMemoTable_Del(new_memo);
4469 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470}
4471
4472static PyObject *
4473Pickler_get_persid(PicklerObject *self)
4474{
4475 if (self->pers_func == NULL)
4476 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4477 else
4478 Py_INCREF(self->pers_func);
4479 return self->pers_func;
4480}
4481
4482static int
4483Pickler_set_persid(PicklerObject *self, PyObject *value)
4484{
4485 PyObject *tmp;
4486
4487 if (value == NULL) {
4488 PyErr_SetString(PyExc_TypeError,
4489 "attribute deletion is not supported");
4490 return -1;
4491 }
4492 if (!PyCallable_Check(value)) {
4493 PyErr_SetString(PyExc_TypeError,
4494 "persistent_id must be a callable taking one argument");
4495 return -1;
4496 }
4497
4498 tmp = self->pers_func;
4499 Py_INCREF(value);
4500 self->pers_func = value;
4501 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4502
4503 return 0;
4504}
4505
4506static PyMemberDef Pickler_members[] = {
4507 {"bin", T_INT, offsetof(PicklerObject, bin)},
4508 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004509 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004510 {NULL}
4511};
4512
4513static PyGetSetDef Pickler_getsets[] = {
4514 {"memo", (getter)Pickler_get_memo,
4515 (setter)Pickler_set_memo},
4516 {"persistent_id", (getter)Pickler_get_persid,
4517 (setter)Pickler_set_persid},
4518 {NULL}
4519};
4520
4521static PyTypeObject Pickler_Type = {
4522 PyVarObject_HEAD_INIT(NULL, 0)
4523 "_pickle.Pickler" , /*tp_name*/
4524 sizeof(PicklerObject), /*tp_basicsize*/
4525 0, /*tp_itemsize*/
4526 (destructor)Pickler_dealloc, /*tp_dealloc*/
4527 0, /*tp_print*/
4528 0, /*tp_getattr*/
4529 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004530 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004531 0, /*tp_repr*/
4532 0, /*tp_as_number*/
4533 0, /*tp_as_sequence*/
4534 0, /*tp_as_mapping*/
4535 0, /*tp_hash*/
4536 0, /*tp_call*/
4537 0, /*tp_str*/
4538 0, /*tp_getattro*/
4539 0, /*tp_setattro*/
4540 0, /*tp_as_buffer*/
4541 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004542 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004543 (traverseproc)Pickler_traverse, /*tp_traverse*/
4544 (inquiry)Pickler_clear, /*tp_clear*/
4545 0, /*tp_richcompare*/
4546 0, /*tp_weaklistoffset*/
4547 0, /*tp_iter*/
4548 0, /*tp_iternext*/
4549 Pickler_methods, /*tp_methods*/
4550 Pickler_members, /*tp_members*/
4551 Pickler_getsets, /*tp_getset*/
4552 0, /*tp_base*/
4553 0, /*tp_dict*/
4554 0, /*tp_descr_get*/
4555 0, /*tp_descr_set*/
4556 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004557 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004558 PyType_GenericAlloc, /*tp_alloc*/
4559 PyType_GenericNew, /*tp_new*/
4560 PyObject_GC_Del, /*tp_free*/
4561 0, /*tp_is_gc*/
4562};
4563
Victor Stinner121aab42011-09-29 23:40:53 +02004564/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004565
4566 XXX: It would be nice to able to avoid Python function call overhead, by
4567 using directly the C version of find_class(), when find_class() is not
4568 overridden by a subclass. Although, this could become rather hackish. A
4569 simpler optimization would be to call the C function when self is not a
4570 subclass instance. */
4571static PyObject *
4572find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4573{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004574 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004575
4576 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4577 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004578}
4579
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004580static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004581marker(UnpicklerObject *self)
4582{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004583 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004584 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004585 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586 return -1;
4587 }
4588
4589 return self->marks[--self->num_marks];
4590}
4591
4592static int
4593load_none(UnpicklerObject *self)
4594{
4595 PDATA_APPEND(self->stack, Py_None, -1);
4596 return 0;
4597}
4598
4599static int
4600bad_readline(void)
4601{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004602 PickleState *st = _Pickle_GetGlobalState();
4603 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604 return -1;
4605}
4606
4607static int
4608load_int(UnpicklerObject *self)
4609{
4610 PyObject *value;
4611 char *endptr, *s;
4612 Py_ssize_t len;
4613 long x;
4614
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004615 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004616 return -1;
4617 if (len < 2)
4618 return bad_readline();
4619
4620 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004621 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004622 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004623 x = strtol(s, &endptr, 0);
4624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004625 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004626 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004627 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004628 errno = 0;
4629 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004630 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004631 if (value == NULL) {
4632 PyErr_SetString(PyExc_ValueError,
4633 "could not convert string to int");
4634 return -1;
4635 }
4636 }
4637 else {
4638 if (len == 3 && (x == 0 || x == 1)) {
4639 if ((value = PyBool_FromLong(x)) == NULL)
4640 return -1;
4641 }
4642 else {
4643 if ((value = PyLong_FromLong(x)) == NULL)
4644 return -1;
4645 }
4646 }
4647
4648 PDATA_PUSH(self->stack, value, -1);
4649 return 0;
4650}
4651
4652static int
4653load_bool(UnpicklerObject *self, PyObject *boolean)
4654{
4655 assert(boolean == Py_True || boolean == Py_False);
4656 PDATA_APPEND(self->stack, boolean, -1);
4657 return 0;
4658}
4659
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004660/* s contains x bytes of an unsigned little-endian integer. Return its value
4661 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4662 */
4663static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004664calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004665{
4666 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004667 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004668 size_t x = 0;
4669
Serhiy Storchakae0606192015-09-29 22:10:07 +03004670 if (nbytes > (int)sizeof(size_t)) {
4671 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4672 * have 64-bit size that can't be represented on 32-bit platform.
4673 */
4674 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4675 if (s[i])
4676 return -1;
4677 }
4678 nbytes = (int)sizeof(size_t);
4679 }
4680 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004681 x |= (size_t) s[i] << (8 * i);
4682 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004683
4684 if (x > PY_SSIZE_T_MAX)
4685 return -1;
4686 else
4687 return (Py_ssize_t) x;
4688}
4689
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004690/* s contains x bytes of a little-endian integer. Return its value as a
4691 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4692 * int, but when x is 4 it's a signed one. This is an historical source
4693 * of x-platform bugs.
4694 */
4695static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004696calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004697{
4698 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004699 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004700 long x = 0;
4701
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004702 for (i = 0; i < nbytes; i++) {
4703 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 }
4705
4706 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4707 * is signed, so on a box with longs bigger than 4 bytes we need
4708 * to extend a BININT's sign bit to the full width.
4709 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004710 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711 x |= -(x & (1L << 31));
4712 }
4713
4714 return x;
4715}
4716
4717static int
4718load_binintx(UnpicklerObject *self, char *s, int size)
4719{
4720 PyObject *value;
4721 long x;
4722
4723 x = calc_binint(s, size);
4724
4725 if ((value = PyLong_FromLong(x)) == NULL)
4726 return -1;
4727
4728 PDATA_PUSH(self->stack, value, -1);
4729 return 0;
4730}
4731
4732static int
4733load_binint(UnpicklerObject *self)
4734{
4735 char *s;
4736
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004737 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004738 return -1;
4739
4740 return load_binintx(self, s, 4);
4741}
4742
4743static int
4744load_binint1(UnpicklerObject *self)
4745{
4746 char *s;
4747
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004748 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004749 return -1;
4750
4751 return load_binintx(self, s, 1);
4752}
4753
4754static int
4755load_binint2(UnpicklerObject *self)
4756{
4757 char *s;
4758
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004759 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004760 return -1;
4761
4762 return load_binintx(self, s, 2);
4763}
4764
4765static int
4766load_long(UnpicklerObject *self)
4767{
4768 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004769 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004770 Py_ssize_t len;
4771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004772 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004773 return -1;
4774 if (len < 2)
4775 return bad_readline();
4776
Mark Dickinson8dd05142009-01-20 20:43:58 +00004777 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4778 the 'L' before calling PyLong_FromString. In order to maintain
4779 compatibility with Python 3.0.0, we don't actually *require*
4780 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004781 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004782 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004783 /* XXX: Should the base argument explicitly set to 10? */
4784 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004785 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004786 return -1;
4787
4788 PDATA_PUSH(self->stack, value, -1);
4789 return 0;
4790}
4791
4792/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4793 * data following.
4794 */
4795static int
4796load_counted_long(UnpicklerObject *self, int size)
4797{
4798 PyObject *value;
4799 char *nbytes;
4800 char *pdata;
4801
4802 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004803 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004804 return -1;
4805
4806 size = calc_binint(nbytes, size);
4807 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004808 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004809 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004810 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004811 "LONG pickle has negative byte count");
4812 return -1;
4813 }
4814
4815 if (size == 0)
4816 value = PyLong_FromLong(0L);
4817 else {
4818 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004819 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004820 return -1;
4821 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4822 1 /* little endian */ , 1 /* signed */ );
4823 }
4824 if (value == NULL)
4825 return -1;
4826 PDATA_PUSH(self->stack, value, -1);
4827 return 0;
4828}
4829
4830static int
4831load_float(UnpicklerObject *self)
4832{
4833 PyObject *value;
4834 char *endptr, *s;
4835 Py_ssize_t len;
4836 double d;
4837
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004838 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004839 return -1;
4840 if (len < 2)
4841 return bad_readline();
4842
4843 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004844 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4845 if (d == -1.0 && PyErr_Occurred())
4846 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004847 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004848 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4849 return -1;
4850 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004851 value = PyFloat_FromDouble(d);
4852 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 return -1;
4854
4855 PDATA_PUSH(self->stack, value, -1);
4856 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004857}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004858
4859static int
4860load_binfloat(UnpicklerObject *self)
4861{
4862 PyObject *value;
4863 double x;
4864 char *s;
4865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004866 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004867 return -1;
4868
4869 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4870 if (x == -1.0 && PyErr_Occurred())
4871 return -1;
4872
4873 if ((value = PyFloat_FromDouble(x)) == NULL)
4874 return -1;
4875
4876 PDATA_PUSH(self->stack, value, -1);
4877 return 0;
4878}
4879
4880static int
4881load_string(UnpicklerObject *self)
4882{
4883 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004884 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 Py_ssize_t len;
4886 char *s, *p;
4887
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004888 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004889 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004890 /* Strip the newline */
4891 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004892 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004893 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004894 p = s + 1;
4895 len -= 2;
4896 }
4897 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004898 PickleState *st = _Pickle_GetGlobalState();
4899 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004900 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004901 return -1;
4902 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004903 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004904
4905 /* Use the PyBytes API to decode the string, since that is what is used
4906 to encode, and then coerce the result to Unicode. */
4907 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 if (bytes == NULL)
4909 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004910
4911 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4912 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4913 if (strcmp(self->encoding, "bytes") == 0) {
4914 obj = bytes;
4915 }
4916 else {
4917 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4918 Py_DECREF(bytes);
4919 if (obj == NULL) {
4920 return -1;
4921 }
4922 }
4923
4924 PDATA_PUSH(self->stack, obj, -1);
4925 return 0;
4926}
4927
4928static int
4929load_counted_binstring(UnpicklerObject *self, int nbytes)
4930{
4931 PyObject *obj;
4932 Py_ssize_t size;
4933 char *s;
4934
4935 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004936 return -1;
4937
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004938 size = calc_binsize(s, nbytes);
4939 if (size < 0) {
4940 PickleState *st = _Pickle_GetGlobalState();
4941 PyErr_Format(st->UnpicklingError,
4942 "BINSTRING exceeds system's maximum size of %zd bytes",
4943 PY_SSIZE_T_MAX);
4944 return -1;
4945 }
4946
4947 if (_Unpickler_Read(self, &s, size) < 0)
4948 return -1;
4949
4950 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4951 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4952 if (strcmp(self->encoding, "bytes") == 0) {
4953 obj = PyBytes_FromStringAndSize(s, size);
4954 }
4955 else {
4956 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4957 }
4958 if (obj == NULL) {
4959 return -1;
4960 }
4961
4962 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004963 return 0;
4964}
4965
4966static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004967load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004968{
4969 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004970 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004971 char *s;
4972
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004973 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004974 return -1;
4975
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004976 size = calc_binsize(s, nbytes);
4977 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004978 PyErr_Format(PyExc_OverflowError,
4979 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004980 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 return -1;
4982 }
4983
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004984 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004985 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004986
4987 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988 if (bytes == NULL)
4989 return -1;
4990
4991 PDATA_PUSH(self->stack, bytes, -1);
4992 return 0;
4993}
4994
4995static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004996load_unicode(UnpicklerObject *self)
4997{
4998 PyObject *str;
4999 Py_ssize_t len;
5000 char *s;
5001
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005002 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005003 return -1;
5004 if (len < 1)
5005 return bad_readline();
5006
5007 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5008 if (str == NULL)
5009 return -1;
5010
5011 PDATA_PUSH(self->stack, str, -1);
5012 return 0;
5013}
5014
5015static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005016load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017{
5018 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005019 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005020 char *s;
5021
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005022 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005023 return -1;
5024
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005025 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005026 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005027 PyErr_Format(PyExc_OverflowError,
5028 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005029 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030 return -1;
5031 }
5032
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005033 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034 return -1;
5035
Victor Stinner485fb562010-04-13 11:07:24 +00005036 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037 if (str == NULL)
5038 return -1;
5039
5040 PDATA_PUSH(self->stack, str, -1);
5041 return 0;
5042}
5043
5044static int
5045load_tuple(UnpicklerObject *self)
5046{
5047 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005048 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005049
5050 if ((i = marker(self)) < 0)
5051 return -1;
5052
5053 tuple = Pdata_poptuple(self->stack, i);
5054 if (tuple == NULL)
5055 return -1;
5056 PDATA_PUSH(self->stack, tuple, -1);
5057 return 0;
5058}
5059
5060static int
5061load_counted_tuple(UnpicklerObject *self, int len)
5062{
5063 PyObject *tuple;
5064
5065 tuple = PyTuple_New(len);
5066 if (tuple == NULL)
5067 return -1;
5068
5069 while (--len >= 0) {
5070 PyObject *item;
5071
5072 PDATA_POP(self->stack, item);
5073 if (item == NULL)
5074 return -1;
5075 PyTuple_SET_ITEM(tuple, len, item);
5076 }
5077 PDATA_PUSH(self->stack, tuple, -1);
5078 return 0;
5079}
5080
5081static int
5082load_empty_list(UnpicklerObject *self)
5083{
5084 PyObject *list;
5085
5086 if ((list = PyList_New(0)) == NULL)
5087 return -1;
5088 PDATA_PUSH(self->stack, list, -1);
5089 return 0;
5090}
5091
5092static int
5093load_empty_dict(UnpicklerObject *self)
5094{
5095 PyObject *dict;
5096
5097 if ((dict = PyDict_New()) == NULL)
5098 return -1;
5099 PDATA_PUSH(self->stack, dict, -1);
5100 return 0;
5101}
5102
5103static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005104load_empty_set(UnpicklerObject *self)
5105{
5106 PyObject *set;
5107
5108 if ((set = PySet_New(NULL)) == NULL)
5109 return -1;
5110 PDATA_PUSH(self->stack, set, -1);
5111 return 0;
5112}
5113
5114static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005115load_list(UnpicklerObject *self)
5116{
5117 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005118 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119
5120 if ((i = marker(self)) < 0)
5121 return -1;
5122
5123 list = Pdata_poplist(self->stack, i);
5124 if (list == NULL)
5125 return -1;
5126 PDATA_PUSH(self->stack, list, -1);
5127 return 0;
5128}
5129
5130static int
5131load_dict(UnpicklerObject *self)
5132{
5133 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005134 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005135
5136 if ((i = marker(self)) < 0)
5137 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005138 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139
5140 if ((dict = PyDict_New()) == NULL)
5141 return -1;
5142
5143 for (k = i + 1; k < j; k += 2) {
5144 key = self->stack->data[k - 1];
5145 value = self->stack->data[k];
5146 if (PyDict_SetItem(dict, key, value) < 0) {
5147 Py_DECREF(dict);
5148 return -1;
5149 }
5150 }
5151 Pdata_clear(self->stack, i);
5152 PDATA_PUSH(self->stack, dict, -1);
5153 return 0;
5154}
5155
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005156static int
5157load_frozenset(UnpicklerObject *self)
5158{
5159 PyObject *items;
5160 PyObject *frozenset;
5161 Py_ssize_t i;
5162
5163 if ((i = marker(self)) < 0)
5164 return -1;
5165
5166 items = Pdata_poptuple(self->stack, i);
5167 if (items == NULL)
5168 return -1;
5169
5170 frozenset = PyFrozenSet_New(items);
5171 Py_DECREF(items);
5172 if (frozenset == NULL)
5173 return -1;
5174
5175 PDATA_PUSH(self->stack, frozenset, -1);
5176 return 0;
5177}
5178
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005179static PyObject *
5180instantiate(PyObject *cls, PyObject *args)
5181{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005182 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005183 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005184 /* Caller must assure args are a tuple. Normally, args come from
5185 Pdata_poptuple which packs objects from the top of the stack
5186 into a newly created tuple. */
5187 assert(PyTuple_Check(args));
5188 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005189 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005190 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005191 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005192 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005193 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005194
5195 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005196 }
5197 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005198}
5199
5200static int
5201load_obj(UnpicklerObject *self)
5202{
5203 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005204 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205
5206 if ((i = marker(self)) < 0)
5207 return -1;
5208
5209 args = Pdata_poptuple(self->stack, i + 1);
5210 if (args == NULL)
5211 return -1;
5212
5213 PDATA_POP(self->stack, cls);
5214 if (cls) {
5215 obj = instantiate(cls, args);
5216 Py_DECREF(cls);
5217 }
5218 Py_DECREF(args);
5219 if (obj == NULL)
5220 return -1;
5221
5222 PDATA_PUSH(self->stack, obj, -1);
5223 return 0;
5224}
5225
5226static int
5227load_inst(UnpicklerObject *self)
5228{
5229 PyObject *cls = NULL;
5230 PyObject *args = NULL;
5231 PyObject *obj = NULL;
5232 PyObject *module_name;
5233 PyObject *class_name;
5234 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005235 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005236 char *s;
5237
5238 if ((i = marker(self)) < 0)
5239 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005240 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005241 return -1;
5242 if (len < 2)
5243 return bad_readline();
5244
5245 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5246 identifiers are permitted in Python 3.0, since the INST opcode is only
5247 supported by older protocols on Python 2.x. */
5248 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5249 if (module_name == NULL)
5250 return -1;
5251
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005252 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005253 if (len < 2)
5254 return bad_readline();
5255 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005256 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257 cls = find_class(self, module_name, class_name);
5258 Py_DECREF(class_name);
5259 }
5260 }
5261 Py_DECREF(module_name);
5262
5263 if (cls == NULL)
5264 return -1;
5265
5266 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5267 obj = instantiate(cls, args);
5268 Py_DECREF(args);
5269 }
5270 Py_DECREF(cls);
5271
5272 if (obj == NULL)
5273 return -1;
5274
5275 PDATA_PUSH(self->stack, obj, -1);
5276 return 0;
5277}
5278
5279static int
5280load_newobj(UnpicklerObject *self)
5281{
5282 PyObject *args = NULL;
5283 PyObject *clsraw = NULL;
5284 PyTypeObject *cls; /* clsraw cast to its true type */
5285 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005286 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005287
5288 /* Stack is ... cls argtuple, and we want to call
5289 * cls.__new__(cls, *argtuple).
5290 */
5291 PDATA_POP(self->stack, args);
5292 if (args == NULL)
5293 goto error;
5294 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005295 PyErr_SetString(st->UnpicklingError,
5296 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005297 goto error;
5298 }
5299
5300 PDATA_POP(self->stack, clsraw);
5301 cls = (PyTypeObject *)clsraw;
5302 if (cls == NULL)
5303 goto error;
5304 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005305 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005306 "isn't a type object");
5307 goto error;
5308 }
5309 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005310 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005311 "has NULL tp_new");
5312 goto error;
5313 }
5314
5315 /* Call __new__. */
5316 obj = cls->tp_new(cls, args, NULL);
5317 if (obj == NULL)
5318 goto error;
5319
5320 Py_DECREF(args);
5321 Py_DECREF(clsraw);
5322 PDATA_PUSH(self->stack, obj, -1);
5323 return 0;
5324
5325 error:
5326 Py_XDECREF(args);
5327 Py_XDECREF(clsraw);
5328 return -1;
5329}
5330
5331static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005332load_newobj_ex(UnpicklerObject *self)
5333{
5334 PyObject *cls, *args, *kwargs;
5335 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005336 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005337
5338 PDATA_POP(self->stack, kwargs);
5339 if (kwargs == NULL) {
5340 return -1;
5341 }
5342 PDATA_POP(self->stack, args);
5343 if (args == NULL) {
5344 Py_DECREF(kwargs);
5345 return -1;
5346 }
5347 PDATA_POP(self->stack, cls);
5348 if (cls == NULL) {
5349 Py_DECREF(kwargs);
5350 Py_DECREF(args);
5351 return -1;
5352 }
Larry Hastings61272b72014-01-07 12:41:53 -08005353
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005354 if (!PyType_Check(cls)) {
5355 Py_DECREF(kwargs);
5356 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005357 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005358 "NEWOBJ_EX class argument must be a type, not %.200s",
5359 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005360 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005361 return -1;
5362 }
5363
5364 if (((PyTypeObject *)cls)->tp_new == NULL) {
5365 Py_DECREF(kwargs);
5366 Py_DECREF(args);
5367 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005368 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005369 "NEWOBJ_EX class argument doesn't have __new__");
5370 return -1;
5371 }
5372 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5373 Py_DECREF(kwargs);
5374 Py_DECREF(args);
5375 Py_DECREF(cls);
5376 if (obj == NULL) {
5377 return -1;
5378 }
5379 PDATA_PUSH(self->stack, obj, -1);
5380 return 0;
5381}
5382
5383static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005384load_global(UnpicklerObject *self)
5385{
5386 PyObject *global = NULL;
5387 PyObject *module_name;
5388 PyObject *global_name;
5389 Py_ssize_t len;
5390 char *s;
5391
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005392 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005393 return -1;
5394 if (len < 2)
5395 return bad_readline();
5396 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5397 if (!module_name)
5398 return -1;
5399
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005400 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005401 if (len < 2) {
5402 Py_DECREF(module_name);
5403 return bad_readline();
5404 }
5405 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5406 if (global_name) {
5407 global = find_class(self, module_name, global_name);
5408 Py_DECREF(global_name);
5409 }
5410 }
5411 Py_DECREF(module_name);
5412
5413 if (global == NULL)
5414 return -1;
5415 PDATA_PUSH(self->stack, global, -1);
5416 return 0;
5417}
5418
5419static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005420load_stack_global(UnpicklerObject *self)
5421{
5422 PyObject *global;
5423 PyObject *module_name;
5424 PyObject *global_name;
5425
5426 PDATA_POP(self->stack, global_name);
5427 PDATA_POP(self->stack, module_name);
5428 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5429 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005430 PickleState *st = _Pickle_GetGlobalState();
5431 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005432 Py_XDECREF(global_name);
5433 Py_XDECREF(module_name);
5434 return -1;
5435 }
5436 global = find_class(self, module_name, global_name);
5437 Py_DECREF(global_name);
5438 Py_DECREF(module_name);
5439 if (global == NULL)
5440 return -1;
5441 PDATA_PUSH(self->stack, global, -1);
5442 return 0;
5443}
5444
5445static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005446load_persid(UnpicklerObject *self)
5447{
5448 PyObject *pid;
5449 Py_ssize_t len;
5450 char *s;
5451
5452 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005453 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005454 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005455 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005456 return bad_readline();
5457
5458 pid = PyBytes_FromStringAndSize(s, len - 1);
5459 if (pid == NULL)
5460 return -1;
5461
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005462 /* This does not leak since _Pickle_FastCall() steals the reference
5463 to pid first. */
5464 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465 if (pid == NULL)
5466 return -1;
5467
5468 PDATA_PUSH(self->stack, pid, -1);
5469 return 0;
5470 }
5471 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005472 PickleState *st = _Pickle_GetGlobalState();
5473 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005474 "A load persistent id instruction was encountered,\n"
5475 "but no persistent_load function was specified.");
5476 return -1;
5477 }
5478}
5479
5480static int
5481load_binpersid(UnpicklerObject *self)
5482{
5483 PyObject *pid;
5484
5485 if (self->pers_func) {
5486 PDATA_POP(self->stack, pid);
5487 if (pid == NULL)
5488 return -1;
5489
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005490 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005491 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005492 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493 if (pid == NULL)
5494 return -1;
5495
5496 PDATA_PUSH(self->stack, pid, -1);
5497 return 0;
5498 }
5499 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005500 PickleState *st = _Pickle_GetGlobalState();
5501 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005502 "A load persistent id instruction was encountered,\n"
5503 "but no persistent_load function was specified.");
5504 return -1;
5505 }
5506}
5507
5508static int
5509load_pop(UnpicklerObject *self)
5510{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005511 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512
5513 /* Note that we split the (pickle.py) stack into two stacks,
5514 * an object stack and a mark stack. We have to be clever and
5515 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005516 * mark stack first, and only signalling a stack underflow if
5517 * the object stack is empty and the mark stack doesn't match
5518 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005519 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005520 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005521 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005522 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 len--;
5524 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005525 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005526 } else {
5527 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005528 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005529 return 0;
5530}
5531
5532static int
5533load_pop_mark(UnpicklerObject *self)
5534{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005535 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536
5537 if ((i = marker(self)) < 0)
5538 return -1;
5539
5540 Pdata_clear(self->stack, i);
5541
5542 return 0;
5543}
5544
5545static int
5546load_dup(UnpicklerObject *self)
5547{
5548 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005549 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005551 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005552 return stack_underflow();
5553 last = self->stack->data[len - 1];
5554 PDATA_APPEND(self->stack, last, -1);
5555 return 0;
5556}
5557
5558static int
5559load_get(UnpicklerObject *self)
5560{
5561 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005562 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005563 Py_ssize_t len;
5564 char *s;
5565
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005566 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005567 return -1;
5568 if (len < 2)
5569 return bad_readline();
5570
5571 key = PyLong_FromString(s, NULL, 10);
5572 if (key == NULL)
5573 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005574 idx = PyLong_AsSsize_t(key);
5575 if (idx == -1 && PyErr_Occurred()) {
5576 Py_DECREF(key);
5577 return -1;
5578 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005579
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005581 if (value == NULL) {
5582 if (!PyErr_Occurred())
5583 PyErr_SetObject(PyExc_KeyError, key);
5584 Py_DECREF(key);
5585 return -1;
5586 }
5587 Py_DECREF(key);
5588
5589 PDATA_APPEND(self->stack, value, -1);
5590 return 0;
5591}
5592
5593static int
5594load_binget(UnpicklerObject *self)
5595{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005596 PyObject *value;
5597 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005598 char *s;
5599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601 return -1;
5602
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005603 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005604
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005605 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005606 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005607 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005608 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005609 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005610 Py_DECREF(key);
5611 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005612 return -1;
5613 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005614
5615 PDATA_APPEND(self->stack, value, -1);
5616 return 0;
5617}
5618
5619static int
5620load_long_binget(UnpicklerObject *self)
5621{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005622 PyObject *value;
5623 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005625
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 return -1;
5628
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005629 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005631 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005633 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005634 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005635 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005636 Py_DECREF(key);
5637 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005638 return -1;
5639 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005640
5641 PDATA_APPEND(self->stack, value, -1);
5642 return 0;
5643}
5644
5645/* Push an object from the extension registry (EXT[124]). nbytes is
5646 * the number of bytes following the opcode, holding the index (code) value.
5647 */
5648static int
5649load_extension(UnpicklerObject *self, int nbytes)
5650{
5651 char *codebytes; /* the nbytes bytes after the opcode */
5652 long code; /* calc_binint returns long */
5653 PyObject *py_code; /* code as a Python int */
5654 PyObject *obj; /* the object to push */
5655 PyObject *pair; /* (module_name, class_name) */
5656 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005657 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658
5659 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005660 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005661 return -1;
5662 code = calc_binint(codebytes, nbytes);
5663 if (code <= 0) { /* note that 0 is forbidden */
5664 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005665 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005666 return -1;
5667 }
5668
5669 /* Look for the code in the cache. */
5670 py_code = PyLong_FromLong(code);
5671 if (py_code == NULL)
5672 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005673 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005674 if (obj != NULL) {
5675 /* Bingo. */
5676 Py_DECREF(py_code);
5677 PDATA_APPEND(self->stack, obj, -1);
5678 return 0;
5679 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005680 if (PyErr_Occurred()) {
5681 Py_DECREF(py_code);
5682 return -1;
5683 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684
5685 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005686 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005687 if (pair == NULL) {
5688 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005689 if (!PyErr_Occurred()) {
5690 PyErr_Format(PyExc_ValueError, "unregistered extension "
5691 "code %ld", code);
5692 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005693 return -1;
5694 }
5695 /* Since the extension registry is manipulable via Python code,
5696 * confirm that pair is really a 2-tuple of strings.
5697 */
5698 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5699 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5700 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5701 Py_DECREF(py_code);
5702 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5703 "isn't a 2-tuple of strings", code);
5704 return -1;
5705 }
5706 /* Load the object. */
5707 obj = find_class(self, module_name, class_name);
5708 if (obj == NULL) {
5709 Py_DECREF(py_code);
5710 return -1;
5711 }
5712 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005713 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005714 Py_DECREF(py_code);
5715 if (code < 0) {
5716 Py_DECREF(obj);
5717 return -1;
5718 }
5719 PDATA_PUSH(self->stack, obj, -1);
5720 return 0;
5721}
5722
5723static int
5724load_put(UnpicklerObject *self)
5725{
5726 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005727 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005728 Py_ssize_t len;
5729 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005730
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005731 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 return -1;
5733 if (len < 2)
5734 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005735 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005737 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005738
5739 key = PyLong_FromString(s, NULL, 10);
5740 if (key == NULL)
5741 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005742 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005743 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005744 if (idx < 0) {
5745 if (!PyErr_Occurred())
5746 PyErr_SetString(PyExc_ValueError,
5747 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005748 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005749 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005750
5751 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005752}
5753
5754static int
5755load_binput(UnpicklerObject *self)
5756{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005757 PyObject *value;
5758 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005759 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005762 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005763
5764 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005767
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771}
5772
5773static int
5774load_long_binput(UnpicklerObject *self)
5775{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005776 PyObject *value;
5777 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005779
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005782
5783 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005786
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005787 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005788 if (idx < 0) {
5789 PyErr_SetString(PyExc_ValueError,
5790 "negative LONG_BINPUT argument");
5791 return -1;
5792 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005795}
5796
5797static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005798load_memoize(UnpicklerObject *self)
5799{
5800 PyObject *value;
5801
5802 if (Py_SIZE(self->stack) <= 0)
5803 return stack_underflow();
5804 value = self->stack->data[Py_SIZE(self->stack) - 1];
5805
5806 return _Unpickler_MemoPut(self, self->memo_len, value);
5807}
5808
5809static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005810do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005811{
5812 PyObject *value;
5813 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005814 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005816 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817 if (x > len || x <= 0)
5818 return stack_underflow();
5819 if (len == x) /* nothing to do */
5820 return 0;
5821
5822 list = self->stack->data[x - 1];
5823
5824 if (PyList_Check(list)) {
5825 PyObject *slice;
5826 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005827 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005828
5829 slice = Pdata_poplist(self->stack, x);
5830 if (!slice)
5831 return -1;
5832 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005833 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005834 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005835 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005836 }
5837 else {
5838 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005839 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005841 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005842 if (append_func == NULL)
5843 return -1;
5844 for (i = x; i < len; i++) {
5845 PyObject *result;
5846
5847 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005848 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005849 if (result == NULL) {
5850 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005851 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005852 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005853 return -1;
5854 }
5855 Py_DECREF(result);
5856 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005857 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005858 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005859 }
5860
5861 return 0;
5862}
5863
5864static int
5865load_append(UnpicklerObject *self)
5866{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005867 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005868}
5869
5870static int
5871load_appends(UnpicklerObject *self)
5872{
5873 return do_append(self, marker(self));
5874}
5875
5876static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005877do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005878{
5879 PyObject *value, *key;
5880 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005881 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882 int status = 0;
5883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005884 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005885 if (x > len || x <= 0)
5886 return stack_underflow();
5887 if (len == x) /* nothing to do */
5888 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005889 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005890 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005891 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005892 PyErr_SetString(st->UnpicklingError,
5893 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005894 return -1;
5895 }
5896
5897 /* Here, dict does not actually need to be a PyDict; it could be anything
5898 that supports the __setitem__ attribute. */
5899 dict = self->stack->data[x - 1];
5900
5901 for (i = x + 1; i < len; i += 2) {
5902 key = self->stack->data[i - 1];
5903 value = self->stack->data[i];
5904 if (PyObject_SetItem(dict, key, value) < 0) {
5905 status = -1;
5906 break;
5907 }
5908 }
5909
5910 Pdata_clear(self->stack, x);
5911 return status;
5912}
5913
5914static int
5915load_setitem(UnpicklerObject *self)
5916{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005917 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918}
5919
5920static int
5921load_setitems(UnpicklerObject *self)
5922{
5923 return do_setitems(self, marker(self));
5924}
5925
5926static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005927load_additems(UnpicklerObject *self)
5928{
5929 PyObject *set;
5930 Py_ssize_t mark, len, i;
5931
5932 mark = marker(self);
5933 len = Py_SIZE(self->stack);
5934 if (mark > len || mark <= 0)
5935 return stack_underflow();
5936 if (len == mark) /* nothing to do */
5937 return 0;
5938
5939 set = self->stack->data[mark - 1];
5940
5941 if (PySet_Check(set)) {
5942 PyObject *items;
5943 int status;
5944
5945 items = Pdata_poptuple(self->stack, mark);
5946 if (items == NULL)
5947 return -1;
5948
5949 status = _PySet_Update(set, items);
5950 Py_DECREF(items);
5951 return status;
5952 }
5953 else {
5954 PyObject *add_func;
5955 _Py_IDENTIFIER(add);
5956
5957 add_func = _PyObject_GetAttrId(set, &PyId_add);
5958 if (add_func == NULL)
5959 return -1;
5960 for (i = mark; i < len; i++) {
5961 PyObject *result;
5962 PyObject *item;
5963
5964 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005965 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005966 if (result == NULL) {
5967 Pdata_clear(self->stack, i + 1);
5968 Py_SIZE(self->stack) = mark;
5969 return -1;
5970 }
5971 Py_DECREF(result);
5972 }
5973 Py_SIZE(self->stack) = mark;
5974 }
5975
5976 return 0;
5977}
5978
5979static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005980load_build(UnpicklerObject *self)
5981{
5982 PyObject *state, *inst, *slotstate;
5983 PyObject *setstate;
5984 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005985 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005986
5987 /* Stack is ... instance, state. We want to leave instance at
5988 * the stack top, possibly mutated via instance.__setstate__(state).
5989 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005990 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005991 return stack_underflow();
5992
5993 PDATA_POP(self->stack, state);
5994 if (state == NULL)
5995 return -1;
5996
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005997 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005998
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005999 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006000 if (setstate == NULL) {
6001 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6002 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006003 else {
6004 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006005 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006006 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006007 }
6008 else {
6009 PyObject *result;
6010
6011 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006012 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006013 Py_DECREF(setstate);
6014 if (result == NULL)
6015 return -1;
6016 Py_DECREF(result);
6017 return 0;
6018 }
6019
6020 /* A default __setstate__. First see whether state embeds a
6021 * slot state dict too (a proto 2 addition).
6022 */
6023 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6024 PyObject *tmp = state;
6025
6026 state = PyTuple_GET_ITEM(tmp, 0);
6027 slotstate = PyTuple_GET_ITEM(tmp, 1);
6028 Py_INCREF(state);
6029 Py_INCREF(slotstate);
6030 Py_DECREF(tmp);
6031 }
6032 else
6033 slotstate = NULL;
6034
6035 /* Set inst.__dict__ from the state dict (if any). */
6036 if (state != Py_None) {
6037 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006038 PyObject *d_key, *d_value;
6039 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006040 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041
6042 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006043 PickleState *st = _Pickle_GetGlobalState();
6044 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006045 goto error;
6046 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006047 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006048 if (dict == NULL)
6049 goto error;
6050
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006051 i = 0;
6052 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6053 /* normally the keys for instance attributes are
6054 interned. we should try to do that here. */
6055 Py_INCREF(d_key);
6056 if (PyUnicode_CheckExact(d_key))
6057 PyUnicode_InternInPlace(&d_key);
6058 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6059 Py_DECREF(d_key);
6060 goto error;
6061 }
6062 Py_DECREF(d_key);
6063 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006064 Py_DECREF(dict);
6065 }
6066
6067 /* Also set instance attributes from the slotstate dict (if any). */
6068 if (slotstate != NULL) {
6069 PyObject *d_key, *d_value;
6070 Py_ssize_t i;
6071
6072 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006073 PickleState *st = _Pickle_GetGlobalState();
6074 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006075 "slot state is not a dictionary");
6076 goto error;
6077 }
6078 i = 0;
6079 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6080 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6081 goto error;
6082 }
6083 }
6084
6085 if (0) {
6086 error:
6087 status = -1;
6088 }
6089
6090 Py_DECREF(state);
6091 Py_XDECREF(slotstate);
6092 return status;
6093}
6094
6095static int
6096load_mark(UnpicklerObject *self)
6097{
6098
6099 /* Note that we split the (pickle.py) stack into two stacks, an
6100 * object stack and a mark stack. Here we push a mark onto the
6101 * mark stack.
6102 */
6103
6104 if ((self->num_marks + 1) >= self->marks_size) {
6105 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006106
6107 /* Use the size_t type to check for overflow. */
6108 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006109 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006110 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006111 PyErr_NoMemory();
6112 return -1;
6113 }
6114
6115 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006116 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006117 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006118 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6119 if (self->marks == NULL) {
6120 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121 PyErr_NoMemory();
6122 return -1;
6123 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006124 self->marks_size = (Py_ssize_t)alloc;
6125 }
6126
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006127 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128
6129 return 0;
6130}
6131
6132static int
6133load_reduce(UnpicklerObject *self)
6134{
6135 PyObject *callable = NULL;
6136 PyObject *argtup = NULL;
6137 PyObject *obj = NULL;
6138
6139 PDATA_POP(self->stack, argtup);
6140 if (argtup == NULL)
6141 return -1;
6142 PDATA_POP(self->stack, callable);
6143 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006144 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145 Py_DECREF(callable);
6146 }
6147 Py_DECREF(argtup);
6148
6149 if (obj == NULL)
6150 return -1;
6151
6152 PDATA_PUSH(self->stack, obj, -1);
6153 return 0;
6154}
6155
6156/* Just raises an error if we don't know the protocol specified. PROTO
6157 * is the first opcode for protocols >= 2.
6158 */
6159static int
6160load_proto(UnpicklerObject *self)
6161{
6162 char *s;
6163 int i;
6164
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006165 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006166 return -1;
6167
6168 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006169 if (i <= HIGHEST_PROTOCOL) {
6170 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006171 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006172 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006173
6174 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6175 return -1;
6176}
6177
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006178static int
6179load_frame(UnpicklerObject *self)
6180{
6181 char *s;
6182 Py_ssize_t frame_len;
6183
6184 if (_Unpickler_Read(self, &s, 8) < 0)
6185 return -1;
6186
6187 frame_len = calc_binsize(s, 8);
6188 if (frame_len < 0) {
6189 PyErr_Format(PyExc_OverflowError,
6190 "FRAME length exceeds system's maximum of %zd bytes",
6191 PY_SSIZE_T_MAX);
6192 return -1;
6193 }
6194
6195 if (_Unpickler_Read(self, &s, frame_len) < 0)
6196 return -1;
6197
6198 /* Rewind to start of frame */
6199 self->next_read_idx -= frame_len;
6200 return 0;
6201}
6202
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006203static PyObject *
6204load(UnpicklerObject *self)
6205{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006207 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006208
6209 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006210 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006211 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006212 Pdata_clear(self->stack, 0);
6213
6214 /* Convenient macros for the dispatch while-switch loop just below. */
6215#define OP(opcode, load_func) \
6216 case opcode: if (load_func(self) < 0) break; continue;
6217
6218#define OP_ARG(opcode, load_func, arg) \
6219 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6220
6221 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006222 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006223 break;
6224
6225 switch ((enum opcode)s[0]) {
6226 OP(NONE, load_none)
6227 OP(BININT, load_binint)
6228 OP(BININT1, load_binint1)
6229 OP(BININT2, load_binint2)
6230 OP(INT, load_int)
6231 OP(LONG, load_long)
6232 OP_ARG(LONG1, load_counted_long, 1)
6233 OP_ARG(LONG4, load_counted_long, 4)
6234 OP(FLOAT, load_float)
6235 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006236 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6237 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6238 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6239 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6240 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241 OP(STRING, load_string)
6242 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006243 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6244 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6245 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006246 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6247 OP_ARG(TUPLE1, load_counted_tuple, 1)
6248 OP_ARG(TUPLE2, load_counted_tuple, 2)
6249 OP_ARG(TUPLE3, load_counted_tuple, 3)
6250 OP(TUPLE, load_tuple)
6251 OP(EMPTY_LIST, load_empty_list)
6252 OP(LIST, load_list)
6253 OP(EMPTY_DICT, load_empty_dict)
6254 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006255 OP(EMPTY_SET, load_empty_set)
6256 OP(ADDITEMS, load_additems)
6257 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258 OP(OBJ, load_obj)
6259 OP(INST, load_inst)
6260 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006261 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006262 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006263 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006264 OP(APPEND, load_append)
6265 OP(APPENDS, load_appends)
6266 OP(BUILD, load_build)
6267 OP(DUP, load_dup)
6268 OP(BINGET, load_binget)
6269 OP(LONG_BINGET, load_long_binget)
6270 OP(GET, load_get)
6271 OP(MARK, load_mark)
6272 OP(BINPUT, load_binput)
6273 OP(LONG_BINPUT, load_long_binput)
6274 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006275 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006276 OP(POP, load_pop)
6277 OP(POP_MARK, load_pop_mark)
6278 OP(SETITEM, load_setitem)
6279 OP(SETITEMS, load_setitems)
6280 OP(PERSID, load_persid)
6281 OP(BINPERSID, load_binpersid)
6282 OP(REDUCE, load_reduce)
6283 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006284 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006285 OP_ARG(EXT1, load_extension, 1)
6286 OP_ARG(EXT2, load_extension, 2)
6287 OP_ARG(EXT4, load_extension, 4)
6288 OP_ARG(NEWTRUE, load_bool, Py_True)
6289 OP_ARG(NEWFALSE, load_bool, Py_False)
6290
6291 case STOP:
6292 break;
6293
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006294 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006295 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006296 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006297 }
6298 else {
6299 PickleState *st = _Pickle_GetGlobalState();
6300 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006301 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006302 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006303 return NULL;
6304 }
6305
6306 break; /* and we are done! */
6307 }
6308
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006309 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006310 return NULL;
6311 }
6312
Victor Stinner2ae57e32013-10-31 13:39:23 +01006313 if (_Unpickler_SkipConsumed(self) < 0)
6314 return NULL;
6315
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006316 PDATA_POP(self->stack, value);
6317 return value;
6318}
6319
Larry Hastings61272b72014-01-07 12:41:53 -08006320/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006321
6322_pickle.Unpickler.load
6323
6324Load a pickle.
6325
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006326Read a pickled object representation from the open file object given
6327in the constructor, and return the reconstituted object hierarchy
6328specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006329[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006330
Larry Hastings3cceb382014-01-04 11:09:09 -08006331static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006332_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006333/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006334{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006335 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006336
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006337 /* Check whether the Unpickler was initialized correctly. This prevents
6338 segfaulting if a subclass overridden __init__ with a function that does
6339 not call Unpickler.__init__(). Here, we simply ensure that self->read
6340 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006341 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006342 PickleState *st = _Pickle_GetGlobalState();
6343 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006344 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006345 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006346 return NULL;
6347 }
6348
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006349 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350}
6351
6352/* The name of find_class() is misleading. In newer pickle protocols, this
6353 function is used for loading any global (i.e., functions), not just
6354 classes. The name is kept only for backward compatibility. */
6355
Larry Hastings61272b72014-01-07 12:41:53 -08006356/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006357
6358_pickle.Unpickler.find_class
6359
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006360 module_name: object
6361 global_name: object
6362 /
6363
6364Return an object from a specified module.
6365
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006366If necessary, the module will be imported. Subclasses may override
6367this method (e.g. to restrict unpickling of arbitrary classes and
6368functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006369
6370This method is called whenever a class or a function object is
6371needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006372[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006373
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006374static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006375_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6376 PyObject *module_name,
6377 PyObject *global_name)
6378/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006379{
6380 PyObject *global;
6381 PyObject *modules_dict;
6382 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006383 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006384
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006385 /* Try to map the old names used in Python 2.x to the new ones used in
6386 Python 3.x. We do this only with old pickle protocols and when the
6387 user has not disabled the feature. */
6388 if (self->proto < 3 && self->fix_imports) {
6389 PyObject *key;
6390 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006391 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006392
6393 /* Check if the global (i.e., a function or a class) was renamed
6394 or moved to another module. */
6395 key = PyTuple_Pack(2, module_name, global_name);
6396 if (key == NULL)
6397 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006398 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006399 Py_DECREF(key);
6400 if (item) {
6401 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6402 PyErr_Format(PyExc_RuntimeError,
6403 "_compat_pickle.NAME_MAPPING values should be "
6404 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6405 return NULL;
6406 }
6407 module_name = PyTuple_GET_ITEM(item, 0);
6408 global_name = PyTuple_GET_ITEM(item, 1);
6409 if (!PyUnicode_Check(module_name) ||
6410 !PyUnicode_Check(global_name)) {
6411 PyErr_Format(PyExc_RuntimeError,
6412 "_compat_pickle.NAME_MAPPING values should be "
6413 "pairs of str, not (%.200s, %.200s)",
6414 Py_TYPE(module_name)->tp_name,
6415 Py_TYPE(global_name)->tp_name);
6416 return NULL;
6417 }
6418 }
6419 else if (PyErr_Occurred()) {
6420 return NULL;
6421 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006422 else {
6423 /* Check if the module was renamed. */
6424 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6425 if (item) {
6426 if (!PyUnicode_Check(item)) {
6427 PyErr_Format(PyExc_RuntimeError,
6428 "_compat_pickle.IMPORT_MAPPING values should be "
6429 "strings, not %.200s", Py_TYPE(item)->tp_name);
6430 return NULL;
6431 }
6432 module_name = item;
6433 }
6434 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006435 return NULL;
6436 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006437 }
6438 }
6439
Victor Stinnerbb520202013-11-06 22:40:41 +01006440 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006441 if (modules_dict == NULL) {
6442 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006443 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006444 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006445
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006446 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006447 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006448 if (PyErr_Occurred())
6449 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006450 module = PyImport_Import(module_name);
6451 if (module == NULL)
6452 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006453 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006454 Py_DECREF(module);
6455 }
Victor Stinner121aab42011-09-29 23:40:53 +02006456 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006457 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006458 }
6459 return global;
6460}
6461
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006462/*[clinic input]
6463
6464_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6465
6466Returns size in memory, in bytes.
6467[clinic start generated code]*/
6468
6469static Py_ssize_t
6470_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6471/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6472{
6473 Py_ssize_t res;
6474
6475 res = sizeof(UnpicklerObject);
6476 if (self->memo != NULL)
6477 res += self->memo_size * sizeof(PyObject *);
6478 if (self->marks != NULL)
6479 res += self->marks_size * sizeof(Py_ssize_t);
6480 if (self->input_line != NULL)
6481 res += strlen(self->input_line) + 1;
6482 if (self->encoding != NULL)
6483 res += strlen(self->encoding) + 1;
6484 if (self->errors != NULL)
6485 res += strlen(self->errors) + 1;
6486 return res;
6487}
6488
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006489static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006490 _PICKLE_UNPICKLER_LOAD_METHODDEF
6491 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006492 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006493 {NULL, NULL} /* sentinel */
6494};
6495
6496static void
6497Unpickler_dealloc(UnpicklerObject *self)
6498{
6499 PyObject_GC_UnTrack((PyObject *)self);
6500 Py_XDECREF(self->readline);
6501 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006502 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006503 Py_XDECREF(self->stack);
6504 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006505 if (self->buffer.buf != NULL) {
6506 PyBuffer_Release(&self->buffer);
6507 self->buffer.buf = NULL;
6508 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006509
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006510 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006511 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006512 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006513 PyMem_Free(self->encoding);
6514 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006515
6516 Py_TYPE(self)->tp_free((PyObject *)self);
6517}
6518
6519static int
6520Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6521{
6522 Py_VISIT(self->readline);
6523 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006524 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006525 Py_VISIT(self->stack);
6526 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006527 return 0;
6528}
6529
6530static int
6531Unpickler_clear(UnpicklerObject *self)
6532{
6533 Py_CLEAR(self->readline);
6534 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006535 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006536 Py_CLEAR(self->stack);
6537 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006538 if (self->buffer.buf != NULL) {
6539 PyBuffer_Release(&self->buffer);
6540 self->buffer.buf = NULL;
6541 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006543 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544 PyMem_Free(self->marks);
6545 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006546 PyMem_Free(self->input_line);
6547 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006548 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006549 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006550 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006551 self->errors = NULL;
6552
6553 return 0;
6554}
6555
Larry Hastings61272b72014-01-07 12:41:53 -08006556/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006557
6558_pickle.Unpickler.__init__
6559
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006560 file: object
6561 *
6562 fix_imports: bool = True
6563 encoding: str = 'ASCII'
6564 errors: str = 'strict'
6565
6566This takes a binary file for reading a pickle data stream.
6567
6568The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006569protocol argument is needed. Bytes past the pickled object's
6570representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006571
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006572The argument *file* must have two methods, a read() method that takes
6573an integer argument, and a readline() method that requires no
6574arguments. Both methods should return bytes. Thus *file* can be a
6575binary file object opened for reading, a io.BytesIO object, or any
6576other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006577
6578Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6579which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006580generated by Python 2. If *fix_imports* is True, pickle will try to
6581map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006582*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006583instances pickled by Python 2; these default to 'ASCII' and 'strict',
6584respectively. The *encoding* can be 'bytes' to read these 8-bit
6585string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006586[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006587
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006588static int
Larry Hastings89964c42015-04-14 18:07:59 -04006589_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6590 int fix_imports, const char *encoding,
6591 const char *errors)
6592/*[clinic end generated code: output=e2c8ce748edc57b0 input=30b4dc9e976b890c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006593{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006594 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006595
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006596 /* In case of multiple __init__() calls, clear previous content. */
6597 if (self->read != NULL)
6598 (void)Unpickler_clear(self);
6599
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006600 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006601 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006602
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006603 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006604 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006605
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006606 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006607 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006608 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006609
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006610 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006611 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6612 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006613 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006614 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006615 }
6616 else {
6617 self->pers_func = NULL;
6618 }
6619
6620 self->stack = (Pdata *)Pdata_New();
6621 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006622 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006623
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006624 self->memo_size = 32;
6625 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006626 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006627 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006628
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006629 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006630
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006631 return 0;
6632}
6633
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006635/* Define a proxy object for the Unpickler's internal memo object. This is to
6636 * avoid breaking code like:
6637 * unpickler.memo.clear()
6638 * and
6639 * unpickler.memo = saved_memo
6640 * Is this a good idea? Not really, but we don't want to break code that uses
6641 * it. Note that we don't implement the entire mapping API here. This is
6642 * intentional, as these should be treated as black-box implementation details.
6643 *
6644 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006645 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006646 */
6647
Larry Hastings61272b72014-01-07 12:41:53 -08006648/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006649_pickle.UnpicklerMemoProxy.clear
6650
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006651Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006652[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006653
Larry Hastings3cceb382014-01-04 11:09:09 -08006654static PyObject *
6655_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006656/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006657{
6658 _Unpickler_MemoCleanup(self->unpickler);
6659 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6660 if (self->unpickler->memo == NULL)
6661 return NULL;
6662 Py_RETURN_NONE;
6663}
6664
Larry Hastings61272b72014-01-07 12:41:53 -08006665/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006666_pickle.UnpicklerMemoProxy.copy
6667
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006668Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006669[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006670
Larry Hastings3cceb382014-01-04 11:09:09 -08006671static PyObject *
6672_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006673/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006674{
6675 Py_ssize_t i;
6676 PyObject *new_memo = PyDict_New();
6677 if (new_memo == NULL)
6678 return NULL;
6679
6680 for (i = 0; i < self->unpickler->memo_size; i++) {
6681 int status;
6682 PyObject *key, *value;
6683
6684 value = self->unpickler->memo[i];
6685 if (value == NULL)
6686 continue;
6687
6688 key = PyLong_FromSsize_t(i);
6689 if (key == NULL)
6690 goto error;
6691 status = PyDict_SetItem(new_memo, key, value);
6692 Py_DECREF(key);
6693 if (status < 0)
6694 goto error;
6695 }
6696 return new_memo;
6697
6698error:
6699 Py_DECREF(new_memo);
6700 return NULL;
6701}
6702
Larry Hastings61272b72014-01-07 12:41:53 -08006703/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006704_pickle.UnpicklerMemoProxy.__reduce__
6705
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006706Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006707[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006708
Larry Hastings3cceb382014-01-04 11:09:09 -08006709static PyObject *
6710_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006711/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006712{
6713 PyObject *reduce_value;
6714 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006715 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006716 if (contents == NULL)
6717 return NULL;
6718
6719 reduce_value = PyTuple_New(2);
6720 if (reduce_value == NULL) {
6721 Py_DECREF(contents);
6722 return NULL;
6723 }
6724 constructor_args = PyTuple_New(1);
6725 if (constructor_args == NULL) {
6726 Py_DECREF(contents);
6727 Py_DECREF(reduce_value);
6728 return NULL;
6729 }
6730 PyTuple_SET_ITEM(constructor_args, 0, contents);
6731 Py_INCREF((PyObject *)&PyDict_Type);
6732 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6733 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6734 return reduce_value;
6735}
6736
6737static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006738 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6739 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6740 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006741 {NULL, NULL} /* sentinel */
6742};
6743
6744static void
6745UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6746{
6747 PyObject_GC_UnTrack(self);
6748 Py_XDECREF(self->unpickler);
6749 PyObject_GC_Del((PyObject *)self);
6750}
6751
6752static int
6753UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6754 visitproc visit, void *arg)
6755{
6756 Py_VISIT(self->unpickler);
6757 return 0;
6758}
6759
6760static int
6761UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6762{
6763 Py_CLEAR(self->unpickler);
6764 return 0;
6765}
6766
6767static PyTypeObject UnpicklerMemoProxyType = {
6768 PyVarObject_HEAD_INIT(NULL, 0)
6769 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6770 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6771 0,
6772 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6773 0, /* tp_print */
6774 0, /* tp_getattr */
6775 0, /* tp_setattr */
6776 0, /* tp_compare */
6777 0, /* tp_repr */
6778 0, /* tp_as_number */
6779 0, /* tp_as_sequence */
6780 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006781 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006782 0, /* tp_call */
6783 0, /* tp_str */
6784 PyObject_GenericGetAttr, /* tp_getattro */
6785 PyObject_GenericSetAttr, /* tp_setattro */
6786 0, /* tp_as_buffer */
6787 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6788 0, /* tp_doc */
6789 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6790 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6791 0, /* tp_richcompare */
6792 0, /* tp_weaklistoffset */
6793 0, /* tp_iter */
6794 0, /* tp_iternext */
6795 unpicklerproxy_methods, /* tp_methods */
6796};
6797
6798static PyObject *
6799UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6800{
6801 UnpicklerMemoProxyObject *self;
6802
6803 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6804 &UnpicklerMemoProxyType);
6805 if (self == NULL)
6806 return NULL;
6807 Py_INCREF(unpickler);
6808 self->unpickler = unpickler;
6809 PyObject_GC_Track(self);
6810 return (PyObject *)self;
6811}
6812
6813/*****************************************************************************/
6814
6815
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006816static PyObject *
6817Unpickler_get_memo(UnpicklerObject *self)
6818{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006819 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006820}
6821
6822static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006823Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006824{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006825 PyObject **new_memo;
6826 Py_ssize_t new_memo_size = 0;
6827 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006828
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006829 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006830 PyErr_SetString(PyExc_TypeError,
6831 "attribute deletion is not supported");
6832 return -1;
6833 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006834
6835 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6836 UnpicklerObject *unpickler =
6837 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6838
6839 new_memo_size = unpickler->memo_size;
6840 new_memo = _Unpickler_NewMemo(new_memo_size);
6841 if (new_memo == NULL)
6842 return -1;
6843
6844 for (i = 0; i < new_memo_size; i++) {
6845 Py_XINCREF(unpickler->memo[i]);
6846 new_memo[i] = unpickler->memo[i];
6847 }
6848 }
6849 else if (PyDict_Check(obj)) {
6850 Py_ssize_t i = 0;
6851 PyObject *key, *value;
6852
6853 new_memo_size = PyDict_Size(obj);
6854 new_memo = _Unpickler_NewMemo(new_memo_size);
6855 if (new_memo == NULL)
6856 return -1;
6857
6858 while (PyDict_Next(obj, &i, &key, &value)) {
6859 Py_ssize_t idx;
6860 if (!PyLong_Check(key)) {
6861 PyErr_SetString(PyExc_TypeError,
6862 "memo key must be integers");
6863 goto error;
6864 }
6865 idx = PyLong_AsSsize_t(key);
6866 if (idx == -1 && PyErr_Occurred())
6867 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006868 if (idx < 0) {
6869 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006870 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006871 goto error;
6872 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006873 if (_Unpickler_MemoPut(self, idx, value) < 0)
6874 goto error;
6875 }
6876 }
6877 else {
6878 PyErr_Format(PyExc_TypeError,
6879 "'memo' attribute must be an UnpicklerMemoProxy object"
6880 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006881 return -1;
6882 }
6883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006884 _Unpickler_MemoCleanup(self);
6885 self->memo_size = new_memo_size;
6886 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006887
6888 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006889
6890 error:
6891 if (new_memo_size) {
6892 i = new_memo_size;
6893 while (--i >= 0) {
6894 Py_XDECREF(new_memo[i]);
6895 }
6896 PyMem_FREE(new_memo);
6897 }
6898 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006899}
6900
6901static PyObject *
6902Unpickler_get_persload(UnpicklerObject *self)
6903{
6904 if (self->pers_func == NULL)
6905 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6906 else
6907 Py_INCREF(self->pers_func);
6908 return self->pers_func;
6909}
6910
6911static int
6912Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6913{
6914 PyObject *tmp;
6915
6916 if (value == NULL) {
6917 PyErr_SetString(PyExc_TypeError,
6918 "attribute deletion is not supported");
6919 return -1;
6920 }
6921 if (!PyCallable_Check(value)) {
6922 PyErr_SetString(PyExc_TypeError,
6923 "persistent_load must be a callable taking "
6924 "one argument");
6925 return -1;
6926 }
6927
6928 tmp = self->pers_func;
6929 Py_INCREF(value);
6930 self->pers_func = value;
6931 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6932
6933 return 0;
6934}
6935
6936static PyGetSetDef Unpickler_getsets[] = {
6937 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6938 {"persistent_load", (getter)Unpickler_get_persload,
6939 (setter)Unpickler_set_persload},
6940 {NULL}
6941};
6942
6943static PyTypeObject Unpickler_Type = {
6944 PyVarObject_HEAD_INIT(NULL, 0)
6945 "_pickle.Unpickler", /*tp_name*/
6946 sizeof(UnpicklerObject), /*tp_basicsize*/
6947 0, /*tp_itemsize*/
6948 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6949 0, /*tp_print*/
6950 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006951 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006952 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006953 0, /*tp_repr*/
6954 0, /*tp_as_number*/
6955 0, /*tp_as_sequence*/
6956 0, /*tp_as_mapping*/
6957 0, /*tp_hash*/
6958 0, /*tp_call*/
6959 0, /*tp_str*/
6960 0, /*tp_getattro*/
6961 0, /*tp_setattro*/
6962 0, /*tp_as_buffer*/
6963 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006964 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006965 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6966 (inquiry)Unpickler_clear, /*tp_clear*/
6967 0, /*tp_richcompare*/
6968 0, /*tp_weaklistoffset*/
6969 0, /*tp_iter*/
6970 0, /*tp_iternext*/
6971 Unpickler_methods, /*tp_methods*/
6972 0, /*tp_members*/
6973 Unpickler_getsets, /*tp_getset*/
6974 0, /*tp_base*/
6975 0, /*tp_dict*/
6976 0, /*tp_descr_get*/
6977 0, /*tp_descr_set*/
6978 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006979 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006980 PyType_GenericAlloc, /*tp_alloc*/
6981 PyType_GenericNew, /*tp_new*/
6982 PyObject_GC_Del, /*tp_free*/
6983 0, /*tp_is_gc*/
6984};
6985
Larry Hastings61272b72014-01-07 12:41:53 -08006986/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006987
6988_pickle.dump
6989
6990 obj: object
6991 file: object
6992 protocol: object = NULL
6993 *
6994 fix_imports: bool = True
6995
6996Write a pickled representation of obj to the open file object file.
6997
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006998This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
6999be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007000
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007001The optional *protocol* argument tells the pickler to use the given
7002protocol supported protocols are 0, 1, 2, 3 and 4. The default
7003protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007005Specifying a negative protocol version selects the highest protocol
7006version supported. The higher the protocol used, the more recent the
7007version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007009The *file* argument must have a write() method that accepts a single
7010bytes argument. It can thus be a file object opened for binary
7011writing, a io.BytesIO instance, or any other custom object that meets
7012this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007013
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007014If *fix_imports* is True and protocol is less than 3, pickle will try
7015to map the new Python 3 names to the old module names used in Python
70162, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007017[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007018
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007019static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007020_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7021 PyObject *protocol, int fix_imports)
7022/*[clinic end generated code: output=0de7dff89c406816 input=e9e5fdd48de92eae]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007023{
7024 PicklerObject *pickler = _Pickler_New();
7025
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007026 if (pickler == NULL)
7027 return NULL;
7028
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007029 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007030 goto error;
7031
7032 if (_Pickler_SetOutputStream(pickler, file) < 0)
7033 goto error;
7034
7035 if (dump(pickler, obj) < 0)
7036 goto error;
7037
7038 if (_Pickler_FlushToFile(pickler) < 0)
7039 goto error;
7040
7041 Py_DECREF(pickler);
7042 Py_RETURN_NONE;
7043
7044 error:
7045 Py_XDECREF(pickler);
7046 return NULL;
7047}
7048
Larry Hastings61272b72014-01-07 12:41:53 -08007049/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007050
7051_pickle.dumps
7052
7053 obj: object
7054 protocol: object = NULL
7055 *
7056 fix_imports: bool = True
7057
7058Return the pickled representation of the object as a bytes object.
7059
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007060The optional *protocol* argument tells the pickler to use the given
7061protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7062protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007063
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007064Specifying a negative protocol version selects the highest protocol
7065version supported. The higher the protocol used, the more recent the
7066version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007067
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007068If *fix_imports* is True and *protocol* is less than 3, pickle will
7069try to map the new Python 3 names to the old module names used in
7070Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007071[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007072
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007073static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007074_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7075 int fix_imports)
7076/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007077{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007078 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007079 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007080
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007081 if (pickler == NULL)
7082 return NULL;
7083
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007084 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007085 goto error;
7086
7087 if (dump(pickler, obj) < 0)
7088 goto error;
7089
7090 result = _Pickler_GetString(pickler);
7091 Py_DECREF(pickler);
7092 return result;
7093
7094 error:
7095 Py_XDECREF(pickler);
7096 return NULL;
7097}
7098
Larry Hastings61272b72014-01-07 12:41:53 -08007099/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100
7101_pickle.load
7102
7103 file: object
7104 *
7105 fix_imports: bool = True
7106 encoding: str = 'ASCII'
7107 errors: str = 'strict'
7108
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007109Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007110
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007111This is equivalent to ``Unpickler(file).load()``, but may be more
7112efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007113
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007114The protocol version of the pickle is detected automatically, so no
7115protocol argument is needed. Bytes past the pickled object's
7116representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007117
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007118The argument *file* must have two methods, a read() method that takes
7119an integer argument, and a readline() method that requires no
7120arguments. Both methods should return bytes. Thus *file* can be a
7121binary file object opened for reading, a io.BytesIO object, or any
7122other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007123
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007124Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7125which are used to control compatiblity support for pickle stream
7126generated by Python 2. If *fix_imports* is True, pickle will try to
7127map the old Python 2 names to the new names used in Python 3. The
7128*encoding* and *errors* tell pickle how to decode 8-bit string
7129instances pickled by Python 2; these default to 'ASCII' and 'strict',
7130respectively. The *encoding* can be 'bytes' to read these 8-bit
7131string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007132[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007133
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007134static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007135_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7136 const char *encoding, const char *errors)
7137/*[clinic end generated code: output=798f1c57cb2b4eb1 input=da97372e38e510a6]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007139 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007140 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007141
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007142 if (unpickler == NULL)
7143 return NULL;
7144
7145 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7146 goto error;
7147
7148 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7149 goto error;
7150
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007151 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007152
7153 result = load(unpickler);
7154 Py_DECREF(unpickler);
7155 return result;
7156
7157 error:
7158 Py_XDECREF(unpickler);
7159 return NULL;
7160}
7161
Larry Hastings61272b72014-01-07 12:41:53 -08007162/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007163
7164_pickle.loads
7165
7166 data: object
7167 *
7168 fix_imports: bool = True
7169 encoding: str = 'ASCII'
7170 errors: str = 'strict'
7171
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007172Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007173
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007174The protocol version of the pickle is detected automatically, so no
7175protocol argument is needed. Bytes past the pickled object's
7176representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7179which are used to control compatiblity support for pickle stream
7180generated by Python 2. If *fix_imports* is True, pickle will try to
7181map the old Python 2 names to the new names used in Python 3. The
7182*encoding* and *errors* tell pickle how to decode 8-bit string
7183instances pickled by Python 2; these default to 'ASCII' and 'strict',
7184respectively. The *encoding* can be 'bytes' to read these 8-bit
7185string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007186[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007187
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007188static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007189_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7190 const char *encoding, const char *errors)
7191/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007193 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007194 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007195
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007196 if (unpickler == NULL)
7197 return NULL;
7198
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007199 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007200 goto error;
7201
7202 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7203 goto error;
7204
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007205 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007206
7207 result = load(unpickler);
7208 Py_DECREF(unpickler);
7209 return result;
7210
7211 error:
7212 Py_XDECREF(unpickler);
7213 return NULL;
7214}
7215
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007216static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007217 _PICKLE_DUMP_METHODDEF
7218 _PICKLE_DUMPS_METHODDEF
7219 _PICKLE_LOAD_METHODDEF
7220 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007221 {NULL, NULL} /* sentinel */
7222};
7223
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007224static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007225pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007226{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007227 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007228 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007229}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007230
Stefan Krahf483b0f2013-12-14 13:43:10 +01007231static void
7232pickle_free(PyObject *m)
7233{
7234 _Pickle_ClearState(_Pickle_GetState(m));
7235}
7236
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007237static int
7238pickle_traverse(PyObject *m, visitproc visit, void *arg)
7239{
7240 PickleState *st = _Pickle_GetState(m);
7241 Py_VISIT(st->PickleError);
7242 Py_VISIT(st->PicklingError);
7243 Py_VISIT(st->UnpicklingError);
7244 Py_VISIT(st->dispatch_table);
7245 Py_VISIT(st->extension_registry);
7246 Py_VISIT(st->extension_cache);
7247 Py_VISIT(st->inverted_registry);
7248 Py_VISIT(st->name_mapping_2to3);
7249 Py_VISIT(st->import_mapping_2to3);
7250 Py_VISIT(st->name_mapping_3to2);
7251 Py_VISIT(st->import_mapping_3to2);
7252 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007253 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007254 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007255}
7256
7257static struct PyModuleDef _picklemodule = {
7258 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007259 "_pickle", /* m_name */
7260 pickle_module_doc, /* m_doc */
7261 sizeof(PickleState), /* m_size */
7262 pickle_methods, /* m_methods */
7263 NULL, /* m_reload */
7264 pickle_traverse, /* m_traverse */
7265 pickle_clear, /* m_clear */
7266 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007267};
7268
7269PyMODINIT_FUNC
7270PyInit__pickle(void)
7271{
7272 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007273 PickleState *st;
7274
7275 m = PyState_FindModule(&_picklemodule);
7276 if (m) {
7277 Py_INCREF(m);
7278 return m;
7279 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007280
7281 if (PyType_Ready(&Unpickler_Type) < 0)
7282 return NULL;
7283 if (PyType_Ready(&Pickler_Type) < 0)
7284 return NULL;
7285 if (PyType_Ready(&Pdata_Type) < 0)
7286 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007287 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7288 return NULL;
7289 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7290 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007291
7292 /* Create the module and add the functions. */
7293 m = PyModule_Create(&_picklemodule);
7294 if (m == NULL)
7295 return NULL;
7296
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007297 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007298 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7299 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007300 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007301 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7302 return NULL;
7303
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007304 st = _Pickle_GetState(m);
7305
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007306 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007307 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7308 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007309 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007310 st->PicklingError = \
7311 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7312 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007313 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007314 st->UnpicklingError = \
7315 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7316 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007317 return NULL;
7318
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007319 Py_INCREF(st->PickleError);
7320 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007321 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007322 Py_INCREF(st->PicklingError);
7323 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007324 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007325 Py_INCREF(st->UnpicklingError);
7326 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007327 return NULL;
7328
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007329 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007330 return NULL;
7331
7332 return m;
7333}