blob: 06882d08092da6e014b279fba90d801125acbabd [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
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200877 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800878
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{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002113 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002114 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002115 void *data;
2116 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002117 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002118
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002119 if (PyUnicode_READY(obj))
2120 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002121
Victor Stinner358af132015-10-12 22:36:57 +02002122 _PyBytesWriter_Init(&writer);
2123
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002124 size = PyUnicode_GET_LENGTH(obj);
2125 data = PyUnicode_DATA(obj);
2126 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002127
Victor Stinner358af132015-10-12 22:36:57 +02002128 p = _PyBytesWriter_Alloc(&writer, size);
2129 if (p == NULL)
2130 goto error;
2131 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002132
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002133 for (i=0; i < size; i++) {
2134 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002135 /* Map 32-bit characters to '\Uxxxxxxxx' */
2136 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002137 /* -1: substract 1 preallocated byte */
2138 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2139 if (p == NULL)
2140 goto error;
2141
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002142 *p++ = '\\';
2143 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002144 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2145 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2146 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2147 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2148 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2149 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2150 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2151 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002152 }
Victor Stinner358af132015-10-12 22:36:57 +02002153 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002154 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002155 /* -1: substract 1 preallocated byte */
2156 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2157 if (p == NULL)
2158 goto error;
2159
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002160 *p++ = '\\';
2161 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002162 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2163 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2164 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2165 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002166 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002167 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002168 else
2169 *p++ = (char) ch;
2170 }
Victor Stinner358af132015-10-12 22:36:57 +02002171
2172 return _PyBytesWriter_Finish(&writer, p);
2173
2174error:
2175 _PyBytesWriter_Dealloc(&writer);
2176 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002177}
2178
2179static int
Antoine Pitrou299978d2013-04-07 17:38:11 +02002180write_utf8(PicklerObject *self, char *data, Py_ssize_t size)
2181{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002182 char header[9];
2183 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002184
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002185 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002186 if (size <= 0xff && self->proto >= 4) {
2187 header[0] = SHORT_BINUNICODE;
2188 header[1] = (unsigned char)(size & 0xff);
2189 len = 2;
2190 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002191 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002192 header[0] = BINUNICODE;
2193 header[1] = (unsigned char)(size & 0xff);
2194 header[2] = (unsigned char)((size >> 8) & 0xff);
2195 header[3] = (unsigned char)((size >> 16) & 0xff);
2196 header[4] = (unsigned char)((size >> 24) & 0xff);
2197 len = 5;
2198 }
2199 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002200 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002201 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002202 len = 9;
2203 }
2204 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002205 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002206 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002207 return -1;
2208 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002209
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002210 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002211 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002212 if (_Pickler_Write(self, data, size) < 0)
2213 return -1;
2214
2215 return 0;
2216}
2217
2218static int
2219write_unicode_binary(PicklerObject *self, PyObject *obj)
2220{
2221 PyObject *encoded = NULL;
2222 Py_ssize_t size;
2223 char *data;
2224 int r;
2225
2226 if (PyUnicode_READY(obj))
2227 return -1;
2228
2229 data = PyUnicode_AsUTF8AndSize(obj, &size);
2230 if (data != NULL)
2231 return write_utf8(self, data, size);
2232
2233 /* Issue #8383: for strings with lone surrogates, fallback on the
2234 "surrogatepass" error handler. */
2235 PyErr_Clear();
2236 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2237 if (encoded == NULL)
2238 return -1;
2239
2240 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2241 PyBytes_GET_SIZE(encoded));
2242 Py_DECREF(encoded);
2243 return r;
2244}
2245
2246static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002247save_unicode(PicklerObject *self, PyObject *obj)
2248{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002249 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002250 if (write_unicode_binary(self, obj) < 0)
2251 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002252 }
2253 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002254 PyObject *encoded;
2255 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256 const char unicode_op = UNICODE;
2257
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002258 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002259 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002260 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261
Antoine Pitrou299978d2013-04-07 17:38:11 +02002262 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2263 Py_DECREF(encoded);
2264 return -1;
2265 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002266
2267 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002268 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2269 Py_DECREF(encoded);
2270 return -1;
2271 }
2272 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002273
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002274 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002275 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002276 }
2277 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002278 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002279
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002280 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002281}
2282
2283/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2284static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002285store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002286{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002287 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288
2289 assert(PyTuple_Size(t) == len);
2290
2291 for (i = 0; i < len; i++) {
2292 PyObject *element = PyTuple_GET_ITEM(t, i);
2293
2294 if (element == NULL)
2295 return -1;
2296 if (save(self, element, 0) < 0)
2297 return -1;
2298 }
2299
2300 return 0;
2301}
2302
2303/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2304 * used across protocols to minimize the space needed to pickle them.
2305 * Tuples are also the only builtin immutable type that can be recursive
2306 * (a tuple can be reached from itself), and that requires some subtle
2307 * magic so that it works in all cases. IOW, this is a long routine.
2308 */
2309static int
2310save_tuple(PicklerObject *self, PyObject *obj)
2311{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002312 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002313
2314 const char mark_op = MARK;
2315 const char tuple_op = TUPLE;
2316 const char pop_op = POP;
2317 const char pop_mark_op = POP_MARK;
2318 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2319
2320 if ((len = PyTuple_Size(obj)) < 0)
2321 return -1;
2322
2323 if (len == 0) {
2324 char pdata[2];
2325
2326 if (self->proto) {
2327 pdata[0] = EMPTY_TUPLE;
2328 len = 1;
2329 }
2330 else {
2331 pdata[0] = MARK;
2332 pdata[1] = TUPLE;
2333 len = 2;
2334 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002335 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002336 return -1;
2337 return 0;
2338 }
2339
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002340 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002341 * saving the tuple elements, the tuple must be recursive, in
2342 * which case we'll pop everything we put on the stack, and fetch
2343 * its value from the memo.
2344 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345 if (len <= 3 && self->proto >= 2) {
2346 /* Use TUPLE{1,2,3} opcodes. */
2347 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002348 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002349
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002350 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002351 /* pop the len elements */
2352 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002353 if (_Pickler_Write(self, &pop_op, 1) < 0)
2354 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002355 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002356 if (memo_get(self, obj) < 0)
2357 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002359 return 0;
2360 }
2361 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002362 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2363 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364 }
2365 goto memoize;
2366 }
2367
2368 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2369 * Generate MARK e1 e2 ... TUPLE
2370 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002371 if (_Pickler_Write(self, &mark_op, 1) < 0)
2372 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002373
2374 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002375 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002376
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002377 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002378 /* pop the stack stuff we pushed */
2379 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002380 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2381 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002382 }
2383 else {
2384 /* Note that we pop one more than len, to remove
2385 * the MARK too.
2386 */
2387 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002388 if (_Pickler_Write(self, &pop_op, 1) < 0)
2389 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002390 }
2391 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002392 if (memo_get(self, obj) < 0)
2393 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002394
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002395 return 0;
2396 }
2397 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002398 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2399 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002400 }
2401
2402 memoize:
2403 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002404 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002405
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002406 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002407}
2408
2409/* iter is an iterator giving items, and we batch up chunks of
2410 * MARK item item ... item APPENDS
2411 * opcode sequences. Calling code should have arranged to first create an
2412 * empty list, or list-like object, for the APPENDS to operate on.
2413 * Returns 0 on success, <0 on error.
2414 */
2415static int
2416batch_list(PicklerObject *self, PyObject *iter)
2417{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002418 PyObject *obj = NULL;
2419 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002420 int i, n;
2421
2422 const char mark_op = MARK;
2423 const char append_op = APPEND;
2424 const char appends_op = APPENDS;
2425
2426 assert(iter != NULL);
2427
2428 /* XXX: I think this function could be made faster by avoiding the
2429 iterator interface and fetching objects directly from list using
2430 PyList_GET_ITEM.
2431 */
2432
2433 if (self->proto == 0) {
2434 /* APPENDS isn't available; do one at a time. */
2435 for (;;) {
2436 obj = PyIter_Next(iter);
2437 if (obj == NULL) {
2438 if (PyErr_Occurred())
2439 return -1;
2440 break;
2441 }
2442 i = save(self, obj, 0);
2443 Py_DECREF(obj);
2444 if (i < 0)
2445 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002446 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002447 return -1;
2448 }
2449 return 0;
2450 }
2451
2452 /* proto > 0: write in batches of BATCHSIZE. */
2453 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002454 /* Get first item */
2455 firstitem = PyIter_Next(iter);
2456 if (firstitem == NULL) {
2457 if (PyErr_Occurred())
2458 goto error;
2459
2460 /* nothing more to add */
2461 break;
2462 }
2463
2464 /* Try to get a second item */
2465 obj = PyIter_Next(iter);
2466 if (obj == NULL) {
2467 if (PyErr_Occurred())
2468 goto error;
2469
2470 /* Only one item to write */
2471 if (save(self, firstitem, 0) < 0)
2472 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002473 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002474 goto error;
2475 Py_CLEAR(firstitem);
2476 break;
2477 }
2478
2479 /* More than one item to write */
2480
2481 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002482 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002483 goto error;
2484
2485 if (save(self, firstitem, 0) < 0)
2486 goto error;
2487 Py_CLEAR(firstitem);
2488 n = 1;
2489
2490 /* Fetch and save up to BATCHSIZE items */
2491 while (obj) {
2492 if (save(self, obj, 0) < 0)
2493 goto error;
2494 Py_CLEAR(obj);
2495 n += 1;
2496
2497 if (n == BATCHSIZE)
2498 break;
2499
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002500 obj = PyIter_Next(iter);
2501 if (obj == NULL) {
2502 if (PyErr_Occurred())
2503 goto error;
2504 break;
2505 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002506 }
2507
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002508 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002509 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002510
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002511 } while (n == BATCHSIZE);
2512 return 0;
2513
2514 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002515 Py_XDECREF(firstitem);
2516 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002517 return -1;
2518}
2519
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002520/* This is a variant of batch_list() above, specialized for lists (with no
2521 * support for list subclasses). Like batch_list(), we batch up chunks of
2522 * MARK item item ... item APPENDS
2523 * opcode sequences. Calling code should have arranged to first create an
2524 * empty list, or list-like object, for the APPENDS to operate on.
2525 * Returns 0 on success, -1 on error.
2526 *
2527 * This version is considerably faster than batch_list(), if less general.
2528 *
2529 * Note that this only works for protocols > 0.
2530 */
2531static int
2532batch_list_exact(PicklerObject *self, PyObject *obj)
2533{
2534 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002535 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002536
2537 const char append_op = APPEND;
2538 const char appends_op = APPENDS;
2539 const char mark_op = MARK;
2540
2541 assert(obj != NULL);
2542 assert(self->proto > 0);
2543 assert(PyList_CheckExact(obj));
2544
2545 if (PyList_GET_SIZE(obj) == 1) {
2546 item = PyList_GET_ITEM(obj, 0);
2547 if (save(self, item, 0) < 0)
2548 return -1;
2549 if (_Pickler_Write(self, &append_op, 1) < 0)
2550 return -1;
2551 return 0;
2552 }
2553
2554 /* Write in batches of BATCHSIZE. */
2555 total = 0;
2556 do {
2557 this_batch = 0;
2558 if (_Pickler_Write(self, &mark_op, 1) < 0)
2559 return -1;
2560 while (total < PyList_GET_SIZE(obj)) {
2561 item = PyList_GET_ITEM(obj, total);
2562 if (save(self, item, 0) < 0)
2563 return -1;
2564 total++;
2565 if (++this_batch == BATCHSIZE)
2566 break;
2567 }
2568 if (_Pickler_Write(self, &appends_op, 1) < 0)
2569 return -1;
2570
2571 } while (total < PyList_GET_SIZE(obj));
2572
2573 return 0;
2574}
2575
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002576static int
2577save_list(PicklerObject *self, PyObject *obj)
2578{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002579 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002580 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002581 int status = 0;
2582
2583 if (self->fast && !fast_save_enter(self, obj))
2584 goto error;
2585
2586 /* Create an empty list. */
2587 if (self->bin) {
2588 header[0] = EMPTY_LIST;
2589 len = 1;
2590 }
2591 else {
2592 header[0] = MARK;
2593 header[1] = LIST;
2594 len = 2;
2595 }
2596
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002597 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002598 goto error;
2599
2600 /* Get list length, and bow out early if empty. */
2601 if ((len = PyList_Size(obj)) < 0)
2602 goto error;
2603
2604 if (memo_put(self, obj) < 0)
2605 goto error;
2606
2607 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002608 /* Materialize the list elements. */
2609 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002610 if (Py_EnterRecursiveCall(" while pickling an object"))
2611 goto error;
2612 status = batch_list_exact(self, obj);
2613 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002614 } else {
2615 PyObject *iter = PyObject_GetIter(obj);
2616 if (iter == NULL)
2617 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002618
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002619 if (Py_EnterRecursiveCall(" while pickling an object")) {
2620 Py_DECREF(iter);
2621 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002622 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002623 status = batch_list(self, iter);
2624 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002625 Py_DECREF(iter);
2626 }
2627 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002628 if (0) {
2629 error:
2630 status = -1;
2631 }
2632
2633 if (self->fast && !fast_save_leave(self, obj))
2634 status = -1;
2635
2636 return status;
2637}
2638
2639/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2640 * MARK key value ... key value SETITEMS
2641 * opcode sequences. Calling code should have arranged to first create an
2642 * empty dict, or dict-like object, for the SETITEMS to operate on.
2643 * Returns 0 on success, <0 on error.
2644 *
2645 * This is very much like batch_list(). The difference between saving
2646 * elements directly, and picking apart two-tuples, is so long-winded at
2647 * the C level, though, that attempts to combine these routines were too
2648 * ugly to bear.
2649 */
2650static int
2651batch_dict(PicklerObject *self, PyObject *iter)
2652{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002653 PyObject *obj = NULL;
2654 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002655 int i, n;
2656
2657 const char mark_op = MARK;
2658 const char setitem_op = SETITEM;
2659 const char setitems_op = SETITEMS;
2660
2661 assert(iter != NULL);
2662
2663 if (self->proto == 0) {
2664 /* SETITEMS isn't available; do one at a time. */
2665 for (;;) {
2666 obj = PyIter_Next(iter);
2667 if (obj == NULL) {
2668 if (PyErr_Occurred())
2669 return -1;
2670 break;
2671 }
2672 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2673 PyErr_SetString(PyExc_TypeError, "dict items "
2674 "iterator must return 2-tuples");
2675 return -1;
2676 }
2677 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2678 if (i >= 0)
2679 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2680 Py_DECREF(obj);
2681 if (i < 0)
2682 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002683 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002684 return -1;
2685 }
2686 return 0;
2687 }
2688
2689 /* proto > 0: write in batches of BATCHSIZE. */
2690 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002691 /* Get first item */
2692 firstitem = PyIter_Next(iter);
2693 if (firstitem == NULL) {
2694 if (PyErr_Occurred())
2695 goto error;
2696
2697 /* nothing more to add */
2698 break;
2699 }
2700 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2701 PyErr_SetString(PyExc_TypeError, "dict items "
2702 "iterator must return 2-tuples");
2703 goto error;
2704 }
2705
2706 /* Try to get a second item */
2707 obj = PyIter_Next(iter);
2708 if (obj == NULL) {
2709 if (PyErr_Occurred())
2710 goto error;
2711
2712 /* Only one item to write */
2713 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2714 goto error;
2715 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2716 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002717 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002718 goto error;
2719 Py_CLEAR(firstitem);
2720 break;
2721 }
2722
2723 /* More than one item to write */
2724
2725 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002726 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002727 goto error;
2728
2729 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2730 goto error;
2731 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2732 goto error;
2733 Py_CLEAR(firstitem);
2734 n = 1;
2735
2736 /* Fetch and save up to BATCHSIZE items */
2737 while (obj) {
2738 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2739 PyErr_SetString(PyExc_TypeError, "dict items "
2740 "iterator must return 2-tuples");
2741 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002742 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002743 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2744 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2745 goto error;
2746 Py_CLEAR(obj);
2747 n += 1;
2748
2749 if (n == BATCHSIZE)
2750 break;
2751
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002752 obj = PyIter_Next(iter);
2753 if (obj == NULL) {
2754 if (PyErr_Occurred())
2755 goto error;
2756 break;
2757 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002758 }
2759
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002760 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002761 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002762
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002763 } while (n == BATCHSIZE);
2764 return 0;
2765
2766 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002767 Py_XDECREF(firstitem);
2768 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002769 return -1;
2770}
2771
Collin Winter5c9b02d2009-05-25 05:43:30 +00002772/* This is a variant of batch_dict() above that specializes for dicts, with no
2773 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2774 * MARK key value ... key value SETITEMS
2775 * opcode sequences. Calling code should have arranged to first create an
2776 * empty dict, or dict-like object, for the SETITEMS to operate on.
2777 * Returns 0 on success, -1 on error.
2778 *
2779 * Note that this currently doesn't work for protocol 0.
2780 */
2781static int
2782batch_dict_exact(PicklerObject *self, PyObject *obj)
2783{
2784 PyObject *key = NULL, *value = NULL;
2785 int i;
2786 Py_ssize_t dict_size, ppos = 0;
2787
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002788 const char mark_op = MARK;
2789 const char setitem_op = SETITEM;
2790 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002791
2792 assert(obj != NULL);
2793 assert(self->proto > 0);
2794
2795 dict_size = PyDict_Size(obj);
2796
2797 /* Special-case len(d) == 1 to save space. */
2798 if (dict_size == 1) {
2799 PyDict_Next(obj, &ppos, &key, &value);
2800 if (save(self, key, 0) < 0)
2801 return -1;
2802 if (save(self, value, 0) < 0)
2803 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002804 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002805 return -1;
2806 return 0;
2807 }
2808
2809 /* Write in batches of BATCHSIZE. */
2810 do {
2811 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002812 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002813 return -1;
2814 while (PyDict_Next(obj, &ppos, &key, &value)) {
2815 if (save(self, key, 0) < 0)
2816 return -1;
2817 if (save(self, value, 0) < 0)
2818 return -1;
2819 if (++i == BATCHSIZE)
2820 break;
2821 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002822 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002823 return -1;
2824 if (PyDict_Size(obj) != dict_size) {
2825 PyErr_Format(
2826 PyExc_RuntimeError,
2827 "dictionary changed size during iteration");
2828 return -1;
2829 }
2830
2831 } while (i == BATCHSIZE);
2832 return 0;
2833}
2834
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002835static int
2836save_dict(PicklerObject *self, PyObject *obj)
2837{
2838 PyObject *items, *iter;
2839 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002840 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002841 int status = 0;
2842
2843 if (self->fast && !fast_save_enter(self, obj))
2844 goto error;
2845
2846 /* Create an empty dict. */
2847 if (self->bin) {
2848 header[0] = EMPTY_DICT;
2849 len = 1;
2850 }
2851 else {
2852 header[0] = MARK;
2853 header[1] = DICT;
2854 len = 2;
2855 }
2856
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002857 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002858 goto error;
2859
2860 /* Get dict size, and bow out early if empty. */
2861 if ((len = PyDict_Size(obj)) < 0)
2862 goto error;
2863
2864 if (memo_put(self, obj) < 0)
2865 goto error;
2866
2867 if (len != 0) {
2868 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002869 if (PyDict_CheckExact(obj) && self->proto > 0) {
2870 /* We can take certain shortcuts if we know this is a dict and
2871 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002872 if (Py_EnterRecursiveCall(" while pickling an object"))
2873 goto error;
2874 status = batch_dict_exact(self, obj);
2875 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002876 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002877 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002878
2879 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002880 if (items == NULL)
2881 goto error;
2882 iter = PyObject_GetIter(items);
2883 Py_DECREF(items);
2884 if (iter == NULL)
2885 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002886 if (Py_EnterRecursiveCall(" while pickling an object")) {
2887 Py_DECREF(iter);
2888 goto error;
2889 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002890 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002891 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002892 Py_DECREF(iter);
2893 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002894 }
2895
2896 if (0) {
2897 error:
2898 status = -1;
2899 }
2900
2901 if (self->fast && !fast_save_leave(self, obj))
2902 status = -1;
2903
2904 return status;
2905}
2906
2907static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002908save_set(PicklerObject *self, PyObject *obj)
2909{
2910 PyObject *item;
2911 int i;
2912 Py_ssize_t set_size, ppos = 0;
2913 Py_hash_t hash;
2914
2915 const char empty_set_op = EMPTY_SET;
2916 const char mark_op = MARK;
2917 const char additems_op = ADDITEMS;
2918
2919 if (self->proto < 4) {
2920 PyObject *items;
2921 PyObject *reduce_value;
2922 int status;
2923
2924 items = PySequence_List(obj);
2925 if (items == NULL) {
2926 return -1;
2927 }
2928 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2929 Py_DECREF(items);
2930 if (reduce_value == NULL) {
2931 return -1;
2932 }
2933 /* save_reduce() will memoize the object automatically. */
2934 status = save_reduce(self, reduce_value, obj);
2935 Py_DECREF(reduce_value);
2936 return status;
2937 }
2938
2939 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2940 return -1;
2941
2942 if (memo_put(self, obj) < 0)
2943 return -1;
2944
2945 set_size = PySet_GET_SIZE(obj);
2946 if (set_size == 0)
2947 return 0; /* nothing to do */
2948
2949 /* Write in batches of BATCHSIZE. */
2950 do {
2951 i = 0;
2952 if (_Pickler_Write(self, &mark_op, 1) < 0)
2953 return -1;
2954 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2955 if (save(self, item, 0) < 0)
2956 return -1;
2957 if (++i == BATCHSIZE)
2958 break;
2959 }
2960 if (_Pickler_Write(self, &additems_op, 1) < 0)
2961 return -1;
2962 if (PySet_GET_SIZE(obj) != set_size) {
2963 PyErr_Format(
2964 PyExc_RuntimeError,
2965 "set changed size during iteration");
2966 return -1;
2967 }
2968 } while (i == BATCHSIZE);
2969
2970 return 0;
2971}
2972
2973static int
2974save_frozenset(PicklerObject *self, PyObject *obj)
2975{
2976 PyObject *iter;
2977
2978 const char mark_op = MARK;
2979 const char frozenset_op = FROZENSET;
2980
2981 if (self->fast && !fast_save_enter(self, obj))
2982 return -1;
2983
2984 if (self->proto < 4) {
2985 PyObject *items;
2986 PyObject *reduce_value;
2987 int status;
2988
2989 items = PySequence_List(obj);
2990 if (items == NULL) {
2991 return -1;
2992 }
2993 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
2994 items);
2995 Py_DECREF(items);
2996 if (reduce_value == NULL) {
2997 return -1;
2998 }
2999 /* save_reduce() will memoize the object automatically. */
3000 status = save_reduce(self, reduce_value, obj);
3001 Py_DECREF(reduce_value);
3002 return status;
3003 }
3004
3005 if (_Pickler_Write(self, &mark_op, 1) < 0)
3006 return -1;
3007
3008 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003009 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003010 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003011 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003012 for (;;) {
3013 PyObject *item;
3014
3015 item = PyIter_Next(iter);
3016 if (item == NULL) {
3017 if (PyErr_Occurred()) {
3018 Py_DECREF(iter);
3019 return -1;
3020 }
3021 break;
3022 }
3023 if (save(self, item, 0) < 0) {
3024 Py_DECREF(item);
3025 Py_DECREF(iter);
3026 return -1;
3027 }
3028 Py_DECREF(item);
3029 }
3030 Py_DECREF(iter);
3031
3032 /* If the object is already in the memo, this means it is
3033 recursive. In this case, throw away everything we put on the
3034 stack, and fetch the object back from the memo. */
3035 if (PyMemoTable_Get(self->memo, obj)) {
3036 const char pop_mark_op = POP_MARK;
3037
3038 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3039 return -1;
3040 if (memo_get(self, obj) < 0)
3041 return -1;
3042 return 0;
3043 }
3044
3045 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3046 return -1;
3047 if (memo_put(self, obj) < 0)
3048 return -1;
3049
3050 return 0;
3051}
3052
3053static int
3054fix_imports(PyObject **module_name, PyObject **global_name)
3055{
3056 PyObject *key;
3057 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003058 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003059
3060 key = PyTuple_Pack(2, *module_name, *global_name);
3061 if (key == NULL)
3062 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003063 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003064 Py_DECREF(key);
3065 if (item) {
3066 PyObject *fixed_module_name;
3067 PyObject *fixed_global_name;
3068
3069 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3070 PyErr_Format(PyExc_RuntimeError,
3071 "_compat_pickle.REVERSE_NAME_MAPPING values "
3072 "should be 2-tuples, not %.200s",
3073 Py_TYPE(item)->tp_name);
3074 return -1;
3075 }
3076 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3077 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3078 if (!PyUnicode_Check(fixed_module_name) ||
3079 !PyUnicode_Check(fixed_global_name)) {
3080 PyErr_Format(PyExc_RuntimeError,
3081 "_compat_pickle.REVERSE_NAME_MAPPING values "
3082 "should be pairs of str, not (%.200s, %.200s)",
3083 Py_TYPE(fixed_module_name)->tp_name,
3084 Py_TYPE(fixed_global_name)->tp_name);
3085 return -1;
3086 }
3087
3088 Py_CLEAR(*module_name);
3089 Py_CLEAR(*global_name);
3090 Py_INCREF(fixed_module_name);
3091 Py_INCREF(fixed_global_name);
3092 *module_name = fixed_module_name;
3093 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003094 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003095 }
3096 else if (PyErr_Occurred()) {
3097 return -1;
3098 }
3099
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003100 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003101 if (item) {
3102 if (!PyUnicode_Check(item)) {
3103 PyErr_Format(PyExc_RuntimeError,
3104 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3105 "should be strings, not %.200s",
3106 Py_TYPE(item)->tp_name);
3107 return -1;
3108 }
3109 Py_CLEAR(*module_name);
3110 Py_INCREF(item);
3111 *module_name = item;
3112 }
3113 else if (PyErr_Occurred()) {
3114 return -1;
3115 }
3116
3117 return 0;
3118}
3119
3120static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003121save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3122{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003123 PyObject *global_name = NULL;
3124 PyObject *module_name = NULL;
3125 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003126 PyObject *parent = NULL;
3127 PyObject *dotted_path = NULL;
3128 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003130 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003132 _Py_IDENTIFIER(__name__);
3133 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003134
3135 const char global_op = GLOBAL;
3136
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003137 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003138 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003140 }
3141 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003142 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3143 if (global_name == NULL) {
3144 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3145 goto error;
3146 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003147 }
3148 if (global_name == NULL) {
3149 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3150 if (global_name == NULL)
3151 goto error;
3152 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003153 }
3154
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003155 dotted_path = get_dotted_path(module, global_name);
3156 if (dotted_path == NULL)
3157 goto error;
3158 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003159 if (module_name == NULL)
3160 goto error;
3161
3162 /* XXX: Change to use the import C API directly with level=0 to disallow
3163 relative imports.
3164
3165 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3166 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3167 custom import functions (IMHO, this would be a nice security
3168 feature). The import C API would need to be extended to support the
3169 extra parameters of __import__ to fix that. */
3170 module = PyImport_Import(module_name);
3171 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003172 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003173 "Can't pickle %R: import of module %R failed",
3174 obj, module_name);
3175 goto error;
3176 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003177 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3178 Py_INCREF(lastname);
3179 cls = get_deep_attribute(module, dotted_path, &parent);
3180 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003182 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003183 "Can't pickle %R: attribute lookup %S on %S failed",
3184 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003185 goto error;
3186 }
3187 if (cls != obj) {
3188 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003189 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003190 "Can't pickle %R: it's not the same object as %S.%S",
3191 obj, module_name, global_name);
3192 goto error;
3193 }
3194 Py_DECREF(cls);
3195
3196 if (self->proto >= 2) {
3197 /* See whether this is in the extension registry, and if
3198 * so generate an EXT opcode.
3199 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003200 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003201 PyObject *code_obj; /* extension code as Python object */
3202 long code; /* extension code as C value */
3203 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003204 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003205
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003206 extension_key = PyTuple_Pack(2, module_name, global_name);
3207 if (extension_key == NULL) {
3208 goto error;
3209 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003210 code_obj = PyDict_GetItemWithError(st->extension_registry,
3211 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003212 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003213 /* The object is not registered in the extension registry.
3214 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003215 if (code_obj == NULL) {
3216 if (PyErr_Occurred()) {
3217 goto error;
3218 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003219 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003220 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003221
3222 /* XXX: pickle.py doesn't check neither the type, nor the range
3223 of the value returned by the extension_registry. It should for
3224 consistency. */
3225
3226 /* Verify code_obj has the right type and value. */
3227 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003228 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003229 "Can't pickle %R: extension code %R isn't an integer",
3230 obj, code_obj);
3231 goto error;
3232 }
3233 code = PyLong_AS_LONG(code_obj);
3234 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003235 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3237 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003238 goto error;
3239 }
3240
3241 /* Generate an EXT opcode. */
3242 if (code <= 0xff) {
3243 pdata[0] = EXT1;
3244 pdata[1] = (unsigned char)code;
3245 n = 2;
3246 }
3247 else if (code <= 0xffff) {
3248 pdata[0] = EXT2;
3249 pdata[1] = (unsigned char)(code & 0xff);
3250 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3251 n = 3;
3252 }
3253 else {
3254 pdata[0] = EXT4;
3255 pdata[1] = (unsigned char)(code & 0xff);
3256 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3257 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3258 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3259 n = 5;
3260 }
3261
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003262 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003263 goto error;
3264 }
3265 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003266 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003267 if (parent == module) {
3268 Py_INCREF(lastname);
3269 Py_DECREF(global_name);
3270 global_name = lastname;
3271 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003272 if (self->proto >= 4) {
3273 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274
Christian Heimese8b1ba12013-11-23 21:13:39 +01003275 if (save(self, module_name, 0) < 0)
3276 goto error;
3277 if (save(self, global_name, 0) < 0)
3278 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003279
3280 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3281 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003282 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003283 else if (parent != module) {
3284 PickleState *st = _Pickle_GetGlobalState();
3285 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3286 st->getattr, parent, lastname);
3287 status = save_reduce(self, reduce_value, NULL);
3288 Py_DECREF(reduce_value);
3289 if (status < 0)
3290 goto error;
3291 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003292 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003293 /* Generate a normal global opcode if we are using a pickle
3294 protocol < 4, or if the object is not registered in the
3295 extension registry. */
3296 PyObject *encoded;
3297 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003298
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003299 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003300 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003301
3302 /* For protocol < 3 and if the user didn't request against doing
3303 so, we convert module names to the old 2.x module names. */
3304 if (self->proto < 3 && self->fix_imports) {
3305 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003306 goto error;
3307 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003308 }
3309
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003310 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3311 both the module name and the global name using UTF-8. We do so
3312 only when we are using the pickle protocol newer than version
3313 3. This is to ensure compatibility with older Unpickler running
3314 on Python 2.x. */
3315 if (self->proto == 3) {
3316 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003317 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003318 else {
3319 unicode_encoder = PyUnicode_AsASCIIString;
3320 }
3321 encoded = unicode_encoder(module_name);
3322 if (encoded == NULL) {
3323 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003324 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003325 "can't pickle module identifier '%S' using "
3326 "pickle protocol %i",
3327 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003328 goto error;
3329 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003330 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3331 PyBytes_GET_SIZE(encoded)) < 0) {
3332 Py_DECREF(encoded);
3333 goto error;
3334 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003335 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003336 if(_Pickler_Write(self, "\n", 1) < 0)
3337 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003338
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003339 /* Save the name of the module. */
3340 encoded = unicode_encoder(global_name);
3341 if (encoded == NULL) {
3342 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003343 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003344 "can't pickle global identifier '%S' using "
3345 "pickle protocol %i",
3346 global_name, self->proto);
3347 goto error;
3348 }
3349 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3350 PyBytes_GET_SIZE(encoded)) < 0) {
3351 Py_DECREF(encoded);
3352 goto error;
3353 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003354 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003355 if (_Pickler_Write(self, "\n", 1) < 0)
3356 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003357 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003358 /* Memoize the object. */
3359 if (memo_put(self, obj) < 0)
3360 goto error;
3361 }
3362
3363 if (0) {
3364 error:
3365 status = -1;
3366 }
3367 Py_XDECREF(module_name);
3368 Py_XDECREF(global_name);
3369 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003370 Py_XDECREF(parent);
3371 Py_XDECREF(dotted_path);
3372 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003373
3374 return status;
3375}
3376
3377static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003378save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3379{
3380 PyObject *reduce_value;
3381 int status;
3382
3383 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3384 if (reduce_value == NULL) {
3385 return -1;
3386 }
3387 status = save_reduce(self, reduce_value, obj);
3388 Py_DECREF(reduce_value);
3389 return status;
3390}
3391
3392static int
3393save_type(PicklerObject *self, PyObject *obj)
3394{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003395 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003396 return save_singleton_type(self, obj, Py_None);
3397 }
3398 else if (obj == (PyObject *)&PyEllipsis_Type) {
3399 return save_singleton_type(self, obj, Py_Ellipsis);
3400 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003401 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003402 return save_singleton_type(self, obj, Py_NotImplemented);
3403 }
3404 return save_global(self, obj, NULL);
3405}
3406
3407static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003408save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3409{
3410 PyObject *pid = NULL;
3411 int status = 0;
3412
3413 const char persid_op = PERSID;
3414 const char binpersid_op = BINPERSID;
3415
3416 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003417 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003418 if (pid == NULL)
3419 return -1;
3420
3421 if (pid != Py_None) {
3422 if (self->bin) {
3423 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003424 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003425 goto error;
3426 }
3427 else {
3428 PyObject *pid_str = NULL;
3429 char *pid_ascii_bytes;
3430 Py_ssize_t size;
3431
3432 pid_str = PyObject_Str(pid);
3433 if (pid_str == NULL)
3434 goto error;
3435
3436 /* XXX: Should it check whether the persistent id only contains
3437 ASCII characters? And what if the pid contains embedded
3438 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003439 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003440 Py_DECREF(pid_str);
3441 if (pid_ascii_bytes == NULL)
3442 goto error;
3443
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003444 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3445 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3446 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003447 goto error;
3448 }
3449 status = 1;
3450 }
3451
3452 if (0) {
3453 error:
3454 status = -1;
3455 }
3456 Py_XDECREF(pid);
3457
3458 return status;
3459}
3460
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003461static PyObject *
3462get_class(PyObject *obj)
3463{
3464 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003465 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003466
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003467 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003468 if (cls == NULL) {
3469 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3470 PyErr_Clear();
3471 cls = (PyObject *) Py_TYPE(obj);
3472 Py_INCREF(cls);
3473 }
3474 }
3475 return cls;
3476}
3477
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003478/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3479 * appropriate __reduce__ method for obj.
3480 */
3481static int
3482save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3483{
3484 PyObject *callable;
3485 PyObject *argtup;
3486 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003487 PyObject *listitems = Py_None;
3488 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003489 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003490 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003491 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003492
3493 const char reduce_op = REDUCE;
3494 const char build_op = BUILD;
3495 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003496 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003497
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003498 size = PyTuple_Size(args);
3499 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003500 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003501 "__reduce__ must contain 2 through 5 elements");
3502 return -1;
3503 }
3504
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003505 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3506 &callable, &argtup, &state, &listitems, &dictitems))
3507 return -1;
3508
3509 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003510 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003511 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003512 return -1;
3513 }
3514 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003515 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003516 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003517 return -1;
3518 }
3519
3520 if (state == Py_None)
3521 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003522
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003523 if (listitems == Py_None)
3524 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003525 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003526 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003527 "returned by __reduce__ must be an iterator, not %s",
3528 Py_TYPE(listitems)->tp_name);
3529 return -1;
3530 }
3531
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003532 if (dictitems == Py_None)
3533 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003534 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003535 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003536 "returned by __reduce__ must be an iterator, not %s",
3537 Py_TYPE(dictitems)->tp_name);
3538 return -1;
3539 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003541 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003542 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003543 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003544
Victor Stinner804e05e2013-11-14 01:26:17 +01003545 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003546 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003547 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003549 }
3550 PyErr_Clear();
3551 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003552 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003553 _Py_IDENTIFIER(__newobj_ex__);
3554 use_newobj_ex = PyUnicode_Compare(
3555 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003556 if (!use_newobj_ex) {
3557 _Py_IDENTIFIER(__newobj__);
3558 use_newobj = PyUnicode_Compare(
3559 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3560 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003561 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003562 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003563 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003564
3565 if (use_newobj_ex) {
3566 PyObject *cls;
3567 PyObject *args;
3568 PyObject *kwargs;
3569
3570 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003571 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572 "length of the NEWOBJ_EX argument tuple must be "
3573 "exactly 3, not %zd", Py_SIZE(argtup));
3574 return -1;
3575 }
3576
3577 cls = PyTuple_GET_ITEM(argtup, 0);
3578 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003579 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003580 "first item from NEWOBJ_EX argument tuple must "
3581 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3582 return -1;
3583 }
3584 args = PyTuple_GET_ITEM(argtup, 1);
3585 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003586 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003587 "second item from NEWOBJ_EX argument tuple must "
3588 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3589 return -1;
3590 }
3591 kwargs = PyTuple_GET_ITEM(argtup, 2);
3592 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003593 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003594 "third item from NEWOBJ_EX argument tuple must "
3595 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3596 return -1;
3597 }
3598
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003599 if (self->proto >= 4) {
3600 if (save(self, cls, 0) < 0 ||
3601 save(self, args, 0) < 0 ||
3602 save(self, kwargs, 0) < 0 ||
3603 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3604 return -1;
3605 }
3606 }
3607 else {
3608 PyObject *newargs;
3609 PyObject *cls_new;
3610 Py_ssize_t i;
3611 _Py_IDENTIFIER(__new__);
3612
3613 newargs = PyTuple_New(Py_SIZE(args) + 2);
3614 if (newargs == NULL)
3615 return -1;
3616
3617 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3618 if (cls_new == NULL) {
3619 Py_DECREF(newargs);
3620 return -1;
3621 }
3622 PyTuple_SET_ITEM(newargs, 0, cls_new);
3623 Py_INCREF(cls);
3624 PyTuple_SET_ITEM(newargs, 1, cls);
3625 for (i = 0; i < Py_SIZE(args); i++) {
3626 PyObject *item = PyTuple_GET_ITEM(args, i);
3627 Py_INCREF(item);
3628 PyTuple_SET_ITEM(newargs, i + 2, item);
3629 }
3630
3631 callable = PyObject_Call(st->partial, newargs, kwargs);
3632 Py_DECREF(newargs);
3633 if (callable == NULL)
3634 return -1;
3635
3636 newargs = PyTuple_New(0);
3637 if (newargs == NULL) {
3638 Py_DECREF(callable);
3639 return -1;
3640 }
3641
3642 if (save(self, callable, 0) < 0 ||
3643 save(self, newargs, 0) < 0 ||
3644 _Pickler_Write(self, &reduce_op, 1) < 0) {
3645 Py_DECREF(newargs);
3646 Py_DECREF(callable);
3647 return -1;
3648 }
3649 Py_DECREF(newargs);
3650 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003651 }
3652 }
3653 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003654 PyObject *cls;
3655 PyObject *newargtup;
3656 PyObject *obj_class;
3657 int p;
3658
3659 /* Sanity checks. */
3660 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003661 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003662 return -1;
3663 }
3664
3665 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003666 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003667 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003668 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003669 return -1;
3670 }
3671
3672 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003673 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003674 p = obj_class != cls; /* true iff a problem */
3675 Py_DECREF(obj_class);
3676 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003677 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003678 "__newobj__ args has the wrong class");
3679 return -1;
3680 }
3681 }
3682 /* XXX: These calls save() are prone to infinite recursion. Imagine
3683 what happen if the value returned by the __reduce__() method of
3684 some extension type contains another object of the same type. Ouch!
3685
3686 Here is a quick example, that I ran into, to illustrate what I
3687 mean:
3688
3689 >>> import pickle, copyreg
3690 >>> copyreg.dispatch_table.pop(complex)
3691 >>> pickle.dumps(1+2j)
3692 Traceback (most recent call last):
3693 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003694 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003695
3696 Removing the complex class from copyreg.dispatch_table made the
3697 __reduce_ex__() method emit another complex object:
3698
3699 >>> (1+1j).__reduce_ex__(2)
3700 (<function __newobj__ at 0xb7b71c3c>,
3701 (<class 'complex'>, (1+1j)), None, None, None)
3702
3703 Thus when save() was called on newargstup (the 2nd item) recursion
3704 ensued. Of course, the bug was in the complex class which had a
3705 broken __getnewargs__() that emitted another complex object. But,
3706 the point, here, is it is quite easy to end up with a broken reduce
3707 function. */
3708
3709 /* Save the class and its __new__ arguments. */
3710 if (save(self, cls, 0) < 0)
3711 return -1;
3712
3713 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3714 if (newargtup == NULL)
3715 return -1;
3716
3717 p = save(self, newargtup, 0);
3718 Py_DECREF(newargtup);
3719 if (p < 0)
3720 return -1;
3721
3722 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003723 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003724 return -1;
3725 }
3726 else { /* Not using NEWOBJ. */
3727 if (save(self, callable, 0) < 0 ||
3728 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003729 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003730 return -1;
3731 }
3732
3733 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3734 the caller do not want to memoize the object. Not particularly useful,
3735 but that is to mimic the behavior save_reduce() in pickle.py when
3736 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003737 if (obj != NULL) {
3738 /* If the object is already in the memo, this means it is
3739 recursive. In this case, throw away everything we put on the
3740 stack, and fetch the object back from the memo. */
3741 if (PyMemoTable_Get(self->memo, obj)) {
3742 const char pop_op = POP;
3743
3744 if (_Pickler_Write(self, &pop_op, 1) < 0)
3745 return -1;
3746 if (memo_get(self, obj) < 0)
3747 return -1;
3748
3749 return 0;
3750 }
3751 else if (memo_put(self, obj) < 0)
3752 return -1;
3753 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003754
3755 if (listitems && batch_list(self, listitems) < 0)
3756 return -1;
3757
3758 if (dictitems && batch_dict(self, dictitems) < 0)
3759 return -1;
3760
3761 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003762 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003763 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003764 return -1;
3765 }
3766
3767 return 0;
3768}
3769
3770static int
3771save(PicklerObject *self, PyObject *obj, int pers_save)
3772{
3773 PyTypeObject *type;
3774 PyObject *reduce_func = NULL;
3775 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003776 int status = 0;
3777
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003778 if (_Pickler_OpcodeBoundary(self) < 0)
3779 return -1;
3780
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003781 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003782 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003783
3784 /* The extra pers_save argument is necessary to avoid calling save_pers()
3785 on its returned object. */
3786 if (!pers_save && self->pers_func) {
3787 /* save_pers() returns:
3788 -1 to signal an error;
3789 0 if it did nothing successfully;
3790 1 if a persistent id was saved.
3791 */
3792 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3793 goto done;
3794 }
3795
3796 type = Py_TYPE(obj);
3797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003798 /* The old cPickle had an optimization that used switch-case statement
3799 dispatching on the first letter of the type name. This has was removed
3800 since benchmarks shown that this optimization was actually slowing
3801 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003802
3803 /* Atom types; these aren't memoized, so don't check the memo. */
3804
3805 if (obj == Py_None) {
3806 status = save_none(self, obj);
3807 goto done;
3808 }
3809 else if (obj == Py_False || obj == Py_True) {
3810 status = save_bool(self, obj);
3811 goto done;
3812 }
3813 else if (type == &PyLong_Type) {
3814 status = save_long(self, obj);
3815 goto done;
3816 }
3817 else if (type == &PyFloat_Type) {
3818 status = save_float(self, obj);
3819 goto done;
3820 }
3821
3822 /* Check the memo to see if it has the object. If so, generate
3823 a GET (or BINGET) opcode, instead of pickling the object
3824 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003825 if (PyMemoTable_Get(self->memo, obj)) {
3826 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003827 goto error;
3828 goto done;
3829 }
3830
3831 if (type == &PyBytes_Type) {
3832 status = save_bytes(self, obj);
3833 goto done;
3834 }
3835 else if (type == &PyUnicode_Type) {
3836 status = save_unicode(self, obj);
3837 goto done;
3838 }
3839 else if (type == &PyDict_Type) {
3840 status = save_dict(self, obj);
3841 goto done;
3842 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003843 else if (type == &PySet_Type) {
3844 status = save_set(self, obj);
3845 goto done;
3846 }
3847 else if (type == &PyFrozenSet_Type) {
3848 status = save_frozenset(self, obj);
3849 goto done;
3850 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003851 else if (type == &PyList_Type) {
3852 status = save_list(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyTuple_Type) {
3856 status = save_tuple(self, obj);
3857 goto done;
3858 }
3859 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003860 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003861 goto done;
3862 }
3863 else if (type == &PyFunction_Type) {
3864 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003865 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003866 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003867
3868 /* XXX: This part needs some unit tests. */
3869
3870 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003871 * self.dispatch_table, copyreg.dispatch_table, the object's
3872 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003873 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003874 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003875 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003876 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3877 (PyObject *)type);
3878 if (reduce_func == NULL) {
3879 if (PyErr_Occurred()) {
3880 goto error;
3881 }
3882 } else {
3883 /* PyDict_GetItemWithError() returns a borrowed reference.
3884 Increase the reference count to be consistent with
3885 PyObject_GetItem and _PyObject_GetAttrId used below. */
3886 Py_INCREF(reduce_func);
3887 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003888 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003889 reduce_func = PyObject_GetItem(self->dispatch_table,
3890 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003891 if (reduce_func == NULL) {
3892 if (PyErr_ExceptionMatches(PyExc_KeyError))
3893 PyErr_Clear();
3894 else
3895 goto error;
3896 }
3897 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003898 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003899 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003900 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003901 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003902 else if (PyType_IsSubtype(type, &PyType_Type)) {
3903 status = save_global(self, obj, NULL);
3904 goto done;
3905 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003906 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003907 _Py_IDENTIFIER(__reduce__);
3908 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003910
3911 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3912 automatically defined as __reduce__. While this is convenient, this
3913 make it impossible to know which method was actually called. Of
3914 course, this is not a big deal. But still, it would be nice to let
3915 the user know which method was called when something go
3916 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3917 don't actually have to check for a __reduce__ method. */
3918
3919 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003920 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003921 if (reduce_func != NULL) {
3922 PyObject *proto;
3923 proto = PyLong_FromLong(self->proto);
3924 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003925 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003926 }
3927 }
3928 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003929 PickleState *st = _Pickle_GetGlobalState();
3930
3931 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003932 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003933 }
3934 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003935 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003936 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003937 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003938 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003939 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003940 PyObject *empty_tuple = PyTuple_New(0);
3941 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003942 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003943 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003944 }
3945 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003946 PyErr_Format(st->PicklingError,
3947 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003948 type->tp_name, obj);
3949 goto error;
3950 }
3951 }
3952 }
3953
3954 if (reduce_value == NULL)
3955 goto error;
3956
3957 if (PyUnicode_Check(reduce_value)) {
3958 status = save_global(self, obj, reduce_value);
3959 goto done;
3960 }
3961
3962 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003963 PickleState *st = _Pickle_GetGlobalState();
3964 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003965 "__reduce__ must return a string or tuple");
3966 goto error;
3967 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003968
3969 status = save_reduce(self, reduce_value, obj);
3970
3971 if (0) {
3972 error:
3973 status = -1;
3974 }
3975 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003976
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003977 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003978 Py_XDECREF(reduce_func);
3979 Py_XDECREF(reduce_value);
3980
3981 return status;
3982}
3983
3984static int
3985dump(PicklerObject *self, PyObject *obj)
3986{
3987 const char stop_op = STOP;
3988
3989 if (self->proto >= 2) {
3990 char header[2];
3991
3992 header[0] = PROTO;
3993 assert(self->proto >= 0 && self->proto < 256);
3994 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003995 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003996 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003997 if (self->proto >= 4)
3998 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003999 }
4000
4001 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004002 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004003 return -1;
4004
4005 return 0;
4006}
4007
Larry Hastings61272b72014-01-07 12:41:53 -08004008/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004009
4010_pickle.Pickler.clear_memo
4011
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004012Clears the pickler's "memo".
4013
4014The memo is the data structure that remembers which objects the
4015pickler has already seen, so that shared or recursive objects are
4016pickled by reference and not by value. This method is useful when
4017re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004018[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004019
Larry Hastings3cceb382014-01-04 11:09:09 -08004020static PyObject *
4021_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004022/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004023{
4024 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004025 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004026
4027 Py_RETURN_NONE;
4028}
4029
Larry Hastings61272b72014-01-07 12:41:53 -08004030/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004031
4032_pickle.Pickler.dump
4033
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004034 obj: object
4035 /
4036
4037Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004038[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004040static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004041_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004042/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004043{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004044 /* Check whether the Pickler was initialized correctly (issue3664).
4045 Developers often forget to call __init__() in their subclasses, which
4046 would trigger a segfault without this check. */
4047 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004048 PickleState *st = _Pickle_GetGlobalState();
4049 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004050 "Pickler.__init__() was not called by %s.__init__()",
4051 Py_TYPE(self)->tp_name);
4052 return NULL;
4053 }
4054
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004055 if (_Pickler_ClearBuffer(self) < 0)
4056 return NULL;
4057
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004058 if (dump(self, obj) < 0)
4059 return NULL;
4060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004061 if (_Pickler_FlushToFile(self) < 0)
4062 return NULL;
4063
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004064 Py_RETURN_NONE;
4065}
4066
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004067/*[clinic input]
4068
4069_pickle.Pickler.__sizeof__ -> Py_ssize_t
4070
4071Returns size in memory, in bytes.
4072[clinic start generated code]*/
4073
4074static Py_ssize_t
4075_pickle_Pickler___sizeof___impl(PicklerObject *self)
4076/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4077{
4078 Py_ssize_t res, s;
4079
4080 res = sizeof(PicklerObject);
4081 if (self->memo != NULL) {
4082 res += sizeof(PyMemoTable);
4083 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4084 }
4085 if (self->output_buffer != NULL) {
4086 s = _PySys_GetSizeOf(self->output_buffer);
4087 if (s == -1)
4088 return -1;
4089 res += s;
4090 }
4091 return res;
4092}
4093
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004094static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004095 _PICKLE_PICKLER_DUMP_METHODDEF
4096 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004097 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004098 {NULL, NULL} /* sentinel */
4099};
4100
4101static void
4102Pickler_dealloc(PicklerObject *self)
4103{
4104 PyObject_GC_UnTrack(self);
4105
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004106 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004107 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004108 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004109 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004110 Py_XDECREF(self->fast_memo);
4111
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004112 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004113
4114 Py_TYPE(self)->tp_free((PyObject *)self);
4115}
4116
4117static int
4118Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4119{
4120 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004122 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004123 Py_VISIT(self->fast_memo);
4124 return 0;
4125}
4126
4127static int
4128Pickler_clear(PicklerObject *self)
4129{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004130 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004132 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004133 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004134 Py_CLEAR(self->fast_memo);
4135
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004136 if (self->memo != NULL) {
4137 PyMemoTable *memo = self->memo;
4138 self->memo = NULL;
4139 PyMemoTable_Del(memo);
4140 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004141 return 0;
4142}
4143
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004144
Larry Hastings61272b72014-01-07 12:41:53 -08004145/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004146
4147_pickle.Pickler.__init__
4148
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004149 file: object
4150 protocol: object = NULL
4151 fix_imports: bool = True
4152
4153This takes a binary file for writing a pickle data stream.
4154
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004155The optional *protocol* argument tells the pickler to use the given
4156protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4157protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004158
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004159Specifying a negative protocol version selects the highest protocol
4160version supported. The higher the protocol used, the more recent the
4161version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004162
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004163The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004164bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004165writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004166this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004167
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004168If *fix_imports* is True and protocol is less than 3, pickle will try
4169to map the new Python 3 names to the old module names used in Python
41702, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004171[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004173static int
Larry Hastings89964c42015-04-14 18:07:59 -04004174_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4175 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004176/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004177{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004178 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004179 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004180
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004181 /* In case of multiple __init__() calls, clear previous content. */
4182 if (self->write != NULL)
4183 (void)Pickler_clear(self);
4184
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004186 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004187
4188 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004189 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004190
4191 /* memo and output_buffer may have already been created in _Pickler_New */
4192 if (self->memo == NULL) {
4193 self->memo = PyMemoTable_New();
4194 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004195 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004196 }
4197 self->output_len = 0;
4198 if (self->output_buffer == NULL) {
4199 self->max_output_len = WRITE_BUF_SIZE;
4200 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4201 self->max_output_len);
4202 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004203 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004204 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004205
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004206 self->fast = 0;
4207 self->fast_nesting = 0;
4208 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004209 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004210 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4211 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4212 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004214 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004215 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004216 self->dispatch_table = NULL;
4217 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4218 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4219 &PyId_dispatch_table);
4220 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004221 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004222 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004223
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004224 return 0;
4225}
4226
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004227
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004228/* Define a proxy object for the Pickler's internal memo object. This is to
4229 * avoid breaking code like:
4230 * pickler.memo.clear()
4231 * and
4232 * pickler.memo = saved_memo
4233 * Is this a good idea? Not really, but we don't want to break code that uses
4234 * it. Note that we don't implement the entire mapping API here. This is
4235 * intentional, as these should be treated as black-box implementation details.
4236 */
4237
Larry Hastings61272b72014-01-07 12:41:53 -08004238/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004239_pickle.PicklerMemoProxy.clear
4240
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004241Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004242[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004243
Larry Hastings3cceb382014-01-04 11:09:09 -08004244static PyObject *
4245_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004246/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004247{
4248 if (self->pickler->memo)
4249 PyMemoTable_Clear(self->pickler->memo);
4250 Py_RETURN_NONE;
4251}
4252
Larry Hastings61272b72014-01-07 12:41:53 -08004253/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004254_pickle.PicklerMemoProxy.copy
4255
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004256Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004257[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004258
Larry Hastings3cceb382014-01-04 11:09:09 -08004259static PyObject *
4260_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004261/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004262{
4263 Py_ssize_t i;
4264 PyMemoTable *memo;
4265 PyObject *new_memo = PyDict_New();
4266 if (new_memo == NULL)
4267 return NULL;
4268
4269 memo = self->pickler->memo;
4270 for (i = 0; i < memo->mt_allocated; ++i) {
4271 PyMemoEntry entry = memo->mt_table[i];
4272 if (entry.me_key != NULL) {
4273 int status;
4274 PyObject *key, *value;
4275
4276 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004277 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004278
4279 if (key == NULL || value == NULL) {
4280 Py_XDECREF(key);
4281 Py_XDECREF(value);
4282 goto error;
4283 }
4284 status = PyDict_SetItem(new_memo, key, value);
4285 Py_DECREF(key);
4286 Py_DECREF(value);
4287 if (status < 0)
4288 goto error;
4289 }
4290 }
4291 return new_memo;
4292
4293 error:
4294 Py_XDECREF(new_memo);
4295 return NULL;
4296}
4297
Larry Hastings61272b72014-01-07 12:41:53 -08004298/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004299_pickle.PicklerMemoProxy.__reduce__
4300
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004301Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004302[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004303
Larry Hastings3cceb382014-01-04 11:09:09 -08004304static PyObject *
4305_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004306/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004307{
4308 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004309 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004310 if (contents == NULL)
4311 return NULL;
4312
4313 reduce_value = PyTuple_New(2);
4314 if (reduce_value == NULL) {
4315 Py_DECREF(contents);
4316 return NULL;
4317 }
4318 dict_args = PyTuple_New(1);
4319 if (dict_args == NULL) {
4320 Py_DECREF(contents);
4321 Py_DECREF(reduce_value);
4322 return NULL;
4323 }
4324 PyTuple_SET_ITEM(dict_args, 0, contents);
4325 Py_INCREF((PyObject *)&PyDict_Type);
4326 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4327 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4328 return reduce_value;
4329}
4330
4331static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004332 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4333 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4334 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004335 {NULL, NULL} /* sentinel */
4336};
4337
4338static void
4339PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4340{
4341 PyObject_GC_UnTrack(self);
4342 Py_XDECREF(self->pickler);
4343 PyObject_GC_Del((PyObject *)self);
4344}
4345
4346static int
4347PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4348 visitproc visit, void *arg)
4349{
4350 Py_VISIT(self->pickler);
4351 return 0;
4352}
4353
4354static int
4355PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4356{
4357 Py_CLEAR(self->pickler);
4358 return 0;
4359}
4360
4361static PyTypeObject PicklerMemoProxyType = {
4362 PyVarObject_HEAD_INIT(NULL, 0)
4363 "_pickle.PicklerMemoProxy", /*tp_name*/
4364 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4365 0,
4366 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4367 0, /* tp_print */
4368 0, /* tp_getattr */
4369 0, /* tp_setattr */
4370 0, /* tp_compare */
4371 0, /* tp_repr */
4372 0, /* tp_as_number */
4373 0, /* tp_as_sequence */
4374 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004375 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004376 0, /* tp_call */
4377 0, /* tp_str */
4378 PyObject_GenericGetAttr, /* tp_getattro */
4379 PyObject_GenericSetAttr, /* tp_setattro */
4380 0, /* tp_as_buffer */
4381 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4382 0, /* tp_doc */
4383 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4384 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4385 0, /* tp_richcompare */
4386 0, /* tp_weaklistoffset */
4387 0, /* tp_iter */
4388 0, /* tp_iternext */
4389 picklerproxy_methods, /* tp_methods */
4390};
4391
4392static PyObject *
4393PicklerMemoProxy_New(PicklerObject *pickler)
4394{
4395 PicklerMemoProxyObject *self;
4396
4397 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4398 if (self == NULL)
4399 return NULL;
4400 Py_INCREF(pickler);
4401 self->pickler = pickler;
4402 PyObject_GC_Track(self);
4403 return (PyObject *)self;
4404}
4405
4406/*****************************************************************************/
4407
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004408static PyObject *
4409Pickler_get_memo(PicklerObject *self)
4410{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004411 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004412}
4413
4414static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004415Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004417 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004418
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004420 PyErr_SetString(PyExc_TypeError,
4421 "attribute deletion is not supported");
4422 return -1;
4423 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004424
4425 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4426 PicklerObject *pickler =
4427 ((PicklerMemoProxyObject *)obj)->pickler;
4428
4429 new_memo = PyMemoTable_Copy(pickler->memo);
4430 if (new_memo == NULL)
4431 return -1;
4432 }
4433 else if (PyDict_Check(obj)) {
4434 Py_ssize_t i = 0;
4435 PyObject *key, *value;
4436
4437 new_memo = PyMemoTable_New();
4438 if (new_memo == NULL)
4439 return -1;
4440
4441 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004442 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004443 PyObject *memo_obj;
4444
4445 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4446 PyErr_SetString(PyExc_TypeError,
4447 "'memo' values must be 2-item tuples");
4448 goto error;
4449 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004450 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004451 if (memo_id == -1 && PyErr_Occurred())
4452 goto error;
4453 memo_obj = PyTuple_GET_ITEM(value, 1);
4454 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4455 goto error;
4456 }
4457 }
4458 else {
4459 PyErr_Format(PyExc_TypeError,
4460 "'memo' attribute must be an PicklerMemoProxy object"
4461 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004462 return -1;
4463 }
4464
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004465 PyMemoTable_Del(self->memo);
4466 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004467
4468 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004469
4470 error:
4471 if (new_memo)
4472 PyMemoTable_Del(new_memo);
4473 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004474}
4475
4476static PyObject *
4477Pickler_get_persid(PicklerObject *self)
4478{
4479 if (self->pers_func == NULL)
4480 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4481 else
4482 Py_INCREF(self->pers_func);
4483 return self->pers_func;
4484}
4485
4486static int
4487Pickler_set_persid(PicklerObject *self, PyObject *value)
4488{
4489 PyObject *tmp;
4490
4491 if (value == NULL) {
4492 PyErr_SetString(PyExc_TypeError,
4493 "attribute deletion is not supported");
4494 return -1;
4495 }
4496 if (!PyCallable_Check(value)) {
4497 PyErr_SetString(PyExc_TypeError,
4498 "persistent_id must be a callable taking one argument");
4499 return -1;
4500 }
4501
4502 tmp = self->pers_func;
4503 Py_INCREF(value);
4504 self->pers_func = value;
4505 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4506
4507 return 0;
4508}
4509
4510static PyMemberDef Pickler_members[] = {
4511 {"bin", T_INT, offsetof(PicklerObject, bin)},
4512 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004513 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004514 {NULL}
4515};
4516
4517static PyGetSetDef Pickler_getsets[] = {
4518 {"memo", (getter)Pickler_get_memo,
4519 (setter)Pickler_set_memo},
4520 {"persistent_id", (getter)Pickler_get_persid,
4521 (setter)Pickler_set_persid},
4522 {NULL}
4523};
4524
4525static PyTypeObject Pickler_Type = {
4526 PyVarObject_HEAD_INIT(NULL, 0)
4527 "_pickle.Pickler" , /*tp_name*/
4528 sizeof(PicklerObject), /*tp_basicsize*/
4529 0, /*tp_itemsize*/
4530 (destructor)Pickler_dealloc, /*tp_dealloc*/
4531 0, /*tp_print*/
4532 0, /*tp_getattr*/
4533 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004534 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004535 0, /*tp_repr*/
4536 0, /*tp_as_number*/
4537 0, /*tp_as_sequence*/
4538 0, /*tp_as_mapping*/
4539 0, /*tp_hash*/
4540 0, /*tp_call*/
4541 0, /*tp_str*/
4542 0, /*tp_getattro*/
4543 0, /*tp_setattro*/
4544 0, /*tp_as_buffer*/
4545 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004546 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004547 (traverseproc)Pickler_traverse, /*tp_traverse*/
4548 (inquiry)Pickler_clear, /*tp_clear*/
4549 0, /*tp_richcompare*/
4550 0, /*tp_weaklistoffset*/
4551 0, /*tp_iter*/
4552 0, /*tp_iternext*/
4553 Pickler_methods, /*tp_methods*/
4554 Pickler_members, /*tp_members*/
4555 Pickler_getsets, /*tp_getset*/
4556 0, /*tp_base*/
4557 0, /*tp_dict*/
4558 0, /*tp_descr_get*/
4559 0, /*tp_descr_set*/
4560 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004561 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004562 PyType_GenericAlloc, /*tp_alloc*/
4563 PyType_GenericNew, /*tp_new*/
4564 PyObject_GC_Del, /*tp_free*/
4565 0, /*tp_is_gc*/
4566};
4567
Victor Stinner121aab42011-09-29 23:40:53 +02004568/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004569
4570 XXX: It would be nice to able to avoid Python function call overhead, by
4571 using directly the C version of find_class(), when find_class() is not
4572 overridden by a subclass. Although, this could become rather hackish. A
4573 simpler optimization would be to call the C function when self is not a
4574 subclass instance. */
4575static PyObject *
4576find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4577{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004578 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004579
4580 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4581 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004582}
4583
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004584static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004585marker(UnpicklerObject *self)
4586{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004587 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004588 if (self->num_marks < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004589 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004590 return -1;
4591 }
4592
4593 return self->marks[--self->num_marks];
4594}
4595
4596static int
4597load_none(UnpicklerObject *self)
4598{
4599 PDATA_APPEND(self->stack, Py_None, -1);
4600 return 0;
4601}
4602
4603static int
4604bad_readline(void)
4605{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004606 PickleState *st = _Pickle_GetGlobalState();
4607 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004608 return -1;
4609}
4610
4611static int
4612load_int(UnpicklerObject *self)
4613{
4614 PyObject *value;
4615 char *endptr, *s;
4616 Py_ssize_t len;
4617 long x;
4618
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004619 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004620 return -1;
4621 if (len < 2)
4622 return bad_readline();
4623
4624 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004625 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004626 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004627 x = strtol(s, &endptr, 0);
4628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004629 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004630 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004631 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004632 errno = 0;
4633 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004634 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004635 if (value == NULL) {
4636 PyErr_SetString(PyExc_ValueError,
4637 "could not convert string to int");
4638 return -1;
4639 }
4640 }
4641 else {
4642 if (len == 3 && (x == 0 || x == 1)) {
4643 if ((value = PyBool_FromLong(x)) == NULL)
4644 return -1;
4645 }
4646 else {
4647 if ((value = PyLong_FromLong(x)) == NULL)
4648 return -1;
4649 }
4650 }
4651
4652 PDATA_PUSH(self->stack, value, -1);
4653 return 0;
4654}
4655
4656static int
4657load_bool(UnpicklerObject *self, PyObject *boolean)
4658{
4659 assert(boolean == Py_True || boolean == Py_False);
4660 PDATA_APPEND(self->stack, boolean, -1);
4661 return 0;
4662}
4663
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004664/* s contains x bytes of an unsigned little-endian integer. Return its value
4665 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4666 */
4667static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004668calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004669{
4670 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004671 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004672 size_t x = 0;
4673
Serhiy Storchakae0606192015-09-29 22:10:07 +03004674 if (nbytes > (int)sizeof(size_t)) {
4675 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4676 * have 64-bit size that can't be represented on 32-bit platform.
4677 */
4678 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4679 if (s[i])
4680 return -1;
4681 }
4682 nbytes = (int)sizeof(size_t);
4683 }
4684 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004685 x |= (size_t) s[i] << (8 * i);
4686 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004687
4688 if (x > PY_SSIZE_T_MAX)
4689 return -1;
4690 else
4691 return (Py_ssize_t) x;
4692}
4693
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004694/* s contains x bytes of a little-endian integer. Return its value as a
4695 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4696 * int, but when x is 4 it's a signed one. This is an historical source
4697 * of x-platform bugs.
4698 */
4699static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004700calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004701{
4702 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004703 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704 long x = 0;
4705
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004706 for (i = 0; i < nbytes; i++) {
4707 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004708 }
4709
4710 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4711 * is signed, so on a box with longs bigger than 4 bytes we need
4712 * to extend a BININT's sign bit to the full width.
4713 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004714 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715 x |= -(x & (1L << 31));
4716 }
4717
4718 return x;
4719}
4720
4721static int
4722load_binintx(UnpicklerObject *self, char *s, int size)
4723{
4724 PyObject *value;
4725 long x;
4726
4727 x = calc_binint(s, size);
4728
4729 if ((value = PyLong_FromLong(x)) == NULL)
4730 return -1;
4731
4732 PDATA_PUSH(self->stack, value, -1);
4733 return 0;
4734}
4735
4736static int
4737load_binint(UnpicklerObject *self)
4738{
4739 char *s;
4740
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004741 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004742 return -1;
4743
4744 return load_binintx(self, s, 4);
4745}
4746
4747static int
4748load_binint1(UnpicklerObject *self)
4749{
4750 char *s;
4751
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004752 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004753 return -1;
4754
4755 return load_binintx(self, s, 1);
4756}
4757
4758static int
4759load_binint2(UnpicklerObject *self)
4760{
4761 char *s;
4762
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004763 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004764 return -1;
4765
4766 return load_binintx(self, s, 2);
4767}
4768
4769static int
4770load_long(UnpicklerObject *self)
4771{
4772 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004773 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 Py_ssize_t len;
4775
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004776 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004777 return -1;
4778 if (len < 2)
4779 return bad_readline();
4780
Mark Dickinson8dd05142009-01-20 20:43:58 +00004781 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4782 the 'L' before calling PyLong_FromString. In order to maintain
4783 compatibility with Python 3.0.0, we don't actually *require*
4784 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004785 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004786 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004787 /* XXX: Should the base argument explicitly set to 10? */
4788 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004789 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004790 return -1;
4791
4792 PDATA_PUSH(self->stack, value, -1);
4793 return 0;
4794}
4795
4796/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4797 * data following.
4798 */
4799static int
4800load_counted_long(UnpicklerObject *self, int size)
4801{
4802 PyObject *value;
4803 char *nbytes;
4804 char *pdata;
4805
4806 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004807 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004808 return -1;
4809
4810 size = calc_binint(nbytes, size);
4811 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004812 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004813 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004814 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004815 "LONG pickle has negative byte count");
4816 return -1;
4817 }
4818
4819 if (size == 0)
4820 value = PyLong_FromLong(0L);
4821 else {
4822 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004823 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004824 return -1;
4825 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4826 1 /* little endian */ , 1 /* signed */ );
4827 }
4828 if (value == NULL)
4829 return -1;
4830 PDATA_PUSH(self->stack, value, -1);
4831 return 0;
4832}
4833
4834static int
4835load_float(UnpicklerObject *self)
4836{
4837 PyObject *value;
4838 char *endptr, *s;
4839 Py_ssize_t len;
4840 double d;
4841
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004842 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004843 return -1;
4844 if (len < 2)
4845 return bad_readline();
4846
4847 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004848 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4849 if (d == -1.0 && PyErr_Occurred())
4850 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004851 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004852 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4853 return -1;
4854 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004855 value = PyFloat_FromDouble(d);
4856 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 return -1;
4858
4859 PDATA_PUSH(self->stack, value, -1);
4860 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004861}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862
4863static int
4864load_binfloat(UnpicklerObject *self)
4865{
4866 PyObject *value;
4867 double x;
4868 char *s;
4869
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004870 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004871 return -1;
4872
4873 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4874 if (x == -1.0 && PyErr_Occurred())
4875 return -1;
4876
4877 if ((value = PyFloat_FromDouble(x)) == NULL)
4878 return -1;
4879
4880 PDATA_PUSH(self->stack, value, -1);
4881 return 0;
4882}
4883
4884static int
4885load_string(UnpicklerObject *self)
4886{
4887 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004888 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004889 Py_ssize_t len;
4890 char *s, *p;
4891
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004892 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004893 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004894 /* Strip the newline */
4895 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004896 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004897 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004898 p = s + 1;
4899 len -= 2;
4900 }
4901 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004902 PickleState *st = _Pickle_GetGlobalState();
4903 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004904 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004905 return -1;
4906 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004907 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908
4909 /* Use the PyBytes API to decode the string, since that is what is used
4910 to encode, and then coerce the result to Unicode. */
4911 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004912 if (bytes == NULL)
4913 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004914
4915 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4916 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4917 if (strcmp(self->encoding, "bytes") == 0) {
4918 obj = bytes;
4919 }
4920 else {
4921 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4922 Py_DECREF(bytes);
4923 if (obj == NULL) {
4924 return -1;
4925 }
4926 }
4927
4928 PDATA_PUSH(self->stack, obj, -1);
4929 return 0;
4930}
4931
4932static int
4933load_counted_binstring(UnpicklerObject *self, int nbytes)
4934{
4935 PyObject *obj;
4936 Py_ssize_t size;
4937 char *s;
4938
4939 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004940 return -1;
4941
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004942 size = calc_binsize(s, nbytes);
4943 if (size < 0) {
4944 PickleState *st = _Pickle_GetGlobalState();
4945 PyErr_Format(st->UnpicklingError,
4946 "BINSTRING exceeds system's maximum size of %zd bytes",
4947 PY_SSIZE_T_MAX);
4948 return -1;
4949 }
4950
4951 if (_Unpickler_Read(self, &s, size) < 0)
4952 return -1;
4953
4954 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4955 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4956 if (strcmp(self->encoding, "bytes") == 0) {
4957 obj = PyBytes_FromStringAndSize(s, size);
4958 }
4959 else {
4960 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4961 }
4962 if (obj == NULL) {
4963 return -1;
4964 }
4965
4966 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004967 return 0;
4968}
4969
4970static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004971load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004972{
4973 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004974 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004975 char *s;
4976
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004977 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004978 return -1;
4979
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004980 size = calc_binsize(s, nbytes);
4981 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004982 PyErr_Format(PyExc_OverflowError,
4983 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004984 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004985 return -1;
4986 }
4987
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004988 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004990
4991 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004992 if (bytes == NULL)
4993 return -1;
4994
4995 PDATA_PUSH(self->stack, bytes, -1);
4996 return 0;
4997}
4998
4999static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005000load_unicode(UnpicklerObject *self)
5001{
5002 PyObject *str;
5003 Py_ssize_t len;
5004 char *s;
5005
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005006 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005007 return -1;
5008 if (len < 1)
5009 return bad_readline();
5010
5011 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5012 if (str == NULL)
5013 return -1;
5014
5015 PDATA_PUSH(self->stack, str, -1);
5016 return 0;
5017}
5018
5019static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005020load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021{
5022 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005023 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005024 char *s;
5025
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005026 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005027 return -1;
5028
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005029 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005030 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005031 PyErr_Format(PyExc_OverflowError,
5032 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005033 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034 return -1;
5035 }
5036
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005037 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005038 return -1;
5039
Victor Stinner485fb562010-04-13 11:07:24 +00005040 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041 if (str == NULL)
5042 return -1;
5043
5044 PDATA_PUSH(self->stack, str, -1);
5045 return 0;
5046}
5047
5048static int
5049load_tuple(UnpicklerObject *self)
5050{
5051 PyObject *tuple;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005052 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005053
5054 if ((i = marker(self)) < 0)
5055 return -1;
5056
5057 tuple = Pdata_poptuple(self->stack, i);
5058 if (tuple == NULL)
5059 return -1;
5060 PDATA_PUSH(self->stack, tuple, -1);
5061 return 0;
5062}
5063
5064static int
5065load_counted_tuple(UnpicklerObject *self, int len)
5066{
5067 PyObject *tuple;
5068
5069 tuple = PyTuple_New(len);
5070 if (tuple == NULL)
5071 return -1;
5072
5073 while (--len >= 0) {
5074 PyObject *item;
5075
5076 PDATA_POP(self->stack, item);
5077 if (item == NULL)
5078 return -1;
5079 PyTuple_SET_ITEM(tuple, len, item);
5080 }
5081 PDATA_PUSH(self->stack, tuple, -1);
5082 return 0;
5083}
5084
5085static int
5086load_empty_list(UnpicklerObject *self)
5087{
5088 PyObject *list;
5089
5090 if ((list = PyList_New(0)) == NULL)
5091 return -1;
5092 PDATA_PUSH(self->stack, list, -1);
5093 return 0;
5094}
5095
5096static int
5097load_empty_dict(UnpicklerObject *self)
5098{
5099 PyObject *dict;
5100
5101 if ((dict = PyDict_New()) == NULL)
5102 return -1;
5103 PDATA_PUSH(self->stack, dict, -1);
5104 return 0;
5105}
5106
5107static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005108load_empty_set(UnpicklerObject *self)
5109{
5110 PyObject *set;
5111
5112 if ((set = PySet_New(NULL)) == NULL)
5113 return -1;
5114 PDATA_PUSH(self->stack, set, -1);
5115 return 0;
5116}
5117
5118static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005119load_list(UnpicklerObject *self)
5120{
5121 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005122 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005123
5124 if ((i = marker(self)) < 0)
5125 return -1;
5126
5127 list = Pdata_poplist(self->stack, i);
5128 if (list == NULL)
5129 return -1;
5130 PDATA_PUSH(self->stack, list, -1);
5131 return 0;
5132}
5133
5134static int
5135load_dict(UnpicklerObject *self)
5136{
5137 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005138 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005139
5140 if ((i = marker(self)) < 0)
5141 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005142 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005143
5144 if ((dict = PyDict_New()) == NULL)
5145 return -1;
5146
5147 for (k = i + 1; k < j; k += 2) {
5148 key = self->stack->data[k - 1];
5149 value = self->stack->data[k];
5150 if (PyDict_SetItem(dict, key, value) < 0) {
5151 Py_DECREF(dict);
5152 return -1;
5153 }
5154 }
5155 Pdata_clear(self->stack, i);
5156 PDATA_PUSH(self->stack, dict, -1);
5157 return 0;
5158}
5159
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005160static int
5161load_frozenset(UnpicklerObject *self)
5162{
5163 PyObject *items;
5164 PyObject *frozenset;
5165 Py_ssize_t i;
5166
5167 if ((i = marker(self)) < 0)
5168 return -1;
5169
5170 items = Pdata_poptuple(self->stack, i);
5171 if (items == NULL)
5172 return -1;
5173
5174 frozenset = PyFrozenSet_New(items);
5175 Py_DECREF(items);
5176 if (frozenset == NULL)
5177 return -1;
5178
5179 PDATA_PUSH(self->stack, frozenset, -1);
5180 return 0;
5181}
5182
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005183static PyObject *
5184instantiate(PyObject *cls, PyObject *args)
5185{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005186 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005187 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005188 /* Caller must assure args are a tuple. Normally, args come from
5189 Pdata_poptuple which packs objects from the top of the stack
5190 into a newly created tuple. */
5191 assert(PyTuple_Check(args));
5192 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005193 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005194 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005195 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005196 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005197 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005198
5199 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005200 }
5201 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005202}
5203
5204static int
5205load_obj(UnpicklerObject *self)
5206{
5207 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005208 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005209
5210 if ((i = marker(self)) < 0)
5211 return -1;
5212
5213 args = Pdata_poptuple(self->stack, i + 1);
5214 if (args == NULL)
5215 return -1;
5216
5217 PDATA_POP(self->stack, cls);
5218 if (cls) {
5219 obj = instantiate(cls, args);
5220 Py_DECREF(cls);
5221 }
5222 Py_DECREF(args);
5223 if (obj == NULL)
5224 return -1;
5225
5226 PDATA_PUSH(self->stack, obj, -1);
5227 return 0;
5228}
5229
5230static int
5231load_inst(UnpicklerObject *self)
5232{
5233 PyObject *cls = NULL;
5234 PyObject *args = NULL;
5235 PyObject *obj = NULL;
5236 PyObject *module_name;
5237 PyObject *class_name;
5238 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005239 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005240 char *s;
5241
5242 if ((i = marker(self)) < 0)
5243 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005244 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005245 return -1;
5246 if (len < 2)
5247 return bad_readline();
5248
5249 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5250 identifiers are permitted in Python 3.0, since the INST opcode is only
5251 supported by older protocols on Python 2.x. */
5252 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5253 if (module_name == NULL)
5254 return -1;
5255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005256 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005257 if (len < 2)
5258 return bad_readline();
5259 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005260 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005261 cls = find_class(self, module_name, class_name);
5262 Py_DECREF(class_name);
5263 }
5264 }
5265 Py_DECREF(module_name);
5266
5267 if (cls == NULL)
5268 return -1;
5269
5270 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5271 obj = instantiate(cls, args);
5272 Py_DECREF(args);
5273 }
5274 Py_DECREF(cls);
5275
5276 if (obj == NULL)
5277 return -1;
5278
5279 PDATA_PUSH(self->stack, obj, -1);
5280 return 0;
5281}
5282
5283static int
5284load_newobj(UnpicklerObject *self)
5285{
5286 PyObject *args = NULL;
5287 PyObject *clsraw = NULL;
5288 PyTypeObject *cls; /* clsraw cast to its true type */
5289 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005290 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005291
5292 /* Stack is ... cls argtuple, and we want to call
5293 * cls.__new__(cls, *argtuple).
5294 */
5295 PDATA_POP(self->stack, args);
5296 if (args == NULL)
5297 goto error;
5298 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005299 PyErr_SetString(st->UnpicklingError,
5300 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005301 goto error;
5302 }
5303
5304 PDATA_POP(self->stack, clsraw);
5305 cls = (PyTypeObject *)clsraw;
5306 if (cls == NULL)
5307 goto error;
5308 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005309 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005310 "isn't a type object");
5311 goto error;
5312 }
5313 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005314 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005315 "has NULL tp_new");
5316 goto error;
5317 }
5318
5319 /* Call __new__. */
5320 obj = cls->tp_new(cls, args, NULL);
5321 if (obj == NULL)
5322 goto error;
5323
5324 Py_DECREF(args);
5325 Py_DECREF(clsraw);
5326 PDATA_PUSH(self->stack, obj, -1);
5327 return 0;
5328
5329 error:
5330 Py_XDECREF(args);
5331 Py_XDECREF(clsraw);
5332 return -1;
5333}
5334
5335static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005336load_newobj_ex(UnpicklerObject *self)
5337{
5338 PyObject *cls, *args, *kwargs;
5339 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005340 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005341
5342 PDATA_POP(self->stack, kwargs);
5343 if (kwargs == NULL) {
5344 return -1;
5345 }
5346 PDATA_POP(self->stack, args);
5347 if (args == NULL) {
5348 Py_DECREF(kwargs);
5349 return -1;
5350 }
5351 PDATA_POP(self->stack, cls);
5352 if (cls == NULL) {
5353 Py_DECREF(kwargs);
5354 Py_DECREF(args);
5355 return -1;
5356 }
Larry Hastings61272b72014-01-07 12:41:53 -08005357
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005358 if (!PyType_Check(cls)) {
5359 Py_DECREF(kwargs);
5360 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005361 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005362 "NEWOBJ_EX class argument must be a type, not %.200s",
5363 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005364 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005365 return -1;
5366 }
5367
5368 if (((PyTypeObject *)cls)->tp_new == NULL) {
5369 Py_DECREF(kwargs);
5370 Py_DECREF(args);
5371 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005372 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005373 "NEWOBJ_EX class argument doesn't have __new__");
5374 return -1;
5375 }
5376 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5377 Py_DECREF(kwargs);
5378 Py_DECREF(args);
5379 Py_DECREF(cls);
5380 if (obj == NULL) {
5381 return -1;
5382 }
5383 PDATA_PUSH(self->stack, obj, -1);
5384 return 0;
5385}
5386
5387static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005388load_global(UnpicklerObject *self)
5389{
5390 PyObject *global = NULL;
5391 PyObject *module_name;
5392 PyObject *global_name;
5393 Py_ssize_t len;
5394 char *s;
5395
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005396 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005397 return -1;
5398 if (len < 2)
5399 return bad_readline();
5400 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5401 if (!module_name)
5402 return -1;
5403
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005404 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005405 if (len < 2) {
5406 Py_DECREF(module_name);
5407 return bad_readline();
5408 }
5409 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5410 if (global_name) {
5411 global = find_class(self, module_name, global_name);
5412 Py_DECREF(global_name);
5413 }
5414 }
5415 Py_DECREF(module_name);
5416
5417 if (global == NULL)
5418 return -1;
5419 PDATA_PUSH(self->stack, global, -1);
5420 return 0;
5421}
5422
5423static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005424load_stack_global(UnpicklerObject *self)
5425{
5426 PyObject *global;
5427 PyObject *module_name;
5428 PyObject *global_name;
5429
5430 PDATA_POP(self->stack, global_name);
5431 PDATA_POP(self->stack, module_name);
5432 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5433 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005434 PickleState *st = _Pickle_GetGlobalState();
5435 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005436 Py_XDECREF(global_name);
5437 Py_XDECREF(module_name);
5438 return -1;
5439 }
5440 global = find_class(self, module_name, global_name);
5441 Py_DECREF(global_name);
5442 Py_DECREF(module_name);
5443 if (global == NULL)
5444 return -1;
5445 PDATA_PUSH(self->stack, global, -1);
5446 return 0;
5447}
5448
5449static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005450load_persid(UnpicklerObject *self)
5451{
5452 PyObject *pid;
5453 Py_ssize_t len;
5454 char *s;
5455
5456 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005457 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005458 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005459 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005460 return bad_readline();
5461
5462 pid = PyBytes_FromStringAndSize(s, len - 1);
5463 if (pid == NULL)
5464 return -1;
5465
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005466 /* This does not leak since _Pickle_FastCall() steals the reference
5467 to pid first. */
5468 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 if (pid == NULL)
5470 return -1;
5471
5472 PDATA_PUSH(self->stack, pid, -1);
5473 return 0;
5474 }
5475 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005476 PickleState *st = _Pickle_GetGlobalState();
5477 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005478 "A load persistent id instruction was encountered,\n"
5479 "but no persistent_load function was specified.");
5480 return -1;
5481 }
5482}
5483
5484static int
5485load_binpersid(UnpicklerObject *self)
5486{
5487 PyObject *pid;
5488
5489 if (self->pers_func) {
5490 PDATA_POP(self->stack, pid);
5491 if (pid == NULL)
5492 return -1;
5493
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005494 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005495 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005496 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005497 if (pid == NULL)
5498 return -1;
5499
5500 PDATA_PUSH(self->stack, pid, -1);
5501 return 0;
5502 }
5503 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005504 PickleState *st = _Pickle_GetGlobalState();
5505 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506 "A load persistent id instruction was encountered,\n"
5507 "but no persistent_load function was specified.");
5508 return -1;
5509 }
5510}
5511
5512static int
5513load_pop(UnpicklerObject *self)
5514{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005515 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005516
5517 /* Note that we split the (pickle.py) stack into two stacks,
5518 * an object stack and a mark stack. We have to be clever and
5519 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005520 * mark stack first, and only signalling a stack underflow if
5521 * the object stack is empty and the mark stack doesn't match
5522 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005523 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005524 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005525 self->num_marks--;
Antoine Pitrou01a15ea2010-01-07 17:57:31 +00005526 } else if (len > 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005527 len--;
5528 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005529 Py_SIZE(self->stack) = len;
Collin Winter8ca69de2009-05-26 16:53:41 +00005530 } else {
5531 return stack_underflow();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005532 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005533 return 0;
5534}
5535
5536static int
5537load_pop_mark(UnpicklerObject *self)
5538{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005539 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540
5541 if ((i = marker(self)) < 0)
5542 return -1;
5543
5544 Pdata_clear(self->stack, i);
5545
5546 return 0;
5547}
5548
5549static int
5550load_dup(UnpicklerObject *self)
5551{
5552 PyObject *last;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005553 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005555 if ((len = Py_SIZE(self->stack)) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005556 return stack_underflow();
5557 last = self->stack->data[len - 1];
5558 PDATA_APPEND(self->stack, last, -1);
5559 return 0;
5560}
5561
5562static int
5563load_get(UnpicklerObject *self)
5564{
5565 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005566 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005567 Py_ssize_t len;
5568 char *s;
5569
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005570 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571 return -1;
5572 if (len < 2)
5573 return bad_readline();
5574
5575 key = PyLong_FromString(s, NULL, 10);
5576 if (key == NULL)
5577 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005578 idx = PyLong_AsSsize_t(key);
5579 if (idx == -1 && PyErr_Occurred()) {
5580 Py_DECREF(key);
5581 return -1;
5582 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005584 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 if (value == NULL) {
5586 if (!PyErr_Occurred())
5587 PyErr_SetObject(PyExc_KeyError, key);
5588 Py_DECREF(key);
5589 return -1;
5590 }
5591 Py_DECREF(key);
5592
5593 PDATA_APPEND(self->stack, value, -1);
5594 return 0;
5595}
5596
5597static int
5598load_binget(UnpicklerObject *self)
5599{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005600 PyObject *value;
5601 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005602 char *s;
5603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005604 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005605 return -1;
5606
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005607 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005608
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005609 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005610 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005611 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005612 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005613 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005614 Py_DECREF(key);
5615 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 return -1;
5617 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005618
5619 PDATA_APPEND(self->stack, value, -1);
5620 return 0;
5621}
5622
5623static int
5624load_long_binget(UnpicklerObject *self)
5625{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005626 PyObject *value;
5627 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005629
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005630 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 return -1;
5632
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005633 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005635 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005637 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005638 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005639 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005640 Py_DECREF(key);
5641 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642 return -1;
5643 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005644
5645 PDATA_APPEND(self->stack, value, -1);
5646 return 0;
5647}
5648
5649/* Push an object from the extension registry (EXT[124]). nbytes is
5650 * the number of bytes following the opcode, holding the index (code) value.
5651 */
5652static int
5653load_extension(UnpicklerObject *self, int nbytes)
5654{
5655 char *codebytes; /* the nbytes bytes after the opcode */
5656 long code; /* calc_binint returns long */
5657 PyObject *py_code; /* code as a Python int */
5658 PyObject *obj; /* the object to push */
5659 PyObject *pair; /* (module_name, class_name) */
5660 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005661 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662
5663 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005664 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005665 return -1;
5666 code = calc_binint(codebytes, nbytes);
5667 if (code <= 0) { /* note that 0 is forbidden */
5668 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005669 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005670 return -1;
5671 }
5672
5673 /* Look for the code in the cache. */
5674 py_code = PyLong_FromLong(code);
5675 if (py_code == NULL)
5676 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005677 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005678 if (obj != NULL) {
5679 /* Bingo. */
5680 Py_DECREF(py_code);
5681 PDATA_APPEND(self->stack, obj, -1);
5682 return 0;
5683 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005684 if (PyErr_Occurred()) {
5685 Py_DECREF(py_code);
5686 return -1;
5687 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688
5689 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005690 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005691 if (pair == NULL) {
5692 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005693 if (!PyErr_Occurred()) {
5694 PyErr_Format(PyExc_ValueError, "unregistered extension "
5695 "code %ld", code);
5696 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005697 return -1;
5698 }
5699 /* Since the extension registry is manipulable via Python code,
5700 * confirm that pair is really a 2-tuple of strings.
5701 */
5702 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5703 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5704 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5705 Py_DECREF(py_code);
5706 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5707 "isn't a 2-tuple of strings", code);
5708 return -1;
5709 }
5710 /* Load the object. */
5711 obj = find_class(self, module_name, class_name);
5712 if (obj == NULL) {
5713 Py_DECREF(py_code);
5714 return -1;
5715 }
5716 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005717 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005718 Py_DECREF(py_code);
5719 if (code < 0) {
5720 Py_DECREF(obj);
5721 return -1;
5722 }
5723 PDATA_PUSH(self->stack, obj, -1);
5724 return 0;
5725}
5726
5727static int
5728load_put(UnpicklerObject *self)
5729{
5730 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005731 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 Py_ssize_t len;
5733 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005734
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005735 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 return -1;
5737 if (len < 2)
5738 return bad_readline();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005739 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005740 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005741 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005742
5743 key = PyLong_FromString(s, NULL, 10);
5744 if (key == NULL)
5745 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005746 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005747 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005748 if (idx < 0) {
5749 if (!PyErr_Occurred())
5750 PyErr_SetString(PyExc_ValueError,
5751 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005752 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005753 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005754
5755 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756}
5757
5758static int
5759load_binput(UnpicklerObject *self)
5760{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005761 PyObject *value;
5762 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005763 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005764
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005765 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005766 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005767
5768 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005769 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005771
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005773
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005774 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005775}
5776
5777static int
5778load_long_binput(UnpicklerObject *self)
5779{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005780 PyObject *value;
5781 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005783
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005784 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005785 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786
5787 if (Py_SIZE(self->stack) <= 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005788 return stack_underflow();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005789 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005790
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005791 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005792 if (idx < 0) {
5793 PyErr_SetString(PyExc_ValueError,
5794 "negative LONG_BINPUT argument");
5795 return -1;
5796 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799}
5800
5801static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005802load_memoize(UnpicklerObject *self)
5803{
5804 PyObject *value;
5805
5806 if (Py_SIZE(self->stack) <= 0)
5807 return stack_underflow();
5808 value = self->stack->data[Py_SIZE(self->stack) - 1];
5809
5810 return _Unpickler_MemoPut(self, self->memo_len, value);
5811}
5812
5813static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005814do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815{
5816 PyObject *value;
5817 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005818 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005819
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005820 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005821 if (x > len || x <= 0)
5822 return stack_underflow();
5823 if (len == x) /* nothing to do */
5824 return 0;
5825
5826 list = self->stack->data[x - 1];
5827
5828 if (PyList_Check(list)) {
5829 PyObject *slice;
5830 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005831 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005832
5833 slice = Pdata_poplist(self->stack, x);
5834 if (!slice)
5835 return -1;
5836 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005837 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005838 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005839 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005840 }
5841 else {
5842 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005843 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005844
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005845 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846 if (append_func == NULL)
5847 return -1;
5848 for (i = x; i < len; i++) {
5849 PyObject *result;
5850
5851 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005852 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005853 if (result == NULL) {
5854 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005855 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005856 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005857 return -1;
5858 }
5859 Py_DECREF(result);
5860 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005861 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005862 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005863 }
5864
5865 return 0;
5866}
5867
5868static int
5869load_append(UnpicklerObject *self)
5870{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005871 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005872}
5873
5874static int
5875load_appends(UnpicklerObject *self)
5876{
5877 return do_append(self, marker(self));
5878}
5879
5880static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005881do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005882{
5883 PyObject *value, *key;
5884 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005885 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005886 int status = 0;
5887
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005888 len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005889 if (x > len || x <= 0)
5890 return stack_underflow();
5891 if (len == x) /* nothing to do */
5892 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005893 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005894 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005895 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005896 PyErr_SetString(st->UnpicklingError,
5897 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005898 return -1;
5899 }
5900
5901 /* Here, dict does not actually need to be a PyDict; it could be anything
5902 that supports the __setitem__ attribute. */
5903 dict = self->stack->data[x - 1];
5904
5905 for (i = x + 1; i < len; i += 2) {
5906 key = self->stack->data[i - 1];
5907 value = self->stack->data[i];
5908 if (PyObject_SetItem(dict, key, value) < 0) {
5909 status = -1;
5910 break;
5911 }
5912 }
5913
5914 Pdata_clear(self->stack, x);
5915 return status;
5916}
5917
5918static int
5919load_setitem(UnpicklerObject *self)
5920{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005921 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005922}
5923
5924static int
5925load_setitems(UnpicklerObject *self)
5926{
5927 return do_setitems(self, marker(self));
5928}
5929
5930static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005931load_additems(UnpicklerObject *self)
5932{
5933 PyObject *set;
5934 Py_ssize_t mark, len, i;
5935
5936 mark = marker(self);
5937 len = Py_SIZE(self->stack);
5938 if (mark > len || mark <= 0)
5939 return stack_underflow();
5940 if (len == mark) /* nothing to do */
5941 return 0;
5942
5943 set = self->stack->data[mark - 1];
5944
5945 if (PySet_Check(set)) {
5946 PyObject *items;
5947 int status;
5948
5949 items = Pdata_poptuple(self->stack, mark);
5950 if (items == NULL)
5951 return -1;
5952
5953 status = _PySet_Update(set, items);
5954 Py_DECREF(items);
5955 return status;
5956 }
5957 else {
5958 PyObject *add_func;
5959 _Py_IDENTIFIER(add);
5960
5961 add_func = _PyObject_GetAttrId(set, &PyId_add);
5962 if (add_func == NULL)
5963 return -1;
5964 for (i = mark; i < len; i++) {
5965 PyObject *result;
5966 PyObject *item;
5967
5968 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005969 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005970 if (result == NULL) {
5971 Pdata_clear(self->stack, i + 1);
5972 Py_SIZE(self->stack) = mark;
5973 return -1;
5974 }
5975 Py_DECREF(result);
5976 }
5977 Py_SIZE(self->stack) = mark;
5978 }
5979
5980 return 0;
5981}
5982
5983static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005984load_build(UnpicklerObject *self)
5985{
5986 PyObject *state, *inst, *slotstate;
5987 PyObject *setstate;
5988 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005989 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005990
5991 /* Stack is ... instance, state. We want to leave instance at
5992 * the stack top, possibly mutated via instance.__setstate__(state).
5993 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005994 if (Py_SIZE(self->stack) < 2)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005995 return stack_underflow();
5996
5997 PDATA_POP(self->stack, state);
5998 if (state == NULL)
5999 return -1;
6000
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006001 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006002
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006003 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006004 if (setstate == NULL) {
6005 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6006 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006007 else {
6008 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006009 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006010 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006011 }
6012 else {
6013 PyObject *result;
6014
6015 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006016 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006017 Py_DECREF(setstate);
6018 if (result == NULL)
6019 return -1;
6020 Py_DECREF(result);
6021 return 0;
6022 }
6023
6024 /* A default __setstate__. First see whether state embeds a
6025 * slot state dict too (a proto 2 addition).
6026 */
6027 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6028 PyObject *tmp = state;
6029
6030 state = PyTuple_GET_ITEM(tmp, 0);
6031 slotstate = PyTuple_GET_ITEM(tmp, 1);
6032 Py_INCREF(state);
6033 Py_INCREF(slotstate);
6034 Py_DECREF(tmp);
6035 }
6036 else
6037 slotstate = NULL;
6038
6039 /* Set inst.__dict__ from the state dict (if any). */
6040 if (state != Py_None) {
6041 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006042 PyObject *d_key, *d_value;
6043 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006044 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006045
6046 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006047 PickleState *st = _Pickle_GetGlobalState();
6048 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006049 goto error;
6050 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006051 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006052 if (dict == NULL)
6053 goto error;
6054
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006055 i = 0;
6056 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6057 /* normally the keys for instance attributes are
6058 interned. we should try to do that here. */
6059 Py_INCREF(d_key);
6060 if (PyUnicode_CheckExact(d_key))
6061 PyUnicode_InternInPlace(&d_key);
6062 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6063 Py_DECREF(d_key);
6064 goto error;
6065 }
6066 Py_DECREF(d_key);
6067 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006068 Py_DECREF(dict);
6069 }
6070
6071 /* Also set instance attributes from the slotstate dict (if any). */
6072 if (slotstate != NULL) {
6073 PyObject *d_key, *d_value;
6074 Py_ssize_t i;
6075
6076 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006077 PickleState *st = _Pickle_GetGlobalState();
6078 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006079 "slot state is not a dictionary");
6080 goto error;
6081 }
6082 i = 0;
6083 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6084 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6085 goto error;
6086 }
6087 }
6088
6089 if (0) {
6090 error:
6091 status = -1;
6092 }
6093
6094 Py_DECREF(state);
6095 Py_XDECREF(slotstate);
6096 return status;
6097}
6098
6099static int
6100load_mark(UnpicklerObject *self)
6101{
6102
6103 /* Note that we split the (pickle.py) stack into two stacks, an
6104 * object stack and a mark stack. Here we push a mark onto the
6105 * mark stack.
6106 */
6107
6108 if ((self->num_marks + 1) >= self->marks_size) {
6109 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006110
6111 /* Use the size_t type to check for overflow. */
6112 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006113 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006114 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006115 PyErr_NoMemory();
6116 return -1;
6117 }
6118
6119 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006120 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006121 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006122 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6123 if (self->marks == NULL) {
6124 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006125 PyErr_NoMemory();
6126 return -1;
6127 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006128 self->marks_size = (Py_ssize_t)alloc;
6129 }
6130
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006131 self->marks[self->num_marks++] = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006132
6133 return 0;
6134}
6135
6136static int
6137load_reduce(UnpicklerObject *self)
6138{
6139 PyObject *callable = NULL;
6140 PyObject *argtup = NULL;
6141 PyObject *obj = NULL;
6142
6143 PDATA_POP(self->stack, argtup);
6144 if (argtup == NULL)
6145 return -1;
6146 PDATA_POP(self->stack, callable);
6147 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006148 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149 Py_DECREF(callable);
6150 }
6151 Py_DECREF(argtup);
6152
6153 if (obj == NULL)
6154 return -1;
6155
6156 PDATA_PUSH(self->stack, obj, -1);
6157 return 0;
6158}
6159
6160/* Just raises an error if we don't know the protocol specified. PROTO
6161 * is the first opcode for protocols >= 2.
6162 */
6163static int
6164load_proto(UnpicklerObject *self)
6165{
6166 char *s;
6167 int i;
6168
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006169 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006170 return -1;
6171
6172 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006173 if (i <= HIGHEST_PROTOCOL) {
6174 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006175 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006176 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006177
6178 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6179 return -1;
6180}
6181
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006182static int
6183load_frame(UnpicklerObject *self)
6184{
6185 char *s;
6186 Py_ssize_t frame_len;
6187
6188 if (_Unpickler_Read(self, &s, 8) < 0)
6189 return -1;
6190
6191 frame_len = calc_binsize(s, 8);
6192 if (frame_len < 0) {
6193 PyErr_Format(PyExc_OverflowError,
6194 "FRAME length exceeds system's maximum of %zd bytes",
6195 PY_SSIZE_T_MAX);
6196 return -1;
6197 }
6198
6199 if (_Unpickler_Read(self, &s, frame_len) < 0)
6200 return -1;
6201
6202 /* Rewind to start of frame */
6203 self->next_read_idx -= frame_len;
6204 return 0;
6205}
6206
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006207static PyObject *
6208load(UnpicklerObject *self)
6209{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006210 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006211 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006212
6213 self->num_marks = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006214 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006215 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006216 Pdata_clear(self->stack, 0);
6217
6218 /* Convenient macros for the dispatch while-switch loop just below. */
6219#define OP(opcode, load_func) \
6220 case opcode: if (load_func(self) < 0) break; continue;
6221
6222#define OP_ARG(opcode, load_func, arg) \
6223 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6224
6225 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006226 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006227 break;
6228
6229 switch ((enum opcode)s[0]) {
6230 OP(NONE, load_none)
6231 OP(BININT, load_binint)
6232 OP(BININT1, load_binint1)
6233 OP(BININT2, load_binint2)
6234 OP(INT, load_int)
6235 OP(LONG, load_long)
6236 OP_ARG(LONG1, load_counted_long, 1)
6237 OP_ARG(LONG4, load_counted_long, 4)
6238 OP(FLOAT, load_float)
6239 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006240 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6241 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6242 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6243 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6244 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006245 OP(STRING, load_string)
6246 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006247 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6248 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6249 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006250 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6251 OP_ARG(TUPLE1, load_counted_tuple, 1)
6252 OP_ARG(TUPLE2, load_counted_tuple, 2)
6253 OP_ARG(TUPLE3, load_counted_tuple, 3)
6254 OP(TUPLE, load_tuple)
6255 OP(EMPTY_LIST, load_empty_list)
6256 OP(LIST, load_list)
6257 OP(EMPTY_DICT, load_empty_dict)
6258 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006259 OP(EMPTY_SET, load_empty_set)
6260 OP(ADDITEMS, load_additems)
6261 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006262 OP(OBJ, load_obj)
6263 OP(INST, load_inst)
6264 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006265 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006266 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006267 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006268 OP(APPEND, load_append)
6269 OP(APPENDS, load_appends)
6270 OP(BUILD, load_build)
6271 OP(DUP, load_dup)
6272 OP(BINGET, load_binget)
6273 OP(LONG_BINGET, load_long_binget)
6274 OP(GET, load_get)
6275 OP(MARK, load_mark)
6276 OP(BINPUT, load_binput)
6277 OP(LONG_BINPUT, load_long_binput)
6278 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006279 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006280 OP(POP, load_pop)
6281 OP(POP_MARK, load_pop_mark)
6282 OP(SETITEM, load_setitem)
6283 OP(SETITEMS, load_setitems)
6284 OP(PERSID, load_persid)
6285 OP(BINPERSID, load_binpersid)
6286 OP(REDUCE, load_reduce)
6287 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006288 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 OP_ARG(EXT1, load_extension, 1)
6290 OP_ARG(EXT2, load_extension, 2)
6291 OP_ARG(EXT4, load_extension, 4)
6292 OP_ARG(NEWTRUE, load_bool, Py_True)
6293 OP_ARG(NEWFALSE, load_bool, Py_False)
6294
6295 case STOP:
6296 break;
6297
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006298 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006299 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006300 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006301 }
6302 else {
6303 PickleState *st = _Pickle_GetGlobalState();
6304 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006305 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006306 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307 return NULL;
6308 }
6309
6310 break; /* and we are done! */
6311 }
6312
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006313 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006314 return NULL;
6315 }
6316
Victor Stinner2ae57e32013-10-31 13:39:23 +01006317 if (_Unpickler_SkipConsumed(self) < 0)
6318 return NULL;
6319
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006320 PDATA_POP(self->stack, value);
6321 return value;
6322}
6323
Larry Hastings61272b72014-01-07 12:41:53 -08006324/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006325
6326_pickle.Unpickler.load
6327
6328Load a pickle.
6329
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006330Read a pickled object representation from the open file object given
6331in the constructor, and return the reconstituted object hierarchy
6332specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006333[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006334
Larry Hastings3cceb382014-01-04 11:09:09 -08006335static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006336_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006337/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006339 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006340
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006341 /* Check whether the Unpickler was initialized correctly. This prevents
6342 segfaulting if a subclass overridden __init__ with a function that does
6343 not call Unpickler.__init__(). Here, we simply ensure that self->read
6344 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006345 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006346 PickleState *st = _Pickle_GetGlobalState();
6347 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006348 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006349 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006350 return NULL;
6351 }
6352
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006353 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006354}
6355
6356/* The name of find_class() is misleading. In newer pickle protocols, this
6357 function is used for loading any global (i.e., functions), not just
6358 classes. The name is kept only for backward compatibility. */
6359
Larry Hastings61272b72014-01-07 12:41:53 -08006360/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006361
6362_pickle.Unpickler.find_class
6363
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006364 module_name: object
6365 global_name: object
6366 /
6367
6368Return an object from a specified module.
6369
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006370If necessary, the module will be imported. Subclasses may override
6371this method (e.g. to restrict unpickling of arbitrary classes and
6372functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006373
6374This method is called whenever a class or a function object is
6375needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006376[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006377
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006378static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006379_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6380 PyObject *module_name,
6381 PyObject *global_name)
6382/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006383{
6384 PyObject *global;
6385 PyObject *modules_dict;
6386 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006387 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006388
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006389 /* Try to map the old names used in Python 2.x to the new ones used in
6390 Python 3.x. We do this only with old pickle protocols and when the
6391 user has not disabled the feature. */
6392 if (self->proto < 3 && self->fix_imports) {
6393 PyObject *key;
6394 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006395 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006396
6397 /* Check if the global (i.e., a function or a class) was renamed
6398 or moved to another module. */
6399 key = PyTuple_Pack(2, module_name, global_name);
6400 if (key == NULL)
6401 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006402 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006403 Py_DECREF(key);
6404 if (item) {
6405 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6406 PyErr_Format(PyExc_RuntimeError,
6407 "_compat_pickle.NAME_MAPPING values should be "
6408 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6409 return NULL;
6410 }
6411 module_name = PyTuple_GET_ITEM(item, 0);
6412 global_name = PyTuple_GET_ITEM(item, 1);
6413 if (!PyUnicode_Check(module_name) ||
6414 !PyUnicode_Check(global_name)) {
6415 PyErr_Format(PyExc_RuntimeError,
6416 "_compat_pickle.NAME_MAPPING values should be "
6417 "pairs of str, not (%.200s, %.200s)",
6418 Py_TYPE(module_name)->tp_name,
6419 Py_TYPE(global_name)->tp_name);
6420 return NULL;
6421 }
6422 }
6423 else if (PyErr_Occurred()) {
6424 return NULL;
6425 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006426 else {
6427 /* Check if the module was renamed. */
6428 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6429 if (item) {
6430 if (!PyUnicode_Check(item)) {
6431 PyErr_Format(PyExc_RuntimeError,
6432 "_compat_pickle.IMPORT_MAPPING values should be "
6433 "strings, not %.200s", Py_TYPE(item)->tp_name);
6434 return NULL;
6435 }
6436 module_name = item;
6437 }
6438 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006439 return NULL;
6440 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006441 }
6442 }
6443
Victor Stinnerbb520202013-11-06 22:40:41 +01006444 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006445 if (modules_dict == NULL) {
6446 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006447 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006448 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006449
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006450 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006451 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006452 if (PyErr_Occurred())
6453 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006454 module = PyImport_Import(module_name);
6455 if (module == NULL)
6456 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006457 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006458 Py_DECREF(module);
6459 }
Victor Stinner121aab42011-09-29 23:40:53 +02006460 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006461 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006462 }
6463 return global;
6464}
6465
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006466/*[clinic input]
6467
6468_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6469
6470Returns size in memory, in bytes.
6471[clinic start generated code]*/
6472
6473static Py_ssize_t
6474_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6475/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6476{
6477 Py_ssize_t res;
6478
6479 res = sizeof(UnpicklerObject);
6480 if (self->memo != NULL)
6481 res += self->memo_size * sizeof(PyObject *);
6482 if (self->marks != NULL)
6483 res += self->marks_size * sizeof(Py_ssize_t);
6484 if (self->input_line != NULL)
6485 res += strlen(self->input_line) + 1;
6486 if (self->encoding != NULL)
6487 res += strlen(self->encoding) + 1;
6488 if (self->errors != NULL)
6489 res += strlen(self->errors) + 1;
6490 return res;
6491}
6492
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006493static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006494 _PICKLE_UNPICKLER_LOAD_METHODDEF
6495 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006496 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006497 {NULL, NULL} /* sentinel */
6498};
6499
6500static void
6501Unpickler_dealloc(UnpicklerObject *self)
6502{
6503 PyObject_GC_UnTrack((PyObject *)self);
6504 Py_XDECREF(self->readline);
6505 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006506 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006507 Py_XDECREF(self->stack);
6508 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006509 if (self->buffer.buf != NULL) {
6510 PyBuffer_Release(&self->buffer);
6511 self->buffer.buf = NULL;
6512 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006513
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006514 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006515 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006516 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006517 PyMem_Free(self->encoding);
6518 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006519
6520 Py_TYPE(self)->tp_free((PyObject *)self);
6521}
6522
6523static int
6524Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6525{
6526 Py_VISIT(self->readline);
6527 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006528 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006529 Py_VISIT(self->stack);
6530 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006531 return 0;
6532}
6533
6534static int
6535Unpickler_clear(UnpicklerObject *self)
6536{
6537 Py_CLEAR(self->readline);
6538 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006539 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540 Py_CLEAR(self->stack);
6541 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006542 if (self->buffer.buf != NULL) {
6543 PyBuffer_Release(&self->buffer);
6544 self->buffer.buf = NULL;
6545 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006547 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006548 PyMem_Free(self->marks);
6549 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006550 PyMem_Free(self->input_line);
6551 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006552 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006553 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006554 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006555 self->errors = NULL;
6556
6557 return 0;
6558}
6559
Larry Hastings61272b72014-01-07 12:41:53 -08006560/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006561
6562_pickle.Unpickler.__init__
6563
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006564 file: object
6565 *
6566 fix_imports: bool = True
6567 encoding: str = 'ASCII'
6568 errors: str = 'strict'
6569
6570This takes a binary file for reading a pickle data stream.
6571
6572The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006573protocol argument is needed. Bytes past the pickled object's
6574representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006575
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006576The argument *file* must have two methods, a read() method that takes
6577an integer argument, and a readline() method that requires no
6578arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006579binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006580other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006581
6582Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6583which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006584generated by Python 2. If *fix_imports* is True, pickle will try to
6585map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006586*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006587instances pickled by Python 2; these default to 'ASCII' and 'strict',
6588respectively. The *encoding* can be 'bytes' to read these 8-bit
6589string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006590[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006591
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006592static int
Larry Hastings89964c42015-04-14 18:07:59 -04006593_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6594 int fix_imports, const char *encoding,
6595 const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00006596/*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006597{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006598 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006599
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006600 /* In case of multiple __init__() calls, clear previous content. */
6601 if (self->read != NULL)
6602 (void)Unpickler_clear(self);
6603
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006604 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006605 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006606
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006607 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006608 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006609
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006610 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006611 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006612 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006613
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006614 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006615 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6616 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006617 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006618 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006619 }
6620 else {
6621 self->pers_func = NULL;
6622 }
6623
6624 self->stack = (Pdata *)Pdata_New();
6625 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006626 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006628 self->memo_size = 32;
6629 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006631 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006632
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006633 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006634
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006635 return 0;
6636}
6637
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006638
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006639/* Define a proxy object for the Unpickler's internal memo object. This is to
6640 * avoid breaking code like:
6641 * unpickler.memo.clear()
6642 * and
6643 * unpickler.memo = saved_memo
6644 * Is this a good idea? Not really, but we don't want to break code that uses
6645 * it. Note that we don't implement the entire mapping API here. This is
6646 * intentional, as these should be treated as black-box implementation details.
6647 *
6648 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006649 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006650 */
6651
Larry Hastings61272b72014-01-07 12:41:53 -08006652/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006653_pickle.UnpicklerMemoProxy.clear
6654
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006655Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006656[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006657
Larry Hastings3cceb382014-01-04 11:09:09 -08006658static PyObject *
6659_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006660/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006661{
6662 _Unpickler_MemoCleanup(self->unpickler);
6663 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6664 if (self->unpickler->memo == NULL)
6665 return NULL;
6666 Py_RETURN_NONE;
6667}
6668
Larry Hastings61272b72014-01-07 12:41:53 -08006669/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006670_pickle.UnpicklerMemoProxy.copy
6671
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006672Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006673[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006674
Larry Hastings3cceb382014-01-04 11:09:09 -08006675static PyObject *
6676_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006677/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006678{
6679 Py_ssize_t i;
6680 PyObject *new_memo = PyDict_New();
6681 if (new_memo == NULL)
6682 return NULL;
6683
6684 for (i = 0; i < self->unpickler->memo_size; i++) {
6685 int status;
6686 PyObject *key, *value;
6687
6688 value = self->unpickler->memo[i];
6689 if (value == NULL)
6690 continue;
6691
6692 key = PyLong_FromSsize_t(i);
6693 if (key == NULL)
6694 goto error;
6695 status = PyDict_SetItem(new_memo, key, value);
6696 Py_DECREF(key);
6697 if (status < 0)
6698 goto error;
6699 }
6700 return new_memo;
6701
6702error:
6703 Py_DECREF(new_memo);
6704 return NULL;
6705}
6706
Larry Hastings61272b72014-01-07 12:41:53 -08006707/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006708_pickle.UnpicklerMemoProxy.__reduce__
6709
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006710Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006711[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006712
Larry Hastings3cceb382014-01-04 11:09:09 -08006713static PyObject *
6714_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006715/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006716{
6717 PyObject *reduce_value;
6718 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006719 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006720 if (contents == NULL)
6721 return NULL;
6722
6723 reduce_value = PyTuple_New(2);
6724 if (reduce_value == NULL) {
6725 Py_DECREF(contents);
6726 return NULL;
6727 }
6728 constructor_args = PyTuple_New(1);
6729 if (constructor_args == NULL) {
6730 Py_DECREF(contents);
6731 Py_DECREF(reduce_value);
6732 return NULL;
6733 }
6734 PyTuple_SET_ITEM(constructor_args, 0, contents);
6735 Py_INCREF((PyObject *)&PyDict_Type);
6736 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6737 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6738 return reduce_value;
6739}
6740
6741static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006742 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6743 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6744 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006745 {NULL, NULL} /* sentinel */
6746};
6747
6748static void
6749UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6750{
6751 PyObject_GC_UnTrack(self);
6752 Py_XDECREF(self->unpickler);
6753 PyObject_GC_Del((PyObject *)self);
6754}
6755
6756static int
6757UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6758 visitproc visit, void *arg)
6759{
6760 Py_VISIT(self->unpickler);
6761 return 0;
6762}
6763
6764static int
6765UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6766{
6767 Py_CLEAR(self->unpickler);
6768 return 0;
6769}
6770
6771static PyTypeObject UnpicklerMemoProxyType = {
6772 PyVarObject_HEAD_INIT(NULL, 0)
6773 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6774 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6775 0,
6776 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6777 0, /* tp_print */
6778 0, /* tp_getattr */
6779 0, /* tp_setattr */
6780 0, /* tp_compare */
6781 0, /* tp_repr */
6782 0, /* tp_as_number */
6783 0, /* tp_as_sequence */
6784 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006785 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006786 0, /* tp_call */
6787 0, /* tp_str */
6788 PyObject_GenericGetAttr, /* tp_getattro */
6789 PyObject_GenericSetAttr, /* tp_setattro */
6790 0, /* tp_as_buffer */
6791 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6792 0, /* tp_doc */
6793 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6794 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6795 0, /* tp_richcompare */
6796 0, /* tp_weaklistoffset */
6797 0, /* tp_iter */
6798 0, /* tp_iternext */
6799 unpicklerproxy_methods, /* tp_methods */
6800};
6801
6802static PyObject *
6803UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6804{
6805 UnpicklerMemoProxyObject *self;
6806
6807 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6808 &UnpicklerMemoProxyType);
6809 if (self == NULL)
6810 return NULL;
6811 Py_INCREF(unpickler);
6812 self->unpickler = unpickler;
6813 PyObject_GC_Track(self);
6814 return (PyObject *)self;
6815}
6816
6817/*****************************************************************************/
6818
6819
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006820static PyObject *
6821Unpickler_get_memo(UnpicklerObject *self)
6822{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006823 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006824}
6825
6826static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006827Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006828{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006829 PyObject **new_memo;
6830 Py_ssize_t new_memo_size = 0;
6831 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006832
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006833 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006834 PyErr_SetString(PyExc_TypeError,
6835 "attribute deletion is not supported");
6836 return -1;
6837 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006838
6839 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6840 UnpicklerObject *unpickler =
6841 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6842
6843 new_memo_size = unpickler->memo_size;
6844 new_memo = _Unpickler_NewMemo(new_memo_size);
6845 if (new_memo == NULL)
6846 return -1;
6847
6848 for (i = 0; i < new_memo_size; i++) {
6849 Py_XINCREF(unpickler->memo[i]);
6850 new_memo[i] = unpickler->memo[i];
6851 }
6852 }
6853 else if (PyDict_Check(obj)) {
6854 Py_ssize_t i = 0;
6855 PyObject *key, *value;
6856
6857 new_memo_size = PyDict_Size(obj);
6858 new_memo = _Unpickler_NewMemo(new_memo_size);
6859 if (new_memo == NULL)
6860 return -1;
6861
6862 while (PyDict_Next(obj, &i, &key, &value)) {
6863 Py_ssize_t idx;
6864 if (!PyLong_Check(key)) {
6865 PyErr_SetString(PyExc_TypeError,
6866 "memo key must be integers");
6867 goto error;
6868 }
6869 idx = PyLong_AsSsize_t(key);
6870 if (idx == -1 && PyErr_Occurred())
6871 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006872 if (idx < 0) {
6873 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006874 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006875 goto error;
6876 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006877 if (_Unpickler_MemoPut(self, idx, value) < 0)
6878 goto error;
6879 }
6880 }
6881 else {
6882 PyErr_Format(PyExc_TypeError,
6883 "'memo' attribute must be an UnpicklerMemoProxy object"
6884 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006885 return -1;
6886 }
6887
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006888 _Unpickler_MemoCleanup(self);
6889 self->memo_size = new_memo_size;
6890 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006891
6892 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006893
6894 error:
6895 if (new_memo_size) {
6896 i = new_memo_size;
6897 while (--i >= 0) {
6898 Py_XDECREF(new_memo[i]);
6899 }
6900 PyMem_FREE(new_memo);
6901 }
6902 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006903}
6904
6905static PyObject *
6906Unpickler_get_persload(UnpicklerObject *self)
6907{
6908 if (self->pers_func == NULL)
6909 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6910 else
6911 Py_INCREF(self->pers_func);
6912 return self->pers_func;
6913}
6914
6915static int
6916Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6917{
6918 PyObject *tmp;
6919
6920 if (value == NULL) {
6921 PyErr_SetString(PyExc_TypeError,
6922 "attribute deletion is not supported");
6923 return -1;
6924 }
6925 if (!PyCallable_Check(value)) {
6926 PyErr_SetString(PyExc_TypeError,
6927 "persistent_load must be a callable taking "
6928 "one argument");
6929 return -1;
6930 }
6931
6932 tmp = self->pers_func;
6933 Py_INCREF(value);
6934 self->pers_func = value;
6935 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6936
6937 return 0;
6938}
6939
6940static PyGetSetDef Unpickler_getsets[] = {
6941 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6942 {"persistent_load", (getter)Unpickler_get_persload,
6943 (setter)Unpickler_set_persload},
6944 {NULL}
6945};
6946
6947static PyTypeObject Unpickler_Type = {
6948 PyVarObject_HEAD_INIT(NULL, 0)
6949 "_pickle.Unpickler", /*tp_name*/
6950 sizeof(UnpicklerObject), /*tp_basicsize*/
6951 0, /*tp_itemsize*/
6952 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6953 0, /*tp_print*/
6954 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006955 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006956 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006957 0, /*tp_repr*/
6958 0, /*tp_as_number*/
6959 0, /*tp_as_sequence*/
6960 0, /*tp_as_mapping*/
6961 0, /*tp_hash*/
6962 0, /*tp_call*/
6963 0, /*tp_str*/
6964 0, /*tp_getattro*/
6965 0, /*tp_setattro*/
6966 0, /*tp_as_buffer*/
6967 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006968 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006969 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6970 (inquiry)Unpickler_clear, /*tp_clear*/
6971 0, /*tp_richcompare*/
6972 0, /*tp_weaklistoffset*/
6973 0, /*tp_iter*/
6974 0, /*tp_iternext*/
6975 Unpickler_methods, /*tp_methods*/
6976 0, /*tp_members*/
6977 Unpickler_getsets, /*tp_getset*/
6978 0, /*tp_base*/
6979 0, /*tp_dict*/
6980 0, /*tp_descr_get*/
6981 0, /*tp_descr_set*/
6982 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006983 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006984 PyType_GenericAlloc, /*tp_alloc*/
6985 PyType_GenericNew, /*tp_new*/
6986 PyObject_GC_Del, /*tp_free*/
6987 0, /*tp_is_gc*/
6988};
6989
Larry Hastings61272b72014-01-07 12:41:53 -08006990/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006991
6992_pickle.dump
6993
6994 obj: object
6995 file: object
6996 protocol: object = NULL
6997 *
6998 fix_imports: bool = True
6999
7000Write a pickled representation of obj to the open file object file.
7001
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007002This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7003be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007004
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007005The optional *protocol* argument tells the pickler to use the given
7006protocol supported protocols are 0, 1, 2, 3 and 4. The default
7007protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007008
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007009Specifying a negative protocol version selects the highest protocol
7010version supported. The higher the protocol used, the more recent the
7011version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007012
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007013The *file* argument must have a write() method that accepts a single
7014bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007015writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007016this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007017
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007018If *fix_imports* is True and protocol is less than 3, pickle will try
7019to map the new Python 3 names to the old module names used in Python
70202, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007021[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007022
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007023static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007024_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7025 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00007026/*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007027{
7028 PicklerObject *pickler = _Pickler_New();
7029
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007030 if (pickler == NULL)
7031 return NULL;
7032
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007033 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007034 goto error;
7035
7036 if (_Pickler_SetOutputStream(pickler, file) < 0)
7037 goto error;
7038
7039 if (dump(pickler, obj) < 0)
7040 goto error;
7041
7042 if (_Pickler_FlushToFile(pickler) < 0)
7043 goto error;
7044
7045 Py_DECREF(pickler);
7046 Py_RETURN_NONE;
7047
7048 error:
7049 Py_XDECREF(pickler);
7050 return NULL;
7051}
7052
Larry Hastings61272b72014-01-07 12:41:53 -08007053/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007054
7055_pickle.dumps
7056
7057 obj: object
7058 protocol: object = NULL
7059 *
7060 fix_imports: bool = True
7061
7062Return the pickled representation of the object as a bytes object.
7063
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007064The optional *protocol* argument tells the pickler to use the given
7065protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7066protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007067
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007068Specifying a negative protocol version selects the highest protocol
7069version supported. The higher the protocol used, the more recent the
7070version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007071
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007072If *fix_imports* is True and *protocol* is less than 3, pickle will
7073try to map the new Python 3 names to the old module names used in
7074Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007075[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007076
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007077static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007078_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7079 int fix_imports)
7080/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007081{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007082 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007083 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007084
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007085 if (pickler == NULL)
7086 return NULL;
7087
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007088 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007089 goto error;
7090
7091 if (dump(pickler, obj) < 0)
7092 goto error;
7093
7094 result = _Pickler_GetString(pickler);
7095 Py_DECREF(pickler);
7096 return result;
7097
7098 error:
7099 Py_XDECREF(pickler);
7100 return NULL;
7101}
7102
Larry Hastings61272b72014-01-07 12:41:53 -08007103/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007104
7105_pickle.load
7106
7107 file: object
7108 *
7109 fix_imports: bool = True
7110 encoding: str = 'ASCII'
7111 errors: str = 'strict'
7112
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007113Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007114
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007115This is equivalent to ``Unpickler(file).load()``, but may be more
7116efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007117
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007118The protocol version of the pickle is detected automatically, so no
7119protocol argument is needed. Bytes past the pickled object's
7120representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007121
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007122The argument *file* must have two methods, a read() method that takes
7123an integer argument, and a readline() method that requires no
7124arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007125binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007126other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007127
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007128Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7129which are used to control compatiblity support for pickle stream
7130generated by Python 2. If *fix_imports* is True, pickle will try to
7131map the old Python 2 names to the new names used in Python 3. The
7132*encoding* and *errors* tell pickle how to decode 8-bit string
7133instances pickled by Python 2; these default to 'ASCII' and 'strict',
7134respectively. The *encoding* can be 'bytes' to read these 8-bit
7135string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007136[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007137
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007138static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007139_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7140 const char *encoding, const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00007141/*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007142{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007143 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007145
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007146 if (unpickler == NULL)
7147 return NULL;
7148
7149 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7150 goto error;
7151
7152 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7153 goto error;
7154
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007155 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007156
7157 result = load(unpickler);
7158 Py_DECREF(unpickler);
7159 return result;
7160
7161 error:
7162 Py_XDECREF(unpickler);
7163 return NULL;
7164}
7165
Larry Hastings61272b72014-01-07 12:41:53 -08007166/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167
7168_pickle.loads
7169
7170 data: object
7171 *
7172 fix_imports: bool = True
7173 encoding: str = 'ASCII'
7174 errors: str = 'strict'
7175
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007176Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007177
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007178The protocol version of the pickle is detected automatically, so no
7179protocol argument is needed. Bytes past the pickled object's
7180representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007181
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007182Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7183which are used to control compatiblity support for pickle stream
7184generated by Python 2. If *fix_imports* is True, pickle will try to
7185map the old Python 2 names to the new names used in Python 3. The
7186*encoding* and *errors* tell pickle how to decode 8-bit string
7187instances pickled by Python 2; these default to 'ASCII' and 'strict',
7188respectively. The *encoding* can be 'bytes' to read these 8-bit
7189string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007190[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007191
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007192static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007193_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7194 const char *encoding, const char *errors)
7195/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007196{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007197 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007199
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007200 if (unpickler == NULL)
7201 return NULL;
7202
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007203 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007204 goto error;
7205
7206 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7207 goto error;
7208
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007209 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007210
7211 result = load(unpickler);
7212 Py_DECREF(unpickler);
7213 return result;
7214
7215 error:
7216 Py_XDECREF(unpickler);
7217 return NULL;
7218}
7219
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007220static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221 _PICKLE_DUMP_METHODDEF
7222 _PICKLE_DUMPS_METHODDEF
7223 _PICKLE_LOAD_METHODDEF
7224 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007225 {NULL, NULL} /* sentinel */
7226};
7227
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007228static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007229pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007230{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007231 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007232 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007233}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007234
Stefan Krahf483b0f2013-12-14 13:43:10 +01007235static void
7236pickle_free(PyObject *m)
7237{
7238 _Pickle_ClearState(_Pickle_GetState(m));
7239}
7240
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007241static int
7242pickle_traverse(PyObject *m, visitproc visit, void *arg)
7243{
7244 PickleState *st = _Pickle_GetState(m);
7245 Py_VISIT(st->PickleError);
7246 Py_VISIT(st->PicklingError);
7247 Py_VISIT(st->UnpicklingError);
7248 Py_VISIT(st->dispatch_table);
7249 Py_VISIT(st->extension_registry);
7250 Py_VISIT(st->extension_cache);
7251 Py_VISIT(st->inverted_registry);
7252 Py_VISIT(st->name_mapping_2to3);
7253 Py_VISIT(st->import_mapping_2to3);
7254 Py_VISIT(st->name_mapping_3to2);
7255 Py_VISIT(st->import_mapping_3to2);
7256 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007257 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007258 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007259}
7260
7261static struct PyModuleDef _picklemodule = {
7262 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007263 "_pickle", /* m_name */
7264 pickle_module_doc, /* m_doc */
7265 sizeof(PickleState), /* m_size */
7266 pickle_methods, /* m_methods */
7267 NULL, /* m_reload */
7268 pickle_traverse, /* m_traverse */
7269 pickle_clear, /* m_clear */
7270 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007271};
7272
7273PyMODINIT_FUNC
7274PyInit__pickle(void)
7275{
7276 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007277 PickleState *st;
7278
7279 m = PyState_FindModule(&_picklemodule);
7280 if (m) {
7281 Py_INCREF(m);
7282 return m;
7283 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007284
7285 if (PyType_Ready(&Unpickler_Type) < 0)
7286 return NULL;
7287 if (PyType_Ready(&Pickler_Type) < 0)
7288 return NULL;
7289 if (PyType_Ready(&Pdata_Type) < 0)
7290 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007291 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7292 return NULL;
7293 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7294 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007295
7296 /* Create the module and add the functions. */
7297 m = PyModule_Create(&_picklemodule);
7298 if (m == NULL)
7299 return NULL;
7300
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007301 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007302 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7303 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007304 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007305 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7306 return NULL;
7307
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007308 st = _Pickle_GetState(m);
7309
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007310 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007311 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7312 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007313 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007314 st->PicklingError = \
7315 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7316 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007317 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007318 st->UnpicklingError = \
7319 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7320 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007321 return NULL;
7322
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007323 Py_INCREF(st->PickleError);
7324 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007325 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007326 Py_INCREF(st->PicklingError);
7327 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007328 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007329 Py_INCREF(st->UnpicklingError);
7330 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007331 return NULL;
7332
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007333 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007334 return NULL;
7335
7336 return m;
7337}