blob: 5c3530b96265ea70fe7e214049e9fd7df86d6111 [file] [log] [blame]
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001#include "Python.h"
2#include "structmember.h"
3
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004PyDoc_STRVAR(pickle_module_doc,
5"Optimized C implementation for the Python pickle module.");
6
Larry Hastings61272b72014-01-07 12:41:53 -08007/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08008module _pickle
Larry Hastingsc2047262014-01-25 20:43:29 -08009class _pickle.Pickler "PicklerObject *" "&Pickler_Type"
10class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType"
11class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type"
12class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType"
Larry Hastings61272b72014-01-07 12:41:53 -080013[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030014/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -080015
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000016/* Bump this when new opcodes are added to the pickle protocol. */
17enum {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010018 HIGHEST_PROTOCOL = 4,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000019 DEFAULT_PROTOCOL = 3
20};
21
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000022/* Pickle opcodes. These must be kept updated with pickle.py.
23 Extensive docs are in pickletools.py. */
24enum opcode {
25 MARK = '(',
26 STOP = '.',
27 POP = '0',
28 POP_MARK = '1',
29 DUP = '2',
30 FLOAT = 'F',
31 INT = 'I',
32 BININT = 'J',
33 BININT1 = 'K',
34 LONG = 'L',
35 BININT2 = 'M',
36 NONE = 'N',
37 PERSID = 'P',
38 BINPERSID = 'Q',
39 REDUCE = 'R',
40 STRING = 'S',
41 BINSTRING = 'T',
42 SHORT_BINSTRING = 'U',
43 UNICODE = 'V',
44 BINUNICODE = 'X',
45 APPEND = 'a',
46 BUILD = 'b',
47 GLOBAL = 'c',
48 DICT = 'd',
49 EMPTY_DICT = '}',
50 APPENDS = 'e',
51 GET = 'g',
52 BINGET = 'h',
53 INST = 'i',
54 LONG_BINGET = 'j',
55 LIST = 'l',
56 EMPTY_LIST = ']',
57 OBJ = 'o',
58 PUT = 'p',
59 BINPUT = 'q',
60 LONG_BINPUT = 'r',
61 SETITEM = 's',
62 TUPLE = 't',
63 EMPTY_TUPLE = ')',
64 SETITEMS = 'u',
65 BINFLOAT = 'G',
66
67 /* Protocol 2. */
68 PROTO = '\x80',
69 NEWOBJ = '\x81',
70 EXT1 = '\x82',
71 EXT2 = '\x83',
72 EXT4 = '\x84',
73 TUPLE1 = '\x85',
74 TUPLE2 = '\x86',
75 TUPLE3 = '\x87',
76 NEWTRUE = '\x88',
77 NEWFALSE = '\x89',
78 LONG1 = '\x8a',
79 LONG4 = '\x8b',
80
81 /* Protocol 3 (Python 3.x) */
82 BINBYTES = 'B',
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +010083 SHORT_BINBYTES = 'C',
84
85 /* Protocol 4 */
86 SHORT_BINUNICODE = '\x8c',
87 BINUNICODE8 = '\x8d',
88 BINBYTES8 = '\x8e',
89 EMPTY_SET = '\x8f',
90 ADDITEMS = '\x90',
91 FROZENSET = '\x91',
92 NEWOBJ_EX = '\x92',
93 STACK_GLOBAL = '\x93',
94 MEMOIZE = '\x94',
95 FRAME = '\x95'
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000096};
97
Alexandre Vassalottica2d6102008-06-12 18:26:05 +000098enum {
99 /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements
100 batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will
101 break if this gets out of synch with pickle.py, but it's unclear that would
102 help anything either. */
103 BATCHSIZE = 1000,
104
105 /* Nesting limit until Pickler, when running in "fast mode", starts
106 checking for self-referential data-structures. */
107 FAST_NESTING_LIMIT = 50,
108
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000109 /* Initial size of the write buffer of Pickler. */
110 WRITE_BUF_SIZE = 4096,
111
Antoine Pitrou04248a82010-10-12 20:51:21 +0000112 /* Prefetch size when unpickling (disabled on unpeekable streams) */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100113 PREFETCH = 8192 * 16,
114
115 FRAME_SIZE_TARGET = 64 * 1024,
116
117 FRAME_HEADER_SIZE = 9
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000118};
119
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800120/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000121
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800122/* State of the pickle module, per PEP 3121. */
123typedef struct {
124 /* Exception classes for pickle. */
125 PyObject *PickleError;
126 PyObject *PicklingError;
127 PyObject *UnpicklingError;
Larry Hastings61272b72014-01-07 12:41:53 -0800128
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800129 /* copyreg.dispatch_table, {type_object: pickling_function} */
130 PyObject *dispatch_table;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000131
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800132 /* For the extension opcodes EXT1, EXT2 and EXT4. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000133
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800134 /* copyreg._extension_registry, {(module_name, function_name): code} */
135 PyObject *extension_registry;
136 /* copyreg._extension_cache, {code: object} */
137 PyObject *extension_cache;
138 /* copyreg._inverted_registry, {code: (module_name, function_name)} */
139 PyObject *inverted_registry;
140
141 /* Import mappings for compatibility with Python 2.x */
142
143 /* _compat_pickle.NAME_MAPPING,
144 {(oldmodule, oldname): (newmodule, newname)} */
145 PyObject *name_mapping_2to3;
146 /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */
147 PyObject *import_mapping_2to3;
148 /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */
149 PyObject *name_mapping_3to2;
150 PyObject *import_mapping_3to2;
151
152 /* codecs.encode, used for saving bytes in older protocols */
153 PyObject *codecs_encode;
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300154 /* builtins.getattr, used for saving nested names with protocol < 4 */
155 PyObject *getattr;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300156 /* functools.partial, used for implementing __newobj_ex__ with protocols
157 2 and 3 */
158 PyObject *partial;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800159} PickleState;
160
161/* Forward declaration of the _pickle module definition. */
162static struct PyModuleDef _picklemodule;
163
164/* Given a module object, get its per-module state. */
165static PickleState *
166_Pickle_GetState(PyObject *module)
167{
168 return (PickleState *)PyModule_GetState(module);
169}
170
171/* Find the module instance imported in the currently running sub-interpreter
172 and get its state. */
173static PickleState *
174_Pickle_GetGlobalState(void)
175{
176 return _Pickle_GetState(PyState_FindModule(&_picklemodule));
177}
178
179/* Clear the given pickle module state. */
180static void
181_Pickle_ClearState(PickleState *st)
182{
183 Py_CLEAR(st->PickleError);
184 Py_CLEAR(st->PicklingError);
185 Py_CLEAR(st->UnpicklingError);
186 Py_CLEAR(st->dispatch_table);
187 Py_CLEAR(st->extension_registry);
188 Py_CLEAR(st->extension_cache);
189 Py_CLEAR(st->inverted_registry);
190 Py_CLEAR(st->name_mapping_2to3);
191 Py_CLEAR(st->import_mapping_2to3);
192 Py_CLEAR(st->name_mapping_3to2);
193 Py_CLEAR(st->import_mapping_3to2);
194 Py_CLEAR(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300195 Py_CLEAR(st->getattr);
Victor Stinner9ba97df2015-11-17 12:15:07 +0100196 Py_CLEAR(st->partial);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800197}
198
199/* Initialize the given pickle module state. */
200static int
201_Pickle_InitState(PickleState *st)
202{
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300203 PyObject *builtins;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800204 PyObject *copyreg = NULL;
205 PyObject *compat_pickle = NULL;
206 PyObject *codecs = NULL;
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300207 PyObject *functools = NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800208
Serhiy Storchaka58e41342015-03-31 14:07:24 +0300209 builtins = PyEval_GetBuiltins();
210 if (builtins == NULL)
211 goto error;
212 st->getattr = PyDict_GetItemString(builtins, "getattr");
213 if (st->getattr == NULL)
214 goto error;
215 Py_INCREF(st->getattr);
216
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800217 copyreg = PyImport_ImportModule("copyreg");
218 if (!copyreg)
219 goto error;
220 st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table");
221 if (!st->dispatch_table)
222 goto error;
223 if (!PyDict_CheckExact(st->dispatch_table)) {
224 PyErr_Format(PyExc_RuntimeError,
225 "copyreg.dispatch_table should be a dict, not %.200s",
226 Py_TYPE(st->dispatch_table)->tp_name);
227 goto error;
228 }
229 st->extension_registry = \
230 PyObject_GetAttrString(copyreg, "_extension_registry");
231 if (!st->extension_registry)
232 goto error;
233 if (!PyDict_CheckExact(st->extension_registry)) {
234 PyErr_Format(PyExc_RuntimeError,
235 "copyreg._extension_registry should be a dict, "
236 "not %.200s", Py_TYPE(st->extension_registry)->tp_name);
237 goto error;
238 }
239 st->inverted_registry = \
240 PyObject_GetAttrString(copyreg, "_inverted_registry");
241 if (!st->inverted_registry)
242 goto error;
243 if (!PyDict_CheckExact(st->inverted_registry)) {
244 PyErr_Format(PyExc_RuntimeError,
245 "copyreg._inverted_registry should be a dict, "
246 "not %.200s", Py_TYPE(st->inverted_registry)->tp_name);
247 goto error;
248 }
249 st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache");
250 if (!st->extension_cache)
251 goto error;
252 if (!PyDict_CheckExact(st->extension_cache)) {
253 PyErr_Format(PyExc_RuntimeError,
254 "copyreg._extension_cache should be a dict, "
255 "not %.200s", Py_TYPE(st->extension_cache)->tp_name);
256 goto error;
257 }
258 Py_CLEAR(copyreg);
259
260 /* Load the 2.x -> 3.x stdlib module mapping tables */
261 compat_pickle = PyImport_ImportModule("_compat_pickle");
262 if (!compat_pickle)
263 goto error;
264 st->name_mapping_2to3 = \
265 PyObject_GetAttrString(compat_pickle, "NAME_MAPPING");
266 if (!st->name_mapping_2to3)
267 goto error;
268 if (!PyDict_CheckExact(st->name_mapping_2to3)) {
269 PyErr_Format(PyExc_RuntimeError,
270 "_compat_pickle.NAME_MAPPING should be a dict, not %.200s",
271 Py_TYPE(st->name_mapping_2to3)->tp_name);
272 goto error;
273 }
274 st->import_mapping_2to3 = \
275 PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING");
276 if (!st->import_mapping_2to3)
277 goto error;
278 if (!PyDict_CheckExact(st->import_mapping_2to3)) {
279 PyErr_Format(PyExc_RuntimeError,
280 "_compat_pickle.IMPORT_MAPPING should be a dict, "
281 "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name);
282 goto error;
283 }
284 /* ... and the 3.x -> 2.x mapping tables */
285 st->name_mapping_3to2 = \
286 PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING");
287 if (!st->name_mapping_3to2)
288 goto error;
289 if (!PyDict_CheckExact(st->name_mapping_3to2)) {
290 PyErr_Format(PyExc_RuntimeError,
291 "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, "
292 "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name);
293 goto error;
294 }
295 st->import_mapping_3to2 = \
296 PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING");
297 if (!st->import_mapping_3to2)
298 goto error;
299 if (!PyDict_CheckExact(st->import_mapping_3to2)) {
300 PyErr_Format(PyExc_RuntimeError,
301 "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, "
302 "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name);
303 goto error;
304 }
305 Py_CLEAR(compat_pickle);
306
307 codecs = PyImport_ImportModule("codecs");
308 if (codecs == NULL)
309 goto error;
310 st->codecs_encode = PyObject_GetAttrString(codecs, "encode");
311 if (st->codecs_encode == NULL) {
312 goto error;
313 }
314 if (!PyCallable_Check(st->codecs_encode)) {
315 PyErr_Format(PyExc_RuntimeError,
316 "codecs.encode should be a callable, not %.200s",
317 Py_TYPE(st->codecs_encode)->tp_name);
318 goto error;
319 }
320 Py_CLEAR(codecs);
321
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300322 functools = PyImport_ImportModule("functools");
323 if (!functools)
324 goto error;
325 st->partial = PyObject_GetAttrString(functools, "partial");
326 if (!st->partial)
327 goto error;
328 Py_CLEAR(functools);
329
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800330 return 0;
331
332 error:
333 Py_CLEAR(copyreg);
334 Py_CLEAR(compat_pickle);
335 Py_CLEAR(codecs);
Serhiy Storchaka0d554d72015-10-10 22:42:18 +0300336 Py_CLEAR(functools);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800337 _Pickle_ClearState(st);
338 return -1;
339}
340
341/* Helper for calling a function with a single argument quickly.
342
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800343 This function steals the reference of the given argument. */
344static PyObject *
345_Pickle_FastCall(PyObject *func, PyObject *obj)
346{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800347 PyObject *result;
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800348 PyObject *arg_tuple = PyTuple_New(1);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800349
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800350 /* Note: this function used to reuse the argument tuple. This used to give
351 a slight performance boost with older pickle implementations where many
352 unbuffered reads occurred (thus needing many function calls).
353
354 However, this optimization was removed because it was too complicated
355 to get right. It abused the C API for tuples to mutate them which led
356 to subtle reference counting and concurrency bugs. Furthermore, the
357 introduction of protocol 4 and the prefetching optimization via peek()
358 significantly reduced the number of function calls we do. Thus, the
359 benefits became marginal at best. */
360
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800361 if (arg_tuple == NULL) {
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800362 Py_DECREF(obj);
363 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800364 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800365 PyTuple_SET_ITEM(arg_tuple, 0, obj);
366 result = PyObject_Call(func, arg_tuple, NULL);
Alexandre Vassalottib13e6bc2013-11-28 14:56:09 -0800367 Py_CLEAR(arg_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -0800368 return result;
369}
370
371/*************************************************************************/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000372
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000373/* Internal data type used as the unpickling stack. */
374typedef struct {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000375 PyObject_VAR_HEAD
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000376 PyObject **data;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200377 int mark_set; /* is MARK set? */
378 Py_ssize_t fence; /* position of top MARK or 0 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000379 Py_ssize_t allocated; /* number of slots in data allocated */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000380} Pdata;
381
382static void
383Pdata_dealloc(Pdata *self)
384{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200385 Py_ssize_t i = Py_SIZE(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000386 while (--i >= 0) {
387 Py_DECREF(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000388 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000389 PyMem_FREE(self->data);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000390 PyObject_Del(self);
391}
392
393static PyTypeObject Pdata_Type = {
394 PyVarObject_HEAD_INIT(NULL, 0)
395 "_pickle.Pdata", /*tp_name*/
396 sizeof(Pdata), /*tp_basicsize*/
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200397 sizeof(PyObject *), /*tp_itemsize*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000398 (destructor)Pdata_dealloc, /*tp_dealloc*/
399};
400
401static PyObject *
402Pdata_New(void)
403{
404 Pdata *self;
405
406 if (!(self = PyObject_New(Pdata, &Pdata_Type)))
407 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000408 Py_SIZE(self) = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200409 self->mark_set = 0;
410 self->fence = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000411 self->allocated = 8;
412 self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000413 if (self->data)
414 return (PyObject *)self;
415 Py_DECREF(self);
416 return PyErr_NoMemory();
417}
418
419
420/* Retain only the initial clearto items. If clearto >= the current
421 * number of items, this is a (non-erroneous) NOP.
422 */
423static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200424Pdata_clear(Pdata *self, Py_ssize_t clearto)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000425{
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200426 Py_ssize_t i = Py_SIZE(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000427
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200428 assert(clearto >= self->fence);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000429 if (clearto >= i)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000430 return 0;
431
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000432 while (--i >= clearto) {
433 Py_CLEAR(self->data[i]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000434 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000435 Py_SIZE(self) = clearto;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000436 return 0;
437}
438
439static int
440Pdata_grow(Pdata *self)
441{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000442 PyObject **data = self->data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200443 size_t allocated = (size_t)self->allocated;
444 size_t new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000445
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000446 new_allocated = (allocated >> 3) + 6;
447 /* check for integer overflow */
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200448 if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000449 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000450 new_allocated += allocated;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500451 PyMem_RESIZE(data, PyObject *, new_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000452 if (data == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000453 goto nomemory;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000454
455 self->data = data;
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200456 self->allocated = (Py_ssize_t)new_allocated;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000457 return 0;
458
459 nomemory:
460 PyErr_NoMemory();
461 return -1;
462}
463
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200464static int
465Pdata_stack_underflow(Pdata *self)
466{
467 PickleState *st = _Pickle_GetGlobalState();
468 PyErr_SetString(st->UnpicklingError,
469 self->mark_set ?
470 "unexpected MARK found" :
471 "unpickling stack underflow");
472 return -1;
473}
474
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000475/* D is a Pdata*. Pop the topmost element and store it into V, which
476 * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError
477 * is raised and V is set to NULL.
478 */
479static PyObject *
480Pdata_pop(Pdata *self)
481{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200482 if (Py_SIZE(self) <= self->fence) {
483 Pdata_stack_underflow(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000484 return NULL;
485 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000486 return self->data[--Py_SIZE(self)];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000487}
488#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
489
490static int
491Pdata_push(Pdata *self, PyObject *obj)
492{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000493 if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000494 return -1;
495 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000496 self->data[Py_SIZE(self)++] = obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000497 return 0;
498}
499
500/* Push an object on stack, transferring its ownership to the stack. */
501#define PDATA_PUSH(D, O, ER) do { \
502 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
503
504/* Push an object on stack, adding a new reference to the object. */
505#define PDATA_APPEND(D, O, ER) do { \
506 Py_INCREF((O)); \
507 if (Pdata_push((D), (O)) < 0) return (ER); } while(0)
508
509static PyObject *
510Pdata_poptuple(Pdata *self, Py_ssize_t start)
511{
512 PyObject *tuple;
513 Py_ssize_t len, i, j;
514
Serhiy Storchaka59fb6342015-12-06 22:01:35 +0200515 if (start < self->fence) {
516 Pdata_stack_underflow(self);
517 return NULL;
518 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000519 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000520 tuple = PyTuple_New(len);
521 if (tuple == NULL)
522 return NULL;
523 for (i = start, j = 0; j < len; i++, j++)
524 PyTuple_SET_ITEM(tuple, j, self->data[i]);
525
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000526 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000527 return tuple;
528}
529
530static PyObject *
531Pdata_poplist(Pdata *self, Py_ssize_t start)
532{
533 PyObject *list;
534 Py_ssize_t len, i, j;
535
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000536 len = Py_SIZE(self) - start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000537 list = PyList_New(len);
538 if (list == NULL)
539 return NULL;
540 for (i = start, j = 0; j < len; i++, j++)
541 PyList_SET_ITEM(list, j, self->data[i]);
542
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000543 Py_SIZE(self) = start;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000544 return list;
545}
546
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000547typedef struct {
548 PyObject *me_key;
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200549 Py_ssize_t me_value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000550} PyMemoEntry;
551
552typedef struct {
553 Py_ssize_t mt_mask;
554 Py_ssize_t mt_used;
555 Py_ssize_t mt_allocated;
556 PyMemoEntry *mt_table;
557} PyMemoTable;
558
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000559typedef struct PicklerObject {
560 PyObject_HEAD
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000561 PyMemoTable *memo; /* Memo table, keep track of the seen
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000562 objects to support self-referential objects
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000563 pickling. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000564 PyObject *pers_func; /* persistent_id() method, can be NULL */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +0100565 PyObject *dispatch_table; /* private dispatch_table, can be NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000566
567 PyObject *write; /* write() method of the output stream. */
568 PyObject *output_buffer; /* Write into a local bytearray buffer before
569 flushing to the stream. */
570 Py_ssize_t output_len; /* Length of output_buffer. */
571 Py_ssize_t max_output_len; /* Allocation size of output_buffer. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000572 int proto; /* Pickle protocol number, >= 0 */
573 int bin; /* Boolean, true if proto > 0 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100574 int framing; /* True when framing is enabled, proto >= 4 */
575 Py_ssize_t frame_start; /* Position in output_buffer where the
576 where the current frame begins. -1 if there
577 is no frame currently open. */
578
579 Py_ssize_t buf_size; /* Size of the current buffered pickle data */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000580 int fast; /* Enable fast mode if set to a true value.
581 The fast mode disable the usage of memo,
582 therefore speeding the pickling process by
583 not generating superfluous PUT opcodes. It
584 should not be used if with self-referential
585 objects. */
586 int fast_nesting;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000587 int fix_imports; /* Indicate whether Pickler should fix
588 the name of globals for Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000589 PyObject *fast_memo;
590} PicklerObject;
591
592typedef struct UnpicklerObject {
593 PyObject_HEAD
594 Pdata *stack; /* Pickle data stack, store unpickled objects. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000595
596 /* The unpickler memo is just an array of PyObject *s. Using a dict
597 is unnecessary, since the keys are contiguous ints. */
598 PyObject **memo;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100599 Py_ssize_t memo_size; /* Capacity of the memo array */
600 Py_ssize_t memo_len; /* Number of objects in the memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000601
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000602 PyObject *pers_func; /* persistent_load() method, can be NULL. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000603
604 Py_buffer buffer;
605 char *input_buffer;
606 char *input_line;
607 Py_ssize_t input_len;
608 Py_ssize_t next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +0000609 Py_ssize_t prefetched_idx; /* index of first prefetched byte */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100610
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000611 PyObject *read; /* read() method of the input stream. */
612 PyObject *readline; /* readline() method of the input stream. */
Antoine Pitrou04248a82010-10-12 20:51:21 +0000613 PyObject *peek; /* peek() method of the input stream, or NULL */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000614
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000615 char *encoding; /* Name of the encoding to be used for
616 decoding strings pickled using Python
617 2.x. The default value is "ASCII" */
618 char *errors; /* Name of errors handling scheme to used when
619 decoding strings. The default value is
620 "strict". */
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -0500621 Py_ssize_t *marks; /* Mark stack, used for unpickling container
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000622 objects. */
623 Py_ssize_t num_marks; /* Number of marks in the mark stack. */
624 Py_ssize_t marks_size; /* Current allocated size of the mark stack. */
Antoine Pitroud9dfaa92009-06-04 20:32:06 +0000625 int proto; /* Protocol of the pickle loaded. */
626 int fix_imports; /* Indicate whether Unpickler should fix
627 the name of globals pickled by Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000628} UnpicklerObject;
629
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200630typedef struct {
631 PyObject_HEAD
632 PicklerObject *pickler; /* Pickler whose memo table we're proxying. */
633} PicklerMemoProxyObject;
634
635typedef struct {
636 PyObject_HEAD
637 UnpicklerObject *unpickler;
638} UnpicklerMemoProxyObject;
639
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000640/* Forward declarations */
641static int save(PicklerObject *, PyObject *, int);
642static int save_reduce(PicklerObject *, PyObject *, PyObject *);
643static PyTypeObject Pickler_Type;
644static PyTypeObject Unpickler_Type;
645
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200646#include "clinic/_pickle.c.h"
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000648/*************************************************************************
Serhiy Storchaka95949422013-08-27 19:40:23 +0300649 A custom hashtable mapping void* to Python ints. This is used by the pickler
650 for memoization. Using a custom hashtable rather than PyDict allows us to skip
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000651 a bunch of unnecessary object creation. This makes a huge performance
652 difference. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000653
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000654#define MT_MINSIZE 8
655#define PERTURB_SHIFT 5
656
657
658static PyMemoTable *
659PyMemoTable_New(void)
660{
661 PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
662 if (memo == NULL) {
663 PyErr_NoMemory();
664 return NULL;
665 }
666
667 memo->mt_used = 0;
668 memo->mt_allocated = MT_MINSIZE;
669 memo->mt_mask = MT_MINSIZE - 1;
670 memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
671 if (memo->mt_table == NULL) {
672 PyMem_FREE(memo);
673 PyErr_NoMemory();
674 return NULL;
675 }
676 memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry));
677
678 return memo;
679}
680
681static PyMemoTable *
682PyMemoTable_Copy(PyMemoTable *self)
683{
684 Py_ssize_t i;
685 PyMemoTable *new = PyMemoTable_New();
686 if (new == NULL)
687 return NULL;
688
689 new->mt_used = self->mt_used;
690 new->mt_allocated = self->mt_allocated;
691 new->mt_mask = self->mt_mask;
692 /* The table we get from _New() is probably smaller than we wanted.
693 Free it and allocate one that's the right size. */
694 PyMem_FREE(new->mt_table);
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500695 new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000696 if (new->mt_table == NULL) {
697 PyMem_FREE(new);
Victor Stinner42024562013-07-12 00:53:57 +0200698 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000699 return NULL;
700 }
701 for (i = 0; i < self->mt_allocated; i++) {
702 Py_XINCREF(self->mt_table[i].me_key);
703 }
704 memcpy(new->mt_table, self->mt_table,
705 sizeof(PyMemoEntry) * self->mt_allocated);
706
707 return new;
708}
709
710static Py_ssize_t
711PyMemoTable_Size(PyMemoTable *self)
712{
713 return self->mt_used;
714}
715
716static int
717PyMemoTable_Clear(PyMemoTable *self)
718{
719 Py_ssize_t i = self->mt_allocated;
720
721 while (--i >= 0) {
722 Py_XDECREF(self->mt_table[i].me_key);
723 }
724 self->mt_used = 0;
725 memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry));
726 return 0;
727}
728
729static void
730PyMemoTable_Del(PyMemoTable *self)
731{
732 if (self == NULL)
733 return;
734 PyMemoTable_Clear(self);
735
736 PyMem_FREE(self->mt_table);
737 PyMem_FREE(self);
738}
739
740/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
741 can be considerably simpler than dictobject.c's lookdict(). */
742static PyMemoEntry *
743_PyMemoTable_Lookup(PyMemoTable *self, PyObject *key)
744{
745 size_t i;
746 size_t perturb;
747 size_t mask = (size_t)self->mt_mask;
748 PyMemoEntry *table = self->mt_table;
749 PyMemoEntry *entry;
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000750 Py_hash_t hash = (Py_hash_t)key >> 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000751
752 i = hash & mask;
753 entry = &table[i];
754 if (entry->me_key == NULL || entry->me_key == key)
755 return entry;
756
757 for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
758 i = (i << 2) + i + perturb + 1;
759 entry = &table[i & mask];
760 if (entry->me_key == NULL || entry->me_key == key)
761 return entry;
762 }
763 assert(0); /* Never reached */
764 return NULL;
765}
766
767/* Returns -1 on failure, 0 on success. */
768static int
769_PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size)
770{
771 PyMemoEntry *oldtable = NULL;
772 PyMemoEntry *oldentry, *newentry;
773 Py_ssize_t new_size = MT_MINSIZE;
774 Py_ssize_t to_process;
775
776 assert(min_size > 0);
777
778 /* Find the smallest valid table size >= min_size. */
779 while (new_size < min_size && new_size > 0)
780 new_size <<= 1;
781 if (new_size <= 0) {
782 PyErr_NoMemory();
783 return -1;
784 }
785 /* new_size needs to be a power of two. */
786 assert((new_size & (new_size - 1)) == 0);
787
788 /* Allocate new table. */
789 oldtable = self->mt_table;
Benjamin Peterson59b08c12015-06-27 13:41:33 -0500790 self->mt_table = PyMem_NEW(PyMemoEntry, new_size);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000791 if (self->mt_table == NULL) {
Victor Stinner8ca72e22013-07-12 00:53:26 +0200792 self->mt_table = oldtable;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000793 PyErr_NoMemory();
794 return -1;
795 }
796 self->mt_allocated = new_size;
797 self->mt_mask = new_size - 1;
798 memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size);
799
800 /* Copy entries from the old table. */
801 to_process = self->mt_used;
802 for (oldentry = oldtable; to_process > 0; oldentry++) {
803 if (oldentry->me_key != NULL) {
804 to_process--;
805 /* newentry is a pointer to a chunk of the new
806 mt_table, so we're setting the key:value pair
807 in-place. */
808 newentry = _PyMemoTable_Lookup(self, oldentry->me_key);
809 newentry->me_key = oldentry->me_key;
810 newentry->me_value = oldentry->me_value;
811 }
812 }
813
814 /* Deallocate the old table. */
815 PyMem_FREE(oldtable);
816 return 0;
817}
818
819/* Returns NULL on failure, a pointer to the value otherwise. */
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200820static Py_ssize_t *
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000821PyMemoTable_Get(PyMemoTable *self, PyObject *key)
822{
823 PyMemoEntry *entry = _PyMemoTable_Lookup(self, key);
824 if (entry->me_key == NULL)
825 return NULL;
826 return &entry->me_value;
827}
828
829/* Returns -1 on failure, 0 on success. */
830static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200831PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000832{
833 PyMemoEntry *entry;
834
835 assert(key != NULL);
836
837 entry = _PyMemoTable_Lookup(self, key);
838 if (entry->me_key != NULL) {
839 entry->me_value = value;
840 return 0;
841 }
842 Py_INCREF(key);
843 entry->me_key = key;
844 entry->me_value = value;
845 self->mt_used++;
846
847 /* If we added a key, we can safely resize. Otherwise just return!
848 * If used >= 2/3 size, adjust size. Normally, this quaduples the size.
849 *
850 * Quadrupling the size improves average table sparseness
851 * (reducing collisions) at the cost of some memory. It also halves
852 * the number of expensive resize operations in a growing memo table.
853 *
854 * Very large memo tables (over 50K items) use doubling instead.
855 * This may help applications with severe memory constraints.
856 */
857 if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2))
858 return 0;
859 return _PyMemoTable_ResizeTable(self,
860 (self->mt_used > 50000 ? 2 : 4) * self->mt_used);
861}
862
863#undef MT_MINSIZE
864#undef PERTURB_SHIFT
865
866/*************************************************************************/
867
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000868
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000869static int
870_Pickler_ClearBuffer(PicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000871{
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +0200872 Py_SETREF(self->output_buffer,
873 PyBytes_FromStringAndSize(NULL, self->max_output_len));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000874 if (self->output_buffer == NULL)
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000875 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000876 self->output_len = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100877 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000878 return 0;
879}
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +0000880
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100881static void
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100882_write_size64(char *out, size_t value)
883{
Victor Stinnerf13c46c2014-08-17 21:05:55 +0200884 size_t i;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800885
Serhiy Storchakafad85aa2015-11-07 15:42:38 +0200886 Py_BUILD_ASSERT(sizeof(size_t) <= 8);
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -0800887
888 for (i = 0; i < sizeof(size_t); i++) {
889 out[i] = (unsigned char)((value >> (8 * i)) & 0xff);
890 }
891 for (i = sizeof(size_t); i < 8; i++) {
892 out[i] = 0;
Alexandre Vassalottided929b2013-11-24 22:41:13 -0800893 }
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100894}
895
896static void
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100897_Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len)
898{
Antoine Pitrou8f2ee6e2013-11-23 21:05:08 +0100899 qdata[0] = FRAME;
900 _write_size64(qdata + 1, frame_len);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100901}
902
903static int
904_Pickler_CommitFrame(PicklerObject *self)
905{
906 size_t frame_len;
907 char *qdata;
908
909 if (!self->framing || self->frame_start == -1)
910 return 0;
911 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
912 qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start;
913 _Pickler_WriteFrameHeader(self, qdata, frame_len);
914 self->frame_start = -1;
915 return 0;
916}
917
918static int
919_Pickler_OpcodeBoundary(PicklerObject *self)
920{
921 Py_ssize_t frame_len;
922
923 if (!self->framing || self->frame_start == -1)
924 return 0;
925 frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE;
926 if (frame_len >= FRAME_SIZE_TARGET)
927 return _Pickler_CommitFrame(self);
928 else
929 return 0;
930}
931
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000932static PyObject *
933_Pickler_GetString(PicklerObject *self)
934{
935 PyObject *output_buffer = self->output_buffer;
936
937 assert(self->output_buffer != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100938
939 if (_Pickler_CommitFrame(self))
940 return NULL;
941
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000942 self->output_buffer = NULL;
943 /* Resize down to exact size */
944 if (_PyBytes_Resize(&output_buffer, self->output_len) < 0)
945 return NULL;
946 return output_buffer;
947}
948
949static int
950_Pickler_FlushToFile(PicklerObject *self)
951{
952 PyObject *output, *result;
953
954 assert(self->write != NULL);
955
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100956 /* This will commit the frame first */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000957 output = _Pickler_GetString(self);
958 if (output == NULL)
959 return -1;
960
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -0800961 result = _Pickle_FastCall(self->write, output);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000962 Py_XDECREF(result);
963 return (result == NULL) ? -1 : 0;
964}
965
Antoine Pitrou82be19f2011-08-29 23:09:33 +0200966static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100967_Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000968{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100969 Py_ssize_t i, n, required;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000970 char *buffer;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100971 int need_new_frame;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000972
973 assert(s != NULL);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100974 need_new_frame = (self->framing && self->frame_start == -1);
975
976 if (need_new_frame)
977 n = data_len + FRAME_HEADER_SIZE;
978 else
979 n = data_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000980
981 required = self->output_len + n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100982 if (required > self->max_output_len) {
983 /* Make place in buffer for the pickle chunk */
984 if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) {
985 PyErr_NoMemory();
986 return -1;
987 }
988 self->max_output_len = (self->output_len + n) / 2 * 3;
989 if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0)
990 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +0000991 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +0000992 buffer = PyBytes_AS_STRING(self->output_buffer);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +0100993 if (need_new_frame) {
994 /* Setup new frame */
995 Py_ssize_t frame_start = self->output_len;
996 self->frame_start = frame_start;
997 for (i = 0; i < FRAME_HEADER_SIZE; i++) {
998 /* Write an invalid value, for debugging */
999 buffer[frame_start + i] = 0xFE;
1000 }
1001 self->output_len += FRAME_HEADER_SIZE;
1002 }
1003 if (data_len < 8) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001004 /* This is faster than memcpy when the string is short. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001005 for (i = 0; i < data_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001006 buffer[self->output_len + i] = s[i];
1007 }
1008 }
1009 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001010 memcpy(buffer + self->output_len, s, data_len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001011 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001012 self->output_len += data_len;
1013 return data_len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001014}
1015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001016static PicklerObject *
1017_Pickler_New(void)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001018{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001019 PicklerObject *self;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001020
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001021 self = PyObject_GC_New(PicklerObject, &Pickler_Type);
1022 if (self == NULL)
1023 return NULL;
1024
1025 self->pers_func = NULL;
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01001026 self->dispatch_table = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001027 self->write = NULL;
1028 self->proto = 0;
1029 self->bin = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001030 self->framing = 0;
1031 self->frame_start = -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001032 self->fast = 0;
1033 self->fast_nesting = 0;
1034 self->fix_imports = 0;
1035 self->fast_memo = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001036 self->max_output_len = WRITE_BUF_SIZE;
1037 self->output_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001038
1039 self->memo = PyMemoTable_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001040 self->output_buffer = PyBytes_FromStringAndSize(NULL,
1041 self->max_output_len);
Victor Stinner68c8ea22013-07-11 22:56:25 +02001042
1043 if (self->memo == NULL || self->output_buffer == NULL) {
Victor Stinnerc31df042013-07-12 00:08:59 +02001044 Py_DECREF(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001045 return NULL;
1046 }
1047 return self;
1048}
1049
1050static int
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001051_Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001052{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001053 long proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001054
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001055 if (protocol == NULL || protocol == Py_None) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001056 proto = DEFAULT_PROTOCOL;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001057 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001058 else {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001059 proto = PyLong_AsLong(protocol);
1060 if (proto < 0) {
1061 if (proto == -1 && PyErr_Occurred())
1062 return -1;
1063 proto = HIGHEST_PROTOCOL;
1064 }
1065 else if (proto > HIGHEST_PROTOCOL) {
1066 PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d",
1067 HIGHEST_PROTOCOL);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001068 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001069 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001070 }
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08001071 self->proto = (int)proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001072 self->bin = proto > 0;
1073 self->fix_imports = fix_imports && proto < 3;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001074 return 0;
1075}
1076
1077/* Returns -1 (with an exception set) on failure, 0 on success. This may
1078 be called once on a freshly created Pickler. */
1079static int
1080_Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
1081{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001082 _Py_IDENTIFIER(write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001083 assert(file != NULL);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001084 self->write = _PyObject_GetAttrId(file, &PyId_write);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001085 if (self->write == NULL) {
1086 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1087 PyErr_SetString(PyExc_TypeError,
1088 "file must have a 'write' attribute");
1089 return -1;
1090 }
1091
1092 return 0;
1093}
1094
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001095/* Returns the size of the input on success, -1 on failure. This takes its
1096 own reference to `input`. */
1097static Py_ssize_t
1098_Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input)
1099{
1100 if (self->buffer.buf != NULL)
1101 PyBuffer_Release(&self->buffer);
1102 if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0)
1103 return -1;
1104 self->input_buffer = self->buffer.buf;
1105 self->input_len = self->buffer.len;
1106 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001107 self->prefetched_idx = self->input_len;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001108 return self->input_len;
1109}
1110
Antoine Pitrou04248a82010-10-12 20:51:21 +00001111static int
1112_Unpickler_SkipConsumed(UnpicklerObject *self)
1113{
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001114 Py_ssize_t consumed;
1115 PyObject *r;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001116
Victor Stinnerb43ad1d2013-10-31 13:38:42 +01001117 consumed = self->next_read_idx - self->prefetched_idx;
1118 if (consumed <= 0)
1119 return 0;
1120
1121 assert(self->peek); /* otherwise we did something wrong */
1122 /* This makes an useless copy... */
1123 r = PyObject_CallFunction(self->read, "n", consumed);
1124 if (r == NULL)
1125 return -1;
1126 Py_DECREF(r);
1127
1128 self->prefetched_idx = self->next_read_idx;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001129 return 0;
1130}
1131
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001132static const Py_ssize_t READ_WHOLE_LINE = -1;
1133
1134/* If reading from a file, we need to only pull the bytes we need, since there
1135 may be multiple pickle objects arranged contiguously in the same input
1136 buffer.
1137
1138 If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n`
1139 bytes from the input stream/buffer.
1140
1141 Update the unpickler's input buffer with the newly-read data. Returns -1 on
1142 failure; on success, returns the number of bytes read from the file.
1143
1144 On success, self->input_len will be 0; this is intentional so that when
1145 unpickling from a file, the "we've run out of data" code paths will trigger,
1146 causing the Unpickler to go back to the file for more data. Use the returned
1147 size to tell you how much data you can process. */
1148static Py_ssize_t
1149_Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
1150{
1151 PyObject *data;
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001152 Py_ssize_t read_size;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001153
1154 assert(self->read != NULL);
Victor Stinner121aab42011-09-29 23:40:53 +02001155
Antoine Pitrou04248a82010-10-12 20:51:21 +00001156 if (_Unpickler_SkipConsumed(self) < 0)
1157 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001158
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001159 if (n == READ_WHOLE_LINE) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08001160 PyObject *empty_tuple = PyTuple_New(0);
1161 data = PyObject_Call(self->readline, empty_tuple, NULL);
1162 Py_DECREF(empty_tuple);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001163 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001164 else {
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001165 PyObject *len;
1166 /* Prefetch some data without advancing the file pointer, if possible */
1167 if (self->peek && n < PREFETCH) {
1168 len = PyLong_FromSsize_t(PREFETCH);
1169 if (len == NULL)
1170 return -1;
1171 data = _Pickle_FastCall(self->peek, len);
1172 if (data == NULL) {
1173 if (!PyErr_ExceptionMatches(PyExc_NotImplementedError))
1174 return -1;
1175 /* peek() is probably not supported by the given file object */
1176 PyErr_Clear();
1177 Py_CLEAR(self->peek);
1178 }
1179 else {
1180 read_size = _Unpickler_SetStringInput(self, data);
1181 Py_DECREF(data);
1182 self->prefetched_idx = 0;
1183 if (n <= read_size)
1184 return n;
1185 }
1186 }
1187 len = PyLong_FromSsize_t(n);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001188 if (len == NULL)
1189 return -1;
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08001190 data = _Pickle_FastCall(self->read, len);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001191 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001192 if (data == NULL)
1193 return -1;
1194
Serhiy Storchaka6fe39b72013-11-30 23:15:38 +02001195 read_size = _Unpickler_SetStringInput(self, data);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001196 Py_DECREF(data);
1197 return read_size;
1198}
1199
1200/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
1201
1202 This should be used for all data reads, rather than accessing the unpickler's
1203 input buffer directly. This method deals correctly with reading from input
1204 streams, which the input buffer doesn't deal with.
1205
1206 Note that when reading from a file-like object, self->next_read_idx won't
1207 be updated (it should remain at 0 for the entire unpickling process). You
1208 should use this function's return value to know how many bytes you can
1209 consume.
1210
1211 Returns -1 (with an exception set) on failure. On success, return the
1212 number of chars read. */
1213static Py_ssize_t
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08001214_Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001215{
Antoine Pitrou04248a82010-10-12 20:51:21 +00001216 Py_ssize_t num_read;
1217
Benjamin Peterson6aa15642015-09-27 01:16:03 -07001218 *s = NULL;
Benjamin Petersone48cf7e2015-09-26 00:08:34 -07001219 if (self->next_read_idx > PY_SSIZE_T_MAX - n) {
1220 PickleState *st = _Pickle_GetGlobalState();
1221 PyErr_SetString(st->UnpicklingError,
1222 "read would overflow (invalid bytecode)");
1223 return -1;
1224 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001225 if (self->next_read_idx + n <= self->input_len) {
1226 *s = self->input_buffer + self->next_read_idx;
1227 self->next_read_idx += n;
1228 return n;
1229 }
1230 if (!self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001231 PyErr_Format(PyExc_EOFError, "Ran out of input");
Amaury Forgeot d'Arc3e4e72f2008-11-11 20:05:06 +00001232 return -1;
1233 }
Antoine Pitrou04248a82010-10-12 20:51:21 +00001234 num_read = _Unpickler_ReadFromFile(self, n);
1235 if (num_read < 0)
1236 return -1;
1237 if (num_read < n) {
1238 PyErr_Format(PyExc_EOFError, "Ran out of input");
1239 return -1;
1240 }
1241 *s = self->input_buffer;
1242 self->next_read_idx = n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001243 return n;
1244}
1245
1246static Py_ssize_t
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001247_Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len,
1248 char **result)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001249{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001250 char *input_line = PyMem_Realloc(self->input_line, len + 1);
Victor Stinner42024562013-07-12 00:53:57 +02001251 if (input_line == NULL) {
1252 PyErr_NoMemory();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001253 return -1;
Victor Stinner42024562013-07-12 00:53:57 +02001254 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001255
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001256 memcpy(input_line, line, len);
1257 input_line[len] = '\0';
1258 self->input_line = input_line;
1259 *result = self->input_line;
1260 return len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001261}
1262
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001263/* Read a line from the input stream/buffer. If we run off the end of the input
1264 before hitting \n, return the data we found.
1265
1266 Returns the number of chars read, or -1 on failure. */
1267static Py_ssize_t
1268_Unpickler_Readline(UnpicklerObject *self, char **result)
1269{
1270 Py_ssize_t i, num_read;
1271
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001272 for (i = self->next_read_idx; i < self->input_len; i++) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001273 if (self->input_buffer[i] == '\n') {
1274 char *line_start = self->input_buffer + self->next_read_idx;
1275 num_read = i - self->next_read_idx + 1;
1276 self->next_read_idx = i + 1;
1277 return _Unpickler_CopyLine(self, line_start, num_read, result);
1278 }
1279 }
1280 if (self->read) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001281 num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE);
1282 if (num_read < 0)
1283 return -1;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001284 self->next_read_idx = num_read;
Antoine Pitrouf6c7a852011-08-11 21:04:02 +02001285 return _Unpickler_CopyLine(self, self->input_buffer, num_read, result);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001286 }
Victor Stinner121aab42011-09-29 23:40:53 +02001287
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001288 /* If we get here, we've run off the end of the input string. Return the
1289 remaining string and let the caller figure it out. */
1290 *result = self->input_buffer + self->next_read_idx;
1291 num_read = i - self->next_read_idx;
1292 self->next_read_idx = i;
1293 return num_read;
1294}
1295
1296/* Returns -1 (with an exception set) on failure, 0 on success. The memo array
1297 will be modified in place. */
1298static int
1299_Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size)
1300{
1301 Py_ssize_t i;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001302
1303 assert(new_size > self->memo_size);
1304
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001305 PyMem_RESIZE(self->memo, PyObject *, new_size);
1306 if (self->memo == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001307 PyErr_NoMemory();
1308 return -1;
1309 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001310 for (i = self->memo_size; i < new_size; i++)
1311 self->memo[i] = NULL;
1312 self->memo_size = new_size;
1313 return 0;
1314}
1315
1316/* Returns NULL if idx is out of bounds. */
1317static PyObject *
1318_Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx)
1319{
1320 if (idx < 0 || idx >= self->memo_size)
1321 return NULL;
1322
1323 return self->memo[idx];
1324}
1325
1326/* Returns -1 (with an exception set) on failure, 0 on success.
1327 This takes its own reference to `value`. */
1328static int
1329_Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value)
1330{
1331 PyObject *old_item;
1332
1333 if (idx >= self->memo_size) {
1334 if (_Unpickler_ResizeMemoList(self, idx * 2) < 0)
1335 return -1;
1336 assert(idx < self->memo_size);
1337 }
1338 Py_INCREF(value);
1339 old_item = self->memo[idx];
1340 self->memo[idx] = value;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001341 if (old_item != NULL) {
1342 Py_DECREF(old_item);
1343 }
1344 else {
1345 self->memo_len++;
1346 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001347 return 0;
1348}
1349
1350static PyObject **
1351_Unpickler_NewMemo(Py_ssize_t new_size)
1352{
Benjamin Peterson59b08c12015-06-27 13:41:33 -05001353 PyObject **memo = PyMem_NEW(PyObject *, new_size);
Victor Stinner42024562013-07-12 00:53:57 +02001354 if (memo == NULL) {
1355 PyErr_NoMemory();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001356 return NULL;
Victor Stinner42024562013-07-12 00:53:57 +02001357 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001358 memset(memo, 0, new_size * sizeof(PyObject *));
1359 return memo;
1360}
1361
1362/* Free the unpickler's memo, taking care to decref any items left in it. */
1363static void
1364_Unpickler_MemoCleanup(UnpicklerObject *self)
1365{
1366 Py_ssize_t i;
1367 PyObject **memo = self->memo;
1368
1369 if (self->memo == NULL)
1370 return;
1371 self->memo = NULL;
1372 i = self->memo_size;
1373 while (--i >= 0) {
1374 Py_XDECREF(memo[i]);
1375 }
1376 PyMem_FREE(memo);
1377}
1378
1379static UnpicklerObject *
1380_Unpickler_New(void)
1381{
1382 UnpicklerObject *self;
1383
1384 self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type);
1385 if (self == NULL)
1386 return NULL;
1387
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001388 self->pers_func = NULL;
1389 self->input_buffer = NULL;
1390 self->input_line = NULL;
1391 self->input_len = 0;
1392 self->next_read_idx = 0;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001393 self->prefetched_idx = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001394 self->read = NULL;
1395 self->readline = NULL;
Antoine Pitrou04248a82010-10-12 20:51:21 +00001396 self->peek = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001397 self->encoding = NULL;
1398 self->errors = NULL;
1399 self->marks = NULL;
1400 self->num_marks = 0;
1401 self->marks_size = 0;
1402 self->proto = 0;
1403 self->fix_imports = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001404 memset(&self->buffer, 0, sizeof(Py_buffer));
1405 self->memo_size = 32;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001406 self->memo_len = 0;
Victor Stinner68c8ea22013-07-11 22:56:25 +02001407 self->memo = _Unpickler_NewMemo(self->memo_size);
1408 self->stack = (Pdata *)Pdata_New();
1409
1410 if (self->memo == NULL || self->stack == NULL) {
1411 Py_DECREF(self);
1412 return NULL;
1413 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001414
1415 return self;
1416}
1417
1418/* Returns -1 (with an exception set) on failure, 0 on success. This may
1419 be called once on a freshly created Pickler. */
1420static int
1421_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
1422{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001423 _Py_IDENTIFIER(peek);
1424 _Py_IDENTIFIER(read);
1425 _Py_IDENTIFIER(readline);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001426
1427 self->peek = _PyObject_GetAttrId(file, &PyId_peek);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001428 if (self->peek == NULL) {
1429 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1430 PyErr_Clear();
1431 else
1432 return -1;
1433 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02001434 self->read = _PyObject_GetAttrId(file, &PyId_read);
1435 self->readline = _PyObject_GetAttrId(file, &PyId_readline);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001436 if (self->readline == NULL || self->read == NULL) {
1437 if (PyErr_ExceptionMatches(PyExc_AttributeError))
1438 PyErr_SetString(PyExc_TypeError,
1439 "file must have 'read' and 'readline' attributes");
1440 Py_CLEAR(self->read);
1441 Py_CLEAR(self->readline);
Antoine Pitrou04248a82010-10-12 20:51:21 +00001442 Py_CLEAR(self->peek);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001443 return -1;
1444 }
1445 return 0;
1446}
1447
1448/* Returns -1 (with an exception set) on failure, 0 on success. This may
1449 be called once on a freshly created Pickler. */
1450static int
1451_Unpickler_SetInputEncoding(UnpicklerObject *self,
1452 const char *encoding,
1453 const char *errors)
1454{
1455 if (encoding == NULL)
1456 encoding = "ASCII";
1457 if (errors == NULL)
1458 errors = "strict";
1459
Victor Stinner49fc8ec2013-07-07 23:30:24 +02001460 self->encoding = _PyMem_Strdup(encoding);
1461 self->errors = _PyMem_Strdup(errors);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001462 if (self->encoding == NULL || self->errors == NULL) {
1463 PyErr_NoMemory();
1464 return -1;
1465 }
1466 return 0;
1467}
1468
1469/* Generate a GET opcode for an object stored in the memo. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001470static int
1471memo_get(PicklerObject *self, PyObject *key)
1472{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001473 Py_ssize_t *value;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001474 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001475 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001476
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001477 value = PyMemoTable_Get(self->memo, key);
1478 if (value == NULL) {
1479 PyErr_SetObject(PyExc_KeyError, key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001480 return -1;
1481 }
1482
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001483 if (!self->bin) {
1484 pdata[0] = GET;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001485 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
1486 "%" PY_FORMAT_SIZE_T "d\n", *value);
1487 len = strlen(pdata);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001488 }
1489 else {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001490 if (*value < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001491 pdata[0] = BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001492 pdata[1] = (unsigned char)(*value & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001493 len = 2;
1494 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001495 else if ((size_t)*value <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001496 pdata[0] = LONG_BINGET;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001497 pdata[1] = (unsigned char)(*value & 0xff);
1498 pdata[2] = (unsigned char)((*value >> 8) & 0xff);
1499 pdata[3] = (unsigned char)((*value >> 16) & 0xff);
1500 pdata[4] = (unsigned char)((*value >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001501 len = 5;
1502 }
1503 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001504 PickleState *st = _Pickle_GetGlobalState();
1505 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001506 "memo id too large for LONG_BINGET");
1507 return -1;
1508 }
1509 }
1510
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001511 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001512 return -1;
1513
1514 return 0;
1515}
1516
1517/* Store an object in the memo, assign it a new unique ID based on the number
1518 of objects currently stored in the memo and generate a PUT opcode. */
1519static int
1520memo_put(PicklerObject *self, PyObject *obj)
1521{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001522 char pdata[30];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001523 Py_ssize_t len;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001524 Py_ssize_t idx;
1525
1526 const char memoize_op = MEMOIZE;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001527
1528 if (self->fast)
1529 return 0;
1530
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001531 idx = PyMemoTable_Size(self->memo);
1532 if (PyMemoTable_Set(self->memo, obj, idx) < 0)
1533 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001534
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001535 if (self->proto >= 4) {
1536 if (_Pickler_Write(self, &memoize_op, 1) < 0)
1537 return -1;
1538 return 0;
1539 }
1540 else if (!self->bin) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001541 pdata[0] = PUT;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001542 PyOS_snprintf(pdata + 1, sizeof(pdata) - 1,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001543 "%" PY_FORMAT_SIZE_T "d\n", idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001544 len = strlen(pdata);
1545 }
1546 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001547 if (idx < 256) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001548 pdata[0] = BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001549 pdata[1] = (unsigned char)idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001550 len = 2;
1551 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03001552 else if ((size_t)idx <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001553 pdata[0] = LONG_BINPUT;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001554 pdata[1] = (unsigned char)(idx & 0xff);
1555 pdata[2] = (unsigned char)((idx >> 8) & 0xff);
1556 pdata[3] = (unsigned char)((idx >> 16) & 0xff);
1557 pdata[4] = (unsigned char)((idx >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001558 len = 5;
1559 }
1560 else { /* unlikely */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08001561 PickleState *st = _Pickle_GetGlobalState();
1562 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001563 "memo id too large for LONG_BINPUT");
1564 return -1;
1565 }
1566 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001567 if (_Pickler_Write(self, pdata, len) < 0)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001568 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001569
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001570 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001571}
1572
1573static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001574get_dotted_path(PyObject *obj, PyObject *name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001575 _Py_static_string(PyId_dot, ".");
1576 _Py_static_string(PyId_locals, "<locals>");
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001577 PyObject *dotted_path;
1578 Py_ssize_t i, n;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001579
1580 dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001581 if (dotted_path == NULL)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001582 return NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001583 n = PyList_GET_SIZE(dotted_path);
1584 assert(n >= 1);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001585 for (i = 0; i < n; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001586 PyObject *subpath = PyList_GET_ITEM(dotted_path, i);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001587 PyObject *result = PyUnicode_RichCompare(
1588 subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ);
1589 int is_equal = (result == Py_True);
1590 assert(PyBool_Check(result));
1591 Py_DECREF(result);
1592 if (is_equal) {
Antoine Pitrou6cd5eda2014-12-02 00:20:03 +01001593 if (obj == NULL)
1594 PyErr_Format(PyExc_AttributeError,
1595 "Can't pickle local object %R", name);
1596 else
1597 PyErr_Format(PyExc_AttributeError,
1598 "Can't pickle local attribute %R on %R", name, obj);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001599 Py_DECREF(dotted_path);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001600 return NULL;
1601 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001602 }
1603 return dotted_path;
1604}
1605
1606static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001607get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent)
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001608{
1609 Py_ssize_t i, n;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001610 PyObject *parent = NULL;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001611
1612 assert(PyList_CheckExact(names));
1613 Py_INCREF(obj);
1614 n = PyList_GET_SIZE(names);
1615 for (i = 0; i < n; i++) {
1616 PyObject *name = PyList_GET_ITEM(names, i);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001617 Py_XDECREF(parent);
1618 parent = obj;
1619 obj = PyObject_GetAttr(parent, name);
1620 if (obj == NULL) {
1621 Py_DECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001622 return NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001623 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001624 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001625 if (pparent != NULL)
1626 *pparent = parent;
1627 else
1628 Py_XDECREF(parent);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001629 return obj;
1630}
1631
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001632static void
1633reformat_attribute_error(PyObject *obj, PyObject *name)
1634{
1635 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1636 PyErr_Clear();
1637 PyErr_Format(PyExc_AttributeError,
1638 "Can't get attribute %R on %R", name, obj);
1639 }
1640}
1641
1642
1643static PyObject *
1644getattribute(PyObject *obj, PyObject *name, int allow_qualname)
1645{
1646 PyObject *dotted_path, *attr;
1647
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001648 if (allow_qualname) {
1649 dotted_path = get_dotted_path(obj, name);
1650 if (dotted_path == NULL)
1651 return NULL;
1652 attr = get_deep_attribute(obj, dotted_path, NULL);
1653 Py_DECREF(dotted_path);
1654 }
1655 else
1656 attr = PyObject_GetAttr(obj, name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001657 if (attr == NULL)
1658 reformat_attribute_error(obj, name);
1659 return attr;
1660}
1661
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001662static PyObject *
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001663whichmodule(PyObject *global, PyObject *dotted_path)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001664{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001665 PyObject *module_name;
1666 PyObject *modules_dict;
1667 PyObject *module;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001668 Py_ssize_t i;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001669 _Py_IDENTIFIER(__module__);
1670 _Py_IDENTIFIER(modules);
1671 _Py_IDENTIFIER(__main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001672
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001673 module_name = _PyObject_GetAttrId(global, &PyId___module__);
1674
1675 if (module_name == NULL) {
1676 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001677 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001678 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001679 }
1680 else {
1681 /* In some rare cases (e.g., bound methods of extension types),
1682 __module__ can be None. If it is so, then search sys.modules for
1683 the module of global. */
1684 if (module_name != Py_None)
1685 return module_name;
1686 Py_CLEAR(module_name);
1687 }
1688 assert(module_name == NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001689
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001690 /* Fallback on walking sys.modules */
Victor Stinnerbb520202013-11-06 22:40:41 +01001691 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02001692 if (modules_dict == NULL) {
1693 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001694 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02001695 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001696
1697 i = 0;
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001698 while (PyDict_Next(modules_dict, &i, &module_name, &module)) {
1699 PyObject *candidate;
1700 if (PyUnicode_Check(module_name) &&
1701 !PyUnicode_CompareWithASCIIString(module_name, "__main__"))
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001702 continue;
1703 if (module == Py_None)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001704 continue;
1705
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001706 candidate = get_deep_attribute(module, dotted_path, NULL);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001707 if (candidate == NULL) {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03001708 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001709 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001710 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001711 continue;
1712 }
1713
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001714 if (candidate == global) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001715 Py_INCREF(module_name);
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001716 Py_DECREF(candidate);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001717 return module_name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001718 }
Antoine Pitroufce60ea2014-10-23 22:47:50 +02001719 Py_DECREF(candidate);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001720 }
1721
1722 /* If no module is found, use __main__. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01001723 module_name = _PyUnicode_FromId(&PyId___main__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001724 Py_INCREF(module_name);
1725 return module_name;
1726}
1727
1728/* fast_save_enter() and fast_save_leave() are guards against recursive
1729 objects when Pickler is used with the "fast mode" (i.e., with object
1730 memoization disabled). If the nesting of a list or dict object exceed
1731 FAST_NESTING_LIMIT, these guards will start keeping an internal
1732 reference to the seen list or dict objects and check whether these objects
1733 are recursive. These are not strictly necessary, since save() has a
1734 hard-coded recursion limit, but they give a nicer error message than the
1735 typical RuntimeError. */
1736static int
1737fast_save_enter(PicklerObject *self, PyObject *obj)
1738{
1739 /* if fast_nesting < 0, we're doing an error exit. */
1740 if (++self->fast_nesting >= FAST_NESTING_LIMIT) {
1741 PyObject *key = NULL;
1742 if (self->fast_memo == NULL) {
1743 self->fast_memo = PyDict_New();
1744 if (self->fast_memo == NULL) {
1745 self->fast_nesting = -1;
1746 return 0;
1747 }
1748 }
1749 key = PyLong_FromVoidPtr(obj);
1750 if (key == NULL)
1751 return 0;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001752 if (PyDict_GetItemWithError(self->fast_memo, key)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001753 Py_DECREF(key);
1754 PyErr_Format(PyExc_ValueError,
1755 "fast mode: can't pickle cyclic objects "
1756 "including object type %.200s at %p",
1757 obj->ob_type->tp_name, obj);
1758 self->fast_nesting = -1;
1759 return 0;
1760 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08001761 if (PyErr_Occurred()) {
1762 return 0;
1763 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001764 if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) {
1765 Py_DECREF(key);
1766 self->fast_nesting = -1;
1767 return 0;
1768 }
1769 Py_DECREF(key);
1770 }
1771 return 1;
1772}
1773
1774static int
1775fast_save_leave(PicklerObject *self, PyObject *obj)
1776{
1777 if (self->fast_nesting-- >= FAST_NESTING_LIMIT) {
1778 PyObject *key = PyLong_FromVoidPtr(obj);
1779 if (key == NULL)
1780 return 0;
1781 if (PyDict_DelItem(self->fast_memo, key) < 0) {
1782 Py_DECREF(key);
1783 return 0;
1784 }
1785 Py_DECREF(key);
1786 }
1787 return 1;
1788}
1789
1790static int
1791save_none(PicklerObject *self, PyObject *obj)
1792{
1793 const char none_op = NONE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001794 if (_Pickler_Write(self, &none_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001795 return -1;
1796
1797 return 0;
1798}
1799
1800static int
1801save_bool(PicklerObject *self, PyObject *obj)
1802{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001803 if (self->proto >= 2) {
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001804 const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001805 if (_Pickler_Write(self, &bool_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001806 return -1;
1807 }
Alexandre Vassalotti8a67f522013-11-24 21:40:18 -08001808 else {
1809 /* These aren't opcodes -- they're ways to pickle bools before protocol 2
1810 * so that unpicklers written before bools were introduced unpickle them
1811 * as ints, but unpicklers after can recognize that bools were intended.
1812 * Note that protocol 2 added direct ways to pickle bools.
1813 */
1814 const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n";
1815 if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0)
1816 return -1;
1817 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001818 return 0;
1819}
1820
1821static int
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001822save_long(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001823{
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001824 PyObject *repr = NULL;
1825 Py_ssize_t size;
1826 long val;
1827 int status = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001828
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001829 const char long_op = LONG;
1830
1831 val= PyLong_AsLong(obj);
1832 if (val == -1 && PyErr_Occurred()) {
1833 /* out of range for int pickling */
1834 PyErr_Clear();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001835 }
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001836 else if (self->bin &&
1837 (sizeof(long) <= 4 ||
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001838 (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
Larry Hastings61272b72014-01-07 12:41:53 -08001839 /* result fits in a signed 4-byte integer.
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08001840
1841 Note: we can't use -0x80000000L in the above condition because some
1842 compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type
1843 before applying the unary minus when sizeof(long) <= 4. The
1844 resulting value stays unsigned which is commonly not what we want,
1845 so MSVC happily warns us about it. However, that result would have
1846 been fine because we guard for sizeof(long) <= 4 which turns the
1847 condition true in that particular case. */
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001848 char pdata[32];
1849 Py_ssize_t len = 0;
1850
1851 pdata[1] = (unsigned char)(val & 0xff);
1852 pdata[2] = (unsigned char)((val >> 8) & 0xff);
1853 pdata[3] = (unsigned char)((val >> 16) & 0xff);
1854 pdata[4] = (unsigned char)((val >> 24) & 0xff);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001855
1856 if ((pdata[4] == 0) && (pdata[3] == 0)) {
1857 if (pdata[2] == 0) {
1858 pdata[0] = BININT1;
1859 len = 2;
1860 }
1861 else {
1862 pdata[0] = BININT2;
1863 len = 3;
1864 }
1865 }
1866 else {
1867 pdata[0] = BININT;
1868 len = 5;
1869 }
1870
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001871 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001872 return -1;
Alexandre Vassalottided929b2013-11-24 22:41:13 -08001873
1874 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001875 }
1876
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001877 if (self->proto >= 2) {
1878 /* Linear-time pickling. */
1879 size_t nbits;
1880 size_t nbytes;
1881 unsigned char *pdata;
1882 char header[5];
1883 int i;
1884 int sign = _PyLong_Sign(obj);
1885
1886 if (sign == 0) {
1887 header[0] = LONG1;
1888 header[1] = 0; /* It's 0 -- an empty bytestring. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001889 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001890 goto error;
1891 return 0;
1892 }
1893 nbits = _PyLong_NumBits(obj);
1894 if (nbits == (size_t)-1 && PyErr_Occurred())
1895 goto error;
1896 /* How many bytes do we need? There are nbits >> 3 full
1897 * bytes of data, and nbits & 7 leftover bits. If there
1898 * are any leftover bits, then we clearly need another
1899 * byte. Wnat's not so obvious is that we *probably*
1900 * need another byte even if there aren't any leftovers:
1901 * the most-significant bit of the most-significant byte
1902 * acts like a sign bit, and it's usually got a sense
Serhiy Storchaka95949422013-08-27 19:40:23 +03001903 * opposite of the one we need. The exception is ints
1904 * of the form -(2**(8*j-1)) for j > 0. Such an int is
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001905 * its own 256's-complement, so has the right sign bit
1906 * even without the extra byte. That's a pain to check
1907 * for in advance, though, so we always grab an extra
1908 * byte at the start, and cut it back later if possible.
1909 */
1910 nbytes = (nbits >> 3) + 1;
Antoine Pitroubf6ecf92012-11-24 20:40:21 +01001911 if (nbytes > 0x7fffffffL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001912 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchaka95949422013-08-27 19:40:23 +03001913 "int too large to pickle");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001914 goto error;
1915 }
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001916 repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001917 if (repr == NULL)
1918 goto error;
Neal Norwitz6ae2eb22008-08-24 23:50:08 +00001919 pdata = (unsigned char *)PyBytes_AS_STRING(repr);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001920 i = _PyLong_AsByteArray((PyLongObject *)obj,
1921 pdata, nbytes,
1922 1 /* little endian */ , 1 /* signed */ );
1923 if (i < 0)
1924 goto error;
Serhiy Storchaka95949422013-08-27 19:40:23 +03001925 /* If the int is negative, this may be a byte more than
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001926 * needed. This is so iff the MSB is all redundant sign
1927 * bits.
1928 */
1929 if (sign < 0 &&
Victor Stinner121aab42011-09-29 23:40:53 +02001930 nbytes > 1 &&
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001931 pdata[nbytes - 1] == 0xff &&
1932 (pdata[nbytes - 2] & 0x80) != 0) {
1933 nbytes--;
1934 }
1935
1936 if (nbytes < 256) {
1937 header[0] = LONG1;
1938 header[1] = (unsigned char)nbytes;
1939 size = 2;
1940 }
1941 else {
1942 header[0] = LONG4;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02001943 size = (Py_ssize_t) nbytes;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001944 for (i = 1; i < 5; i++) {
1945 header[i] = (unsigned char)(size & 0xff);
1946 size >>= 8;
1947 }
1948 size = 5;
1949 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001950 if (_Pickler_Write(self, header, size) < 0 ||
1951 _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001952 goto error;
1953 }
1954 else {
1955 char *string;
1956
Mark Dickinson8dd05142009-01-20 20:43:58 +00001957 /* proto < 2: write the repr and newline. This is quadratic-time (in
1958 the number of digits), in both directions. We add a trailing 'L'
1959 to the repr, for compatibility with Python 2.x. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001960
1961 repr = PyObject_Repr(obj);
1962 if (repr == NULL)
1963 goto error;
1964
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001965 string = _PyUnicode_AsStringAndSize(repr, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001966 if (string == NULL)
1967 goto error;
1968
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001969 if (_Pickler_Write(self, &long_op, 1) < 0 ||
1970 _Pickler_Write(self, string, size) < 0 ||
1971 _Pickler_Write(self, "L\n", 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001972 goto error;
1973 }
1974
1975 if (0) {
1976 error:
1977 status = -1;
1978 }
1979 Py_XDECREF(repr);
1980
1981 return status;
1982}
1983
1984static int
1985save_float(PicklerObject *self, PyObject *obj)
1986{
1987 double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj);
1988
1989 if (self->bin) {
1990 char pdata[9];
1991 pdata[0] = BINFLOAT;
1992 if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0)
1993 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00001994 if (_Pickler_Write(self, pdata, 9) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001995 return -1;
Victor Stinner121aab42011-09-29 23:40:53 +02001996 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00001997 else {
Eric Smith0923d1d2009-04-16 20:16:10 +00001998 int result = -1;
1999 char *buf = NULL;
2000 char op = FLOAT;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002001
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002002 if (_Pickler_Write(self, &op, 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002003 goto done;
2004
Serhiy Storchakac86ca262015-02-15 14:18:32 +02002005 buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
Eric Smith0923d1d2009-04-16 20:16:10 +00002006 if (!buf) {
2007 PyErr_NoMemory();
2008 goto done;
2009 }
2010
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002011 if (_Pickler_Write(self, buf, strlen(buf)) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002012 goto done;
2013
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002014 if (_Pickler_Write(self, "\n", 1) < 0)
Eric Smith0923d1d2009-04-16 20:16:10 +00002015 goto done;
2016
2017 result = 0;
2018done:
2019 PyMem_Free(buf);
2020 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002021 }
2022
2023 return 0;
2024}
2025
2026static int
2027save_bytes(PicklerObject *self, PyObject *obj)
2028{
2029 if (self->proto < 3) {
2030 /* Older pickle protocols do not have an opcode for pickling bytes
2031 objects. Therefore, we need to fake the copy protocol (i.e.,
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002032 the __reduce__ method) to permit bytes object unpickling.
2033
2034 Here we use a hack to be compatible with Python 2. Since in Python
2035 2 'bytes' is just an alias for 'str' (which has different
2036 parameters than the actual bytes object), we use codecs.encode
2037 to create the appropriate 'str' object when unpickled using
2038 Python 2 *and* the appropriate 'bytes' object when unpickled
2039 using Python 3. Again this is a hack and we don't need to do this
2040 with newer protocols. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002041 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002042 int status;
2043
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002044 if (PyBytes_GET_SIZE(obj) == 0) {
2045 reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type);
2046 }
2047 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002048 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002049 PyObject *unicode_str =
2050 PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj),
2051 PyBytes_GET_SIZE(obj),
2052 "strict");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002053 _Py_IDENTIFIER(latin1);
2054
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002055 if (unicode_str == NULL)
2056 return -1;
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002057 reduce_value = Py_BuildValue("(O(OO))",
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08002058 st->codecs_encode, unicode_str,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002059 _PyUnicode_FromId(&PyId_latin1));
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002060 Py_DECREF(unicode_str);
2061 }
2062
2063 if (reduce_value == NULL)
2064 return -1;
2065
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002066 /* save_reduce() will memoize the object automatically. */
2067 status = save_reduce(self, reduce_value, obj);
2068 Py_DECREF(reduce_value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002069 return status;
2070 }
2071 else {
2072 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002073 char header[9];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002074 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002075
Alexandre Vassalotti3bfc65a2011-12-13 13:08:09 -05002076 size = PyBytes_GET_SIZE(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002077 if (size < 0)
2078 return -1;
2079
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002080 if (size <= 0xff) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002081 header[0] = SHORT_BINBYTES;
2082 header[1] = (unsigned char)size;
2083 len = 2;
2084 }
Serhiy Storchaka67c719b2014-09-05 10:10:23 +03002085 else if ((size_t)size <= 0xffffffffUL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002086 header[0] = BINBYTES;
2087 header[1] = (unsigned char)(size & 0xff);
2088 header[2] = (unsigned char)((size >> 8) & 0xff);
2089 header[3] = (unsigned char)((size >> 16) & 0xff);
2090 header[4] = (unsigned char)((size >> 24) & 0xff);
2091 len = 5;
2092 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002093 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002094 header[0] = BINBYTES8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002095 _write_size64(header + 1, size);
Alexandre Vassalotti6e73ff12013-12-05 19:29:32 -08002096 len = 9;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002097 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002098 else {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002099 PyErr_SetString(PyExc_OverflowError,
Serhiy Storchakaf8def282013-02-16 17:29:56 +02002100 "cannot serialize a bytes object larger than 4 GiB");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002101 return -1; /* string too large */
2102 }
2103
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002104 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002105 return -1;
2106
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002107 if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002108 return -1;
2109
2110 if (memo_put(self, obj) < 0)
2111 return -1;
2112
2113 return 0;
2114 }
2115}
2116
2117/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
2118 backslash and newline characters to \uXXXX escapes. */
2119static PyObject *
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002120raw_unicode_escape(PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002121{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002122 char *p;
Victor Stinner049e5092014-08-17 22:20:00 +02002123 Py_ssize_t i, size;
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002124 void *data;
2125 unsigned int kind;
Victor Stinner358af132015-10-12 22:36:57 +02002126 _PyBytesWriter writer;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002127
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002128 if (PyUnicode_READY(obj))
2129 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002130
Victor Stinner358af132015-10-12 22:36:57 +02002131 _PyBytesWriter_Init(&writer);
2132
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002133 size = PyUnicode_GET_LENGTH(obj);
2134 data = PyUnicode_DATA(obj);
2135 kind = PyUnicode_KIND(obj);
Victor Stinner121aab42011-09-29 23:40:53 +02002136
Victor Stinner358af132015-10-12 22:36:57 +02002137 p = _PyBytesWriter_Alloc(&writer, size);
2138 if (p == NULL)
2139 goto error;
2140 writer.overallocate = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002141
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002142 for (i=0; i < size; i++) {
2143 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002144 /* Map 32-bit characters to '\Uxxxxxxxx' */
2145 if (ch >= 0x10000) {
Victor Stinner358af132015-10-12 22:36:57 +02002146 /* -1: substract 1 preallocated byte */
2147 p = _PyBytesWriter_Prepare(&writer, p, 10-1);
2148 if (p == NULL)
2149 goto error;
2150
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002151 *p++ = '\\';
2152 *p++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002153 *p++ = Py_hexdigits[(ch >> 28) & 0xf];
2154 *p++ = Py_hexdigits[(ch >> 24) & 0xf];
2155 *p++ = Py_hexdigits[(ch >> 20) & 0xf];
2156 *p++ = Py_hexdigits[(ch >> 16) & 0xf];
2157 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2158 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2159 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2160 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002161 }
Victor Stinner358af132015-10-12 22:36:57 +02002162 /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002163 else if (ch >= 256 || ch == '\\' || ch == '\n') {
Victor Stinner358af132015-10-12 22:36:57 +02002164 /* -1: substract 1 preallocated byte */
2165 p = _PyBytesWriter_Prepare(&writer, p, 6-1);
2166 if (p == NULL)
2167 goto error;
2168
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002169 *p++ = '\\';
2170 *p++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +02002171 *p++ = Py_hexdigits[(ch >> 12) & 0xf];
2172 *p++ = Py_hexdigits[(ch >> 8) & 0xf];
2173 *p++ = Py_hexdigits[(ch >> 4) & 0xf];
2174 *p++ = Py_hexdigits[ch & 15];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002175 }
Alexandre Vassalotti554d8782008-12-27 07:32:41 +00002176 /* Copy everything else as-is */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002177 else
2178 *p++ = (char) ch;
2179 }
Victor Stinner358af132015-10-12 22:36:57 +02002180
2181 return _PyBytesWriter_Finish(&writer, p);
2182
2183error:
2184 _PyBytesWriter_Dealloc(&writer);
2185 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002186}
2187
2188static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002189write_utf8(PicklerObject *self, const char *data, Py_ssize_t size)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002190{
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002191 char header[9];
2192 Py_ssize_t len;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002193
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002194 assert(size >= 0);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002195 if (size <= 0xff && self->proto >= 4) {
2196 header[0] = SHORT_BINUNICODE;
2197 header[1] = (unsigned char)(size & 0xff);
2198 len = 2;
2199 }
Victor Stinnerf13c46c2014-08-17 21:05:55 +02002200 else if ((size_t)size <= 0xffffffffUL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002201 header[0] = BINUNICODE;
2202 header[1] = (unsigned char)(size & 0xff);
2203 header[2] = (unsigned char)((size >> 8) & 0xff);
2204 header[3] = (unsigned char)((size >> 16) & 0xff);
2205 header[4] = (unsigned char)((size >> 24) & 0xff);
2206 len = 5;
2207 }
2208 else if (self->proto >= 4) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002209 header[0] = BINUNICODE8;
Alexandre Vassalotti1048fb52013-11-25 11:35:46 -08002210 _write_size64(header + 1, size);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002211 len = 9;
2212 }
2213 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002214 PyErr_SetString(PyExc_OverflowError,
Antoine Pitrou4b7b0f02013-04-07 23:46:52 +02002215 "cannot serialize a string larger than 4GiB");
Antoine Pitrou299978d2013-04-07 17:38:11 +02002216 return -1;
2217 }
Antoine Pitrou299978d2013-04-07 17:38:11 +02002218
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002219 if (_Pickler_Write(self, header, len) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002220 return -1;
Antoine Pitrou299978d2013-04-07 17:38:11 +02002221 if (_Pickler_Write(self, data, size) < 0)
2222 return -1;
2223
2224 return 0;
2225}
2226
2227static int
2228write_unicode_binary(PicklerObject *self, PyObject *obj)
2229{
2230 PyObject *encoded = NULL;
2231 Py_ssize_t size;
2232 char *data;
2233 int r;
2234
2235 if (PyUnicode_READY(obj))
2236 return -1;
2237
2238 data = PyUnicode_AsUTF8AndSize(obj, &size);
2239 if (data != NULL)
2240 return write_utf8(self, data, size);
2241
2242 /* Issue #8383: for strings with lone surrogates, fallback on the
2243 "surrogatepass" error handler. */
2244 PyErr_Clear();
2245 encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass");
2246 if (encoded == NULL)
2247 return -1;
2248
2249 r = write_utf8(self, PyBytes_AS_STRING(encoded),
2250 PyBytes_GET_SIZE(encoded));
2251 Py_DECREF(encoded);
2252 return r;
2253}
2254
2255static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002256save_unicode(PicklerObject *self, PyObject *obj)
2257{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002258 if (self->bin) {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002259 if (write_unicode_binary(self, obj) < 0)
2260 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002261 }
2262 else {
Antoine Pitrou299978d2013-04-07 17:38:11 +02002263 PyObject *encoded;
2264 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002265 const char unicode_op = UNICODE;
2266
Victor Stinnerc806fdc2011-09-29 23:50:23 +02002267 encoded = raw_unicode_escape(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002268 if (encoded == NULL)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002269 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002270
Antoine Pitrou299978d2013-04-07 17:38:11 +02002271 if (_Pickler_Write(self, &unicode_op, 1) < 0) {
2272 Py_DECREF(encoded);
2273 return -1;
2274 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002275
2276 size = PyBytes_GET_SIZE(encoded);
Antoine Pitrou299978d2013-04-07 17:38:11 +02002277 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) {
2278 Py_DECREF(encoded);
2279 return -1;
2280 }
2281 Py_DECREF(encoded);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002282
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002283 if (_Pickler_Write(self, "\n", 1) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002284 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002285 }
2286 if (memo_put(self, obj) < 0)
Antoine Pitrou299978d2013-04-07 17:38:11 +02002287 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002288
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002289 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002290}
2291
2292/* A helper for save_tuple. Push the len elements in tuple t on the stack. */
2293static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002294store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002295{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002296 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002297
2298 assert(PyTuple_Size(t) == len);
2299
2300 for (i = 0; i < len; i++) {
2301 PyObject *element = PyTuple_GET_ITEM(t, i);
2302
2303 if (element == NULL)
2304 return -1;
2305 if (save(self, element, 0) < 0)
2306 return -1;
2307 }
2308
2309 return 0;
2310}
2311
2312/* Tuples are ubiquitous in the pickle protocols, so many techniques are
2313 * used across protocols to minimize the space needed to pickle them.
2314 * Tuples are also the only builtin immutable type that can be recursive
2315 * (a tuple can be reached from itself), and that requires some subtle
2316 * magic so that it works in all cases. IOW, this is a long routine.
2317 */
2318static int
2319save_tuple(PicklerObject *self, PyObject *obj)
2320{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002321 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002322
2323 const char mark_op = MARK;
2324 const char tuple_op = TUPLE;
2325 const char pop_op = POP;
2326 const char pop_mark_op = POP_MARK;
2327 const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3};
2328
2329 if ((len = PyTuple_Size(obj)) < 0)
2330 return -1;
2331
2332 if (len == 0) {
2333 char pdata[2];
2334
2335 if (self->proto) {
2336 pdata[0] = EMPTY_TUPLE;
2337 len = 1;
2338 }
2339 else {
2340 pdata[0] = MARK;
2341 pdata[1] = TUPLE;
2342 len = 2;
2343 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002344 if (_Pickler_Write(self, pdata, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002345 return -1;
2346 return 0;
2347 }
2348
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002349 /* The tuple isn't in the memo now. If it shows up there after
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002350 * saving the tuple elements, the tuple must be recursive, in
2351 * which case we'll pop everything we put on the stack, and fetch
2352 * its value from the memo.
2353 */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002354 if (len <= 3 && self->proto >= 2) {
2355 /* Use TUPLE{1,2,3} opcodes. */
2356 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002357 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002358
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002359 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002360 /* pop the len elements */
2361 for (i = 0; i < len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002362 if (_Pickler_Write(self, &pop_op, 1) < 0)
2363 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002364 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002365 if (memo_get(self, obj) < 0)
2366 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002367
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002368 return 0;
2369 }
2370 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002371 if (_Pickler_Write(self, len2opcode + len, 1) < 0)
2372 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002373 }
2374 goto memoize;
2375 }
2376
2377 /* proto < 2 and len > 0, or proto >= 2 and len > 3.
2378 * Generate MARK e1 e2 ... TUPLE
2379 */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002380 if (_Pickler_Write(self, &mark_op, 1) < 0)
2381 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002382
2383 if (store_tuple_elements(self, obj, len) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002384 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002385
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002386 if (PyMemoTable_Get(self->memo, obj)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002387 /* pop the stack stuff we pushed */
2388 if (self->bin) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002389 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
2390 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002391 }
2392 else {
2393 /* Note that we pop one more than len, to remove
2394 * the MARK too.
2395 */
2396 for (i = 0; i <= len; i++)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002397 if (_Pickler_Write(self, &pop_op, 1) < 0)
2398 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002399 }
2400 /* fetch from memo */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002401 if (memo_get(self, obj) < 0)
2402 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002403
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002404 return 0;
2405 }
2406 else { /* Not recursive. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002407 if (_Pickler_Write(self, &tuple_op, 1) < 0)
2408 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002409 }
2410
2411 memoize:
2412 if (memo_put(self, obj) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002413 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002415 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002416}
2417
2418/* iter is an iterator giving items, and we batch up chunks of
2419 * MARK item item ... item APPENDS
2420 * opcode sequences. Calling code should have arranged to first create an
2421 * empty list, or list-like object, for the APPENDS to operate on.
2422 * Returns 0 on success, <0 on error.
2423 */
2424static int
2425batch_list(PicklerObject *self, PyObject *iter)
2426{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002427 PyObject *obj = NULL;
2428 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002429 int i, n;
2430
2431 const char mark_op = MARK;
2432 const char append_op = APPEND;
2433 const char appends_op = APPENDS;
2434
2435 assert(iter != NULL);
2436
2437 /* XXX: I think this function could be made faster by avoiding the
2438 iterator interface and fetching objects directly from list using
2439 PyList_GET_ITEM.
2440 */
2441
2442 if (self->proto == 0) {
2443 /* APPENDS isn't available; do one at a time. */
2444 for (;;) {
2445 obj = PyIter_Next(iter);
2446 if (obj == NULL) {
2447 if (PyErr_Occurred())
2448 return -1;
2449 break;
2450 }
2451 i = save(self, obj, 0);
2452 Py_DECREF(obj);
2453 if (i < 0)
2454 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002455 if (_Pickler_Write(self, &append_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002456 return -1;
2457 }
2458 return 0;
2459 }
2460
2461 /* proto > 0: write in batches of BATCHSIZE. */
2462 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002463 /* Get first item */
2464 firstitem = PyIter_Next(iter);
2465 if (firstitem == NULL) {
2466 if (PyErr_Occurred())
2467 goto error;
2468
2469 /* nothing more to add */
2470 break;
2471 }
2472
2473 /* Try to get a second item */
2474 obj = PyIter_Next(iter);
2475 if (obj == NULL) {
2476 if (PyErr_Occurred())
2477 goto error;
2478
2479 /* Only one item to write */
2480 if (save(self, firstitem, 0) < 0)
2481 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002482 if (_Pickler_Write(self, &append_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002483 goto error;
2484 Py_CLEAR(firstitem);
2485 break;
2486 }
2487
2488 /* More than one item to write */
2489
2490 /* Pump out MARK, items, APPENDS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002491 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002492 goto error;
2493
2494 if (save(self, firstitem, 0) < 0)
2495 goto error;
2496 Py_CLEAR(firstitem);
2497 n = 1;
2498
2499 /* Fetch and save up to BATCHSIZE items */
2500 while (obj) {
2501 if (save(self, obj, 0) < 0)
2502 goto error;
2503 Py_CLEAR(obj);
2504 n += 1;
2505
2506 if (n == BATCHSIZE)
2507 break;
2508
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002509 obj = PyIter_Next(iter);
2510 if (obj == NULL) {
2511 if (PyErr_Occurred())
2512 goto error;
2513 break;
2514 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002515 }
2516
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002517 if (_Pickler_Write(self, &appends_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002518 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002519
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002520 } while (n == BATCHSIZE);
2521 return 0;
2522
2523 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002524 Py_XDECREF(firstitem);
2525 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002526 return -1;
2527}
2528
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002529/* This is a variant of batch_list() above, specialized for lists (with no
2530 * support for list subclasses). Like batch_list(), we batch up chunks of
2531 * MARK item item ... item APPENDS
2532 * opcode sequences. Calling code should have arranged to first create an
2533 * empty list, or list-like object, for the APPENDS to operate on.
2534 * Returns 0 on success, -1 on error.
2535 *
2536 * This version is considerably faster than batch_list(), if less general.
2537 *
2538 * Note that this only works for protocols > 0.
2539 */
2540static int
2541batch_list_exact(PicklerObject *self, PyObject *obj)
2542{
2543 PyObject *item = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002544 Py_ssize_t this_batch, total;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002545
2546 const char append_op = APPEND;
2547 const char appends_op = APPENDS;
2548 const char mark_op = MARK;
2549
2550 assert(obj != NULL);
2551 assert(self->proto > 0);
2552 assert(PyList_CheckExact(obj));
2553
2554 if (PyList_GET_SIZE(obj) == 1) {
2555 item = PyList_GET_ITEM(obj, 0);
2556 if (save(self, item, 0) < 0)
2557 return -1;
2558 if (_Pickler_Write(self, &append_op, 1) < 0)
2559 return -1;
2560 return 0;
2561 }
2562
2563 /* Write in batches of BATCHSIZE. */
2564 total = 0;
2565 do {
2566 this_batch = 0;
2567 if (_Pickler_Write(self, &mark_op, 1) < 0)
2568 return -1;
2569 while (total < PyList_GET_SIZE(obj)) {
2570 item = PyList_GET_ITEM(obj, total);
2571 if (save(self, item, 0) < 0)
2572 return -1;
2573 total++;
2574 if (++this_batch == BATCHSIZE)
2575 break;
2576 }
2577 if (_Pickler_Write(self, &appends_op, 1) < 0)
2578 return -1;
2579
2580 } while (total < PyList_GET_SIZE(obj));
2581
2582 return 0;
2583}
2584
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002585static int
2586save_list(PicklerObject *self, PyObject *obj)
2587{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002588 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002589 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002590 int status = 0;
2591
2592 if (self->fast && !fast_save_enter(self, obj))
2593 goto error;
2594
2595 /* Create an empty list. */
2596 if (self->bin) {
2597 header[0] = EMPTY_LIST;
2598 len = 1;
2599 }
2600 else {
2601 header[0] = MARK;
2602 header[1] = LIST;
2603 len = 2;
2604 }
2605
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002606 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002607 goto error;
2608
2609 /* Get list length, and bow out early if empty. */
2610 if ((len = PyList_Size(obj)) < 0)
2611 goto error;
2612
2613 if (memo_put(self, obj) < 0)
2614 goto error;
2615
2616 if (len != 0) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002617 /* Materialize the list elements. */
2618 if (PyList_CheckExact(obj) && self->proto > 0) {
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002619 if (Py_EnterRecursiveCall(" while pickling an object"))
2620 goto error;
2621 status = batch_list_exact(self, obj);
2622 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002623 } else {
2624 PyObject *iter = PyObject_GetIter(obj);
2625 if (iter == NULL)
2626 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002627
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002628 if (Py_EnterRecursiveCall(" while pickling an object")) {
2629 Py_DECREF(iter);
2630 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002631 }
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002632 status = batch_list(self, iter);
2633 Py_LeaveRecursiveCall();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002634 Py_DECREF(iter);
2635 }
2636 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002637 if (0) {
2638 error:
2639 status = -1;
2640 }
2641
2642 if (self->fast && !fast_save_leave(self, obj))
2643 status = -1;
2644
2645 return status;
2646}
2647
2648/* iter is an iterator giving (key, value) pairs, and we batch up chunks of
2649 * MARK key value ... key value SETITEMS
2650 * opcode sequences. Calling code should have arranged to first create an
2651 * empty dict, or dict-like object, for the SETITEMS to operate on.
2652 * Returns 0 on success, <0 on error.
2653 *
2654 * This is very much like batch_list(). The difference between saving
2655 * elements directly, and picking apart two-tuples, is so long-winded at
2656 * the C level, though, that attempts to combine these routines were too
2657 * ugly to bear.
2658 */
2659static int
2660batch_dict(PicklerObject *self, PyObject *iter)
2661{
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002662 PyObject *obj = NULL;
2663 PyObject *firstitem = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002664 int i, n;
2665
2666 const char mark_op = MARK;
2667 const char setitem_op = SETITEM;
2668 const char setitems_op = SETITEMS;
2669
2670 assert(iter != NULL);
2671
2672 if (self->proto == 0) {
2673 /* SETITEMS isn't available; do one at a time. */
2674 for (;;) {
2675 obj = PyIter_Next(iter);
2676 if (obj == NULL) {
2677 if (PyErr_Occurred())
2678 return -1;
2679 break;
2680 }
2681 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2682 PyErr_SetString(PyExc_TypeError, "dict items "
2683 "iterator must return 2-tuples");
2684 return -1;
2685 }
2686 i = save(self, PyTuple_GET_ITEM(obj, 0), 0);
2687 if (i >= 0)
2688 i = save(self, PyTuple_GET_ITEM(obj, 1), 0);
2689 Py_DECREF(obj);
2690 if (i < 0)
2691 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002692 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002693 return -1;
2694 }
2695 return 0;
2696 }
2697
2698 /* proto > 0: write in batches of BATCHSIZE. */
2699 do {
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002700 /* Get first item */
2701 firstitem = PyIter_Next(iter);
2702 if (firstitem == NULL) {
2703 if (PyErr_Occurred())
2704 goto error;
2705
2706 /* nothing more to add */
2707 break;
2708 }
2709 if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) {
2710 PyErr_SetString(PyExc_TypeError, "dict items "
2711 "iterator must return 2-tuples");
2712 goto error;
2713 }
2714
2715 /* Try to get a second item */
2716 obj = PyIter_Next(iter);
2717 if (obj == NULL) {
2718 if (PyErr_Occurred())
2719 goto error;
2720
2721 /* Only one item to write */
2722 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2723 goto error;
2724 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2725 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002726 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002727 goto error;
2728 Py_CLEAR(firstitem);
2729 break;
2730 }
2731
2732 /* More than one item to write */
2733
2734 /* Pump out MARK, items, SETITEMS. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002735 if (_Pickler_Write(self, &mark_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002736 goto error;
2737
2738 if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0)
2739 goto error;
2740 if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0)
2741 goto error;
2742 Py_CLEAR(firstitem);
2743 n = 1;
2744
2745 /* Fetch and save up to BATCHSIZE items */
2746 while (obj) {
2747 if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) {
2748 PyErr_SetString(PyExc_TypeError, "dict items "
2749 "iterator must return 2-tuples");
2750 goto error;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002751 }
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002752 if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 ||
2753 save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0)
2754 goto error;
2755 Py_CLEAR(obj);
2756 n += 1;
2757
2758 if (n == BATCHSIZE)
2759 break;
2760
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002761 obj = PyIter_Next(iter);
2762 if (obj == NULL) {
2763 if (PyErr_Occurred())
2764 goto error;
2765 break;
2766 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002767 }
2768
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002769 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002770 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002771
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002772 } while (n == BATCHSIZE);
2773 return 0;
2774
2775 error:
Amaury Forgeot d'Arcfb1a5eb2008-09-11 21:03:37 +00002776 Py_XDECREF(firstitem);
2777 Py_XDECREF(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002778 return -1;
2779}
2780
Collin Winter5c9b02d2009-05-25 05:43:30 +00002781/* This is a variant of batch_dict() above that specializes for dicts, with no
2782 * support for dict subclasses. Like batch_dict(), we batch up chunks of
2783 * MARK key value ... key value SETITEMS
2784 * opcode sequences. Calling code should have arranged to first create an
2785 * empty dict, or dict-like object, for the SETITEMS to operate on.
2786 * Returns 0 on success, -1 on error.
2787 *
2788 * Note that this currently doesn't work for protocol 0.
2789 */
2790static int
2791batch_dict_exact(PicklerObject *self, PyObject *obj)
2792{
2793 PyObject *key = NULL, *value = NULL;
2794 int i;
2795 Py_ssize_t dict_size, ppos = 0;
2796
Alexandre Vassalottif70b1292009-05-25 18:00:52 +00002797 const char mark_op = MARK;
2798 const char setitem_op = SETITEM;
2799 const char setitems_op = SETITEMS;
Collin Winter5c9b02d2009-05-25 05:43:30 +00002800
2801 assert(obj != NULL);
2802 assert(self->proto > 0);
2803
2804 dict_size = PyDict_Size(obj);
2805
2806 /* Special-case len(d) == 1 to save space. */
2807 if (dict_size == 1) {
2808 PyDict_Next(obj, &ppos, &key, &value);
2809 if (save(self, key, 0) < 0)
2810 return -1;
2811 if (save(self, value, 0) < 0)
2812 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002813 if (_Pickler_Write(self, &setitem_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002814 return -1;
2815 return 0;
2816 }
2817
2818 /* Write in batches of BATCHSIZE. */
2819 do {
2820 i = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002821 if (_Pickler_Write(self, &mark_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002822 return -1;
2823 while (PyDict_Next(obj, &ppos, &key, &value)) {
2824 if (save(self, key, 0) < 0)
2825 return -1;
2826 if (save(self, value, 0) < 0)
2827 return -1;
2828 if (++i == BATCHSIZE)
2829 break;
2830 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002831 if (_Pickler_Write(self, &setitems_op, 1) < 0)
Collin Winter5c9b02d2009-05-25 05:43:30 +00002832 return -1;
2833 if (PyDict_Size(obj) != dict_size) {
2834 PyErr_Format(
2835 PyExc_RuntimeError,
2836 "dictionary changed size during iteration");
2837 return -1;
2838 }
2839
2840 } while (i == BATCHSIZE);
2841 return 0;
2842}
2843
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002844static int
2845save_dict(PicklerObject *self, PyObject *obj)
2846{
2847 PyObject *items, *iter;
2848 char header[3];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02002849 Py_ssize_t len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002850 int status = 0;
2851
2852 if (self->fast && !fast_save_enter(self, obj))
2853 goto error;
2854
2855 /* Create an empty dict. */
2856 if (self->bin) {
2857 header[0] = EMPTY_DICT;
2858 len = 1;
2859 }
2860 else {
2861 header[0] = MARK;
2862 header[1] = DICT;
2863 len = 2;
2864 }
2865
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00002866 if (_Pickler_Write(self, header, len) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002867 goto error;
2868
2869 /* Get dict size, and bow out early if empty. */
2870 if ((len = PyDict_Size(obj)) < 0)
2871 goto error;
2872
2873 if (memo_put(self, obj) < 0)
2874 goto error;
2875
2876 if (len != 0) {
2877 /* Save the dict items. */
Collin Winter5c9b02d2009-05-25 05:43:30 +00002878 if (PyDict_CheckExact(obj) && self->proto > 0) {
2879 /* We can take certain shortcuts if we know this is a dict and
2880 not a dict subclass. */
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002881 if (Py_EnterRecursiveCall(" while pickling an object"))
2882 goto error;
2883 status = batch_dict_exact(self, obj);
2884 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002885 } else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02002886 _Py_IDENTIFIER(items);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02002887
2888 items = _PyObject_CallMethodId(obj, &PyId_items, "()");
Collin Winter5c9b02d2009-05-25 05:43:30 +00002889 if (items == NULL)
2890 goto error;
2891 iter = PyObject_GetIter(items);
2892 Py_DECREF(items);
2893 if (iter == NULL)
2894 goto error;
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002895 if (Py_EnterRecursiveCall(" while pickling an object")) {
2896 Py_DECREF(iter);
2897 goto error;
2898 }
Collin Winter5c9b02d2009-05-25 05:43:30 +00002899 status = batch_dict(self, iter);
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00002900 Py_LeaveRecursiveCall();
Collin Winter5c9b02d2009-05-25 05:43:30 +00002901 Py_DECREF(iter);
2902 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00002903 }
2904
2905 if (0) {
2906 error:
2907 status = -1;
2908 }
2909
2910 if (self->fast && !fast_save_leave(self, obj))
2911 status = -1;
2912
2913 return status;
2914}
2915
2916static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01002917save_set(PicklerObject *self, PyObject *obj)
2918{
2919 PyObject *item;
2920 int i;
2921 Py_ssize_t set_size, ppos = 0;
2922 Py_hash_t hash;
2923
2924 const char empty_set_op = EMPTY_SET;
2925 const char mark_op = MARK;
2926 const char additems_op = ADDITEMS;
2927
2928 if (self->proto < 4) {
2929 PyObject *items;
2930 PyObject *reduce_value;
2931 int status;
2932
2933 items = PySequence_List(obj);
2934 if (items == NULL) {
2935 return -1;
2936 }
2937 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items);
2938 Py_DECREF(items);
2939 if (reduce_value == NULL) {
2940 return -1;
2941 }
2942 /* save_reduce() will memoize the object automatically. */
2943 status = save_reduce(self, reduce_value, obj);
2944 Py_DECREF(reduce_value);
2945 return status;
2946 }
2947
2948 if (_Pickler_Write(self, &empty_set_op, 1) < 0)
2949 return -1;
2950
2951 if (memo_put(self, obj) < 0)
2952 return -1;
2953
2954 set_size = PySet_GET_SIZE(obj);
2955 if (set_size == 0)
2956 return 0; /* nothing to do */
2957
2958 /* Write in batches of BATCHSIZE. */
2959 do {
2960 i = 0;
2961 if (_Pickler_Write(self, &mark_op, 1) < 0)
2962 return -1;
2963 while (_PySet_NextEntry(obj, &ppos, &item, &hash)) {
2964 if (save(self, item, 0) < 0)
2965 return -1;
2966 if (++i == BATCHSIZE)
2967 break;
2968 }
2969 if (_Pickler_Write(self, &additems_op, 1) < 0)
2970 return -1;
2971 if (PySet_GET_SIZE(obj) != set_size) {
2972 PyErr_Format(
2973 PyExc_RuntimeError,
2974 "set changed size during iteration");
2975 return -1;
2976 }
2977 } while (i == BATCHSIZE);
2978
2979 return 0;
2980}
2981
2982static int
2983save_frozenset(PicklerObject *self, PyObject *obj)
2984{
2985 PyObject *iter;
2986
2987 const char mark_op = MARK;
2988 const char frozenset_op = FROZENSET;
2989
2990 if (self->fast && !fast_save_enter(self, obj))
2991 return -1;
2992
2993 if (self->proto < 4) {
2994 PyObject *items;
2995 PyObject *reduce_value;
2996 int status;
2997
2998 items = PySequence_List(obj);
2999 if (items == NULL) {
3000 return -1;
3001 }
3002 reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type,
3003 items);
3004 Py_DECREF(items);
3005 if (reduce_value == NULL) {
3006 return -1;
3007 }
3008 /* save_reduce() will memoize the object automatically. */
3009 status = save_reduce(self, reduce_value, obj);
3010 Py_DECREF(reduce_value);
3011 return status;
3012 }
3013
3014 if (_Pickler_Write(self, &mark_op, 1) < 0)
3015 return -1;
3016
3017 iter = PyObject_GetIter(obj);
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003018 if (iter == NULL) {
Christian Heimes74d8d632013-11-23 21:05:31 +01003019 return -1;
Christian Heimesb3d3ee42013-11-23 21:01:40 +01003020 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003021 for (;;) {
3022 PyObject *item;
3023
3024 item = PyIter_Next(iter);
3025 if (item == NULL) {
3026 if (PyErr_Occurred()) {
3027 Py_DECREF(iter);
3028 return -1;
3029 }
3030 break;
3031 }
3032 if (save(self, item, 0) < 0) {
3033 Py_DECREF(item);
3034 Py_DECREF(iter);
3035 return -1;
3036 }
3037 Py_DECREF(item);
3038 }
3039 Py_DECREF(iter);
3040
3041 /* If the object is already in the memo, this means it is
3042 recursive. In this case, throw away everything we put on the
3043 stack, and fetch the object back from the memo. */
3044 if (PyMemoTable_Get(self->memo, obj)) {
3045 const char pop_mark_op = POP_MARK;
3046
3047 if (_Pickler_Write(self, &pop_mark_op, 1) < 0)
3048 return -1;
3049 if (memo_get(self, obj) < 0)
3050 return -1;
3051 return 0;
3052 }
3053
3054 if (_Pickler_Write(self, &frozenset_op, 1) < 0)
3055 return -1;
3056 if (memo_put(self, obj) < 0)
3057 return -1;
3058
3059 return 0;
3060}
3061
3062static int
3063fix_imports(PyObject **module_name, PyObject **global_name)
3064{
3065 PyObject *key;
3066 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003067 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003068
3069 key = PyTuple_Pack(2, *module_name, *global_name);
3070 if (key == NULL)
3071 return -1;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003072 item = PyDict_GetItemWithError(st->name_mapping_3to2, key);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003073 Py_DECREF(key);
3074 if (item) {
3075 PyObject *fixed_module_name;
3076 PyObject *fixed_global_name;
3077
3078 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
3079 PyErr_Format(PyExc_RuntimeError,
3080 "_compat_pickle.REVERSE_NAME_MAPPING values "
3081 "should be 2-tuples, not %.200s",
3082 Py_TYPE(item)->tp_name);
3083 return -1;
3084 }
3085 fixed_module_name = PyTuple_GET_ITEM(item, 0);
3086 fixed_global_name = PyTuple_GET_ITEM(item, 1);
3087 if (!PyUnicode_Check(fixed_module_name) ||
3088 !PyUnicode_Check(fixed_global_name)) {
3089 PyErr_Format(PyExc_RuntimeError,
3090 "_compat_pickle.REVERSE_NAME_MAPPING values "
3091 "should be pairs of str, not (%.200s, %.200s)",
3092 Py_TYPE(fixed_module_name)->tp_name,
3093 Py_TYPE(fixed_global_name)->tp_name);
3094 return -1;
3095 }
3096
3097 Py_CLEAR(*module_name);
3098 Py_CLEAR(*global_name);
3099 Py_INCREF(fixed_module_name);
3100 Py_INCREF(fixed_global_name);
3101 *module_name = fixed_module_name;
3102 *global_name = fixed_global_name;
Serhiy Storchakabfe18242015-03-31 13:12:37 +03003103 return 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003104 }
3105 else if (PyErr_Occurred()) {
3106 return -1;
3107 }
3108
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003109 item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003110 if (item) {
3111 if (!PyUnicode_Check(item)) {
3112 PyErr_Format(PyExc_RuntimeError,
3113 "_compat_pickle.REVERSE_IMPORT_MAPPING values "
3114 "should be strings, not %.200s",
3115 Py_TYPE(item)->tp_name);
3116 return -1;
3117 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003118 Py_INCREF(item);
Serhiy Storchaka4a1e70f2015-12-27 12:36:18 +02003119 Py_SETREF(*module_name, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003120 }
3121 else if (PyErr_Occurred()) {
3122 return -1;
3123 }
3124
3125 return 0;
3126}
3127
3128static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003129save_global(PicklerObject *self, PyObject *obj, PyObject *name)
3130{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003131 PyObject *global_name = NULL;
3132 PyObject *module_name = NULL;
3133 PyObject *module = NULL;
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003134 PyObject *parent = NULL;
3135 PyObject *dotted_path = NULL;
3136 PyObject *lastname = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003137 PyObject *cls;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003138 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003139 int status = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003140 _Py_IDENTIFIER(__name__);
3141 _Py_IDENTIFIER(__qualname__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003142
3143 const char global_op = GLOBAL;
3144
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003145 if (name) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003146 Py_INCREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003147 global_name = name;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003148 }
3149 else {
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003150 global_name = _PyObject_GetAttrId(obj, &PyId___qualname__);
3151 if (global_name == NULL) {
3152 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
3153 goto error;
3154 PyErr_Clear();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003155 }
3156 if (global_name == NULL) {
3157 global_name = _PyObject_GetAttrId(obj, &PyId___name__);
3158 if (global_name == NULL)
3159 goto error;
3160 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003161 }
3162
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003163 dotted_path = get_dotted_path(module, global_name);
3164 if (dotted_path == NULL)
3165 goto error;
3166 module_name = whichmodule(obj, dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003167 if (module_name == NULL)
3168 goto error;
3169
3170 /* XXX: Change to use the import C API directly with level=0 to disallow
3171 relative imports.
3172
3173 XXX: PyImport_ImportModuleLevel could be used. However, this bypasses
3174 builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore
3175 custom import functions (IMHO, this would be a nice security
3176 feature). The import C API would need to be extended to support the
3177 extra parameters of __import__ to fix that. */
3178 module = PyImport_Import(module_name);
3179 if (module == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003180 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003181 "Can't pickle %R: import of module %R failed",
3182 obj, module_name);
3183 goto error;
3184 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003185 lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
3186 Py_INCREF(lastname);
3187 cls = get_deep_attribute(module, dotted_path, &parent);
3188 Py_CLEAR(dotted_path);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003189 if (cls == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003190 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003191 "Can't pickle %R: attribute lookup %S on %S failed",
3192 obj, global_name, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003193 goto error;
3194 }
3195 if (cls != obj) {
3196 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003197 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003198 "Can't pickle %R: it's not the same object as %S.%S",
3199 obj, module_name, global_name);
3200 goto error;
3201 }
3202 Py_DECREF(cls);
3203
3204 if (self->proto >= 2) {
3205 /* See whether this is in the extension registry, and if
3206 * so generate an EXT opcode.
3207 */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003208 PyObject *extension_key;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003209 PyObject *code_obj; /* extension code as Python object */
3210 long code; /* extension code as C value */
3211 char pdata[5];
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003212 Py_ssize_t n;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003213
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003214 extension_key = PyTuple_Pack(2, module_name, global_name);
3215 if (extension_key == NULL) {
3216 goto error;
3217 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003218 code_obj = PyDict_GetItemWithError(st->extension_registry,
3219 extension_key);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003220 Py_DECREF(extension_key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003221 /* The object is not registered in the extension registry.
3222 This is the most likely code path. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003223 if (code_obj == NULL) {
3224 if (PyErr_Occurred()) {
3225 goto error;
3226 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003227 goto gen_global;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003228 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003229
3230 /* XXX: pickle.py doesn't check neither the type, nor the range
3231 of the value returned by the extension_registry. It should for
3232 consistency. */
3233
3234 /* Verify code_obj has the right type and value. */
3235 if (!PyLong_Check(code_obj)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003236 PyErr_Format(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003237 "Can't pickle %R: extension code %R isn't an integer",
3238 obj, code_obj);
3239 goto error;
3240 }
3241 code = PyLong_AS_LONG(code_obj);
3242 if (code <= 0 || code > 0x7fffffffL) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02003243 if (!PyErr_Occurred())
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003244 PyErr_Format(st->PicklingError, "Can't pickle %R: extension "
3245 "code %ld is out of range", obj, code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003246 goto error;
3247 }
3248
3249 /* Generate an EXT opcode. */
3250 if (code <= 0xff) {
3251 pdata[0] = EXT1;
3252 pdata[1] = (unsigned char)code;
3253 n = 2;
3254 }
3255 else if (code <= 0xffff) {
3256 pdata[0] = EXT2;
3257 pdata[1] = (unsigned char)(code & 0xff);
3258 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3259 n = 3;
3260 }
3261 else {
3262 pdata[0] = EXT4;
3263 pdata[1] = (unsigned char)(code & 0xff);
3264 pdata[2] = (unsigned char)((code >> 8) & 0xff);
3265 pdata[3] = (unsigned char)((code >> 16) & 0xff);
3266 pdata[4] = (unsigned char)((code >> 24) & 0xff);
3267 n = 5;
3268 }
3269
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003270 if (_Pickler_Write(self, pdata, n) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003271 goto error;
3272 }
3273 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003274 gen_global:
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003275 if (parent == module) {
3276 Py_INCREF(lastname);
3277 Py_DECREF(global_name);
3278 global_name = lastname;
3279 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003280 if (self->proto >= 4) {
3281 const char stack_global_op = STACK_GLOBAL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003282
Christian Heimese8b1ba12013-11-23 21:13:39 +01003283 if (save(self, module_name, 0) < 0)
3284 goto error;
3285 if (save(self, global_name, 0) < 0)
3286 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003287
3288 if (_Pickler_Write(self, &stack_global_op, 1) < 0)
3289 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003290 }
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003291 else if (parent != module) {
3292 PickleState *st = _Pickle_GetGlobalState();
3293 PyObject *reduce_value = Py_BuildValue("(O(OO))",
3294 st->getattr, parent, lastname);
3295 status = save_reduce(self, reduce_value, NULL);
3296 Py_DECREF(reduce_value);
3297 if (status < 0)
3298 goto error;
3299 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003300 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003301 /* Generate a normal global opcode if we are using a pickle
3302 protocol < 4, or if the object is not registered in the
3303 extension registry. */
3304 PyObject *encoded;
3305 PyObject *(*unicode_encoder)(PyObject *);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003306
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003307 if (_Pickler_Write(self, &global_op, 1) < 0)
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003308 goto error;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003309
3310 /* For protocol < 3 and if the user didn't request against doing
3311 so, we convert module names to the old 2.x module names. */
3312 if (self->proto < 3 && self->fix_imports) {
3313 if (fix_imports(&module_name, &global_name) < 0) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003314 goto error;
3315 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003316 }
3317
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003318 /* Since Python 3.0 now supports non-ASCII identifiers, we encode
3319 both the module name and the global name using UTF-8. We do so
3320 only when we are using the pickle protocol newer than version
3321 3. This is to ensure compatibility with older Unpickler running
3322 on Python 2.x. */
3323 if (self->proto == 3) {
3324 unicode_encoder = PyUnicode_AsUTF8String;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003325 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003326 else {
3327 unicode_encoder = PyUnicode_AsASCIIString;
3328 }
3329 encoded = unicode_encoder(module_name);
3330 if (encoded == NULL) {
3331 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003332 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003333 "can't pickle module identifier '%S' using "
3334 "pickle protocol %i",
3335 module_name, self->proto);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00003336 goto error;
3337 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003338 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3339 PyBytes_GET_SIZE(encoded)) < 0) {
3340 Py_DECREF(encoded);
3341 goto error;
3342 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003343 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003344 if(_Pickler_Write(self, "\n", 1) < 0)
3345 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003346
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003347 /* Save the name of the module. */
3348 encoded = unicode_encoder(global_name);
3349 if (encoded == NULL) {
3350 if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError))
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003351 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003352 "can't pickle global identifier '%S' using "
3353 "pickle protocol %i",
3354 global_name, self->proto);
3355 goto error;
3356 }
3357 if (_Pickler_Write(self, PyBytes_AS_STRING(encoded),
3358 PyBytes_GET_SIZE(encoded)) < 0) {
3359 Py_DECREF(encoded);
3360 goto error;
3361 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003362 Py_DECREF(encoded);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003363 if (_Pickler_Write(self, "\n", 1) < 0)
3364 goto error;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003365 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003366 /* Memoize the object. */
3367 if (memo_put(self, obj) < 0)
3368 goto error;
3369 }
3370
3371 if (0) {
3372 error:
3373 status = -1;
3374 }
3375 Py_XDECREF(module_name);
3376 Py_XDECREF(global_name);
3377 Py_XDECREF(module);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03003378 Py_XDECREF(parent);
3379 Py_XDECREF(dotted_path);
3380 Py_XDECREF(lastname);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003381
3382 return status;
3383}
3384
3385static int
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003386save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton)
3387{
3388 PyObject *reduce_value;
3389 int status;
3390
3391 reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton);
3392 if (reduce_value == NULL) {
3393 return -1;
3394 }
3395 status = save_reduce(self, reduce_value, obj);
3396 Py_DECREF(reduce_value);
3397 return status;
3398}
3399
3400static int
3401save_type(PicklerObject *self, PyObject *obj)
3402{
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003403 if (obj == (PyObject *)&_PyNone_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003404 return save_singleton_type(self, obj, Py_None);
3405 }
3406 else if (obj == (PyObject *)&PyEllipsis_Type) {
3407 return save_singleton_type(self, obj, Py_Ellipsis);
3408 }
Alexandre Vassalotti65846c62013-11-30 17:55:48 -08003409 else if (obj == (PyObject *)&_PyNotImplemented_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003410 return save_singleton_type(self, obj, Py_NotImplemented);
3411 }
3412 return save_global(self, obj, NULL);
3413}
3414
3415static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003416save_pers(PicklerObject *self, PyObject *obj, PyObject *func)
3417{
3418 PyObject *pid = NULL;
3419 int status = 0;
3420
3421 const char persid_op = PERSID;
3422 const char binpersid_op = BINPERSID;
3423
3424 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003425 pid = _Pickle_FastCall(func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003426 if (pid == NULL)
3427 return -1;
3428
3429 if (pid != Py_None) {
3430 if (self->bin) {
3431 if (save(self, pid, 1) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003432 _Pickler_Write(self, &binpersid_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003433 goto error;
3434 }
3435 else {
3436 PyObject *pid_str = NULL;
3437 char *pid_ascii_bytes;
3438 Py_ssize_t size;
3439
3440 pid_str = PyObject_Str(pid);
3441 if (pid_str == NULL)
3442 goto error;
3443
3444 /* XXX: Should it check whether the persistent id only contains
3445 ASCII characters? And what if the pid contains embedded
3446 newlines? */
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003447 pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003448 Py_DECREF(pid_str);
3449 if (pid_ascii_bytes == NULL)
3450 goto error;
3451
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003452 if (_Pickler_Write(self, &persid_op, 1) < 0 ||
3453 _Pickler_Write(self, pid_ascii_bytes, size) < 0 ||
3454 _Pickler_Write(self, "\n", 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003455 goto error;
3456 }
3457 status = 1;
3458 }
3459
3460 if (0) {
3461 error:
3462 status = -1;
3463 }
3464 Py_XDECREF(pid);
3465
3466 return status;
3467}
3468
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003469static PyObject *
3470get_class(PyObject *obj)
3471{
3472 PyObject *cls;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003473 _Py_IDENTIFIER(__class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003474
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003475 cls = _PyObject_GetAttrId(obj, &PyId___class__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003476 if (cls == NULL) {
3477 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
3478 PyErr_Clear();
3479 cls = (PyObject *) Py_TYPE(obj);
3480 Py_INCREF(cls);
3481 }
3482 }
3483 return cls;
3484}
3485
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003486/* We're saving obj, and args is the 2-thru-5 tuple returned by the
3487 * appropriate __reduce__ method for obj.
3488 */
3489static int
3490save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
3491{
3492 PyObject *callable;
3493 PyObject *argtup;
3494 PyObject *state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003495 PyObject *listitems = Py_None;
3496 PyObject *dictitems = Py_None;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003497 PickleState *st = _Pickle_GetGlobalState();
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003498 Py_ssize_t size;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003499 int use_newobj = 0, use_newobj_ex = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003500
3501 const char reduce_op = REDUCE;
3502 const char build_op = BUILD;
3503 const char newobj_op = NEWOBJ;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003504 const char newobj_ex_op = NEWOBJ_EX;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003505
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003506 size = PyTuple_Size(args);
3507 if (size < 2 || size > 5) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003508 PyErr_SetString(st->PicklingError, "tuple returned by "
Hirokazu Yamamotob46a6332008-11-04 00:35:10 +00003509 "__reduce__ must contain 2 through 5 elements");
3510 return -1;
3511 }
3512
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003513 if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5,
3514 &callable, &argtup, &state, &listitems, &dictitems))
3515 return -1;
3516
3517 if (!PyCallable_Check(callable)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003518 PyErr_SetString(st->PicklingError, "first item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003519 "returned by __reduce__ must be callable");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003520 return -1;
3521 }
3522 if (!PyTuple_Check(argtup)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003523 PyErr_SetString(st->PicklingError, "second item of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003524 "returned by __reduce__ must be a tuple");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003525 return -1;
3526 }
3527
3528 if (state == Py_None)
3529 state = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003530
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003531 if (listitems == Py_None)
3532 listitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003533 else if (!PyIter_Check(listitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003534 PyErr_Format(st->PicklingError, "fourth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003535 "returned by __reduce__ must be an iterator, not %s",
3536 Py_TYPE(listitems)->tp_name);
3537 return -1;
3538 }
3539
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003540 if (dictitems == Py_None)
3541 dictitems = NULL;
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003542 else if (!PyIter_Check(dictitems)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003543 PyErr_Format(st->PicklingError, "fifth element of the tuple "
Amaury Forgeot d'Arc424b4812008-10-30 22:25:31 +00003544 "returned by __reduce__ must be an iterator, not %s",
3545 Py_TYPE(dictitems)->tp_name);
3546 return -1;
3547 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003548
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003549 if (self->proto >= 2) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003550 PyObject *name;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003551 _Py_IDENTIFIER(__name__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003552
Victor Stinner804e05e2013-11-14 01:26:17 +01003553 name = _PyObject_GetAttrId(callable, &PyId___name__);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003554 if (name == NULL) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003555 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003556 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003557 }
3558 PyErr_Clear();
3559 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003560 else if (PyUnicode_Check(name)) {
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003561 _Py_IDENTIFIER(__newobj_ex__);
3562 use_newobj_ex = PyUnicode_Compare(
3563 name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0;
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003564 if (!use_newobj_ex) {
3565 _Py_IDENTIFIER(__newobj__);
3566 use_newobj = PyUnicode_Compare(
3567 name, _PyUnicode_FromId(&PyId___newobj__)) == 0;
3568 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003569 }
Serhiy Storchaka707b5cc2014-12-16 19:43:46 +02003570 Py_XDECREF(name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003571 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003572
3573 if (use_newobj_ex) {
3574 PyObject *cls;
3575 PyObject *args;
3576 PyObject *kwargs;
3577
3578 if (Py_SIZE(argtup) != 3) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003579 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003580 "length of the NEWOBJ_EX argument tuple must be "
3581 "exactly 3, not %zd", Py_SIZE(argtup));
3582 return -1;
3583 }
3584
3585 cls = PyTuple_GET_ITEM(argtup, 0);
3586 if (!PyType_Check(cls)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003587 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003588 "first item from NEWOBJ_EX argument tuple must "
3589 "be a class, not %.200s", Py_TYPE(cls)->tp_name);
3590 return -1;
3591 }
3592 args = PyTuple_GET_ITEM(argtup, 1);
3593 if (!PyTuple_Check(args)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003594 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003595 "second item from NEWOBJ_EX argument tuple must "
3596 "be a tuple, not %.200s", Py_TYPE(args)->tp_name);
3597 return -1;
3598 }
3599 kwargs = PyTuple_GET_ITEM(argtup, 2);
3600 if (!PyDict_Check(kwargs)) {
Larry Hastings61272b72014-01-07 12:41:53 -08003601 PyErr_Format(st->PicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003602 "third item from NEWOBJ_EX argument tuple must "
3603 "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name);
3604 return -1;
3605 }
3606
Serhiy Storchaka0d554d72015-10-10 22:42:18 +03003607 if (self->proto >= 4) {
3608 if (save(self, cls, 0) < 0 ||
3609 save(self, args, 0) < 0 ||
3610 save(self, kwargs, 0) < 0 ||
3611 _Pickler_Write(self, &newobj_ex_op, 1) < 0) {
3612 return -1;
3613 }
3614 }
3615 else {
3616 PyObject *newargs;
3617 PyObject *cls_new;
3618 Py_ssize_t i;
3619 _Py_IDENTIFIER(__new__);
3620
3621 newargs = PyTuple_New(Py_SIZE(args) + 2);
3622 if (newargs == NULL)
3623 return -1;
3624
3625 cls_new = _PyObject_GetAttrId(cls, &PyId___new__);
3626 if (cls_new == NULL) {
3627 Py_DECREF(newargs);
3628 return -1;
3629 }
3630 PyTuple_SET_ITEM(newargs, 0, cls_new);
3631 Py_INCREF(cls);
3632 PyTuple_SET_ITEM(newargs, 1, cls);
3633 for (i = 0; i < Py_SIZE(args); i++) {
3634 PyObject *item = PyTuple_GET_ITEM(args, i);
3635 Py_INCREF(item);
3636 PyTuple_SET_ITEM(newargs, i + 2, item);
3637 }
3638
3639 callable = PyObject_Call(st->partial, newargs, kwargs);
3640 Py_DECREF(newargs);
3641 if (callable == NULL)
3642 return -1;
3643
3644 newargs = PyTuple_New(0);
3645 if (newargs == NULL) {
3646 Py_DECREF(callable);
3647 return -1;
3648 }
3649
3650 if (save(self, callable, 0) < 0 ||
3651 save(self, newargs, 0) < 0 ||
3652 _Pickler_Write(self, &reduce_op, 1) < 0) {
3653 Py_DECREF(newargs);
3654 Py_DECREF(callable);
3655 return -1;
3656 }
3657 Py_DECREF(newargs);
3658 Py_DECREF(callable);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003659 }
3660 }
3661 else if (use_newobj) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003662 PyObject *cls;
3663 PyObject *newargtup;
3664 PyObject *obj_class;
3665 int p;
3666
3667 /* Sanity checks. */
3668 if (Py_SIZE(argtup) < 1) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003669 PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003670 return -1;
3671 }
3672
3673 cls = PyTuple_GET_ITEM(argtup, 0);
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003674 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003675 PyErr_SetString(st->PicklingError, "args[0] from "
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003676 "__newobj__ args is not a type");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003677 return -1;
3678 }
3679
3680 if (obj != NULL) {
Antoine Pitrou16c4ce12011-03-11 21:30:43 +01003681 obj_class = get_class(obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003682 p = obj_class != cls; /* true iff a problem */
3683 Py_DECREF(obj_class);
3684 if (p) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003685 PyErr_SetString(st->PicklingError, "args[0] from "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003686 "__newobj__ args has the wrong class");
3687 return -1;
3688 }
3689 }
3690 /* XXX: These calls save() are prone to infinite recursion. Imagine
3691 what happen if the value returned by the __reduce__() method of
3692 some extension type contains another object of the same type. Ouch!
3693
3694 Here is a quick example, that I ran into, to illustrate what I
3695 mean:
3696
3697 >>> import pickle, copyreg
3698 >>> copyreg.dispatch_table.pop(complex)
3699 >>> pickle.dumps(1+2j)
3700 Traceback (most recent call last):
3701 ...
Yury Selivanovf488fb42015-07-03 01:04:23 -04003702 RecursionError: maximum recursion depth exceeded
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003703
3704 Removing the complex class from copyreg.dispatch_table made the
3705 __reduce_ex__() method emit another complex object:
3706
3707 >>> (1+1j).__reduce_ex__(2)
3708 (<function __newobj__ at 0xb7b71c3c>,
3709 (<class 'complex'>, (1+1j)), None, None, None)
3710
3711 Thus when save() was called on newargstup (the 2nd item) recursion
3712 ensued. Of course, the bug was in the complex class which had a
3713 broken __getnewargs__() that emitted another complex object. But,
3714 the point, here, is it is quite easy to end up with a broken reduce
3715 function. */
3716
3717 /* Save the class and its __new__ arguments. */
3718 if (save(self, cls, 0) < 0)
3719 return -1;
3720
3721 newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup));
3722 if (newargtup == NULL)
3723 return -1;
3724
3725 p = save(self, newargtup, 0);
3726 Py_DECREF(newargtup);
3727 if (p < 0)
3728 return -1;
3729
3730 /* Add NEWOBJ opcode. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003731 if (_Pickler_Write(self, &newobj_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003732 return -1;
3733 }
3734 else { /* Not using NEWOBJ. */
3735 if (save(self, callable, 0) < 0 ||
3736 save(self, argtup, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003737 _Pickler_Write(self, &reduce_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003738 return -1;
3739 }
3740
3741 /* obj can be NULL when save_reduce() is used directly. A NULL obj means
3742 the caller do not want to memoize the object. Not particularly useful,
3743 but that is to mimic the behavior save_reduce() in pickle.py when
3744 obj is None. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003745 if (obj != NULL) {
3746 /* If the object is already in the memo, this means it is
3747 recursive. In this case, throw away everything we put on the
3748 stack, and fetch the object back from the memo. */
3749 if (PyMemoTable_Get(self->memo, obj)) {
3750 const char pop_op = POP;
3751
3752 if (_Pickler_Write(self, &pop_op, 1) < 0)
3753 return -1;
3754 if (memo_get(self, obj) < 0)
3755 return -1;
3756
3757 return 0;
3758 }
3759 else if (memo_put(self, obj) < 0)
3760 return -1;
3761 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003762
3763 if (listitems && batch_list(self, listitems) < 0)
3764 return -1;
3765
3766 if (dictitems && batch_dict(self, dictitems) < 0)
3767 return -1;
3768
3769 if (state) {
Victor Stinner121aab42011-09-29 23:40:53 +02003770 if (save(self, state, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003771 _Pickler_Write(self, &build_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003772 return -1;
3773 }
3774
3775 return 0;
3776}
3777
3778static int
3779save(PicklerObject *self, PyObject *obj, int pers_save)
3780{
3781 PyTypeObject *type;
3782 PyObject *reduce_func = NULL;
3783 PyObject *reduce_value = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003784 int status = 0;
3785
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003786 if (_Pickler_OpcodeBoundary(self) < 0)
3787 return -1;
3788
Antoine Pitroue6d4c5b2011-01-23 17:12:25 +00003789 if (Py_EnterRecursiveCall(" while pickling an object"))
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003790 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003791
3792 /* The extra pers_save argument is necessary to avoid calling save_pers()
3793 on its returned object. */
3794 if (!pers_save && self->pers_func) {
3795 /* save_pers() returns:
3796 -1 to signal an error;
3797 0 if it did nothing successfully;
3798 1 if a persistent id was saved.
3799 */
3800 if ((status = save_pers(self, obj, self->pers_func)) != 0)
3801 goto done;
3802 }
3803
3804 type = Py_TYPE(obj);
3805
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003806 /* The old cPickle had an optimization that used switch-case statement
3807 dispatching on the first letter of the type name. This has was removed
3808 since benchmarks shown that this optimization was actually slowing
3809 things down. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003810
3811 /* Atom types; these aren't memoized, so don't check the memo. */
3812
3813 if (obj == Py_None) {
3814 status = save_none(self, obj);
3815 goto done;
3816 }
3817 else if (obj == Py_False || obj == Py_True) {
3818 status = save_bool(self, obj);
3819 goto done;
3820 }
3821 else if (type == &PyLong_Type) {
3822 status = save_long(self, obj);
3823 goto done;
3824 }
3825 else if (type == &PyFloat_Type) {
3826 status = save_float(self, obj);
3827 goto done;
3828 }
3829
3830 /* Check the memo to see if it has the object. If so, generate
3831 a GET (or BINGET) opcode, instead of pickling the object
3832 once again. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00003833 if (PyMemoTable_Get(self->memo, obj)) {
3834 if (memo_get(self, obj) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003835 goto error;
3836 goto done;
3837 }
3838
3839 if (type == &PyBytes_Type) {
3840 status = save_bytes(self, obj);
3841 goto done;
3842 }
3843 else if (type == &PyUnicode_Type) {
3844 status = save_unicode(self, obj);
3845 goto done;
3846 }
3847 else if (type == &PyDict_Type) {
3848 status = save_dict(self, obj);
3849 goto done;
3850 }
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003851 else if (type == &PySet_Type) {
3852 status = save_set(self, obj);
3853 goto done;
3854 }
3855 else if (type == &PyFrozenSet_Type) {
3856 status = save_frozenset(self, obj);
3857 goto done;
3858 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003859 else if (type == &PyList_Type) {
3860 status = save_list(self, obj);
3861 goto done;
3862 }
3863 else if (type == &PyTuple_Type) {
3864 status = save_tuple(self, obj);
3865 goto done;
3866 }
3867 else if (type == &PyType_Type) {
Alexandre Vassalotti19b6fa62013-11-30 16:06:39 -08003868 status = save_type(self, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003869 goto done;
3870 }
3871 else if (type == &PyFunction_Type) {
3872 status = save_global(self, obj, NULL);
Alexandre Vassalottifc912852013-11-24 03:07:35 -08003873 goto done;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003874 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003875
3876 /* XXX: This part needs some unit tests. */
3877
3878 /* Get a reduction callable, and call it. This may come from
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003879 * self.dispatch_table, copyreg.dispatch_table, the object's
3880 * __reduce_ex__ method, or the object's __reduce__ method.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003881 */
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003882 if (self->dispatch_table == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003883 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003884 reduce_func = PyDict_GetItemWithError(st->dispatch_table,
3885 (PyObject *)type);
3886 if (reduce_func == NULL) {
3887 if (PyErr_Occurred()) {
3888 goto error;
3889 }
3890 } else {
3891 /* PyDict_GetItemWithError() returns a borrowed reference.
3892 Increase the reference count to be consistent with
3893 PyObject_GetItem and _PyObject_GetAttrId used below. */
3894 Py_INCREF(reduce_func);
3895 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003896 } else {
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08003897 reduce_func = PyObject_GetItem(self->dispatch_table,
3898 (PyObject *)type);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01003899 if (reduce_func == NULL) {
3900 if (PyErr_ExceptionMatches(PyExc_KeyError))
3901 PyErr_Clear();
3902 else
3903 goto error;
3904 }
3905 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003906 if (reduce_func != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003907 Py_INCREF(obj);
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003908 reduce_value = _Pickle_FastCall(reduce_func, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003909 }
Antoine Pitrouffd41d92011-10-04 09:23:04 +02003910 else if (PyType_IsSubtype(type, &PyType_Type)) {
3911 status = save_global(self, obj, NULL);
3912 goto done;
3913 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003914 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003915 _Py_IDENTIFIER(__reduce__);
3916 _Py_IDENTIFIER(__reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003917
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003918
3919 /* XXX: If the __reduce__ method is defined, __reduce_ex__ is
3920 automatically defined as __reduce__. While this is convenient, this
3921 make it impossible to know which method was actually called. Of
3922 course, this is not a big deal. But still, it would be nice to let
3923 the user know which method was called when something go
3924 wrong. Incidentally, this means if __reduce_ex__ is not defined, we
3925 don't actually have to check for a __reduce__ method. */
3926
3927 /* Check for a __reduce_ex__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003928 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003929 if (reduce_func != NULL) {
3930 PyObject *proto;
3931 proto = PyLong_FromLong(self->proto);
3932 if (proto != NULL) {
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08003933 reduce_value = _Pickle_FastCall(reduce_func, proto);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003934 }
3935 }
3936 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003937 PickleState *st = _Pickle_GetGlobalState();
3938
3939 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003940 PyErr_Clear();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003941 }
3942 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003943 goto error;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003944 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003945 /* Check for a __reduce__ method. */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01003946 reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003947 if (reduce_func != NULL) {
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003948 PyObject *empty_tuple = PyTuple_New(0);
3949 reduce_value = PyObject_Call(reduce_func, empty_tuple,
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003950 NULL);
Alexandre Vassalotti6bf41e52013-11-28 15:17:29 -08003951 Py_DECREF(empty_tuple);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003952 }
3953 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003954 PyErr_Format(st->PicklingError,
3955 "can't pickle '%.200s' object: %R",
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003956 type->tp_name, obj);
3957 goto error;
3958 }
3959 }
3960 }
3961
3962 if (reduce_value == NULL)
3963 goto error;
3964
3965 if (PyUnicode_Check(reduce_value)) {
3966 status = save_global(self, obj, reduce_value);
3967 goto done;
3968 }
3969
3970 if (!PyTuple_Check(reduce_value)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08003971 PickleState *st = _Pickle_GetGlobalState();
3972 PyErr_SetString(st->PicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003973 "__reduce__ must return a string or tuple");
3974 goto error;
3975 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003976
3977 status = save_reduce(self, reduce_value, obj);
3978
3979 if (0) {
3980 error:
3981 status = -1;
3982 }
3983 done:
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08003984
Alexandre Vassalottidff18342008-07-13 18:48:30 +00003985 Py_LeaveRecursiveCall();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00003986 Py_XDECREF(reduce_func);
3987 Py_XDECREF(reduce_value);
3988
3989 return status;
3990}
3991
3992static int
3993dump(PicklerObject *self, PyObject *obj)
3994{
3995 const char stop_op = STOP;
3996
3997 if (self->proto >= 2) {
3998 char header[2];
3999
4000 header[0] = PROTO;
4001 assert(self->proto >= 0 && self->proto < 256);
4002 header[1] = (unsigned char)self->proto;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004003 if (_Pickler_Write(self, header, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004004 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004005 if (self->proto >= 4)
4006 self->framing = 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004007 }
4008
4009 if (save(self, obj, 0) < 0 ||
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004010 _Pickler_Write(self, &stop_op, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004011 return -1;
4012
4013 return 0;
4014}
4015
Larry Hastings61272b72014-01-07 12:41:53 -08004016/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004017
4018_pickle.Pickler.clear_memo
4019
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004020Clears the pickler's "memo".
4021
4022The memo is the data structure that remembers which objects the
4023pickler has already seen, so that shared or recursive objects are
4024pickled by reference and not by value. This method is useful when
4025re-using picklers.
Larry Hastings61272b72014-01-07 12:41:53 -08004026[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004027
Larry Hastings3cceb382014-01-04 11:09:09 -08004028static PyObject *
4029_pickle_Pickler_clear_memo_impl(PicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004030/*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004031{
4032 if (self->memo)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004033 PyMemoTable_Clear(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004034
4035 Py_RETURN_NONE;
4036}
4037
Larry Hastings61272b72014-01-07 12:41:53 -08004038/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004039
4040_pickle.Pickler.dump
4041
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004042 obj: object
4043 /
4044
4045Write a pickled representation of the given object to the open file.
Larry Hastings61272b72014-01-07 12:41:53 -08004046[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004047
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004048static PyObject *
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004049_pickle_Pickler_dump(PicklerObject *self, PyObject *obj)
Larry Hastings581ee362014-01-28 05:00:08 -08004050/*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004051{
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004052 /* Check whether the Pickler was initialized correctly (issue3664).
4053 Developers often forget to call __init__() in their subclasses, which
4054 would trigger a segfault without this check. */
4055 if (self->write == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004056 PickleState *st = _Pickle_GetGlobalState();
4057 PyErr_Format(st->PicklingError,
Amaury Forgeot d'Arc87eee632008-10-17 20:15:53 +00004058 "Pickler.__init__() was not called by %s.__init__()",
4059 Py_TYPE(self)->tp_name);
4060 return NULL;
4061 }
4062
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004063 if (_Pickler_ClearBuffer(self) < 0)
4064 return NULL;
4065
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004066 if (dump(self, obj) < 0)
4067 return NULL;
4068
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004069 if (_Pickler_FlushToFile(self) < 0)
4070 return NULL;
4071
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004072 Py_RETURN_NONE;
4073}
4074
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004075/*[clinic input]
4076
4077_pickle.Pickler.__sizeof__ -> Py_ssize_t
4078
4079Returns size in memory, in bytes.
4080[clinic start generated code]*/
4081
4082static Py_ssize_t
4083_pickle_Pickler___sizeof___impl(PicklerObject *self)
4084/*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/
4085{
4086 Py_ssize_t res, s;
4087
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02004088 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004089 if (self->memo != NULL) {
4090 res += sizeof(PyMemoTable);
4091 res += self->memo->mt_allocated * sizeof(PyMemoEntry);
4092 }
4093 if (self->output_buffer != NULL) {
4094 s = _PySys_GetSizeOf(self->output_buffer);
4095 if (s == -1)
4096 return -1;
4097 res += s;
4098 }
4099 return res;
4100}
4101
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004102static struct PyMethodDef Pickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004103 _PICKLE_PICKLER_DUMP_METHODDEF
4104 _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02004105 _PICKLE_PICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004106 {NULL, NULL} /* sentinel */
4107};
4108
4109static void
4110Pickler_dealloc(PicklerObject *self)
4111{
4112 PyObject_GC_UnTrack(self);
4113
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004114 Py_XDECREF(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004115 Py_XDECREF(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004116 Py_XDECREF(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004117 Py_XDECREF(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004118 Py_XDECREF(self->fast_memo);
4119
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004120 PyMemoTable_Del(self->memo);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004121
4122 Py_TYPE(self)->tp_free((PyObject *)self);
4123}
4124
4125static int
4126Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
4127{
4128 Py_VISIT(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004129 Py_VISIT(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004130 Py_VISIT(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004131 Py_VISIT(self->fast_memo);
4132 return 0;
4133}
4134
4135static int
4136Pickler_clear(PicklerObject *self)
4137{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004138 Py_CLEAR(self->output_buffer);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004139 Py_CLEAR(self->write);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004140 Py_CLEAR(self->pers_func);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004141 Py_CLEAR(self->dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004142 Py_CLEAR(self->fast_memo);
4143
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004144 if (self->memo != NULL) {
4145 PyMemoTable *memo = self->memo;
4146 self->memo = NULL;
4147 PyMemoTable_Del(memo);
4148 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004149 return 0;
4150}
4151
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004152
Larry Hastings61272b72014-01-07 12:41:53 -08004153/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004154
4155_pickle.Pickler.__init__
4156
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004157 file: object
4158 protocol: object = NULL
4159 fix_imports: bool = True
4160
4161This takes a binary file for writing a pickle data stream.
4162
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004163The optional *protocol* argument tells the pickler to use the given
4164protocol; supported protocols are 0, 1, 2, 3 and 4. The default
4165protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004166
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004167Specifying a negative protocol version selects the highest protocol
4168version supported. The higher the protocol used, the more recent the
4169version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004170
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004171The *file* argument must have a write() method that accepts a single
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004172bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00004173writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004174this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004175
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004176If *fix_imports* is True and protocol is less than 3, pickle will try
4177to map the new Python 3 names to the old module names used in Python
41782, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08004179[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004180
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004181static int
Larry Hastings89964c42015-04-14 18:07:59 -04004182_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
4183 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00004184/*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004185{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004186 _Py_IDENTIFIER(persistent_id);
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004187 _Py_IDENTIFIER(dispatch_table);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004188
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004189 /* In case of multiple __init__() calls, clear previous content. */
4190 if (self->write != NULL)
4191 (void)Pickler_clear(self);
4192
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004193 if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004194 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004195
4196 if (_Pickler_SetOutputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004197 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004198
4199 /* memo and output_buffer may have already been created in _Pickler_New */
4200 if (self->memo == NULL) {
4201 self->memo = PyMemoTable_New();
4202 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004203 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004204 }
4205 self->output_len = 0;
4206 if (self->output_buffer == NULL) {
4207 self->max_output_len = WRITE_BUF_SIZE;
4208 self->output_buffer = PyBytes_FromStringAndSize(NULL,
4209 self->max_output_len);
4210 if (self->output_buffer == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004211 return -1;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004212 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004213
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00004214 self->fast = 0;
4215 self->fast_nesting = 0;
4216 self->fast_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004217 self->pers_func = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02004218 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) {
4219 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
4220 &PyId_persistent_id);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004221 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004222 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004223 }
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004224 self->dispatch_table = NULL;
4225 if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) {
4226 self->dispatch_table = _PyObject_GetAttrId((PyObject *)self,
4227 &PyId_dispatch_table);
4228 if (self->dispatch_table == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004229 return -1;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004230 }
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004231
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004232 return 0;
4233}
4234
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004235
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004236/* Define a proxy object for the Pickler's internal memo object. This is to
4237 * avoid breaking code like:
4238 * pickler.memo.clear()
4239 * and
4240 * pickler.memo = saved_memo
4241 * Is this a good idea? Not really, but we don't want to break code that uses
4242 * it. Note that we don't implement the entire mapping API here. This is
4243 * intentional, as these should be treated as black-box implementation details.
4244 */
4245
Larry Hastings61272b72014-01-07 12:41:53 -08004246/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004247_pickle.PicklerMemoProxy.clear
4248
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004249Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08004250[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004251
Larry Hastings3cceb382014-01-04 11:09:09 -08004252static PyObject *
4253_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004254/*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004255{
4256 if (self->pickler->memo)
4257 PyMemoTable_Clear(self->pickler->memo);
4258 Py_RETURN_NONE;
4259}
4260
Larry Hastings61272b72014-01-07 12:41:53 -08004261/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004262_pickle.PicklerMemoProxy.copy
4263
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004264Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08004265[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004266
Larry Hastings3cceb382014-01-04 11:09:09 -08004267static PyObject *
4268_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004269/*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004270{
4271 Py_ssize_t i;
4272 PyMemoTable *memo;
4273 PyObject *new_memo = PyDict_New();
4274 if (new_memo == NULL)
4275 return NULL;
4276
4277 memo = self->pickler->memo;
4278 for (i = 0; i < memo->mt_allocated; ++i) {
4279 PyMemoEntry entry = memo->mt_table[i];
4280 if (entry.me_key != NULL) {
4281 int status;
4282 PyObject *key, *value;
4283
4284 key = PyLong_FromVoidPtr(entry.me_key);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004285 value = Py_BuildValue("nO", entry.me_value, entry.me_key);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004286
4287 if (key == NULL || value == NULL) {
4288 Py_XDECREF(key);
4289 Py_XDECREF(value);
4290 goto error;
4291 }
4292 status = PyDict_SetItem(new_memo, key, value);
4293 Py_DECREF(key);
4294 Py_DECREF(value);
4295 if (status < 0)
4296 goto error;
4297 }
4298 }
4299 return new_memo;
4300
4301 error:
4302 Py_XDECREF(new_memo);
4303 return NULL;
4304}
4305
Larry Hastings61272b72014-01-07 12:41:53 -08004306/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004307_pickle.PicklerMemoProxy.__reduce__
4308
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004309Implement pickle support.
Larry Hastings61272b72014-01-07 12:41:53 -08004310[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004311
Larry Hastings3cceb382014-01-04 11:09:09 -08004312static PyObject *
4313_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08004314/*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004315{
4316 PyObject *reduce_value, *dict_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08004317 PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004318 if (contents == NULL)
4319 return NULL;
4320
4321 reduce_value = PyTuple_New(2);
4322 if (reduce_value == NULL) {
4323 Py_DECREF(contents);
4324 return NULL;
4325 }
4326 dict_args = PyTuple_New(1);
4327 if (dict_args == NULL) {
4328 Py_DECREF(contents);
4329 Py_DECREF(reduce_value);
4330 return NULL;
4331 }
4332 PyTuple_SET_ITEM(dict_args, 0, contents);
4333 Py_INCREF((PyObject *)&PyDict_Type);
4334 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
4335 PyTuple_SET_ITEM(reduce_value, 1, dict_args);
4336 return reduce_value;
4337}
4338
4339static PyMethodDef picklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004340 _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF
4341 _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF
4342 _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004343 {NULL, NULL} /* sentinel */
4344};
4345
4346static void
4347PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self)
4348{
4349 PyObject_GC_UnTrack(self);
4350 Py_XDECREF(self->pickler);
4351 PyObject_GC_Del((PyObject *)self);
4352}
4353
4354static int
4355PicklerMemoProxy_traverse(PicklerMemoProxyObject *self,
4356 visitproc visit, void *arg)
4357{
4358 Py_VISIT(self->pickler);
4359 return 0;
4360}
4361
4362static int
4363PicklerMemoProxy_clear(PicklerMemoProxyObject *self)
4364{
4365 Py_CLEAR(self->pickler);
4366 return 0;
4367}
4368
4369static PyTypeObject PicklerMemoProxyType = {
4370 PyVarObject_HEAD_INIT(NULL, 0)
4371 "_pickle.PicklerMemoProxy", /*tp_name*/
4372 sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
4373 0,
4374 (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
4375 0, /* tp_print */
4376 0, /* tp_getattr */
4377 0, /* tp_setattr */
4378 0, /* tp_compare */
4379 0, /* tp_repr */
4380 0, /* tp_as_number */
4381 0, /* tp_as_sequence */
4382 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00004383 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004384 0, /* tp_call */
4385 0, /* tp_str */
4386 PyObject_GenericGetAttr, /* tp_getattro */
4387 PyObject_GenericSetAttr, /* tp_setattro */
4388 0, /* tp_as_buffer */
4389 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
4390 0, /* tp_doc */
4391 (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */
4392 (inquiry)PicklerMemoProxy_clear, /* tp_clear */
4393 0, /* tp_richcompare */
4394 0, /* tp_weaklistoffset */
4395 0, /* tp_iter */
4396 0, /* tp_iternext */
4397 picklerproxy_methods, /* tp_methods */
4398};
4399
4400static PyObject *
4401PicklerMemoProxy_New(PicklerObject *pickler)
4402{
4403 PicklerMemoProxyObject *self;
4404
4405 self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
4406 if (self == NULL)
4407 return NULL;
4408 Py_INCREF(pickler);
4409 self->pickler = pickler;
4410 PyObject_GC_Track(self);
4411 return (PyObject *)self;
4412}
4413
4414/*****************************************************************************/
4415
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004416static PyObject *
4417Pickler_get_memo(PicklerObject *self)
4418{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004419 return PicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004420}
4421
4422static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004423Pickler_set_memo(PicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004424{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004425 PyMemoTable *new_memo = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004426
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004427 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004428 PyErr_SetString(PyExc_TypeError,
4429 "attribute deletion is not supported");
4430 return -1;
4431 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004432
4433 if (Py_TYPE(obj) == &PicklerMemoProxyType) {
4434 PicklerObject *pickler =
4435 ((PicklerMemoProxyObject *)obj)->pickler;
4436
4437 new_memo = PyMemoTable_Copy(pickler->memo);
4438 if (new_memo == NULL)
4439 return -1;
4440 }
4441 else if (PyDict_Check(obj)) {
4442 Py_ssize_t i = 0;
4443 PyObject *key, *value;
4444
4445 new_memo = PyMemoTable_New();
4446 if (new_memo == NULL)
4447 return -1;
4448
4449 while (PyDict_Next(obj, &i, &key, &value)) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004450 Py_ssize_t memo_id;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004451 PyObject *memo_obj;
4452
4453 if (!PyTuple_Check(value) || Py_SIZE(value) != 2) {
4454 PyErr_SetString(PyExc_TypeError,
4455 "'memo' values must be 2-item tuples");
4456 goto error;
4457 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004458 memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0));
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004459 if (memo_id == -1 && PyErr_Occurred())
4460 goto error;
4461 memo_obj = PyTuple_GET_ITEM(value, 1);
4462 if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0)
4463 goto error;
4464 }
4465 }
4466 else {
4467 PyErr_Format(PyExc_TypeError,
4468 "'memo' attribute must be an PicklerMemoProxy object"
4469 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004470 return -1;
4471 }
4472
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004473 PyMemoTable_Del(self->memo);
4474 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004475
4476 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004477
4478 error:
4479 if (new_memo)
4480 PyMemoTable_Del(new_memo);
4481 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004482}
4483
4484static PyObject *
4485Pickler_get_persid(PicklerObject *self)
4486{
4487 if (self->pers_func == NULL)
4488 PyErr_SetString(PyExc_AttributeError, "persistent_id");
4489 else
4490 Py_INCREF(self->pers_func);
4491 return self->pers_func;
4492}
4493
4494static int
4495Pickler_set_persid(PicklerObject *self, PyObject *value)
4496{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004497 if (value == NULL) {
4498 PyErr_SetString(PyExc_TypeError,
4499 "attribute deletion is not supported");
4500 return -1;
4501 }
4502 if (!PyCallable_Check(value)) {
4503 PyErr_SetString(PyExc_TypeError,
4504 "persistent_id must be a callable taking one argument");
4505 return -1;
4506 }
4507
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004508 Py_INCREF(value);
Serhiy Storchaka576f1322016-01-05 21:27:54 +02004509 Py_SETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004510
4511 return 0;
4512}
4513
4514static PyMemberDef Pickler_members[] = {
4515 {"bin", T_INT, offsetof(PicklerObject, bin)},
4516 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004517 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004518 {NULL}
4519};
4520
4521static PyGetSetDef Pickler_getsets[] = {
4522 {"memo", (getter)Pickler_get_memo,
4523 (setter)Pickler_set_memo},
4524 {"persistent_id", (getter)Pickler_get_persid,
4525 (setter)Pickler_set_persid},
4526 {NULL}
4527};
4528
4529static PyTypeObject Pickler_Type = {
4530 PyVarObject_HEAD_INIT(NULL, 0)
4531 "_pickle.Pickler" , /*tp_name*/
4532 sizeof(PicklerObject), /*tp_basicsize*/
4533 0, /*tp_itemsize*/
4534 (destructor)Pickler_dealloc, /*tp_dealloc*/
4535 0, /*tp_print*/
4536 0, /*tp_getattr*/
4537 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004538 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004539 0, /*tp_repr*/
4540 0, /*tp_as_number*/
4541 0, /*tp_as_sequence*/
4542 0, /*tp_as_mapping*/
4543 0, /*tp_hash*/
4544 0, /*tp_call*/
4545 0, /*tp_str*/
4546 0, /*tp_getattro*/
4547 0, /*tp_setattro*/
4548 0, /*tp_as_buffer*/
4549 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004550 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004551 (traverseproc)Pickler_traverse, /*tp_traverse*/
4552 (inquiry)Pickler_clear, /*tp_clear*/
4553 0, /*tp_richcompare*/
4554 0, /*tp_weaklistoffset*/
4555 0, /*tp_iter*/
4556 0, /*tp_iternext*/
4557 Pickler_methods, /*tp_methods*/
4558 Pickler_members, /*tp_members*/
4559 Pickler_getsets, /*tp_getset*/
4560 0, /*tp_base*/
4561 0, /*tp_dict*/
4562 0, /*tp_descr_get*/
4563 0, /*tp_descr_set*/
4564 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004565 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004566 PyType_GenericAlloc, /*tp_alloc*/
4567 PyType_GenericNew, /*tp_new*/
4568 PyObject_GC_Del, /*tp_free*/
4569 0, /*tp_is_gc*/
4570};
4571
Victor Stinner121aab42011-09-29 23:40:53 +02004572/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004573
4574 XXX: It would be nice to able to avoid Python function call overhead, by
4575 using directly the C version of find_class(), when find_class() is not
4576 overridden by a subclass. Although, this could become rather hackish. A
4577 simpler optimization would be to call the C function when self is not a
4578 subclass instance. */
4579static PyObject *
4580find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4581{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004582 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004583
4584 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4585 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004586}
4587
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004588static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004589marker(UnpicklerObject *self)
4590{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004591 Py_ssize_t mark;
4592
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004593 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004594 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004595 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004596 return -1;
4597 }
4598
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004599 mark = self->marks[--self->num_marks];
4600 self->stack->mark_set = self->num_marks != 0;
4601 self->stack->fence = self->num_marks ?
4602 self->marks[self->num_marks - 1] : 0;
4603 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004604}
4605
4606static int
4607load_none(UnpicklerObject *self)
4608{
4609 PDATA_APPEND(self->stack, Py_None, -1);
4610 return 0;
4611}
4612
4613static int
4614bad_readline(void)
4615{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004616 PickleState *st = _Pickle_GetGlobalState();
4617 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004618 return -1;
4619}
4620
4621static int
4622load_int(UnpicklerObject *self)
4623{
4624 PyObject *value;
4625 char *endptr, *s;
4626 Py_ssize_t len;
4627 long x;
4628
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004629 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004630 return -1;
4631 if (len < 2)
4632 return bad_readline();
4633
4634 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004635 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004636 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004637 x = strtol(s, &endptr, 0);
4638
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004639 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004640 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004641 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004642 errno = 0;
4643 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004644 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004645 if (value == NULL) {
4646 PyErr_SetString(PyExc_ValueError,
4647 "could not convert string to int");
4648 return -1;
4649 }
4650 }
4651 else {
4652 if (len == 3 && (x == 0 || x == 1)) {
4653 if ((value = PyBool_FromLong(x)) == NULL)
4654 return -1;
4655 }
4656 else {
4657 if ((value = PyLong_FromLong(x)) == NULL)
4658 return -1;
4659 }
4660 }
4661
4662 PDATA_PUSH(self->stack, value, -1);
4663 return 0;
4664}
4665
4666static int
4667load_bool(UnpicklerObject *self, PyObject *boolean)
4668{
4669 assert(boolean == Py_True || boolean == Py_False);
4670 PDATA_APPEND(self->stack, boolean, -1);
4671 return 0;
4672}
4673
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004674/* s contains x bytes of an unsigned little-endian integer. Return its value
4675 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4676 */
4677static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004678calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004679{
4680 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004681 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004682 size_t x = 0;
4683
Serhiy Storchakae0606192015-09-29 22:10:07 +03004684 if (nbytes > (int)sizeof(size_t)) {
4685 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4686 * have 64-bit size that can't be represented on 32-bit platform.
4687 */
4688 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4689 if (s[i])
4690 return -1;
4691 }
4692 nbytes = (int)sizeof(size_t);
4693 }
4694 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004695 x |= (size_t) s[i] << (8 * i);
4696 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004697
4698 if (x > PY_SSIZE_T_MAX)
4699 return -1;
4700 else
4701 return (Py_ssize_t) x;
4702}
4703
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004704/* s contains x bytes of a little-endian integer. Return its value as a
4705 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4706 * int, but when x is 4 it's a signed one. This is an historical source
4707 * of x-platform bugs.
4708 */
4709static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004710calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004711{
4712 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004713 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004714 long x = 0;
4715
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004716 for (i = 0; i < nbytes; i++) {
4717 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 }
4719
4720 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4721 * is signed, so on a box with longs bigger than 4 bytes we need
4722 * to extend a BININT's sign bit to the full width.
4723 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004724 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004725 x |= -(x & (1L << 31));
4726 }
4727
4728 return x;
4729}
4730
4731static int
4732load_binintx(UnpicklerObject *self, char *s, int size)
4733{
4734 PyObject *value;
4735 long x;
4736
4737 x = calc_binint(s, size);
4738
4739 if ((value = PyLong_FromLong(x)) == NULL)
4740 return -1;
4741
4742 PDATA_PUSH(self->stack, value, -1);
4743 return 0;
4744}
4745
4746static int
4747load_binint(UnpicklerObject *self)
4748{
4749 char *s;
4750
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004751 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004752 return -1;
4753
4754 return load_binintx(self, s, 4);
4755}
4756
4757static int
4758load_binint1(UnpicklerObject *self)
4759{
4760 char *s;
4761
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004762 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004763 return -1;
4764
4765 return load_binintx(self, s, 1);
4766}
4767
4768static int
4769load_binint2(UnpicklerObject *self)
4770{
4771 char *s;
4772
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004773 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004774 return -1;
4775
4776 return load_binintx(self, s, 2);
4777}
4778
4779static int
4780load_long(UnpicklerObject *self)
4781{
4782 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004783 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004784 Py_ssize_t len;
4785
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004786 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004787 return -1;
4788 if (len < 2)
4789 return bad_readline();
4790
Mark Dickinson8dd05142009-01-20 20:43:58 +00004791 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4792 the 'L' before calling PyLong_FromString. In order to maintain
4793 compatibility with Python 3.0.0, we don't actually *require*
4794 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004795 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004796 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004797 /* XXX: Should the base argument explicitly set to 10? */
4798 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004799 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004800 return -1;
4801
4802 PDATA_PUSH(self->stack, value, -1);
4803 return 0;
4804}
4805
4806/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4807 * data following.
4808 */
4809static int
4810load_counted_long(UnpicklerObject *self, int size)
4811{
4812 PyObject *value;
4813 char *nbytes;
4814 char *pdata;
4815
4816 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004817 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004818 return -1;
4819
4820 size = calc_binint(nbytes, size);
4821 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004822 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004823 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004824 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004825 "LONG pickle has negative byte count");
4826 return -1;
4827 }
4828
4829 if (size == 0)
4830 value = PyLong_FromLong(0L);
4831 else {
4832 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004833 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004834 return -1;
4835 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4836 1 /* little endian */ , 1 /* signed */ );
4837 }
4838 if (value == NULL)
4839 return -1;
4840 PDATA_PUSH(self->stack, value, -1);
4841 return 0;
4842}
4843
4844static int
4845load_float(UnpicklerObject *self)
4846{
4847 PyObject *value;
4848 char *endptr, *s;
4849 Py_ssize_t len;
4850 double d;
4851
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004852 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004853 return -1;
4854 if (len < 2)
4855 return bad_readline();
4856
4857 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004858 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4859 if (d == -1.0 && PyErr_Occurred())
4860 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004861 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004862 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4863 return -1;
4864 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004865 value = PyFloat_FromDouble(d);
4866 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004867 return -1;
4868
4869 PDATA_PUSH(self->stack, value, -1);
4870 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004871}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004872
4873static int
4874load_binfloat(UnpicklerObject *self)
4875{
4876 PyObject *value;
4877 double x;
4878 char *s;
4879
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004880 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004881 return -1;
4882
4883 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4884 if (x == -1.0 && PyErr_Occurred())
4885 return -1;
4886
4887 if ((value = PyFloat_FromDouble(x)) == NULL)
4888 return -1;
4889
4890 PDATA_PUSH(self->stack, value, -1);
4891 return 0;
4892}
4893
4894static int
4895load_string(UnpicklerObject *self)
4896{
4897 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004898 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004899 Py_ssize_t len;
4900 char *s, *p;
4901
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004902 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004904 /* Strip the newline */
4905 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004906 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004907 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004908 p = s + 1;
4909 len -= 2;
4910 }
4911 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004912 PickleState *st = _Pickle_GetGlobalState();
4913 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004914 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004915 return -1;
4916 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004917 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004918
4919 /* Use the PyBytes API to decode the string, since that is what is used
4920 to encode, and then coerce the result to Unicode. */
4921 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922 if (bytes == NULL)
4923 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004924
4925 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4926 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4927 if (strcmp(self->encoding, "bytes") == 0) {
4928 obj = bytes;
4929 }
4930 else {
4931 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4932 Py_DECREF(bytes);
4933 if (obj == NULL) {
4934 return -1;
4935 }
4936 }
4937
4938 PDATA_PUSH(self->stack, obj, -1);
4939 return 0;
4940}
4941
4942static int
4943load_counted_binstring(UnpicklerObject *self, int nbytes)
4944{
4945 PyObject *obj;
4946 Py_ssize_t size;
4947 char *s;
4948
4949 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004950 return -1;
4951
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004952 size = calc_binsize(s, nbytes);
4953 if (size < 0) {
4954 PickleState *st = _Pickle_GetGlobalState();
4955 PyErr_Format(st->UnpicklingError,
4956 "BINSTRING exceeds system's maximum size of %zd bytes",
4957 PY_SSIZE_T_MAX);
4958 return -1;
4959 }
4960
4961 if (_Unpickler_Read(self, &s, size) < 0)
4962 return -1;
4963
4964 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4965 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4966 if (strcmp(self->encoding, "bytes") == 0) {
4967 obj = PyBytes_FromStringAndSize(s, size);
4968 }
4969 else {
4970 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4971 }
4972 if (obj == NULL) {
4973 return -1;
4974 }
4975
4976 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004977 return 0;
4978}
4979
4980static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004981load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004982{
4983 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004984 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004985 char *s;
4986
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004987 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004988 return -1;
4989
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004990 size = calc_binsize(s, nbytes);
4991 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004992 PyErr_Format(PyExc_OverflowError,
4993 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004994 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004995 return -1;
4996 }
4997
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004998 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005000
5001 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005002 if (bytes == NULL)
5003 return -1;
5004
5005 PDATA_PUSH(self->stack, bytes, -1);
5006 return 0;
5007}
5008
5009static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005010load_unicode(UnpicklerObject *self)
5011{
5012 PyObject *str;
5013 Py_ssize_t len;
5014 char *s;
5015
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005016 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005017 return -1;
5018 if (len < 1)
5019 return bad_readline();
5020
5021 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5022 if (str == NULL)
5023 return -1;
5024
5025 PDATA_PUSH(self->stack, str, -1);
5026 return 0;
5027}
5028
5029static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005030load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005031{
5032 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005033 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005034 char *s;
5035
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005036 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005037 return -1;
5038
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005039 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005040 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005041 PyErr_Format(PyExc_OverflowError,
5042 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005043 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 return -1;
5045 }
5046
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005047 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
5049
Victor Stinner485fb562010-04-13 11:07:24 +00005050 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005051 if (str == NULL)
5052 return -1;
5053
5054 PDATA_PUSH(self->stack, str, -1);
5055 return 0;
5056}
5057
5058static int
Victor Stinner21b47112016-03-14 18:09:39 +01005059load_counted_tuple(UnpicklerObject *self, Py_ssize_t len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005060{
5061 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005062
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005063 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005064 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005065
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005066 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005067 if (tuple == NULL)
5068 return -1;
5069 PDATA_PUSH(self->stack, tuple, -1);
5070 return 0;
5071}
5072
5073static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005074load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005075{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005076 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005077
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005078 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079 return -1;
5080
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005081 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005082}
5083
5084static int
5085load_empty_list(UnpicklerObject *self)
5086{
5087 PyObject *list;
5088
5089 if ((list = PyList_New(0)) == NULL)
5090 return -1;
5091 PDATA_PUSH(self->stack, list, -1);
5092 return 0;
5093}
5094
5095static int
5096load_empty_dict(UnpicklerObject *self)
5097{
5098 PyObject *dict;
5099
5100 if ((dict = PyDict_New()) == NULL)
5101 return -1;
5102 PDATA_PUSH(self->stack, dict, -1);
5103 return 0;
5104}
5105
5106static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005107load_empty_set(UnpicklerObject *self)
5108{
5109 PyObject *set;
5110
5111 if ((set = PySet_New(NULL)) == NULL)
5112 return -1;
5113 PDATA_PUSH(self->stack, set, -1);
5114 return 0;
5115}
5116
5117static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005118load_list(UnpicklerObject *self)
5119{
5120 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005121 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122
5123 if ((i = marker(self)) < 0)
5124 return -1;
5125
5126 list = Pdata_poplist(self->stack, i);
5127 if (list == NULL)
5128 return -1;
5129 PDATA_PUSH(self->stack, list, -1);
5130 return 0;
5131}
5132
5133static int
5134load_dict(UnpicklerObject *self)
5135{
5136 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005137 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005138
5139 if ((i = marker(self)) < 0)
5140 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005141 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142
5143 if ((dict = PyDict_New()) == NULL)
5144 return -1;
5145
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005146 if ((j - i) % 2 != 0) {
5147 PickleState *st = _Pickle_GetGlobalState();
5148 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005149 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005150 return -1;
5151 }
5152
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005153 for (k = i + 1; k < j; k += 2) {
5154 key = self->stack->data[k - 1];
5155 value = self->stack->data[k];
5156 if (PyDict_SetItem(dict, key, value) < 0) {
5157 Py_DECREF(dict);
5158 return -1;
5159 }
5160 }
5161 Pdata_clear(self->stack, i);
5162 PDATA_PUSH(self->stack, dict, -1);
5163 return 0;
5164}
5165
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005166static int
5167load_frozenset(UnpicklerObject *self)
5168{
5169 PyObject *items;
5170 PyObject *frozenset;
5171 Py_ssize_t i;
5172
5173 if ((i = marker(self)) < 0)
5174 return -1;
5175
5176 items = Pdata_poptuple(self->stack, i);
5177 if (items == NULL)
5178 return -1;
5179
5180 frozenset = PyFrozenSet_New(items);
5181 Py_DECREF(items);
5182 if (frozenset == NULL)
5183 return -1;
5184
5185 PDATA_PUSH(self->stack, frozenset, -1);
5186 return 0;
5187}
5188
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005189static PyObject *
5190instantiate(PyObject *cls, PyObject *args)
5191{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005192 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005193 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005194 /* Caller must assure args are a tuple. Normally, args come from
5195 Pdata_poptuple which packs objects from the top of the stack
5196 into a newly created tuple. */
5197 assert(PyTuple_Check(args));
5198 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005199 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005200 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005201 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005202 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005203 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005204
5205 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005206 }
5207 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005208}
5209
5210static int
5211load_obj(UnpicklerObject *self)
5212{
5213 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005214 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005215
5216 if ((i = marker(self)) < 0)
5217 return -1;
5218
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005219 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005220 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005221
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005222 args = Pdata_poptuple(self->stack, i + 1);
5223 if (args == NULL)
5224 return -1;
5225
5226 PDATA_POP(self->stack, cls);
5227 if (cls) {
5228 obj = instantiate(cls, args);
5229 Py_DECREF(cls);
5230 }
5231 Py_DECREF(args);
5232 if (obj == NULL)
5233 return -1;
5234
5235 PDATA_PUSH(self->stack, obj, -1);
5236 return 0;
5237}
5238
5239static int
5240load_inst(UnpicklerObject *self)
5241{
5242 PyObject *cls = NULL;
5243 PyObject *args = NULL;
5244 PyObject *obj = NULL;
5245 PyObject *module_name;
5246 PyObject *class_name;
5247 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005248 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005249 char *s;
5250
5251 if ((i = marker(self)) < 0)
5252 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005253 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005254 return -1;
5255 if (len < 2)
5256 return bad_readline();
5257
5258 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5259 identifiers are permitted in Python 3.0, since the INST opcode is only
5260 supported by older protocols on Python 2.x. */
5261 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5262 if (module_name == NULL)
5263 return -1;
5264
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005265 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005266 if (len < 2) {
5267 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005268 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005269 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005270 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005271 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005272 cls = find_class(self, module_name, class_name);
5273 Py_DECREF(class_name);
5274 }
5275 }
5276 Py_DECREF(module_name);
5277
5278 if (cls == NULL)
5279 return -1;
5280
5281 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5282 obj = instantiate(cls, args);
5283 Py_DECREF(args);
5284 }
5285 Py_DECREF(cls);
5286
5287 if (obj == NULL)
5288 return -1;
5289
5290 PDATA_PUSH(self->stack, obj, -1);
5291 return 0;
5292}
5293
5294static int
5295load_newobj(UnpicklerObject *self)
5296{
5297 PyObject *args = NULL;
5298 PyObject *clsraw = NULL;
5299 PyTypeObject *cls; /* clsraw cast to its true type */
5300 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005301 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005302
5303 /* Stack is ... cls argtuple, and we want to call
5304 * cls.__new__(cls, *argtuple).
5305 */
5306 PDATA_POP(self->stack, args);
5307 if (args == NULL)
5308 goto error;
5309 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005310 PyErr_SetString(st->UnpicklingError,
5311 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005312 goto error;
5313 }
5314
5315 PDATA_POP(self->stack, clsraw);
5316 cls = (PyTypeObject *)clsraw;
5317 if (cls == NULL)
5318 goto error;
5319 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005320 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005321 "isn't a type object");
5322 goto error;
5323 }
5324 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005325 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005326 "has NULL tp_new");
5327 goto error;
5328 }
5329
5330 /* Call __new__. */
5331 obj = cls->tp_new(cls, args, NULL);
5332 if (obj == NULL)
5333 goto error;
5334
5335 Py_DECREF(args);
5336 Py_DECREF(clsraw);
5337 PDATA_PUSH(self->stack, obj, -1);
5338 return 0;
5339
5340 error:
5341 Py_XDECREF(args);
5342 Py_XDECREF(clsraw);
5343 return -1;
5344}
5345
5346static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005347load_newobj_ex(UnpicklerObject *self)
5348{
5349 PyObject *cls, *args, *kwargs;
5350 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005351 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005352
5353 PDATA_POP(self->stack, kwargs);
5354 if (kwargs == NULL) {
5355 return -1;
5356 }
5357 PDATA_POP(self->stack, args);
5358 if (args == NULL) {
5359 Py_DECREF(kwargs);
5360 return -1;
5361 }
5362 PDATA_POP(self->stack, cls);
5363 if (cls == NULL) {
5364 Py_DECREF(kwargs);
5365 Py_DECREF(args);
5366 return -1;
5367 }
Larry Hastings61272b72014-01-07 12:41:53 -08005368
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005369 if (!PyType_Check(cls)) {
5370 Py_DECREF(kwargs);
5371 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005372 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005373 "NEWOBJ_EX class argument must be a type, not %.200s",
5374 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005375 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005376 return -1;
5377 }
5378
5379 if (((PyTypeObject *)cls)->tp_new == NULL) {
5380 Py_DECREF(kwargs);
5381 Py_DECREF(args);
5382 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005383 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005384 "NEWOBJ_EX class argument doesn't have __new__");
5385 return -1;
5386 }
5387 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5388 Py_DECREF(kwargs);
5389 Py_DECREF(args);
5390 Py_DECREF(cls);
5391 if (obj == NULL) {
5392 return -1;
5393 }
5394 PDATA_PUSH(self->stack, obj, -1);
5395 return 0;
5396}
5397
5398static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005399load_global(UnpicklerObject *self)
5400{
5401 PyObject *global = NULL;
5402 PyObject *module_name;
5403 PyObject *global_name;
5404 Py_ssize_t len;
5405 char *s;
5406
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005407 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005408 return -1;
5409 if (len < 2)
5410 return bad_readline();
5411 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5412 if (!module_name)
5413 return -1;
5414
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005415 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005416 if (len < 2) {
5417 Py_DECREF(module_name);
5418 return bad_readline();
5419 }
5420 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5421 if (global_name) {
5422 global = find_class(self, module_name, global_name);
5423 Py_DECREF(global_name);
5424 }
5425 }
5426 Py_DECREF(module_name);
5427
5428 if (global == NULL)
5429 return -1;
5430 PDATA_PUSH(self->stack, global, -1);
5431 return 0;
5432}
5433
5434static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005435load_stack_global(UnpicklerObject *self)
5436{
5437 PyObject *global;
5438 PyObject *module_name;
5439 PyObject *global_name;
5440
5441 PDATA_POP(self->stack, global_name);
5442 PDATA_POP(self->stack, module_name);
5443 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5444 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005445 PickleState *st = _Pickle_GetGlobalState();
5446 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005447 Py_XDECREF(global_name);
5448 Py_XDECREF(module_name);
5449 return -1;
5450 }
5451 global = find_class(self, module_name, global_name);
5452 Py_DECREF(global_name);
5453 Py_DECREF(module_name);
5454 if (global == NULL)
5455 return -1;
5456 PDATA_PUSH(self->stack, global, -1);
5457 return 0;
5458}
5459
5460static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005461load_persid(UnpicklerObject *self)
5462{
5463 PyObject *pid;
5464 Py_ssize_t len;
5465 char *s;
5466
5467 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005468 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005469 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005470 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005471 return bad_readline();
5472
5473 pid = PyBytes_FromStringAndSize(s, len - 1);
5474 if (pid == NULL)
5475 return -1;
5476
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005477 /* This does not leak since _Pickle_FastCall() steals the reference
5478 to pid first. */
5479 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005480 if (pid == NULL)
5481 return -1;
5482
5483 PDATA_PUSH(self->stack, pid, -1);
5484 return 0;
5485 }
5486 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005487 PickleState *st = _Pickle_GetGlobalState();
5488 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005489 "A load persistent id instruction was encountered,\n"
5490 "but no persistent_load function was specified.");
5491 return -1;
5492 }
5493}
5494
5495static int
5496load_binpersid(UnpicklerObject *self)
5497{
5498 PyObject *pid;
5499
5500 if (self->pers_func) {
5501 PDATA_POP(self->stack, pid);
5502 if (pid == NULL)
5503 return -1;
5504
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005505 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005506 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005507 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005508 if (pid == NULL)
5509 return -1;
5510
5511 PDATA_PUSH(self->stack, pid, -1);
5512 return 0;
5513 }
5514 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005515 PickleState *st = _Pickle_GetGlobalState();
5516 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005517 "A load persistent id instruction was encountered,\n"
5518 "but no persistent_load function was specified.");
5519 return -1;
5520 }
5521}
5522
5523static int
5524load_pop(UnpicklerObject *self)
5525{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005526 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005527
5528 /* Note that we split the (pickle.py) stack into two stacks,
5529 * an object stack and a mark stack. We have to be clever and
5530 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005531 * mark stack first, and only signalling a stack underflow if
5532 * the object stack is empty and the mark stack doesn't match
5533 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005534 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005535 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005536 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005537 self->stack->mark_set = self->num_marks != 0;
5538 self->stack->fence = self->num_marks ?
5539 self->marks[self->num_marks - 1] : 0;
5540 } else if (len <= self->stack->fence)
5541 return Pdata_stack_underflow(self->stack);
5542 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005543 len--;
5544 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005545 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005546 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 return 0;
5548}
5549
5550static int
5551load_pop_mark(UnpicklerObject *self)
5552{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005553 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005554
5555 if ((i = marker(self)) < 0)
5556 return -1;
5557
5558 Pdata_clear(self->stack, i);
5559
5560 return 0;
5561}
5562
5563static int
5564load_dup(UnpicklerObject *self)
5565{
5566 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005567 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005568
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005569 if (len <= self->stack->fence)
5570 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005571 last = self->stack->data[len - 1];
5572 PDATA_APPEND(self->stack, last, -1);
5573 return 0;
5574}
5575
5576static int
5577load_get(UnpicklerObject *self)
5578{
5579 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005580 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005581 Py_ssize_t len;
5582 char *s;
5583
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005584 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 return -1;
5586 if (len < 2)
5587 return bad_readline();
5588
5589 key = PyLong_FromString(s, NULL, 10);
5590 if (key == NULL)
5591 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005592 idx = PyLong_AsSsize_t(key);
5593 if (idx == -1 && PyErr_Occurred()) {
5594 Py_DECREF(key);
5595 return -1;
5596 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005597
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005598 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005599 if (value == NULL) {
5600 if (!PyErr_Occurred())
5601 PyErr_SetObject(PyExc_KeyError, key);
5602 Py_DECREF(key);
5603 return -1;
5604 }
5605 Py_DECREF(key);
5606
5607 PDATA_APPEND(self->stack, value, -1);
5608 return 0;
5609}
5610
5611static int
5612load_binget(UnpicklerObject *self)
5613{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005614 PyObject *value;
5615 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005616 char *s;
5617
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005618 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005619 return -1;
5620
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005621 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005622
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005623 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005624 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005625 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005626 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005627 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005628 Py_DECREF(key);
5629 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005630 return -1;
5631 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005632
5633 PDATA_APPEND(self->stack, value, -1);
5634 return 0;
5635}
5636
5637static int
5638load_long_binget(UnpicklerObject *self)
5639{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005640 PyObject *value;
5641 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005642 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005643
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005644 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005645 return -1;
5646
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005647 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005648
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005649 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005650 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005651 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005652 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005653 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005654 Py_DECREF(key);
5655 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005656 return -1;
5657 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005658
5659 PDATA_APPEND(self->stack, value, -1);
5660 return 0;
5661}
5662
5663/* Push an object from the extension registry (EXT[124]). nbytes is
5664 * the number of bytes following the opcode, holding the index (code) value.
5665 */
5666static int
5667load_extension(UnpicklerObject *self, int nbytes)
5668{
5669 char *codebytes; /* the nbytes bytes after the opcode */
5670 long code; /* calc_binint returns long */
5671 PyObject *py_code; /* code as a Python int */
5672 PyObject *obj; /* the object to push */
5673 PyObject *pair; /* (module_name, class_name) */
5674 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005675 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005676
5677 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005678 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005679 return -1;
5680 code = calc_binint(codebytes, nbytes);
5681 if (code <= 0) { /* note that 0 is forbidden */
5682 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005683 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005684 return -1;
5685 }
5686
5687 /* Look for the code in the cache. */
5688 py_code = PyLong_FromLong(code);
5689 if (py_code == NULL)
5690 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005691 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005692 if (obj != NULL) {
5693 /* Bingo. */
5694 Py_DECREF(py_code);
5695 PDATA_APPEND(self->stack, obj, -1);
5696 return 0;
5697 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005698 if (PyErr_Occurred()) {
5699 Py_DECREF(py_code);
5700 return -1;
5701 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005702
5703 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005704 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005705 if (pair == NULL) {
5706 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005707 if (!PyErr_Occurred()) {
5708 PyErr_Format(PyExc_ValueError, "unregistered extension "
5709 "code %ld", code);
5710 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005711 return -1;
5712 }
5713 /* Since the extension registry is manipulable via Python code,
5714 * confirm that pair is really a 2-tuple of strings.
5715 */
5716 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5717 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5718 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5719 Py_DECREF(py_code);
5720 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5721 "isn't a 2-tuple of strings", code);
5722 return -1;
5723 }
5724 /* Load the object. */
5725 obj = find_class(self, module_name, class_name);
5726 if (obj == NULL) {
5727 Py_DECREF(py_code);
5728 return -1;
5729 }
5730 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005731 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005732 Py_DECREF(py_code);
5733 if (code < 0) {
5734 Py_DECREF(obj);
5735 return -1;
5736 }
5737 PDATA_PUSH(self->stack, obj, -1);
5738 return 0;
5739}
5740
5741static int
5742load_put(UnpicklerObject *self)
5743{
5744 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005745 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005746 Py_ssize_t len;
5747 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005748
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005749 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750 return -1;
5751 if (len < 2)
5752 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005753 if (Py_SIZE(self->stack) <= self->stack->fence)
5754 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005755 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005756
5757 key = PyLong_FromString(s, NULL, 10);
5758 if (key == NULL)
5759 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005760 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005761 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005762 if (idx < 0) {
5763 if (!PyErr_Occurred())
5764 PyErr_SetString(PyExc_ValueError,
5765 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005766 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005767 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005768
5769 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005770}
5771
5772static int
5773load_binput(UnpicklerObject *self)
5774{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005775 PyObject *value;
5776 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005777 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005778
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005780 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005781
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005782 if (Py_SIZE(self->stack) <= self->stack->fence)
5783 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005784 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005785
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005786 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005787
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789}
5790
5791static int
5792load_long_binput(UnpicklerObject *self)
5793{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005794 PyObject *value;
5795 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005796 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005797
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005799 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005800
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005801 if (Py_SIZE(self->stack) <= self->stack->fence)
5802 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005803 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005804
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005805 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005806 if (idx < 0) {
5807 PyErr_SetString(PyExc_ValueError,
5808 "negative LONG_BINPUT argument");
5809 return -1;
5810 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005811
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005812 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005813}
5814
5815static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005816load_memoize(UnpicklerObject *self)
5817{
5818 PyObject *value;
5819
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005820 if (Py_SIZE(self->stack) <= self->stack->fence)
5821 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005822 value = self->stack->data[Py_SIZE(self->stack) - 1];
5823
5824 return _Unpickler_MemoPut(self, self->memo_len, value);
5825}
5826
5827static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005828do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005829{
5830 PyObject *value;
5831 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005832 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005834 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005835 if (x > len || x <= self->stack->fence)
5836 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837 if (len == x) /* nothing to do */
5838 return 0;
5839
5840 list = self->stack->data[x - 1];
5841
5842 if (PyList_Check(list)) {
5843 PyObject *slice;
5844 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005845 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005846
5847 slice = Pdata_poplist(self->stack, x);
5848 if (!slice)
5849 return -1;
5850 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005851 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005852 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005853 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005854 }
5855 else {
5856 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005857 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005859 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005860 if (append_func == NULL)
5861 return -1;
5862 for (i = x; i < len; i++) {
5863 PyObject *result;
5864
5865 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005866 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005867 if (result == NULL) {
5868 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005869 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005870 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871 return -1;
5872 }
5873 Py_DECREF(result);
5874 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005875 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005876 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005877 }
5878
5879 return 0;
5880}
5881
5882static int
5883load_append(UnpicklerObject *self)
5884{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005885 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5886 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005887 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005888}
5889
5890static int
5891load_appends(UnpicklerObject *self)
5892{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005893 Py_ssize_t i = marker(self);
5894 if (i < 0)
5895 return -1;
5896 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005897}
5898
5899static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005900do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901{
5902 PyObject *value, *key;
5903 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005904 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905 int status = 0;
5906
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005907 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005908 if (x > len || x <= self->stack->fence)
5909 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005910 if (len == x) /* nothing to do */
5911 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005912 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005913 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005915 PyErr_SetString(st->UnpicklingError,
5916 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005917 return -1;
5918 }
5919
5920 /* Here, dict does not actually need to be a PyDict; it could be anything
5921 that supports the __setitem__ attribute. */
5922 dict = self->stack->data[x - 1];
5923
5924 for (i = x + 1; i < len; i += 2) {
5925 key = self->stack->data[i - 1];
5926 value = self->stack->data[i];
5927 if (PyObject_SetItem(dict, key, value) < 0) {
5928 status = -1;
5929 break;
5930 }
5931 }
5932
5933 Pdata_clear(self->stack, x);
5934 return status;
5935}
5936
5937static int
5938load_setitem(UnpicklerObject *self)
5939{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005940 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005941}
5942
5943static int
5944load_setitems(UnpicklerObject *self)
5945{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005946 Py_ssize_t i = marker(self);
5947 if (i < 0)
5948 return -1;
5949 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005950}
5951
5952static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005953load_additems(UnpicklerObject *self)
5954{
5955 PyObject *set;
5956 Py_ssize_t mark, len, i;
5957
5958 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005959 if (mark < 0)
5960 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005961 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005962 if (mark > len || mark <= self->stack->fence)
5963 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005964 if (len == mark) /* nothing to do */
5965 return 0;
5966
5967 set = self->stack->data[mark - 1];
5968
5969 if (PySet_Check(set)) {
5970 PyObject *items;
5971 int status;
5972
5973 items = Pdata_poptuple(self->stack, mark);
5974 if (items == NULL)
5975 return -1;
5976
5977 status = _PySet_Update(set, items);
5978 Py_DECREF(items);
5979 return status;
5980 }
5981 else {
5982 PyObject *add_func;
5983 _Py_IDENTIFIER(add);
5984
5985 add_func = _PyObject_GetAttrId(set, &PyId_add);
5986 if (add_func == NULL)
5987 return -1;
5988 for (i = mark; i < len; i++) {
5989 PyObject *result;
5990 PyObject *item;
5991
5992 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005993 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005994 if (result == NULL) {
5995 Pdata_clear(self->stack, i + 1);
5996 Py_SIZE(self->stack) = mark;
5997 return -1;
5998 }
5999 Py_DECREF(result);
6000 }
6001 Py_SIZE(self->stack) = mark;
6002 }
6003
6004 return 0;
6005}
6006
6007static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006008load_build(UnpicklerObject *self)
6009{
6010 PyObject *state, *inst, *slotstate;
6011 PyObject *setstate;
6012 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006013 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006014
6015 /* Stack is ... instance, state. We want to leave instance at
6016 * the stack top, possibly mutated via instance.__setstate__(state).
6017 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006018 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6019 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006020
6021 PDATA_POP(self->stack, state);
6022 if (state == NULL)
6023 return -1;
6024
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006025 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006026
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006027 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006028 if (setstate == NULL) {
6029 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6030 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006031 else {
6032 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006033 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006034 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006035 }
6036 else {
6037 PyObject *result;
6038
6039 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006040 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006041 Py_DECREF(setstate);
6042 if (result == NULL)
6043 return -1;
6044 Py_DECREF(result);
6045 return 0;
6046 }
6047
6048 /* A default __setstate__. First see whether state embeds a
6049 * slot state dict too (a proto 2 addition).
6050 */
6051 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6052 PyObject *tmp = state;
6053
6054 state = PyTuple_GET_ITEM(tmp, 0);
6055 slotstate = PyTuple_GET_ITEM(tmp, 1);
6056 Py_INCREF(state);
6057 Py_INCREF(slotstate);
6058 Py_DECREF(tmp);
6059 }
6060 else
6061 slotstate = NULL;
6062
6063 /* Set inst.__dict__ from the state dict (if any). */
6064 if (state != Py_None) {
6065 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006066 PyObject *d_key, *d_value;
6067 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006068 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006069
6070 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006071 PickleState *st = _Pickle_GetGlobalState();
6072 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006073 goto error;
6074 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006075 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006076 if (dict == NULL)
6077 goto error;
6078
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006079 i = 0;
6080 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6081 /* normally the keys for instance attributes are
6082 interned. we should try to do that here. */
6083 Py_INCREF(d_key);
6084 if (PyUnicode_CheckExact(d_key))
6085 PyUnicode_InternInPlace(&d_key);
6086 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6087 Py_DECREF(d_key);
6088 goto error;
6089 }
6090 Py_DECREF(d_key);
6091 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006092 Py_DECREF(dict);
6093 }
6094
6095 /* Also set instance attributes from the slotstate dict (if any). */
6096 if (slotstate != NULL) {
6097 PyObject *d_key, *d_value;
6098 Py_ssize_t i;
6099
6100 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006101 PickleState *st = _Pickle_GetGlobalState();
6102 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006103 "slot state is not a dictionary");
6104 goto error;
6105 }
6106 i = 0;
6107 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6108 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6109 goto error;
6110 }
6111 }
6112
6113 if (0) {
6114 error:
6115 status = -1;
6116 }
6117
6118 Py_DECREF(state);
6119 Py_XDECREF(slotstate);
6120 return status;
6121}
6122
6123static int
6124load_mark(UnpicklerObject *self)
6125{
6126
6127 /* Note that we split the (pickle.py) stack into two stacks, an
6128 * object stack and a mark stack. Here we push a mark onto the
6129 * mark stack.
6130 */
6131
6132 if ((self->num_marks + 1) >= self->marks_size) {
6133 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006134
6135 /* Use the size_t type to check for overflow. */
6136 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006137 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006138 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006139 PyErr_NoMemory();
6140 return -1;
6141 }
6142
6143 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006144 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006145 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006146 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6147 if (self->marks == NULL) {
6148 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149 PyErr_NoMemory();
6150 return -1;
6151 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006152 self->marks_size = (Py_ssize_t)alloc;
6153 }
6154
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006155 self->stack->mark_set = 1;
6156 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006157
6158 return 0;
6159}
6160
6161static int
6162load_reduce(UnpicklerObject *self)
6163{
6164 PyObject *callable = NULL;
6165 PyObject *argtup = NULL;
6166 PyObject *obj = NULL;
6167
6168 PDATA_POP(self->stack, argtup);
6169 if (argtup == NULL)
6170 return -1;
6171 PDATA_POP(self->stack, callable);
6172 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006173 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006174 Py_DECREF(callable);
6175 }
6176 Py_DECREF(argtup);
6177
6178 if (obj == NULL)
6179 return -1;
6180
6181 PDATA_PUSH(self->stack, obj, -1);
6182 return 0;
6183}
6184
6185/* Just raises an error if we don't know the protocol specified. PROTO
6186 * is the first opcode for protocols >= 2.
6187 */
6188static int
6189load_proto(UnpicklerObject *self)
6190{
6191 char *s;
6192 int i;
6193
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006194 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006195 return -1;
6196
6197 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006198 if (i <= HIGHEST_PROTOCOL) {
6199 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006200 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006201 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006202
6203 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6204 return -1;
6205}
6206
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006207static int
6208load_frame(UnpicklerObject *self)
6209{
6210 char *s;
6211 Py_ssize_t frame_len;
6212
6213 if (_Unpickler_Read(self, &s, 8) < 0)
6214 return -1;
6215
6216 frame_len = calc_binsize(s, 8);
6217 if (frame_len < 0) {
6218 PyErr_Format(PyExc_OverflowError,
6219 "FRAME length exceeds system's maximum of %zd bytes",
6220 PY_SSIZE_T_MAX);
6221 return -1;
6222 }
6223
6224 if (_Unpickler_Read(self, &s, frame_len) < 0)
6225 return -1;
6226
6227 /* Rewind to start of frame */
6228 self->next_read_idx -= frame_len;
6229 return 0;
6230}
6231
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006232static PyObject *
6233load(UnpicklerObject *self)
6234{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006235 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006236 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006237
6238 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006239 self->stack->mark_set = 0;
6240 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006241 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006242 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006243 Pdata_clear(self->stack, 0);
6244
6245 /* Convenient macros for the dispatch while-switch loop just below. */
6246#define OP(opcode, load_func) \
6247 case opcode: if (load_func(self) < 0) break; continue;
6248
6249#define OP_ARG(opcode, load_func, arg) \
6250 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6251
6252 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006253 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006254 break;
6255
6256 switch ((enum opcode)s[0]) {
6257 OP(NONE, load_none)
6258 OP(BININT, load_binint)
6259 OP(BININT1, load_binint1)
6260 OP(BININT2, load_binint2)
6261 OP(INT, load_int)
6262 OP(LONG, load_long)
6263 OP_ARG(LONG1, load_counted_long, 1)
6264 OP_ARG(LONG4, load_counted_long, 4)
6265 OP(FLOAT, load_float)
6266 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006267 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6268 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6269 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6270 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6271 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006272 OP(STRING, load_string)
6273 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006274 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6275 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6276 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006277 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6278 OP_ARG(TUPLE1, load_counted_tuple, 1)
6279 OP_ARG(TUPLE2, load_counted_tuple, 2)
6280 OP_ARG(TUPLE3, load_counted_tuple, 3)
6281 OP(TUPLE, load_tuple)
6282 OP(EMPTY_LIST, load_empty_list)
6283 OP(LIST, load_list)
6284 OP(EMPTY_DICT, load_empty_dict)
6285 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006286 OP(EMPTY_SET, load_empty_set)
6287 OP(ADDITEMS, load_additems)
6288 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006289 OP(OBJ, load_obj)
6290 OP(INST, load_inst)
6291 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006292 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006293 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006294 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006295 OP(APPEND, load_append)
6296 OP(APPENDS, load_appends)
6297 OP(BUILD, load_build)
6298 OP(DUP, load_dup)
6299 OP(BINGET, load_binget)
6300 OP(LONG_BINGET, load_long_binget)
6301 OP(GET, load_get)
6302 OP(MARK, load_mark)
6303 OP(BINPUT, load_binput)
6304 OP(LONG_BINPUT, load_long_binput)
6305 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006306 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006307 OP(POP, load_pop)
6308 OP(POP_MARK, load_pop_mark)
6309 OP(SETITEM, load_setitem)
6310 OP(SETITEMS, load_setitems)
6311 OP(PERSID, load_persid)
6312 OP(BINPERSID, load_binpersid)
6313 OP(REDUCE, load_reduce)
6314 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006315 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006316 OP_ARG(EXT1, load_extension, 1)
6317 OP_ARG(EXT2, load_extension, 2)
6318 OP_ARG(EXT4, load_extension, 4)
6319 OP_ARG(NEWTRUE, load_bool, Py_True)
6320 OP_ARG(NEWFALSE, load_bool, Py_False)
6321
6322 case STOP:
6323 break;
6324
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006325 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006326 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006327 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006328 }
6329 else {
6330 PickleState *st = _Pickle_GetGlobalState();
6331 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006332 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006333 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006334 return NULL;
6335 }
6336
6337 break; /* and we are done! */
6338 }
6339
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006340 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006341 return NULL;
6342 }
6343
Victor Stinner2ae57e32013-10-31 13:39:23 +01006344 if (_Unpickler_SkipConsumed(self) < 0)
6345 return NULL;
6346
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006347 PDATA_POP(self->stack, value);
6348 return value;
6349}
6350
Larry Hastings61272b72014-01-07 12:41:53 -08006351/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006352
6353_pickle.Unpickler.load
6354
6355Load a pickle.
6356
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006357Read a pickled object representation from the open file object given
6358in the constructor, and return the reconstituted object hierarchy
6359specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006360[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006361
Larry Hastings3cceb382014-01-04 11:09:09 -08006362static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006363_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006364/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006365{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006366 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006367
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006368 /* Check whether the Unpickler was initialized correctly. This prevents
6369 segfaulting if a subclass overridden __init__ with a function that does
6370 not call Unpickler.__init__(). Here, we simply ensure that self->read
6371 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006372 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006373 PickleState *st = _Pickle_GetGlobalState();
6374 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006375 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006376 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006377 return NULL;
6378 }
6379
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006380 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381}
6382
6383/* The name of find_class() is misleading. In newer pickle protocols, this
6384 function is used for loading any global (i.e., functions), not just
6385 classes. The name is kept only for backward compatibility. */
6386
Larry Hastings61272b72014-01-07 12:41:53 -08006387/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006388
6389_pickle.Unpickler.find_class
6390
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006391 module_name: object
6392 global_name: object
6393 /
6394
6395Return an object from a specified module.
6396
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006397If necessary, the module will be imported. Subclasses may override
6398this method (e.g. to restrict unpickling of arbitrary classes and
6399functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006400
6401This method is called whenever a class or a function object is
6402needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006403[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006404
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006405static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006406_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6407 PyObject *module_name,
6408 PyObject *global_name)
6409/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006410{
6411 PyObject *global;
6412 PyObject *modules_dict;
6413 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006414 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006415
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006416 /* Try to map the old names used in Python 2.x to the new ones used in
6417 Python 3.x. We do this only with old pickle protocols and when the
6418 user has not disabled the feature. */
6419 if (self->proto < 3 && self->fix_imports) {
6420 PyObject *key;
6421 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006422 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006423
6424 /* Check if the global (i.e., a function or a class) was renamed
6425 or moved to another module. */
6426 key = PyTuple_Pack(2, module_name, global_name);
6427 if (key == NULL)
6428 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006429 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006430 Py_DECREF(key);
6431 if (item) {
6432 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6433 PyErr_Format(PyExc_RuntimeError,
6434 "_compat_pickle.NAME_MAPPING values should be "
6435 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6436 return NULL;
6437 }
6438 module_name = PyTuple_GET_ITEM(item, 0);
6439 global_name = PyTuple_GET_ITEM(item, 1);
6440 if (!PyUnicode_Check(module_name) ||
6441 !PyUnicode_Check(global_name)) {
6442 PyErr_Format(PyExc_RuntimeError,
6443 "_compat_pickle.NAME_MAPPING values should be "
6444 "pairs of str, not (%.200s, %.200s)",
6445 Py_TYPE(module_name)->tp_name,
6446 Py_TYPE(global_name)->tp_name);
6447 return NULL;
6448 }
6449 }
6450 else if (PyErr_Occurred()) {
6451 return NULL;
6452 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006453 else {
6454 /* Check if the module was renamed. */
6455 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6456 if (item) {
6457 if (!PyUnicode_Check(item)) {
6458 PyErr_Format(PyExc_RuntimeError,
6459 "_compat_pickle.IMPORT_MAPPING values should be "
6460 "strings, not %.200s", Py_TYPE(item)->tp_name);
6461 return NULL;
6462 }
6463 module_name = item;
6464 }
6465 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006466 return NULL;
6467 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006468 }
6469 }
6470
Victor Stinnerbb520202013-11-06 22:40:41 +01006471 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006472 if (modules_dict == NULL) {
6473 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006474 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006475 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006476
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006477 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006479 if (PyErr_Occurred())
6480 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006481 module = PyImport_Import(module_name);
6482 if (module == NULL)
6483 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006484 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 Py_DECREF(module);
6486 }
Victor Stinner121aab42011-09-29 23:40:53 +02006487 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006488 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006489 }
6490 return global;
6491}
6492
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006493/*[clinic input]
6494
6495_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6496
6497Returns size in memory, in bytes.
6498[clinic start generated code]*/
6499
6500static Py_ssize_t
6501_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6502/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6503{
6504 Py_ssize_t res;
6505
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006506 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006507 if (self->memo != NULL)
6508 res += self->memo_size * sizeof(PyObject *);
6509 if (self->marks != NULL)
6510 res += self->marks_size * sizeof(Py_ssize_t);
6511 if (self->input_line != NULL)
6512 res += strlen(self->input_line) + 1;
6513 if (self->encoding != NULL)
6514 res += strlen(self->encoding) + 1;
6515 if (self->errors != NULL)
6516 res += strlen(self->errors) + 1;
6517 return res;
6518}
6519
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006520static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006521 _PICKLE_UNPICKLER_LOAD_METHODDEF
6522 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006523 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006524 {NULL, NULL} /* sentinel */
6525};
6526
6527static void
6528Unpickler_dealloc(UnpicklerObject *self)
6529{
6530 PyObject_GC_UnTrack((PyObject *)self);
6531 Py_XDECREF(self->readline);
6532 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006533 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006534 Py_XDECREF(self->stack);
6535 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006536 if (self->buffer.buf != NULL) {
6537 PyBuffer_Release(&self->buffer);
6538 self->buffer.buf = NULL;
6539 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006540
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006541 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006542 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006543 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006544 PyMem_Free(self->encoding);
6545 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546
6547 Py_TYPE(self)->tp_free((PyObject *)self);
6548}
6549
6550static int
6551Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6552{
6553 Py_VISIT(self->readline);
6554 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006555 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006556 Py_VISIT(self->stack);
6557 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006558 return 0;
6559}
6560
6561static int
6562Unpickler_clear(UnpicklerObject *self)
6563{
6564 Py_CLEAR(self->readline);
6565 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006566 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006567 Py_CLEAR(self->stack);
6568 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006569 if (self->buffer.buf != NULL) {
6570 PyBuffer_Release(&self->buffer);
6571 self->buffer.buf = NULL;
6572 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006573
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006574 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006575 PyMem_Free(self->marks);
6576 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006577 PyMem_Free(self->input_line);
6578 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006579 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006580 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006581 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006582 self->errors = NULL;
6583
6584 return 0;
6585}
6586
Larry Hastings61272b72014-01-07 12:41:53 -08006587/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006588
6589_pickle.Unpickler.__init__
6590
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006591 file: object
6592 *
6593 fix_imports: bool = True
6594 encoding: str = 'ASCII'
6595 errors: str = 'strict'
6596
6597This takes a binary file for reading a pickle data stream.
6598
6599The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006600protocol argument is needed. Bytes past the pickled object's
6601representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006602
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006603The argument *file* must have two methods, a read() method that takes
6604an integer argument, and a readline() method that requires no
6605arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006606binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006607other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006608
6609Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6610which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006611generated by Python 2. If *fix_imports* is True, pickle will try to
6612map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006613*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006614instances pickled by Python 2; these default to 'ASCII' and 'strict',
6615respectively. The *encoding* can be 'bytes' to read these 8-bit
6616string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006617[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006618
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006619static int
Larry Hastings89964c42015-04-14 18:07:59 -04006620_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6621 int fix_imports, const char *encoding,
6622 const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00006623/*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006624{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006625 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006626
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006627 /* In case of multiple __init__() calls, clear previous content. */
6628 if (self->read != NULL)
6629 (void)Unpickler_clear(self);
6630
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006631 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006632 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006633
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006634 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006635 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006636
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006637 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006638 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006639 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006640
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006641 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006642 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6643 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006645 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006646 }
6647 else {
6648 self->pers_func = NULL;
6649 }
6650
6651 self->stack = (Pdata *)Pdata_New();
6652 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006653 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006654
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006655 self->memo_size = 32;
6656 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006657 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006658 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006659
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006660 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006661
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006662 return 0;
6663}
6664
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006665
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006666/* Define a proxy object for the Unpickler's internal memo object. This is to
6667 * avoid breaking code like:
6668 * unpickler.memo.clear()
6669 * and
6670 * unpickler.memo = saved_memo
6671 * Is this a good idea? Not really, but we don't want to break code that uses
6672 * it. Note that we don't implement the entire mapping API here. This is
6673 * intentional, as these should be treated as black-box implementation details.
6674 *
6675 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006676 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006677 */
6678
Larry Hastings61272b72014-01-07 12:41:53 -08006679/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006680_pickle.UnpicklerMemoProxy.clear
6681
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006682Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006683[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006684
Larry Hastings3cceb382014-01-04 11:09:09 -08006685static PyObject *
6686_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006687/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006688{
6689 _Unpickler_MemoCleanup(self->unpickler);
6690 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6691 if (self->unpickler->memo == NULL)
6692 return NULL;
6693 Py_RETURN_NONE;
6694}
6695
Larry Hastings61272b72014-01-07 12:41:53 -08006696/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006697_pickle.UnpicklerMemoProxy.copy
6698
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006699Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006700[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006701
Larry Hastings3cceb382014-01-04 11:09:09 -08006702static PyObject *
6703_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006704/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006705{
6706 Py_ssize_t i;
6707 PyObject *new_memo = PyDict_New();
6708 if (new_memo == NULL)
6709 return NULL;
6710
6711 for (i = 0; i < self->unpickler->memo_size; i++) {
6712 int status;
6713 PyObject *key, *value;
6714
6715 value = self->unpickler->memo[i];
6716 if (value == NULL)
6717 continue;
6718
6719 key = PyLong_FromSsize_t(i);
6720 if (key == NULL)
6721 goto error;
6722 status = PyDict_SetItem(new_memo, key, value);
6723 Py_DECREF(key);
6724 if (status < 0)
6725 goto error;
6726 }
6727 return new_memo;
6728
6729error:
6730 Py_DECREF(new_memo);
6731 return NULL;
6732}
6733
Larry Hastings61272b72014-01-07 12:41:53 -08006734/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006735_pickle.UnpicklerMemoProxy.__reduce__
6736
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006737Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006738[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006739
Larry Hastings3cceb382014-01-04 11:09:09 -08006740static PyObject *
6741_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006742/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006743{
6744 PyObject *reduce_value;
6745 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006746 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006747 if (contents == NULL)
6748 return NULL;
6749
6750 reduce_value = PyTuple_New(2);
6751 if (reduce_value == NULL) {
6752 Py_DECREF(contents);
6753 return NULL;
6754 }
6755 constructor_args = PyTuple_New(1);
6756 if (constructor_args == NULL) {
6757 Py_DECREF(contents);
6758 Py_DECREF(reduce_value);
6759 return NULL;
6760 }
6761 PyTuple_SET_ITEM(constructor_args, 0, contents);
6762 Py_INCREF((PyObject *)&PyDict_Type);
6763 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6764 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6765 return reduce_value;
6766}
6767
6768static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006769 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6770 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6771 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006772 {NULL, NULL} /* sentinel */
6773};
6774
6775static void
6776UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6777{
6778 PyObject_GC_UnTrack(self);
6779 Py_XDECREF(self->unpickler);
6780 PyObject_GC_Del((PyObject *)self);
6781}
6782
6783static int
6784UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6785 visitproc visit, void *arg)
6786{
6787 Py_VISIT(self->unpickler);
6788 return 0;
6789}
6790
6791static int
6792UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6793{
6794 Py_CLEAR(self->unpickler);
6795 return 0;
6796}
6797
6798static PyTypeObject UnpicklerMemoProxyType = {
6799 PyVarObject_HEAD_INIT(NULL, 0)
6800 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6801 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6802 0,
6803 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6804 0, /* tp_print */
6805 0, /* tp_getattr */
6806 0, /* tp_setattr */
6807 0, /* tp_compare */
6808 0, /* tp_repr */
6809 0, /* tp_as_number */
6810 0, /* tp_as_sequence */
6811 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006812 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006813 0, /* tp_call */
6814 0, /* tp_str */
6815 PyObject_GenericGetAttr, /* tp_getattro */
6816 PyObject_GenericSetAttr, /* tp_setattro */
6817 0, /* tp_as_buffer */
6818 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6819 0, /* tp_doc */
6820 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6821 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6822 0, /* tp_richcompare */
6823 0, /* tp_weaklistoffset */
6824 0, /* tp_iter */
6825 0, /* tp_iternext */
6826 unpicklerproxy_methods, /* tp_methods */
6827};
6828
6829static PyObject *
6830UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6831{
6832 UnpicklerMemoProxyObject *self;
6833
6834 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6835 &UnpicklerMemoProxyType);
6836 if (self == NULL)
6837 return NULL;
6838 Py_INCREF(unpickler);
6839 self->unpickler = unpickler;
6840 PyObject_GC_Track(self);
6841 return (PyObject *)self;
6842}
6843
6844/*****************************************************************************/
6845
6846
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006847static PyObject *
6848Unpickler_get_memo(UnpicklerObject *self)
6849{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006850 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006851}
6852
6853static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006854Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006855{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006856 PyObject **new_memo;
6857 Py_ssize_t new_memo_size = 0;
6858 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006859
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006861 PyErr_SetString(PyExc_TypeError,
6862 "attribute deletion is not supported");
6863 return -1;
6864 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006865
6866 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6867 UnpicklerObject *unpickler =
6868 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6869
6870 new_memo_size = unpickler->memo_size;
6871 new_memo = _Unpickler_NewMemo(new_memo_size);
6872 if (new_memo == NULL)
6873 return -1;
6874
6875 for (i = 0; i < new_memo_size; i++) {
6876 Py_XINCREF(unpickler->memo[i]);
6877 new_memo[i] = unpickler->memo[i];
6878 }
6879 }
6880 else if (PyDict_Check(obj)) {
6881 Py_ssize_t i = 0;
6882 PyObject *key, *value;
6883
6884 new_memo_size = PyDict_Size(obj);
6885 new_memo = _Unpickler_NewMemo(new_memo_size);
6886 if (new_memo == NULL)
6887 return -1;
6888
6889 while (PyDict_Next(obj, &i, &key, &value)) {
6890 Py_ssize_t idx;
6891 if (!PyLong_Check(key)) {
6892 PyErr_SetString(PyExc_TypeError,
6893 "memo key must be integers");
6894 goto error;
6895 }
6896 idx = PyLong_AsSsize_t(key);
6897 if (idx == -1 && PyErr_Occurred())
6898 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006899 if (idx < 0) {
6900 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006901 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006902 goto error;
6903 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006904 if (_Unpickler_MemoPut(self, idx, value) < 0)
6905 goto error;
6906 }
6907 }
6908 else {
6909 PyErr_Format(PyExc_TypeError,
6910 "'memo' attribute must be an UnpicklerMemoProxy object"
6911 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006912 return -1;
6913 }
6914
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006915 _Unpickler_MemoCleanup(self);
6916 self->memo_size = new_memo_size;
6917 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006918
6919 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006920
6921 error:
6922 if (new_memo_size) {
6923 i = new_memo_size;
6924 while (--i >= 0) {
6925 Py_XDECREF(new_memo[i]);
6926 }
6927 PyMem_FREE(new_memo);
6928 }
6929 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006930}
6931
6932static PyObject *
6933Unpickler_get_persload(UnpicklerObject *self)
6934{
6935 if (self->pers_func == NULL)
6936 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6937 else
6938 Py_INCREF(self->pers_func);
6939 return self->pers_func;
6940}
6941
6942static int
6943Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6944{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006945 if (value == NULL) {
6946 PyErr_SetString(PyExc_TypeError,
6947 "attribute deletion is not supported");
6948 return -1;
6949 }
6950 if (!PyCallable_Check(value)) {
6951 PyErr_SetString(PyExc_TypeError,
6952 "persistent_load must be a callable taking "
6953 "one argument");
6954 return -1;
6955 }
6956
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006957 Py_INCREF(value);
Serhiy Storchaka576f1322016-01-05 21:27:54 +02006958 Py_SETREF(self->pers_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006959
6960 return 0;
6961}
6962
6963static PyGetSetDef Unpickler_getsets[] = {
6964 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6965 {"persistent_load", (getter)Unpickler_get_persload,
6966 (setter)Unpickler_set_persload},
6967 {NULL}
6968};
6969
6970static PyTypeObject Unpickler_Type = {
6971 PyVarObject_HEAD_INIT(NULL, 0)
6972 "_pickle.Unpickler", /*tp_name*/
6973 sizeof(UnpicklerObject), /*tp_basicsize*/
6974 0, /*tp_itemsize*/
6975 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6976 0, /*tp_print*/
6977 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006978 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006979 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006980 0, /*tp_repr*/
6981 0, /*tp_as_number*/
6982 0, /*tp_as_sequence*/
6983 0, /*tp_as_mapping*/
6984 0, /*tp_hash*/
6985 0, /*tp_call*/
6986 0, /*tp_str*/
6987 0, /*tp_getattro*/
6988 0, /*tp_setattro*/
6989 0, /*tp_as_buffer*/
6990 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006991 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006992 (traverseproc)Unpickler_traverse, /*tp_traverse*/
6993 (inquiry)Unpickler_clear, /*tp_clear*/
6994 0, /*tp_richcompare*/
6995 0, /*tp_weaklistoffset*/
6996 0, /*tp_iter*/
6997 0, /*tp_iternext*/
6998 Unpickler_methods, /*tp_methods*/
6999 0, /*tp_members*/
7000 Unpickler_getsets, /*tp_getset*/
7001 0, /*tp_base*/
7002 0, /*tp_dict*/
7003 0, /*tp_descr_get*/
7004 0, /*tp_descr_set*/
7005 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007006 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007007 PyType_GenericAlloc, /*tp_alloc*/
7008 PyType_GenericNew, /*tp_new*/
7009 PyObject_GC_Del, /*tp_free*/
7010 0, /*tp_is_gc*/
7011};
7012
Larry Hastings61272b72014-01-07 12:41:53 -08007013/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007014
7015_pickle.dump
7016
7017 obj: object
7018 file: object
7019 protocol: object = NULL
7020 *
7021 fix_imports: bool = True
7022
7023Write a pickled representation of obj to the open file object file.
7024
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007025This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7026be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007027
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007028The optional *protocol* argument tells the pickler to use the given
7029protocol supported protocols are 0, 1, 2, 3 and 4. The default
7030protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007031
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007032Specifying a negative protocol version selects the highest protocol
7033version supported. The higher the protocol used, the more recent the
7034version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007035
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007036The *file* argument must have a write() method that accepts a single
7037bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007038writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007039this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007040
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007041If *fix_imports* is True and protocol is less than 3, pickle will try
7042to map the new Python 3 names to the old module names used in Python
70432, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007044[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007045
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007046static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007047_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7048 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00007049/*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007050{
7051 PicklerObject *pickler = _Pickler_New();
7052
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007053 if (pickler == NULL)
7054 return NULL;
7055
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007056 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007057 goto error;
7058
7059 if (_Pickler_SetOutputStream(pickler, file) < 0)
7060 goto error;
7061
7062 if (dump(pickler, obj) < 0)
7063 goto error;
7064
7065 if (_Pickler_FlushToFile(pickler) < 0)
7066 goto error;
7067
7068 Py_DECREF(pickler);
7069 Py_RETURN_NONE;
7070
7071 error:
7072 Py_XDECREF(pickler);
7073 return NULL;
7074}
7075
Larry Hastings61272b72014-01-07 12:41:53 -08007076/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007077
7078_pickle.dumps
7079
7080 obj: object
7081 protocol: object = NULL
7082 *
7083 fix_imports: bool = True
7084
7085Return the pickled representation of the object as a bytes object.
7086
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007087The optional *protocol* argument tells the pickler to use the given
7088protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7089protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007090
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007091Specifying a negative protocol version selects the highest protocol
7092version supported. The higher the protocol used, the more recent the
7093version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007094
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007095If *fix_imports* is True and *protocol* is less than 3, pickle will
7096try to map the new Python 3 names to the old module names used in
7097Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007098[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007099
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007100static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007101_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7102 int fix_imports)
7103/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007104{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007105 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007106 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007107
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007108 if (pickler == NULL)
7109 return NULL;
7110
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007111 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007112 goto error;
7113
7114 if (dump(pickler, obj) < 0)
7115 goto error;
7116
7117 result = _Pickler_GetString(pickler);
7118 Py_DECREF(pickler);
7119 return result;
7120
7121 error:
7122 Py_XDECREF(pickler);
7123 return NULL;
7124}
7125
Larry Hastings61272b72014-01-07 12:41:53 -08007126/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007127
7128_pickle.load
7129
7130 file: object
7131 *
7132 fix_imports: bool = True
7133 encoding: str = 'ASCII'
7134 errors: str = 'strict'
7135
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007136Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007137
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007138This is equivalent to ``Unpickler(file).load()``, but may be more
7139efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007140
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007141The protocol version of the pickle is detected automatically, so no
7142protocol argument is needed. Bytes past the pickled object's
7143representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007144
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007145The argument *file* must have two methods, a read() method that takes
7146an integer argument, and a readline() method that requires no
7147arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007148binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007149other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007150
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007151Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7152which are used to control compatiblity support for pickle stream
7153generated by Python 2. If *fix_imports* is True, pickle will try to
7154map the old Python 2 names to the new names used in Python 3. The
7155*encoding* and *errors* tell pickle how to decode 8-bit string
7156instances pickled by Python 2; these default to 'ASCII' and 'strict',
7157respectively. The *encoding* can be 'bytes' to read these 8-bit
7158string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007159[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007160
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007161static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007162_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7163 const char *encoding, const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00007164/*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007165{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007166 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007167 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007168
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007169 if (unpickler == NULL)
7170 return NULL;
7171
7172 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7173 goto error;
7174
7175 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7176 goto error;
7177
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007178 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007179
7180 result = load(unpickler);
7181 Py_DECREF(unpickler);
7182 return result;
7183
7184 error:
7185 Py_XDECREF(unpickler);
7186 return NULL;
7187}
7188
Larry Hastings61272b72014-01-07 12:41:53 -08007189/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007190
7191_pickle.loads
7192
7193 data: object
7194 *
7195 fix_imports: bool = True
7196 encoding: str = 'ASCII'
7197 errors: str = 'strict'
7198
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007199Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007200
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007201The protocol version of the pickle is detected automatically, so no
7202protocol argument is needed. Bytes past the pickled object's
7203representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007204
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007205Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7206which are used to control compatiblity support for pickle stream
7207generated by Python 2. If *fix_imports* is True, pickle will try to
7208map the old Python 2 names to the new names used in Python 3. The
7209*encoding* and *errors* tell pickle how to decode 8-bit string
7210instances pickled by Python 2; these default to 'ASCII' and 'strict',
7211respectively. The *encoding* can be 'bytes' to read these 8-bit
7212string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007213[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007214
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007215static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007216_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7217 const char *encoding, const char *errors)
7218/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007219{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007220 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007221 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007222
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007223 if (unpickler == NULL)
7224 return NULL;
7225
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007226 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007227 goto error;
7228
7229 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7230 goto error;
7231
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007232 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007233
7234 result = load(unpickler);
7235 Py_DECREF(unpickler);
7236 return result;
7237
7238 error:
7239 Py_XDECREF(unpickler);
7240 return NULL;
7241}
7242
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007243static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007244 _PICKLE_DUMP_METHODDEF
7245 _PICKLE_DUMPS_METHODDEF
7246 _PICKLE_LOAD_METHODDEF
7247 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007248 {NULL, NULL} /* sentinel */
7249};
7250
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007251static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007252pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007253{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007254 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007255 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007256}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007257
Stefan Krahf483b0f2013-12-14 13:43:10 +01007258static void
7259pickle_free(PyObject *m)
7260{
7261 _Pickle_ClearState(_Pickle_GetState(m));
7262}
7263
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007264static int
7265pickle_traverse(PyObject *m, visitproc visit, void *arg)
7266{
7267 PickleState *st = _Pickle_GetState(m);
7268 Py_VISIT(st->PickleError);
7269 Py_VISIT(st->PicklingError);
7270 Py_VISIT(st->UnpicklingError);
7271 Py_VISIT(st->dispatch_table);
7272 Py_VISIT(st->extension_registry);
7273 Py_VISIT(st->extension_cache);
7274 Py_VISIT(st->inverted_registry);
7275 Py_VISIT(st->name_mapping_2to3);
7276 Py_VISIT(st->import_mapping_2to3);
7277 Py_VISIT(st->name_mapping_3to2);
7278 Py_VISIT(st->import_mapping_3to2);
7279 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007280 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007281 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007282}
7283
7284static struct PyModuleDef _picklemodule = {
7285 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007286 "_pickle", /* m_name */
7287 pickle_module_doc, /* m_doc */
7288 sizeof(PickleState), /* m_size */
7289 pickle_methods, /* m_methods */
7290 NULL, /* m_reload */
7291 pickle_traverse, /* m_traverse */
7292 pickle_clear, /* m_clear */
7293 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007294};
7295
7296PyMODINIT_FUNC
7297PyInit__pickle(void)
7298{
7299 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007300 PickleState *st;
7301
7302 m = PyState_FindModule(&_picklemodule);
7303 if (m) {
7304 Py_INCREF(m);
7305 return m;
7306 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007307
7308 if (PyType_Ready(&Unpickler_Type) < 0)
7309 return NULL;
7310 if (PyType_Ready(&Pickler_Type) < 0)
7311 return NULL;
7312 if (PyType_Ready(&Pdata_Type) < 0)
7313 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007314 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7315 return NULL;
7316 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7317 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007318
7319 /* Create the module and add the functions. */
7320 m = PyModule_Create(&_picklemodule);
7321 if (m == NULL)
7322 return NULL;
7323
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007324 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007325 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7326 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007327 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007328 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7329 return NULL;
7330
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007331 st = _Pickle_GetState(m);
7332
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007333 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007334 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7335 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007336 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007337 st->PicklingError = \
7338 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7339 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007340 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007341 st->UnpicklingError = \
7342 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7343 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007344 return NULL;
7345
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007346 Py_INCREF(st->PickleError);
7347 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007348 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007349 Py_INCREF(st->PicklingError);
7350 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007351 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007352 Py_INCREF(st->UnpicklingError);
7353 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007354 return NULL;
7355
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007356 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007357 return NULL;
7358
7359 return m;
7360}