blob: b42f4f612f7e59e54f15e1576193a0c902e3acfe [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{
4497 PyObject *tmp;
4498
4499 if (value == NULL) {
4500 PyErr_SetString(PyExc_TypeError,
4501 "attribute deletion is not supported");
4502 return -1;
4503 }
4504 if (!PyCallable_Check(value)) {
4505 PyErr_SetString(PyExc_TypeError,
4506 "persistent_id must be a callable taking one argument");
4507 return -1;
4508 }
4509
4510 tmp = self->pers_func;
4511 Py_INCREF(value);
4512 self->pers_func = value;
4513 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
4514
4515 return 0;
4516}
4517
4518static PyMemberDef Pickler_members[] = {
4519 {"bin", T_INT, offsetof(PicklerObject, bin)},
4520 {"fast", T_INT, offsetof(PicklerObject, fast)},
Antoine Pitrou8d3c2902012-03-04 18:31:48 +01004521 {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)},
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004522 {NULL}
4523};
4524
4525static PyGetSetDef Pickler_getsets[] = {
4526 {"memo", (getter)Pickler_get_memo,
4527 (setter)Pickler_set_memo},
4528 {"persistent_id", (getter)Pickler_get_persid,
4529 (setter)Pickler_set_persid},
4530 {NULL}
4531};
4532
4533static PyTypeObject Pickler_Type = {
4534 PyVarObject_HEAD_INIT(NULL, 0)
4535 "_pickle.Pickler" , /*tp_name*/
4536 sizeof(PicklerObject), /*tp_basicsize*/
4537 0, /*tp_itemsize*/
4538 (destructor)Pickler_dealloc, /*tp_dealloc*/
4539 0, /*tp_print*/
4540 0, /*tp_getattr*/
4541 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00004542 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004543 0, /*tp_repr*/
4544 0, /*tp_as_number*/
4545 0, /*tp_as_sequence*/
4546 0, /*tp_as_mapping*/
4547 0, /*tp_hash*/
4548 0, /*tp_call*/
4549 0, /*tp_str*/
4550 0, /*tp_getattro*/
4551 0, /*tp_setattro*/
4552 0, /*tp_as_buffer*/
4553 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08004554 _pickle_Pickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004555 (traverseproc)Pickler_traverse, /*tp_traverse*/
4556 (inquiry)Pickler_clear, /*tp_clear*/
4557 0, /*tp_richcompare*/
4558 0, /*tp_weaklistoffset*/
4559 0, /*tp_iter*/
4560 0, /*tp_iternext*/
4561 Pickler_methods, /*tp_methods*/
4562 Pickler_members, /*tp_members*/
4563 Pickler_getsets, /*tp_getset*/
4564 0, /*tp_base*/
4565 0, /*tp_dict*/
4566 0, /*tp_descr_get*/
4567 0, /*tp_descr_set*/
4568 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08004569 _pickle_Pickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004570 PyType_GenericAlloc, /*tp_alloc*/
4571 PyType_GenericNew, /*tp_new*/
4572 PyObject_GC_Del, /*tp_free*/
4573 0, /*tp_is_gc*/
4574};
4575
Victor Stinner121aab42011-09-29 23:40:53 +02004576/* Temporary helper for calling self.find_class().
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004577
4578 XXX: It would be nice to able to avoid Python function call overhead, by
4579 using directly the C version of find_class(), when find_class() is not
4580 overridden by a subclass. Although, this could become rather hackish. A
4581 simpler optimization would be to call the C function when self is not a
4582 subclass instance. */
4583static PyObject *
4584find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name)
4585{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02004586 _Py_IDENTIFIER(find_class);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02004587
4588 return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO",
4589 module_name, global_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004590}
4591
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004592static Py_ssize_t
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004593marker(UnpicklerObject *self)
4594{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004595 Py_ssize_t mark;
4596
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004597 if (self->num_marks < 1) {
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004598 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004599 PyErr_SetString(st->UnpicklingError, "could not find MARK");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004600 return -1;
4601 }
4602
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02004603 mark = self->marks[--self->num_marks];
4604 self->stack->mark_set = self->num_marks != 0;
4605 self->stack->fence = self->num_marks ?
4606 self->marks[self->num_marks - 1] : 0;
4607 return mark;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004608}
4609
4610static int
4611load_none(UnpicklerObject *self)
4612{
4613 PDATA_APPEND(self->stack, Py_None, -1);
4614 return 0;
4615}
4616
4617static int
4618bad_readline(void)
4619{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004620 PickleState *st = _Pickle_GetGlobalState();
4621 PyErr_SetString(st->UnpicklingError, "pickle data was truncated");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004622 return -1;
4623}
4624
4625static int
4626load_int(UnpicklerObject *self)
4627{
4628 PyObject *value;
4629 char *endptr, *s;
4630 Py_ssize_t len;
4631 long x;
4632
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004633 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004634 return -1;
4635 if (len < 2)
4636 return bad_readline();
4637
4638 errno = 0;
Victor Stinner121aab42011-09-29 23:40:53 +02004639 /* XXX: Should the base argument of strtol() be explicitly set to 10?
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004640 XXX(avassalotti): Should this uses PyOS_strtol()? */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004641 x = strtol(s, &endptr, 0);
4642
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004643 if (errno || (*endptr != '\n' && *endptr != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004644 /* Hm, maybe we've got something long. Let's try reading
Serhiy Storchaka95949422013-08-27 19:40:23 +03004645 * it as a Python int object. */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004646 errno = 0;
4647 /* XXX: Same thing about the base here. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004648 value = PyLong_FromString(s, NULL, 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004649 if (value == NULL) {
4650 PyErr_SetString(PyExc_ValueError,
4651 "could not convert string to int");
4652 return -1;
4653 }
4654 }
4655 else {
4656 if (len == 3 && (x == 0 || x == 1)) {
4657 if ((value = PyBool_FromLong(x)) == NULL)
4658 return -1;
4659 }
4660 else {
4661 if ((value = PyLong_FromLong(x)) == NULL)
4662 return -1;
4663 }
4664 }
4665
4666 PDATA_PUSH(self->stack, value, -1);
4667 return 0;
4668}
4669
4670static int
4671load_bool(UnpicklerObject *self, PyObject *boolean)
4672{
4673 assert(boolean == Py_True || boolean == Py_False);
4674 PDATA_APPEND(self->stack, boolean, -1);
4675 return 0;
4676}
4677
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004678/* s contains x bytes of an unsigned little-endian integer. Return its value
4679 * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX.
4680 */
4681static Py_ssize_t
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004682calc_binsize(char *bytes, int nbytes)
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004683{
4684 unsigned char *s = (unsigned char *)bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004685 int i;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004686 size_t x = 0;
4687
Serhiy Storchakae0606192015-09-29 22:10:07 +03004688 if (nbytes > (int)sizeof(size_t)) {
4689 /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes
4690 * have 64-bit size that can't be represented on 32-bit platform.
4691 */
4692 for (i = (int)sizeof(size_t); i < nbytes; i++) {
4693 if (s[i])
4694 return -1;
4695 }
4696 nbytes = (int)sizeof(size_t);
4697 }
4698 for (i = 0; i < nbytes; i++) {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004699 x |= (size_t) s[i] << (8 * i);
4700 }
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004701
4702 if (x > PY_SSIZE_T_MAX)
4703 return -1;
4704 else
4705 return (Py_ssize_t) x;
4706}
4707
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004708/* s contains x bytes of a little-endian integer. Return its value as a
4709 * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian
4710 * int, but when x is 4 it's a signed one. This is an historical source
4711 * of x-platform bugs.
4712 */
4713static long
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004714calc_binint(char *bytes, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004715{
4716 unsigned char *s = (unsigned char *)bytes;
Victor Stinnerf13c46c2014-08-17 21:05:55 +02004717 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004718 long x = 0;
4719
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004720 for (i = 0; i < nbytes; i++) {
4721 x |= (long)s[i] << (8 * i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004722 }
4723
4724 /* Unlike BININT1 and BININT2, BININT (more accurately BININT4)
4725 * is signed, so on a box with longs bigger than 4 bytes we need
4726 * to extend a BININT's sign bit to the full width.
4727 */
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004728 if (SIZEOF_LONG > 4 && nbytes == 4) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004729 x |= -(x & (1L << 31));
4730 }
4731
4732 return x;
4733}
4734
4735static int
4736load_binintx(UnpicklerObject *self, char *s, int size)
4737{
4738 PyObject *value;
4739 long x;
4740
4741 x = calc_binint(s, size);
4742
4743 if ((value = PyLong_FromLong(x)) == NULL)
4744 return -1;
4745
4746 PDATA_PUSH(self->stack, value, -1);
4747 return 0;
4748}
4749
4750static int
4751load_binint(UnpicklerObject *self)
4752{
4753 char *s;
4754
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004755 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004756 return -1;
4757
4758 return load_binintx(self, s, 4);
4759}
4760
4761static int
4762load_binint1(UnpicklerObject *self)
4763{
4764 char *s;
4765
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004766 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004767 return -1;
4768
4769 return load_binintx(self, s, 1);
4770}
4771
4772static int
4773load_binint2(UnpicklerObject *self)
4774{
4775 char *s;
4776
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004777 if (_Unpickler_Read(self, &s, 2) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004778 return -1;
4779
4780 return load_binintx(self, s, 2);
4781}
4782
4783static int
4784load_long(UnpicklerObject *self)
4785{
4786 PyObject *value;
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004787 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004788 Py_ssize_t len;
4789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004790 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004791 return -1;
4792 if (len < 2)
4793 return bad_readline();
4794
Mark Dickinson8dd05142009-01-20 20:43:58 +00004795 /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove
4796 the 'L' before calling PyLong_FromString. In order to maintain
4797 compatibility with Python 3.0.0, we don't actually *require*
4798 the 'L' to be present. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004799 if (s[len-2] == 'L')
Alexandre Vassalotti446f7ff2009-01-23 04:43:46 +00004800 s[len-2] = '\0';
Alexandre Vassalottie4bccb72009-01-24 01:47:57 +00004801 /* XXX: Should the base argument explicitly set to 10? */
4802 value = PyLong_FromString(s, NULL, 0);
Mark Dickinson8dd05142009-01-20 20:43:58 +00004803 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004804 return -1;
4805
4806 PDATA_PUSH(self->stack, value, -1);
4807 return 0;
4808}
4809
4810/* 'size' bytes contain the # of bytes of little-endian 256's-complement
4811 * data following.
4812 */
4813static int
4814load_counted_long(UnpicklerObject *self, int size)
4815{
4816 PyObject *value;
4817 char *nbytes;
4818 char *pdata;
4819
4820 assert(size == 1 || size == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004821 if (_Unpickler_Read(self, &nbytes, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004822 return -1;
4823
4824 size = calc_binint(nbytes, size);
4825 if (size < 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004826 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004827 /* Corrupt or hostile pickle -- we never write one like this */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004828 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004829 "LONG pickle has negative byte count");
4830 return -1;
4831 }
4832
4833 if (size == 0)
4834 value = PyLong_FromLong(0L);
4835 else {
4836 /* Read the raw little-endian bytes and convert. */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004837 if (_Unpickler_Read(self, &pdata, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004838 return -1;
4839 value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size,
4840 1 /* little endian */ , 1 /* signed */ );
4841 }
4842 if (value == NULL)
4843 return -1;
4844 PDATA_PUSH(self->stack, value, -1);
4845 return 0;
4846}
4847
4848static int
4849load_float(UnpicklerObject *self)
4850{
4851 PyObject *value;
4852 char *endptr, *s;
4853 Py_ssize_t len;
4854 double d;
4855
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004856 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004857 return -1;
4858 if (len < 2)
4859 return bad_readline();
4860
4861 errno = 0;
Mark Dickinson725bfd82009-05-03 20:33:40 +00004862 d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError);
4863 if (d == -1.0 && PyErr_Occurred())
4864 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004865 if ((endptr[0] != '\n') && (endptr[0] != '\0')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004866 PyErr_SetString(PyExc_ValueError, "could not convert string to float");
4867 return -1;
4868 }
Mark Dickinson725bfd82009-05-03 20:33:40 +00004869 value = PyFloat_FromDouble(d);
4870 if (value == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004871 return -1;
4872
4873 PDATA_PUSH(self->stack, value, -1);
4874 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004875}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004876
4877static int
4878load_binfloat(UnpicklerObject *self)
4879{
4880 PyObject *value;
4881 double x;
4882 char *s;
4883
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004884 if (_Unpickler_Read(self, &s, 8) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004885 return -1;
4886
4887 x = _PyFloat_Unpack8((unsigned char *)s, 0);
4888 if (x == -1.0 && PyErr_Occurred())
4889 return -1;
4890
4891 if ((value = PyFloat_FromDouble(x)) == NULL)
4892 return -1;
4893
4894 PDATA_PUSH(self->stack, value, -1);
4895 return 0;
4896}
4897
4898static int
4899load_string(UnpicklerObject *self)
4900{
4901 PyObject *bytes;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004902 PyObject *obj;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004903 Py_ssize_t len;
4904 char *s, *p;
4905
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00004906 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004907 return -1;
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004908 /* Strip the newline */
4909 len--;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004910 /* Strip outermost quotes */
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004911 if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004912 p = s + 1;
4913 len -= 2;
4914 }
4915 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08004916 PickleState *st = _Pickle_GetGlobalState();
4917 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004918 "the STRING opcode argument must be quoted");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004919 return -1;
4920 }
Alexandre Vassalotti7c5e0942013-04-15 23:14:55 -07004921 assert(len >= 0);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004922
4923 /* Use the PyBytes API to decode the string, since that is what is used
4924 to encode, and then coerce the result to Unicode. */
4925 bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004926 if (bytes == NULL)
4927 return -1;
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004928
4929 /* Leave the Python 2.x strings as bytes if the *encoding* given to the
4930 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4931 if (strcmp(self->encoding, "bytes") == 0) {
4932 obj = bytes;
4933 }
4934 else {
4935 obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors);
4936 Py_DECREF(bytes);
4937 if (obj == NULL) {
4938 return -1;
4939 }
4940 }
4941
4942 PDATA_PUSH(self->stack, obj, -1);
4943 return 0;
4944}
4945
4946static int
4947load_counted_binstring(UnpicklerObject *self, int nbytes)
4948{
4949 PyObject *obj;
4950 Py_ssize_t size;
4951 char *s;
4952
4953 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004954 return -1;
4955
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08004956 size = calc_binsize(s, nbytes);
4957 if (size < 0) {
4958 PickleState *st = _Pickle_GetGlobalState();
4959 PyErr_Format(st->UnpicklingError,
4960 "BINSTRING exceeds system's maximum size of %zd bytes",
4961 PY_SSIZE_T_MAX);
4962 return -1;
4963 }
4964
4965 if (_Unpickler_Read(self, &s, size) < 0)
4966 return -1;
4967
4968 /* Convert Python 2.x strings to bytes if the *encoding* given to the
4969 Unpickler was 'bytes'. Otherwise, convert them to unicode. */
4970 if (strcmp(self->encoding, "bytes") == 0) {
4971 obj = PyBytes_FromStringAndSize(s, size);
4972 }
4973 else {
4974 obj = PyUnicode_Decode(s, size, self->encoding, self->errors);
4975 }
4976 if (obj == NULL) {
4977 return -1;
4978 }
4979
4980 PDATA_PUSH(self->stack, obj, -1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004981 return 0;
4982}
4983
4984static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004985load_counted_binbytes(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004986{
4987 PyObject *bytes;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004988 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004989 char *s;
4990
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004991 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004992 return -1;
4993
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01004994 size = calc_binsize(s, nbytes);
4995 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02004996 PyErr_Format(PyExc_OverflowError,
4997 "BINBYTES exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07004998 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00004999 return -1;
5000 }
5001
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005002 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005003 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005004
5005 bytes = PyBytes_FromStringAndSize(s, size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005006 if (bytes == NULL)
5007 return -1;
5008
5009 PDATA_PUSH(self->stack, bytes, -1);
5010 return 0;
5011}
5012
5013static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005014load_unicode(UnpicklerObject *self)
5015{
5016 PyObject *str;
5017 Py_ssize_t len;
5018 char *s;
5019
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005020 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005021 return -1;
5022 if (len < 1)
5023 return bad_readline();
5024
5025 str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL);
5026 if (str == NULL)
5027 return -1;
5028
5029 PDATA_PUSH(self->stack, str, -1);
5030 return 0;
5031}
5032
5033static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005034load_counted_binunicode(UnpicklerObject *self, int nbytes)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005035{
5036 PyObject *str;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005037 Py_ssize_t size;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005038 char *s;
5039
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005040 if (_Unpickler_Read(self, &s, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005041 return -1;
5042
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005043 size = calc_binsize(s, nbytes);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005044 if (size < 0) {
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005045 PyErr_Format(PyExc_OverflowError,
5046 "BINUNICODE exceeds system's maximum size of %zd bytes",
Alexandre Vassalotticc757172013-04-14 02:25:10 -07005047 PY_SSIZE_T_MAX);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005048 return -1;
5049 }
5050
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005051 if (_Unpickler_Read(self, &s, size) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005052 return -1;
5053
Victor Stinner485fb562010-04-13 11:07:24 +00005054 str = PyUnicode_DecodeUTF8(s, size, "surrogatepass");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005055 if (str == NULL)
5056 return -1;
5057
5058 PDATA_PUSH(self->stack, str, -1);
5059 return 0;
5060}
5061
5062static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005063load_counted_tuple(UnpicklerObject *self, int len)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005064{
5065 PyObject *tuple;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005066
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005067 if (Py_SIZE(self->stack) < len)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005068 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005069
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005070 tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005071 if (tuple == NULL)
5072 return -1;
5073 PDATA_PUSH(self->stack, tuple, -1);
5074 return 0;
5075}
5076
5077static int
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005078load_tuple(UnpicklerObject *self)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005079{
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005080 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005081
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005082 if ((i = marker(self)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005083 return -1;
5084
Serhiy Storchakaa49de6b2015-11-25 15:01:53 +02005085 return load_counted_tuple(self, Py_SIZE(self->stack) - i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005086}
5087
5088static int
5089load_empty_list(UnpicklerObject *self)
5090{
5091 PyObject *list;
5092
5093 if ((list = PyList_New(0)) == NULL)
5094 return -1;
5095 PDATA_PUSH(self->stack, list, -1);
5096 return 0;
5097}
5098
5099static int
5100load_empty_dict(UnpicklerObject *self)
5101{
5102 PyObject *dict;
5103
5104 if ((dict = PyDict_New()) == NULL)
5105 return -1;
5106 PDATA_PUSH(self->stack, dict, -1);
5107 return 0;
5108}
5109
5110static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005111load_empty_set(UnpicklerObject *self)
5112{
5113 PyObject *set;
5114
5115 if ((set = PySet_New(NULL)) == NULL)
5116 return -1;
5117 PDATA_PUSH(self->stack, set, -1);
5118 return 0;
5119}
5120
5121static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005122load_list(UnpicklerObject *self)
5123{
5124 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005125 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005126
5127 if ((i = marker(self)) < 0)
5128 return -1;
5129
5130 list = Pdata_poplist(self->stack, i);
5131 if (list == NULL)
5132 return -1;
5133 PDATA_PUSH(self->stack, list, -1);
5134 return 0;
5135}
5136
5137static int
5138load_dict(UnpicklerObject *self)
5139{
5140 PyObject *dict, *key, *value;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005141 Py_ssize_t i, j, k;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005142
5143 if ((i = marker(self)) < 0)
5144 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005145 j = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005146
5147 if ((dict = PyDict_New()) == NULL)
5148 return -1;
5149
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005150 if ((j - i) % 2 != 0) {
5151 PickleState *st = _Pickle_GetGlobalState();
5152 PyErr_SetString(st->UnpicklingError, "odd number of items for DICT");
Serhiy Storchaka3ac53802015-12-07 11:32:00 +02005153 Py_DECREF(dict);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005154 return -1;
5155 }
5156
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005157 for (k = i + 1; k < j; k += 2) {
5158 key = self->stack->data[k - 1];
5159 value = self->stack->data[k];
5160 if (PyDict_SetItem(dict, key, value) < 0) {
5161 Py_DECREF(dict);
5162 return -1;
5163 }
5164 }
5165 Pdata_clear(self->stack, i);
5166 PDATA_PUSH(self->stack, dict, -1);
5167 return 0;
5168}
5169
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005170static int
5171load_frozenset(UnpicklerObject *self)
5172{
5173 PyObject *items;
5174 PyObject *frozenset;
5175 Py_ssize_t i;
5176
5177 if ((i = marker(self)) < 0)
5178 return -1;
5179
5180 items = Pdata_poptuple(self->stack, i);
5181 if (items == NULL)
5182 return -1;
5183
5184 frozenset = PyFrozenSet_New(items);
5185 Py_DECREF(items);
5186 if (frozenset == NULL)
5187 return -1;
5188
5189 PDATA_PUSH(self->stack, frozenset, -1);
5190 return 0;
5191}
5192
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005193static PyObject *
5194instantiate(PyObject *cls, PyObject *args)
5195{
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005196 PyObject *result = NULL;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005197 _Py_IDENTIFIER(__getinitargs__);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005198 /* Caller must assure args are a tuple. Normally, args come from
5199 Pdata_poptuple which packs objects from the top of the stack
5200 into a newly created tuple. */
5201 assert(PyTuple_Check(args));
5202 if (Py_SIZE(args) > 0 || !PyType_Check(cls) ||
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005203 _PyObject_HasAttrId(cls, &PyId___getinitargs__)) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005204 result = PyObject_CallObject(cls, args);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005205 }
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005206 else {
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005207 _Py_IDENTIFIER(__new__);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02005208
5209 result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls);
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005210 }
5211 return result;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005212}
5213
5214static int
5215load_obj(UnpicklerObject *self)
5216{
5217 PyObject *cls, *args, *obj = NULL;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005218 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005219
5220 if ((i = marker(self)) < 0)
5221 return -1;
5222
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005223 if (Py_SIZE(self->stack) - i < 1)
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005224 return Pdata_stack_underflow(self->stack);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005225
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005226 args = Pdata_poptuple(self->stack, i + 1);
5227 if (args == NULL)
5228 return -1;
5229
5230 PDATA_POP(self->stack, cls);
5231 if (cls) {
5232 obj = instantiate(cls, args);
5233 Py_DECREF(cls);
5234 }
5235 Py_DECREF(args);
5236 if (obj == NULL)
5237 return -1;
5238
5239 PDATA_PUSH(self->stack, obj, -1);
5240 return 0;
5241}
5242
5243static int
5244load_inst(UnpicklerObject *self)
5245{
5246 PyObject *cls = NULL;
5247 PyObject *args = NULL;
5248 PyObject *obj = NULL;
5249 PyObject *module_name;
5250 PyObject *class_name;
5251 Py_ssize_t len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005252 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005253 char *s;
5254
5255 if ((i = marker(self)) < 0)
5256 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005257 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005258 return -1;
5259 if (len < 2)
5260 return bad_readline();
5261
5262 /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII
5263 identifiers are permitted in Python 3.0, since the INST opcode is only
5264 supported by older protocols on Python 2.x. */
5265 module_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
5266 if (module_name == NULL)
5267 return -1;
5268
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005269 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005270 if (len < 2) {
5271 Py_DECREF(module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005272 return bad_readline();
Serhiy Storchakaca28eba2015-12-01 00:18:23 +02005273 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005274 class_name = PyUnicode_DecodeASCII(s, len - 1, "strict");
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00005275 if (class_name != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005276 cls = find_class(self, module_name, class_name);
5277 Py_DECREF(class_name);
5278 }
5279 }
5280 Py_DECREF(module_name);
5281
5282 if (cls == NULL)
5283 return -1;
5284
5285 if ((args = Pdata_poptuple(self->stack, i)) != NULL) {
5286 obj = instantiate(cls, args);
5287 Py_DECREF(args);
5288 }
5289 Py_DECREF(cls);
5290
5291 if (obj == NULL)
5292 return -1;
5293
5294 PDATA_PUSH(self->stack, obj, -1);
5295 return 0;
5296}
5297
5298static int
5299load_newobj(UnpicklerObject *self)
5300{
5301 PyObject *args = NULL;
5302 PyObject *clsraw = NULL;
5303 PyTypeObject *cls; /* clsraw cast to its true type */
5304 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005305 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005306
5307 /* Stack is ... cls argtuple, and we want to call
5308 * cls.__new__(cls, *argtuple).
5309 */
5310 PDATA_POP(self->stack, args);
5311 if (args == NULL)
5312 goto error;
5313 if (!PyTuple_Check(args)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005314 PyErr_SetString(st->UnpicklingError,
5315 "NEWOBJ expected an arg " "tuple.");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005316 goto error;
5317 }
5318
5319 PDATA_POP(self->stack, clsraw);
5320 cls = (PyTypeObject *)clsraw;
5321 if (cls == NULL)
5322 goto error;
5323 if (!PyType_Check(cls)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005324 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005325 "isn't a type object");
5326 goto error;
5327 }
5328 if (cls->tp_new == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005329 PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument "
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005330 "has NULL tp_new");
5331 goto error;
5332 }
5333
5334 /* Call __new__. */
5335 obj = cls->tp_new(cls, args, NULL);
5336 if (obj == NULL)
5337 goto error;
5338
5339 Py_DECREF(args);
5340 Py_DECREF(clsraw);
5341 PDATA_PUSH(self->stack, obj, -1);
5342 return 0;
5343
5344 error:
5345 Py_XDECREF(args);
5346 Py_XDECREF(clsraw);
5347 return -1;
5348}
5349
5350static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005351load_newobj_ex(UnpicklerObject *self)
5352{
5353 PyObject *cls, *args, *kwargs;
5354 PyObject *obj;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005355 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005356
5357 PDATA_POP(self->stack, kwargs);
5358 if (kwargs == NULL) {
5359 return -1;
5360 }
5361 PDATA_POP(self->stack, args);
5362 if (args == NULL) {
5363 Py_DECREF(kwargs);
5364 return -1;
5365 }
5366 PDATA_POP(self->stack, cls);
5367 if (cls == NULL) {
5368 Py_DECREF(kwargs);
5369 Py_DECREF(args);
5370 return -1;
5371 }
Larry Hastings61272b72014-01-07 12:41:53 -08005372
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005373 if (!PyType_Check(cls)) {
5374 Py_DECREF(kwargs);
5375 Py_DECREF(args);
Larry Hastings61272b72014-01-07 12:41:53 -08005376 PyErr_Format(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005377 "NEWOBJ_EX class argument must be a type, not %.200s",
5378 Py_TYPE(cls)->tp_name);
Benjamin Peterson80f78a32015-07-02 16:18:38 -05005379 Py_DECREF(cls);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005380 return -1;
5381 }
5382
5383 if (((PyTypeObject *)cls)->tp_new == NULL) {
5384 Py_DECREF(kwargs);
5385 Py_DECREF(args);
5386 Py_DECREF(cls);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005387 PyErr_SetString(st->UnpicklingError,
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005388 "NEWOBJ_EX class argument doesn't have __new__");
5389 return -1;
5390 }
5391 obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
5392 Py_DECREF(kwargs);
5393 Py_DECREF(args);
5394 Py_DECREF(cls);
5395 if (obj == NULL) {
5396 return -1;
5397 }
5398 PDATA_PUSH(self->stack, obj, -1);
5399 return 0;
5400}
5401
5402static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005403load_global(UnpicklerObject *self)
5404{
5405 PyObject *global = NULL;
5406 PyObject *module_name;
5407 PyObject *global_name;
5408 Py_ssize_t len;
5409 char *s;
5410
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005411 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005412 return -1;
5413 if (len < 2)
5414 return bad_readline();
5415 module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5416 if (!module_name)
5417 return -1;
5418
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005419 if ((len = _Unpickler_Readline(self, &s)) >= 0) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005420 if (len < 2) {
5421 Py_DECREF(module_name);
5422 return bad_readline();
5423 }
5424 global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict");
5425 if (global_name) {
5426 global = find_class(self, module_name, global_name);
5427 Py_DECREF(global_name);
5428 }
5429 }
5430 Py_DECREF(module_name);
5431
5432 if (global == NULL)
5433 return -1;
5434 PDATA_PUSH(self->stack, global, -1);
5435 return 0;
5436}
5437
5438static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005439load_stack_global(UnpicklerObject *self)
5440{
5441 PyObject *global;
5442 PyObject *module_name;
5443 PyObject *global_name;
5444
5445 PDATA_POP(self->stack, global_name);
5446 PDATA_POP(self->stack, module_name);
5447 if (module_name == NULL || !PyUnicode_CheckExact(module_name) ||
5448 global_name == NULL || !PyUnicode_CheckExact(global_name)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005449 PickleState *st = _Pickle_GetGlobalState();
5450 PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str");
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005451 Py_XDECREF(global_name);
5452 Py_XDECREF(module_name);
5453 return -1;
5454 }
5455 global = find_class(self, module_name, global_name);
5456 Py_DECREF(global_name);
5457 Py_DECREF(module_name);
5458 if (global == NULL)
5459 return -1;
5460 PDATA_PUSH(self->stack, global, -1);
5461 return 0;
5462}
5463
5464static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005465load_persid(UnpicklerObject *self)
5466{
5467 PyObject *pid;
5468 Py_ssize_t len;
5469 char *s;
5470
5471 if (self->pers_func) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005472 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005473 return -1;
Alexandre Vassalotti896414f2013-11-30 13:52:35 -08005474 if (len < 1)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005475 return bad_readline();
5476
5477 pid = PyBytes_FromStringAndSize(s, len - 1);
5478 if (pid == NULL)
5479 return -1;
5480
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005481 /* This does not leak since _Pickle_FastCall() steals the reference
5482 to pid first. */
5483 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005484 if (pid == NULL)
5485 return -1;
5486
5487 PDATA_PUSH(self->stack, pid, -1);
5488 return 0;
5489 }
5490 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005491 PickleState *st = _Pickle_GetGlobalState();
5492 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005493 "A load persistent id instruction was encountered,\n"
5494 "but no persistent_load function was specified.");
5495 return -1;
5496 }
5497}
5498
5499static int
5500load_binpersid(UnpicklerObject *self)
5501{
5502 PyObject *pid;
5503
5504 if (self->pers_func) {
5505 PDATA_POP(self->stack, pid);
5506 if (pid == NULL)
5507 return -1;
5508
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005509 /* This does not leak since _Pickle_FastCall() steals the
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005510 reference to pid first. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005511 pid = _Pickle_FastCall(self->pers_func, pid);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005512 if (pid == NULL)
5513 return -1;
5514
5515 PDATA_PUSH(self->stack, pid, -1);
5516 return 0;
5517 }
5518 else {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005519 PickleState *st = _Pickle_GetGlobalState();
5520 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005521 "A load persistent id instruction was encountered,\n"
5522 "but no persistent_load function was specified.");
5523 return -1;
5524 }
5525}
5526
5527static int
5528load_pop(UnpicklerObject *self)
5529{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005530 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005531
5532 /* Note that we split the (pickle.py) stack into two stacks,
5533 * an object stack and a mark stack. We have to be clever and
5534 * pop the right one. We do this by looking at the top of the
Collin Winter8ca69de2009-05-26 16:53:41 +00005535 * mark stack first, and only signalling a stack underflow if
5536 * the object stack is empty and the mark stack doesn't match
5537 * our expectations.
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005538 */
Collin Winter8ca69de2009-05-26 16:53:41 +00005539 if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005540 self->num_marks--;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005541 self->stack->mark_set = self->num_marks != 0;
5542 self->stack->fence = self->num_marks ?
5543 self->marks[self->num_marks - 1] : 0;
5544 } else if (len <= self->stack->fence)
5545 return Pdata_stack_underflow(self->stack);
5546 else {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005547 len--;
5548 Py_DECREF(self->stack->data[len]);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005549 Py_SIZE(self->stack) = len;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005550 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005551 return 0;
5552}
5553
5554static int
5555load_pop_mark(UnpicklerObject *self)
5556{
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005557 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005558
5559 if ((i = marker(self)) < 0)
5560 return -1;
5561
5562 Pdata_clear(self->stack, i);
5563
5564 return 0;
5565}
5566
5567static int
5568load_dup(UnpicklerObject *self)
5569{
5570 PyObject *last;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005571 Py_ssize_t len = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005572
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005573 if (len <= self->stack->fence)
5574 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005575 last = self->stack->data[len - 1];
5576 PDATA_APPEND(self->stack, last, -1);
5577 return 0;
5578}
5579
5580static int
5581load_get(UnpicklerObject *self)
5582{
5583 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005584 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005585 Py_ssize_t len;
5586 char *s;
5587
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005588 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005589 return -1;
5590 if (len < 2)
5591 return bad_readline();
5592
5593 key = PyLong_FromString(s, NULL, 10);
5594 if (key == NULL)
5595 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005596 idx = PyLong_AsSsize_t(key);
5597 if (idx == -1 && PyErr_Occurred()) {
5598 Py_DECREF(key);
5599 return -1;
5600 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005601
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005602 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005603 if (value == NULL) {
5604 if (!PyErr_Occurred())
5605 PyErr_SetObject(PyExc_KeyError, key);
5606 Py_DECREF(key);
5607 return -1;
5608 }
5609 Py_DECREF(key);
5610
5611 PDATA_APPEND(self->stack, value, -1);
5612 return 0;
5613}
5614
5615static int
5616load_binget(UnpicklerObject *self)
5617{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005618 PyObject *value;
5619 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005620 char *s;
5621
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005622 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005623 return -1;
5624
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005625 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005626
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005627 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005628 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005629 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005630 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005631 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005632 Py_DECREF(key);
5633 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005634 return -1;
5635 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005636
5637 PDATA_APPEND(self->stack, value, -1);
5638 return 0;
5639}
5640
5641static int
5642load_long_binget(UnpicklerObject *self)
5643{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005644 PyObject *value;
5645 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005646 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005647
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005648 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005649 return -1;
5650
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005651 idx = calc_binsize(s, 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005652
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005653 value = _Unpickler_MemoGet(self, idx);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005654 if (value == NULL) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005655 PyObject *key = PyLong_FromSsize_t(idx);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005656 if (key != NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005657 PyErr_SetObject(PyExc_KeyError, key);
Christian Heimes9ee5c372013-07-26 22:45:00 +02005658 Py_DECREF(key);
5659 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005660 return -1;
5661 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005662
5663 PDATA_APPEND(self->stack, value, -1);
5664 return 0;
5665}
5666
5667/* Push an object from the extension registry (EXT[124]). nbytes is
5668 * the number of bytes following the opcode, holding the index (code) value.
5669 */
5670static int
5671load_extension(UnpicklerObject *self, int nbytes)
5672{
5673 char *codebytes; /* the nbytes bytes after the opcode */
5674 long code; /* calc_binint returns long */
5675 PyObject *py_code; /* code as a Python int */
5676 PyObject *obj; /* the object to push */
5677 PyObject *pair; /* (module_name, class_name) */
5678 PyObject *module_name, *class_name;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005679 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005680
5681 assert(nbytes == 1 || nbytes == 2 || nbytes == 4);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005682 if (_Unpickler_Read(self, &codebytes, nbytes) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005683 return -1;
5684 code = calc_binint(codebytes, nbytes);
5685 if (code <= 0) { /* note that 0 is forbidden */
5686 /* Corrupt or hostile pickle. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005687 PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005688 return -1;
5689 }
5690
5691 /* Look for the code in the cache. */
5692 py_code = PyLong_FromLong(code);
5693 if (py_code == NULL)
5694 return -1;
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005695 obj = PyDict_GetItemWithError(st->extension_cache, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005696 if (obj != NULL) {
5697 /* Bingo. */
5698 Py_DECREF(py_code);
5699 PDATA_APPEND(self->stack, obj, -1);
5700 return 0;
5701 }
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005702 if (PyErr_Occurred()) {
5703 Py_DECREF(py_code);
5704 return -1;
5705 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005706
5707 /* Look up the (module_name, class_name) pair. */
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005708 pair = PyDict_GetItemWithError(st->inverted_registry, py_code);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005709 if (pair == NULL) {
5710 Py_DECREF(py_code);
Alexandre Vassalotti567eba12013-11-28 17:09:16 -08005711 if (!PyErr_Occurred()) {
5712 PyErr_Format(PyExc_ValueError, "unregistered extension "
5713 "code %ld", code);
5714 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005715 return -1;
5716 }
5717 /* Since the extension registry is manipulable via Python code,
5718 * confirm that pair is really a 2-tuple of strings.
5719 */
5720 if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 ||
5721 !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) ||
5722 !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) {
5723 Py_DECREF(py_code);
5724 PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
5725 "isn't a 2-tuple of strings", code);
5726 return -1;
5727 }
5728 /* Load the object. */
5729 obj = find_class(self, module_name, class_name);
5730 if (obj == NULL) {
5731 Py_DECREF(py_code);
5732 return -1;
5733 }
5734 /* Cache code -> obj. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005735 code = PyDict_SetItem(st->extension_cache, py_code, obj);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005736 Py_DECREF(py_code);
5737 if (code < 0) {
5738 Py_DECREF(obj);
5739 return -1;
5740 }
5741 PDATA_PUSH(self->stack, obj, -1);
5742 return 0;
5743}
5744
5745static int
5746load_put(UnpicklerObject *self)
5747{
5748 PyObject *key, *value;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005749 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005750 Py_ssize_t len;
5751 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005752
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005753 if ((len = _Unpickler_Readline(self, &s)) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005754 return -1;
5755 if (len < 2)
5756 return bad_readline();
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005757 if (Py_SIZE(self->stack) <= self->stack->fence)
5758 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005759 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005760
5761 key = PyLong_FromString(s, NULL, 10);
5762 if (key == NULL)
5763 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005764 idx = PyLong_AsSsize_t(key);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005765 Py_DECREF(key);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005766 if (idx < 0) {
5767 if (!PyErr_Occurred())
5768 PyErr_SetString(PyExc_ValueError,
5769 "negative PUT argument");
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005770 return -1;
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005771 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005772
5773 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005774}
5775
5776static int
5777load_binput(UnpicklerObject *self)
5778{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005779 PyObject *value;
5780 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005781 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005782
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005783 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005784 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005785
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005786 if (Py_SIZE(self->stack) <= self->stack->fence)
5787 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005788 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005789
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005790 idx = Py_CHARMASK(s[0]);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005791
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005792 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005793}
5794
5795static int
5796load_long_binput(UnpicklerObject *self)
5797{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005798 PyObject *value;
5799 Py_ssize_t idx;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005800 char *s;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005801
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005802 if (_Unpickler_Read(self, &s, 4) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005803 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005804
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005805 if (Py_SIZE(self->stack) <= self->stack->fence)
5806 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005807 value = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005808
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005809 idx = calc_binsize(s, 4);
Antoine Pitrou55549ec2011-08-30 00:27:10 +02005810 if (idx < 0) {
5811 PyErr_SetString(PyExc_ValueError,
5812 "negative LONG_BINPUT argument");
5813 return -1;
5814 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005815
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005816 return _Unpickler_MemoPut(self, idx, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005817}
5818
5819static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005820load_memoize(UnpicklerObject *self)
5821{
5822 PyObject *value;
5823
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005824 if (Py_SIZE(self->stack) <= self->stack->fence)
5825 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005826 value = self->stack->data[Py_SIZE(self->stack) - 1];
5827
5828 return _Unpickler_MemoPut(self, self->memo_len, value);
5829}
5830
5831static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005832do_append(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005833{
5834 PyObject *value;
5835 PyObject *list;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005836 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005837
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005838 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005839 if (x > len || x <= self->stack->fence)
5840 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005841 if (len == x) /* nothing to do */
5842 return 0;
5843
5844 list = self->stack->data[x - 1];
5845
5846 if (PyList_Check(list)) {
5847 PyObject *slice;
5848 Py_ssize_t list_len;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005849 int ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005850
5851 slice = Pdata_poplist(self->stack, x);
5852 if (!slice)
5853 return -1;
5854 list_len = PyList_GET_SIZE(list);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005855 ret = PyList_SetSlice(list, list_len, list_len, slice);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005856 Py_DECREF(slice);
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005857 return ret;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005858 }
5859 else {
5860 PyObject *append_func;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02005861 _Py_IDENTIFIER(append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005862
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02005863 append_func = _PyObject_GetAttrId(list, &PyId_append);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005864 if (append_func == NULL)
5865 return -1;
5866 for (i = x; i < len; i++) {
5867 PyObject *result;
5868
5869 value = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005870 result = _Pickle_FastCall(append_func, value);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005871 if (result == NULL) {
5872 Pdata_clear(self->stack, i + 1);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005873 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005874 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005875 return -1;
5876 }
5877 Py_DECREF(result);
5878 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005879 Py_SIZE(self->stack) = x;
Alexandre Vassalotti637c7c42013-04-20 21:28:21 -07005880 Py_DECREF(append_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005881 }
5882
5883 return 0;
5884}
5885
5886static int
5887load_append(UnpicklerObject *self)
5888{
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005889 if (Py_SIZE(self->stack) - 1 <= self->stack->fence)
5890 return Pdata_stack_underflow(self->stack);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005891 return do_append(self, Py_SIZE(self->stack) - 1);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005892}
5893
5894static int
5895load_appends(UnpicklerObject *self)
5896{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005897 Py_ssize_t i = marker(self);
5898 if (i < 0)
5899 return -1;
5900 return do_append(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005901}
5902
5903static int
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005904do_setitems(UnpicklerObject *self, Py_ssize_t x)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005905{
5906 PyObject *value, *key;
5907 PyObject *dict;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02005908 Py_ssize_t len, i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005909 int status = 0;
5910
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005911 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005912 if (x > len || x <= self->stack->fence)
5913 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005914 if (len == x) /* nothing to do */
5915 return 0;
Victor Stinner121aab42011-09-29 23:40:53 +02005916 if ((len - x) % 2 != 0) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005917 PickleState *st = _Pickle_GetGlobalState();
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005918 /* Currupt or hostile pickle -- we never write one like this. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08005919 PyErr_SetString(st->UnpicklingError,
5920 "odd number of items for SETITEMS");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005921 return -1;
5922 }
5923
5924 /* Here, dict does not actually need to be a PyDict; it could be anything
5925 that supports the __setitem__ attribute. */
5926 dict = self->stack->data[x - 1];
5927
5928 for (i = x + 1; i < len; i += 2) {
5929 key = self->stack->data[i - 1];
5930 value = self->stack->data[i];
5931 if (PyObject_SetItem(dict, key, value) < 0) {
5932 status = -1;
5933 break;
5934 }
5935 }
5936
5937 Pdata_clear(self->stack, x);
5938 return status;
5939}
5940
5941static int
5942load_setitem(UnpicklerObject *self)
5943{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00005944 return do_setitems(self, Py_SIZE(self->stack) - 2);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005945}
5946
5947static int
5948load_setitems(UnpicklerObject *self)
5949{
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005950 Py_ssize_t i = marker(self);
5951 if (i < 0)
5952 return -1;
5953 return do_setitems(self, i);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00005954}
5955
5956static int
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005957load_additems(UnpicklerObject *self)
5958{
5959 PyObject *set;
5960 Py_ssize_t mark, len, i;
5961
5962 mark = marker(self);
Serhiy Storchakae9b30742015-11-23 15:17:43 +02005963 if (mark < 0)
5964 return -1;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005965 len = Py_SIZE(self->stack);
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02005966 if (mark > len || mark <= self->stack->fence)
5967 return Pdata_stack_underflow(self->stack);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005968 if (len == mark) /* nothing to do */
5969 return 0;
5970
5971 set = self->stack->data[mark - 1];
5972
5973 if (PySet_Check(set)) {
5974 PyObject *items;
5975 int status;
5976
5977 items = Pdata_poptuple(self->stack, mark);
5978 if (items == NULL)
5979 return -1;
5980
5981 status = _PySet_Update(set, items);
5982 Py_DECREF(items);
5983 return status;
5984 }
5985 else {
5986 PyObject *add_func;
5987 _Py_IDENTIFIER(add);
5988
5989 add_func = _PyObject_GetAttrId(set, &PyId_add);
5990 if (add_func == NULL)
5991 return -1;
5992 for (i = mark; i < len; i++) {
5993 PyObject *result;
5994 PyObject *item;
5995
5996 item = self->stack->data[i];
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08005997 result = _Pickle_FastCall(add_func, item);
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01005998 if (result == NULL) {
5999 Pdata_clear(self->stack, i + 1);
6000 Py_SIZE(self->stack) = mark;
6001 return -1;
6002 }
6003 Py_DECREF(result);
6004 }
6005 Py_SIZE(self->stack) = mark;
6006 }
6007
6008 return 0;
6009}
6010
6011static int
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006012load_build(UnpicklerObject *self)
6013{
6014 PyObject *state, *inst, *slotstate;
6015 PyObject *setstate;
6016 int status = 0;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006017 _Py_IDENTIFIER(__setstate__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006018
6019 /* Stack is ... instance, state. We want to leave instance at
6020 * the stack top, possibly mutated via instance.__setstate__(state).
6021 */
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006022 if (Py_SIZE(self->stack) - 2 < self->stack->fence)
6023 return Pdata_stack_underflow(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006024
6025 PDATA_POP(self->stack, state);
6026 if (state == NULL)
6027 return -1;
6028
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006029 inst = self->stack->data[Py_SIZE(self->stack) - 1];
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006030
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006031 setstate = _PyObject_GetAttrId(inst, &PyId___setstate__);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006032 if (setstate == NULL) {
6033 if (PyErr_ExceptionMatches(PyExc_AttributeError))
6034 PyErr_Clear();
Antoine Pitroud79dc622008-09-05 00:03:33 +00006035 else {
6036 Py_DECREF(state);
Alexandre Vassalotti1f9d9072008-08-15 03:07:47 +00006037 return -1;
Antoine Pitroud79dc622008-09-05 00:03:33 +00006038 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006039 }
6040 else {
6041 PyObject *result;
6042
6043 /* The explicit __setstate__ is responsible for everything. */
Alexandre Vassalotti20c28c12013-11-27 02:26:54 -08006044 result = _Pickle_FastCall(setstate, state);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006045 Py_DECREF(setstate);
6046 if (result == NULL)
6047 return -1;
6048 Py_DECREF(result);
6049 return 0;
6050 }
6051
6052 /* A default __setstate__. First see whether state embeds a
6053 * slot state dict too (a proto 2 addition).
6054 */
6055 if (PyTuple_Check(state) && Py_SIZE(state) == 2) {
6056 PyObject *tmp = state;
6057
6058 state = PyTuple_GET_ITEM(tmp, 0);
6059 slotstate = PyTuple_GET_ITEM(tmp, 1);
6060 Py_INCREF(state);
6061 Py_INCREF(slotstate);
6062 Py_DECREF(tmp);
6063 }
6064 else
6065 slotstate = NULL;
6066
6067 /* Set inst.__dict__ from the state dict (if any). */
6068 if (state != Py_None) {
6069 PyObject *dict;
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006070 PyObject *d_key, *d_value;
6071 Py_ssize_t i;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006072 _Py_IDENTIFIER(__dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006073
6074 if (!PyDict_Check(state)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006075 PickleState *st = _Pickle_GetGlobalState();
6076 PyErr_SetString(st->UnpicklingError, "state is not a dictionary");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006077 goto error;
6078 }
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006079 dict = _PyObject_GetAttrId(inst, &PyId___dict__);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006080 if (dict == NULL)
6081 goto error;
6082
Antoine Pitroua9f48a02009-05-02 21:41:14 +00006083 i = 0;
6084 while (PyDict_Next(state, &i, &d_key, &d_value)) {
6085 /* normally the keys for instance attributes are
6086 interned. we should try to do that here. */
6087 Py_INCREF(d_key);
6088 if (PyUnicode_CheckExact(d_key))
6089 PyUnicode_InternInPlace(&d_key);
6090 if (PyObject_SetItem(dict, d_key, d_value) < 0) {
6091 Py_DECREF(d_key);
6092 goto error;
6093 }
6094 Py_DECREF(d_key);
6095 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006096 Py_DECREF(dict);
6097 }
6098
6099 /* Also set instance attributes from the slotstate dict (if any). */
6100 if (slotstate != NULL) {
6101 PyObject *d_key, *d_value;
6102 Py_ssize_t i;
6103
6104 if (!PyDict_Check(slotstate)) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006105 PickleState *st = _Pickle_GetGlobalState();
6106 PyErr_SetString(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006107 "slot state is not a dictionary");
6108 goto error;
6109 }
6110 i = 0;
6111 while (PyDict_Next(slotstate, &i, &d_key, &d_value)) {
6112 if (PyObject_SetAttr(inst, d_key, d_value) < 0)
6113 goto error;
6114 }
6115 }
6116
6117 if (0) {
6118 error:
6119 status = -1;
6120 }
6121
6122 Py_DECREF(state);
6123 Py_XDECREF(slotstate);
6124 return status;
6125}
6126
6127static int
6128load_mark(UnpicklerObject *self)
6129{
6130
6131 /* Note that we split the (pickle.py) stack into two stacks, an
6132 * object stack and a mark stack. Here we push a mark onto the
6133 * mark stack.
6134 */
6135
6136 if ((self->num_marks + 1) >= self->marks_size) {
6137 size_t alloc;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006138
6139 /* Use the size_t type to check for overflow. */
6140 alloc = ((size_t)self->num_marks << 1) + 20;
Antoine Pitrou82be19f2011-08-29 23:09:33 +02006141 if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) ||
Alexandre Vassalotti7634ff52008-06-13 02:16:06 +00006142 alloc <= ((size_t)self->num_marks + 1)) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006143 PyErr_NoMemory();
6144 return -1;
6145 }
6146
6147 if (self->marks == NULL)
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006148 self->marks = PyMem_NEW(Py_ssize_t, alloc);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006149 else
Benjamin Peterson59b08c12015-06-27 13:41:33 -05006150 PyMem_RESIZE(self->marks, Py_ssize_t, alloc);
6151 if (self->marks == NULL) {
6152 self->marks_size = 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006153 PyErr_NoMemory();
6154 return -1;
6155 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006156 self->marks_size = (Py_ssize_t)alloc;
6157 }
6158
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006159 self->stack->mark_set = 1;
6160 self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006161
6162 return 0;
6163}
6164
6165static int
6166load_reduce(UnpicklerObject *self)
6167{
6168 PyObject *callable = NULL;
6169 PyObject *argtup = NULL;
6170 PyObject *obj = NULL;
6171
6172 PDATA_POP(self->stack, argtup);
6173 if (argtup == NULL)
6174 return -1;
6175 PDATA_POP(self->stack, callable);
6176 if (callable) {
Alexander Belopolskyd92f0402010-07-17 22:50:45 +00006177 obj = PyObject_CallObject(callable, argtup);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006178 Py_DECREF(callable);
6179 }
6180 Py_DECREF(argtup);
6181
6182 if (obj == NULL)
6183 return -1;
6184
6185 PDATA_PUSH(self->stack, obj, -1);
6186 return 0;
6187}
6188
6189/* Just raises an error if we don't know the protocol specified. PROTO
6190 * is the first opcode for protocols >= 2.
6191 */
6192static int
6193load_proto(UnpicklerObject *self)
6194{
6195 char *s;
6196 int i;
6197
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006198 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006199 return -1;
6200
6201 i = (unsigned char)s[0];
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006202 if (i <= HIGHEST_PROTOCOL) {
6203 self->proto = i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006204 return 0;
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006205 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006206
6207 PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i);
6208 return -1;
6209}
6210
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006211static int
6212load_frame(UnpicklerObject *self)
6213{
6214 char *s;
6215 Py_ssize_t frame_len;
6216
6217 if (_Unpickler_Read(self, &s, 8) < 0)
6218 return -1;
6219
6220 frame_len = calc_binsize(s, 8);
6221 if (frame_len < 0) {
6222 PyErr_Format(PyExc_OverflowError,
6223 "FRAME length exceeds system's maximum of %zd bytes",
6224 PY_SSIZE_T_MAX);
6225 return -1;
6226 }
6227
6228 if (_Unpickler_Read(self, &s, frame_len) < 0)
6229 return -1;
6230
6231 /* Rewind to start of frame */
6232 self->next_read_idx -= frame_len;
6233 return 0;
6234}
6235
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006236static PyObject *
6237load(UnpicklerObject *self)
6238{
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006239 PyObject *value = NULL;
Christian Heimes27ea78b2014-01-27 01:03:53 +01006240 char *s = NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006241
6242 self->num_marks = 0;
Serhiy Storchaka59fb6342015-12-06 22:01:35 +02006243 self->stack->mark_set = 0;
6244 self->stack->fence = 0;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006245 self->proto = 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006246 if (Py_SIZE(self->stack))
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006247 Pdata_clear(self->stack, 0);
6248
6249 /* Convenient macros for the dispatch while-switch loop just below. */
6250#define OP(opcode, load_func) \
6251 case opcode: if (load_func(self) < 0) break; continue;
6252
6253#define OP_ARG(opcode, load_func, arg) \
6254 case opcode: if (load_func(self, (arg)) < 0) break; continue;
6255
6256 while (1) {
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006257 if (_Unpickler_Read(self, &s, 1) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006258 break;
6259
6260 switch ((enum opcode)s[0]) {
6261 OP(NONE, load_none)
6262 OP(BININT, load_binint)
6263 OP(BININT1, load_binint1)
6264 OP(BININT2, load_binint2)
6265 OP(INT, load_int)
6266 OP(LONG, load_long)
6267 OP_ARG(LONG1, load_counted_long, 1)
6268 OP_ARG(LONG4, load_counted_long, 4)
6269 OP(FLOAT, load_float)
6270 OP(BINFLOAT, load_binfloat)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006271 OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
6272 OP_ARG(BINBYTES, load_counted_binbytes, 4)
6273 OP_ARG(BINBYTES8, load_counted_binbytes, 8)
6274 OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
6275 OP_ARG(BINSTRING, load_counted_binstring, 4)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006276 OP(STRING, load_string)
6277 OP(UNICODE, load_unicode)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006278 OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1)
6279 OP_ARG(BINUNICODE, load_counted_binunicode, 4)
6280 OP_ARG(BINUNICODE8, load_counted_binunicode, 8)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006281 OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0)
6282 OP_ARG(TUPLE1, load_counted_tuple, 1)
6283 OP_ARG(TUPLE2, load_counted_tuple, 2)
6284 OP_ARG(TUPLE3, load_counted_tuple, 3)
6285 OP(TUPLE, load_tuple)
6286 OP(EMPTY_LIST, load_empty_list)
6287 OP(LIST, load_list)
6288 OP(EMPTY_DICT, load_empty_dict)
6289 OP(DICT, load_dict)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006290 OP(EMPTY_SET, load_empty_set)
6291 OP(ADDITEMS, load_additems)
6292 OP(FROZENSET, load_frozenset)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006293 OP(OBJ, load_obj)
6294 OP(INST, load_inst)
6295 OP(NEWOBJ, load_newobj)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006296 OP(NEWOBJ_EX, load_newobj_ex)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006297 OP(GLOBAL, load_global)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006298 OP(STACK_GLOBAL, load_stack_global)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006299 OP(APPEND, load_append)
6300 OP(APPENDS, load_appends)
6301 OP(BUILD, load_build)
6302 OP(DUP, load_dup)
6303 OP(BINGET, load_binget)
6304 OP(LONG_BINGET, load_long_binget)
6305 OP(GET, load_get)
6306 OP(MARK, load_mark)
6307 OP(BINPUT, load_binput)
6308 OP(LONG_BINPUT, load_long_binput)
6309 OP(PUT, load_put)
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006310 OP(MEMOIZE, load_memoize)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006311 OP(POP, load_pop)
6312 OP(POP_MARK, load_pop_mark)
6313 OP(SETITEM, load_setitem)
6314 OP(SETITEMS, load_setitems)
6315 OP(PERSID, load_persid)
6316 OP(BINPERSID, load_binpersid)
6317 OP(REDUCE, load_reduce)
6318 OP(PROTO, load_proto)
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006319 OP(FRAME, load_frame)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006320 OP_ARG(EXT1, load_extension, 1)
6321 OP_ARG(EXT2, load_extension, 2)
6322 OP_ARG(EXT4, load_extension, 4)
6323 OP_ARG(NEWTRUE, load_bool, Py_True)
6324 OP_ARG(NEWFALSE, load_bool, Py_False)
6325
6326 case STOP:
6327 break;
6328
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006329 default:
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006330 if (s[0] == '\0') {
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006331 PyErr_SetNone(PyExc_EOFError);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006332 }
6333 else {
6334 PickleState *st = _Pickle_GetGlobalState();
6335 PyErr_Format(st->UnpicklingError,
Benjamin Petersonadde86d2011-09-23 13:41:41 -04006336 "invalid load key, '%c'.", s[0]);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006337 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006338 return NULL;
6339 }
6340
6341 break; /* and we are done! */
6342 }
6343
Alexandre Vassalottib6a2f2a2013-11-23 20:30:03 -08006344 if (PyErr_Occurred()) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006345 return NULL;
6346 }
6347
Victor Stinner2ae57e32013-10-31 13:39:23 +01006348 if (_Unpickler_SkipConsumed(self) < 0)
6349 return NULL;
6350
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006351 PDATA_POP(self->stack, value);
6352 return value;
6353}
6354
Larry Hastings61272b72014-01-07 12:41:53 -08006355/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006356
6357_pickle.Unpickler.load
6358
6359Load a pickle.
6360
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006361Read a pickled object representation from the open file object given
6362in the constructor, and return the reconstituted object hierarchy
6363specified therein.
Larry Hastings61272b72014-01-07 12:41:53 -08006364[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006365
Larry Hastings3cceb382014-01-04 11:09:09 -08006366static PyObject *
Larry Hastingsc2047262014-01-25 20:43:29 -08006367_pickle_Unpickler_load_impl(UnpicklerObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006368/*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006369{
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006370 UnpicklerObject *unpickler = (UnpicklerObject*)self;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006371
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006372 /* Check whether the Unpickler was initialized correctly. This prevents
6373 segfaulting if a subclass overridden __init__ with a function that does
6374 not call Unpickler.__init__(). Here, we simply ensure that self->read
6375 is not NULL. */
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006376 if (unpickler->read == NULL) {
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006377 PickleState *st = _Pickle_GetGlobalState();
6378 PyErr_Format(st->UnpicklingError,
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006379 "Unpickler.__init__() was not called by %s.__init__()",
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006380 Py_TYPE(unpickler)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006381 return NULL;
6382 }
6383
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006384 return load(unpickler);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006385}
6386
6387/* The name of find_class() is misleading. In newer pickle protocols, this
6388 function is used for loading any global (i.e., functions), not just
6389 classes. The name is kept only for backward compatibility. */
6390
Larry Hastings61272b72014-01-07 12:41:53 -08006391/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006392
6393_pickle.Unpickler.find_class
6394
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006395 module_name: object
6396 global_name: object
6397 /
6398
6399Return an object from a specified module.
6400
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006401If necessary, the module will be imported. Subclasses may override
6402this method (e.g. to restrict unpickling of arbitrary classes and
6403functions).
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006404
6405This method is called whenever a class or a function object is
6406needed. Both arguments passed are str objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006407[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006408
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006409static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04006410_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
6411 PyObject *module_name,
6412 PyObject *global_name)
6413/*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006414{
6415 PyObject *global;
6416 PyObject *modules_dict;
6417 PyObject *module;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006418 _Py_IDENTIFIER(modules);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006419
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006420 /* Try to map the old names used in Python 2.x to the new ones used in
6421 Python 3.x. We do this only with old pickle protocols and when the
6422 user has not disabled the feature. */
6423 if (self->proto < 3 && self->fix_imports) {
6424 PyObject *key;
6425 PyObject *item;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006426 PickleState *st = _Pickle_GetGlobalState();
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006427
6428 /* Check if the global (i.e., a function or a class) was renamed
6429 or moved to another module. */
6430 key = PyTuple_Pack(2, module_name, global_name);
6431 if (key == NULL)
6432 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08006433 item = PyDict_GetItemWithError(st->name_mapping_2to3, key);
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006434 Py_DECREF(key);
6435 if (item) {
6436 if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
6437 PyErr_Format(PyExc_RuntimeError,
6438 "_compat_pickle.NAME_MAPPING values should be "
6439 "2-tuples, not %.200s", Py_TYPE(item)->tp_name);
6440 return NULL;
6441 }
6442 module_name = PyTuple_GET_ITEM(item, 0);
6443 global_name = PyTuple_GET_ITEM(item, 1);
6444 if (!PyUnicode_Check(module_name) ||
6445 !PyUnicode_Check(global_name)) {
6446 PyErr_Format(PyExc_RuntimeError,
6447 "_compat_pickle.NAME_MAPPING values should be "
6448 "pairs of str, not (%.200s, %.200s)",
6449 Py_TYPE(module_name)->tp_name,
6450 Py_TYPE(global_name)->tp_name);
6451 return NULL;
6452 }
6453 }
6454 else if (PyErr_Occurred()) {
6455 return NULL;
6456 }
Serhiy Storchakabfe18242015-03-31 13:12:37 +03006457 else {
6458 /* Check if the module was renamed. */
6459 item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name);
6460 if (item) {
6461 if (!PyUnicode_Check(item)) {
6462 PyErr_Format(PyExc_RuntimeError,
6463 "_compat_pickle.IMPORT_MAPPING values should be "
6464 "strings, not %.200s", Py_TYPE(item)->tp_name);
6465 return NULL;
6466 }
6467 module_name = item;
6468 }
6469 else if (PyErr_Occurred()) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006470 return NULL;
6471 }
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006472 }
6473 }
6474
Victor Stinnerbb520202013-11-06 22:40:41 +01006475 modules_dict = _PySys_GetObjectId(&PyId_modules);
Victor Stinner1e53bba2013-07-16 22:26:05 +02006476 if (modules_dict == NULL) {
6477 PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules");
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006478 return NULL;
Victor Stinner1e53bba2013-07-16 22:26:05 +02006479 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006480
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006481 module = PyDict_GetItemWithError(modules_dict, module_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006482 if (module == NULL) {
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006483 if (PyErr_Occurred())
6484 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006485 module = PyImport_Import(module_name);
6486 if (module == NULL)
6487 return NULL;
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006488 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006489 Py_DECREF(module);
6490 }
Victor Stinner121aab42011-09-29 23:40:53 +02006491 else {
Antoine Pitrouc9dc4a22013-11-23 18:59:12 +01006492 global = getattribute(module, global_name, self->proto >= 4);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006493 }
6494 return global;
6495}
6496
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006497/*[clinic input]
6498
6499_pickle.Unpickler.__sizeof__ -> Py_ssize_t
6500
6501Returns size in memory, in bytes.
6502[clinic start generated code]*/
6503
6504static Py_ssize_t
6505_pickle_Unpickler___sizeof___impl(UnpicklerObject *self)
6506/*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/
6507{
6508 Py_ssize_t res;
6509
Serhiy Storchaka5c4064e2015-12-19 20:05:25 +02006510 res = _PyObject_SIZE(Py_TYPE(self));
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006511 if (self->memo != NULL)
6512 res += self->memo_size * sizeof(PyObject *);
6513 if (self->marks != NULL)
6514 res += self->marks_size * sizeof(Py_ssize_t);
6515 if (self->input_line != NULL)
6516 res += strlen(self->input_line) + 1;
6517 if (self->encoding != NULL)
6518 res += strlen(self->encoding) + 1;
6519 if (self->errors != NULL)
6520 res += strlen(self->errors) + 1;
6521 return res;
6522}
6523
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006524static struct PyMethodDef Unpickler_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006525 _PICKLE_UNPICKLER_LOAD_METHODDEF
6526 _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +02006527 _PICKLE_UNPICKLER___SIZEOF___METHODDEF
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006528 {NULL, NULL} /* sentinel */
6529};
6530
6531static void
6532Unpickler_dealloc(UnpicklerObject *self)
6533{
6534 PyObject_GC_UnTrack((PyObject *)self);
6535 Py_XDECREF(self->readline);
6536 Py_XDECREF(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006537 Py_XDECREF(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006538 Py_XDECREF(self->stack);
6539 Py_XDECREF(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006540 if (self->buffer.buf != NULL) {
6541 PyBuffer_Release(&self->buffer);
6542 self->buffer.buf = NULL;
6543 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006544
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006545 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006546 PyMem_Free(self->marks);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006547 PyMem_Free(self->input_line);
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006548 PyMem_Free(self->encoding);
6549 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006550
6551 Py_TYPE(self)->tp_free((PyObject *)self);
6552}
6553
6554static int
6555Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
6556{
6557 Py_VISIT(self->readline);
6558 Py_VISIT(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006559 Py_VISIT(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006560 Py_VISIT(self->stack);
6561 Py_VISIT(self->pers_func);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006562 return 0;
6563}
6564
6565static int
6566Unpickler_clear(UnpicklerObject *self)
6567{
6568 Py_CLEAR(self->readline);
6569 Py_CLEAR(self->read);
Antoine Pitrou04248a82010-10-12 20:51:21 +00006570 Py_CLEAR(self->peek);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006571 Py_CLEAR(self->stack);
6572 Py_CLEAR(self->pers_func);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006573 if (self->buffer.buf != NULL) {
6574 PyBuffer_Release(&self->buffer);
6575 self->buffer.buf = NULL;
6576 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006577
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006578 _Unpickler_MemoCleanup(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006579 PyMem_Free(self->marks);
6580 self->marks = NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006581 PyMem_Free(self->input_line);
6582 self->input_line = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006583 PyMem_Free(self->encoding);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006584 self->encoding = NULL;
Victor Stinner49fc8ec2013-07-07 23:30:24 +02006585 PyMem_Free(self->errors);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006586 self->errors = NULL;
6587
6588 return 0;
6589}
6590
Larry Hastings61272b72014-01-07 12:41:53 -08006591/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006592
6593_pickle.Unpickler.__init__
6594
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006595 file: object
6596 *
6597 fix_imports: bool = True
6598 encoding: str = 'ASCII'
6599 errors: str = 'strict'
6600
6601This takes a binary file for reading a pickle data stream.
6602
6603The protocol version of the pickle is detected automatically, so no
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006604protocol argument is needed. Bytes past the pickled object's
6605representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006606
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006607The argument *file* must have two methods, a read() method that takes
6608an integer argument, and a readline() method that requires no
6609arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00006610binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006611other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006612
6613Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
6614which are used to control compatiblity support for pickle stream
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006615generated by Python 2. If *fix_imports* is True, pickle will try to
6616map the old Python 2 names to the new names used in Python 3. The
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006617*encoding* and *errors* tell pickle how to decode 8-bit string
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08006618instances pickled by Python 2; these default to 'ASCII' and 'strict',
6619respectively. The *encoding* can be 'bytes' to read these 8-bit
6620string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08006621[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006622
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006623static int
Larry Hastings89964c42015-04-14 18:07:59 -04006624_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
6625 int fix_imports, const char *encoding,
6626 const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00006627/*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006628{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006629 _Py_IDENTIFIER(persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006630
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006631 /* In case of multiple __init__() calls, clear previous content. */
6632 if (self->read != NULL)
6633 (void)Unpickler_clear(self);
6634
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006635 if (_Unpickler_SetInputStream(self, file) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006636 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006637
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006638 if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006639 return -1;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006640
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006641 self->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006642 if (self->fix_imports == -1)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006643 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006644
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006645 if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006646 self->pers_func = _PyObject_GetAttrId((PyObject *)self,
6647 &PyId_persistent_load);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006648 if (self->pers_func == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006649 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006650 }
6651 else {
6652 self->pers_func = NULL;
6653 }
6654
6655 self->stack = (Pdata *)Pdata_New();
6656 if (self->stack == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006657 return 1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006658
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006659 self->memo_size = 32;
6660 self->memo = _Unpickler_NewMemo(self->memo_size);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006661 if (self->memo == NULL)
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006662 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006663
Antoine Pitroud9dfaa92009-06-04 20:32:06 +00006664 self->proto = 0;
Alexandre Vassalotti0e7aa8c2009-04-03 04:17:41 +00006665
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006666 return 0;
6667}
6668
Larry Hastingsb7ccb202014-01-18 23:50:21 -08006669
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006670/* Define a proxy object for the Unpickler's internal memo object. This is to
6671 * avoid breaking code like:
6672 * unpickler.memo.clear()
6673 * and
6674 * unpickler.memo = saved_memo
6675 * Is this a good idea? Not really, but we don't want to break code that uses
6676 * it. Note that we don't implement the entire mapping API here. This is
6677 * intentional, as these should be treated as black-box implementation details.
6678 *
6679 * We do, however, have to implement pickling/unpickling support because of
Victor Stinner121aab42011-09-29 23:40:53 +02006680 * real-world code like cvs2svn.
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006681 */
6682
Larry Hastings61272b72014-01-07 12:41:53 -08006683/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006684_pickle.UnpicklerMemoProxy.clear
6685
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006686Remove all items from memo.
Larry Hastings61272b72014-01-07 12:41:53 -08006687[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006688
Larry Hastings3cceb382014-01-04 11:09:09 -08006689static PyObject *
6690_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006691/*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006692{
6693 _Unpickler_MemoCleanup(self->unpickler);
6694 self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size);
6695 if (self->unpickler->memo == NULL)
6696 return NULL;
6697 Py_RETURN_NONE;
6698}
6699
Larry Hastings61272b72014-01-07 12:41:53 -08006700/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006701_pickle.UnpicklerMemoProxy.copy
6702
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006703Copy the memo to a new object.
Larry Hastings61272b72014-01-07 12:41:53 -08006704[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006705
Larry Hastings3cceb382014-01-04 11:09:09 -08006706static PyObject *
6707_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006708/*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006709{
6710 Py_ssize_t i;
6711 PyObject *new_memo = PyDict_New();
6712 if (new_memo == NULL)
6713 return NULL;
6714
6715 for (i = 0; i < self->unpickler->memo_size; i++) {
6716 int status;
6717 PyObject *key, *value;
6718
6719 value = self->unpickler->memo[i];
6720 if (value == NULL)
6721 continue;
6722
6723 key = PyLong_FromSsize_t(i);
6724 if (key == NULL)
6725 goto error;
6726 status = PyDict_SetItem(new_memo, key, value);
6727 Py_DECREF(key);
6728 if (status < 0)
6729 goto error;
6730 }
6731 return new_memo;
6732
6733error:
6734 Py_DECREF(new_memo);
6735 return NULL;
6736}
6737
Larry Hastings61272b72014-01-07 12:41:53 -08006738/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006739_pickle.UnpicklerMemoProxy.__reduce__
6740
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006741Implement pickling support.
Larry Hastings61272b72014-01-07 12:41:53 -08006742[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006743
Larry Hastings3cceb382014-01-04 11:09:09 -08006744static PyObject *
6745_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
Larry Hastings581ee362014-01-28 05:00:08 -08006746/*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006747{
6748 PyObject *reduce_value;
6749 PyObject *constructor_args;
Larry Hastings3cceb382014-01-04 11:09:09 -08006750 PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self);
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006751 if (contents == NULL)
6752 return NULL;
6753
6754 reduce_value = PyTuple_New(2);
6755 if (reduce_value == NULL) {
6756 Py_DECREF(contents);
6757 return NULL;
6758 }
6759 constructor_args = PyTuple_New(1);
6760 if (constructor_args == NULL) {
6761 Py_DECREF(contents);
6762 Py_DECREF(reduce_value);
6763 return NULL;
6764 }
6765 PyTuple_SET_ITEM(constructor_args, 0, contents);
6766 Py_INCREF((PyObject *)&PyDict_Type);
6767 PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
6768 PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
6769 return reduce_value;
6770}
6771
6772static PyMethodDef unpicklerproxy_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006773 _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF
6774 _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF
6775 _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006776 {NULL, NULL} /* sentinel */
6777};
6778
6779static void
6780UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self)
6781{
6782 PyObject_GC_UnTrack(self);
6783 Py_XDECREF(self->unpickler);
6784 PyObject_GC_Del((PyObject *)self);
6785}
6786
6787static int
6788UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self,
6789 visitproc visit, void *arg)
6790{
6791 Py_VISIT(self->unpickler);
6792 return 0;
6793}
6794
6795static int
6796UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self)
6797{
6798 Py_CLEAR(self->unpickler);
6799 return 0;
6800}
6801
6802static PyTypeObject UnpicklerMemoProxyType = {
6803 PyVarObject_HEAD_INIT(NULL, 0)
6804 "_pickle.UnpicklerMemoProxy", /*tp_name*/
6805 sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
6806 0,
6807 (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
6808 0, /* tp_print */
6809 0, /* tp_getattr */
6810 0, /* tp_setattr */
6811 0, /* tp_compare */
6812 0, /* tp_repr */
6813 0, /* tp_as_number */
6814 0, /* tp_as_sequence */
6815 0, /* tp_as_mapping */
Georg Brandlf038b322010-10-18 07:35:09 +00006816 PyObject_HashNotImplemented, /* tp_hash */
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006817 0, /* tp_call */
6818 0, /* tp_str */
6819 PyObject_GenericGetAttr, /* tp_getattro */
6820 PyObject_GenericSetAttr, /* tp_setattro */
6821 0, /* tp_as_buffer */
6822 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
6823 0, /* tp_doc */
6824 (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */
6825 (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */
6826 0, /* tp_richcompare */
6827 0, /* tp_weaklistoffset */
6828 0, /* tp_iter */
6829 0, /* tp_iternext */
6830 unpicklerproxy_methods, /* tp_methods */
6831};
6832
6833static PyObject *
6834UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
6835{
6836 UnpicklerMemoProxyObject *self;
6837
6838 self = PyObject_GC_New(UnpicklerMemoProxyObject,
6839 &UnpicklerMemoProxyType);
6840 if (self == NULL)
6841 return NULL;
6842 Py_INCREF(unpickler);
6843 self->unpickler = unpickler;
6844 PyObject_GC_Track(self);
6845 return (PyObject *)self;
6846}
6847
6848/*****************************************************************************/
6849
6850
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006851static PyObject *
6852Unpickler_get_memo(UnpicklerObject *self)
6853{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006854 return UnpicklerMemoProxy_New(self);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006855}
6856
6857static int
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006858Unpickler_set_memo(UnpicklerObject *self, PyObject *obj)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006859{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006860 PyObject **new_memo;
6861 Py_ssize_t new_memo_size = 0;
6862 Py_ssize_t i;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006863
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006864 if (obj == NULL) {
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006865 PyErr_SetString(PyExc_TypeError,
6866 "attribute deletion is not supported");
6867 return -1;
6868 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006869
6870 if (Py_TYPE(obj) == &UnpicklerMemoProxyType) {
6871 UnpicklerObject *unpickler =
6872 ((UnpicklerMemoProxyObject *)obj)->unpickler;
6873
6874 new_memo_size = unpickler->memo_size;
6875 new_memo = _Unpickler_NewMemo(new_memo_size);
6876 if (new_memo == NULL)
6877 return -1;
6878
6879 for (i = 0; i < new_memo_size; i++) {
6880 Py_XINCREF(unpickler->memo[i]);
6881 new_memo[i] = unpickler->memo[i];
6882 }
6883 }
6884 else if (PyDict_Check(obj)) {
6885 Py_ssize_t i = 0;
6886 PyObject *key, *value;
6887
6888 new_memo_size = PyDict_Size(obj);
6889 new_memo = _Unpickler_NewMemo(new_memo_size);
6890 if (new_memo == NULL)
6891 return -1;
6892
6893 while (PyDict_Next(obj, &i, &key, &value)) {
6894 Py_ssize_t idx;
6895 if (!PyLong_Check(key)) {
6896 PyErr_SetString(PyExc_TypeError,
6897 "memo key must be integers");
6898 goto error;
6899 }
6900 idx = PyLong_AsSsize_t(key);
6901 if (idx == -1 && PyErr_Occurred())
6902 goto error;
Christian Heimesa24b4d22013-07-01 15:17:45 +02006903 if (idx < 0) {
6904 PyErr_SetString(PyExc_ValueError,
Christian Heimes80878792013-07-01 15:23:39 +02006905 "memo key must be positive integers.");
Christian Heimesa24b4d22013-07-01 15:17:45 +02006906 goto error;
6907 }
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006908 if (_Unpickler_MemoPut(self, idx, value) < 0)
6909 goto error;
6910 }
6911 }
6912 else {
6913 PyErr_Format(PyExc_TypeError,
6914 "'memo' attribute must be an UnpicklerMemoProxy object"
6915 "or dict, not %.200s", Py_TYPE(obj)->tp_name);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006916 return -1;
6917 }
6918
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006919 _Unpickler_MemoCleanup(self);
6920 self->memo_size = new_memo_size;
6921 self->memo = new_memo;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006922
6923 return 0;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006924
6925 error:
6926 if (new_memo_size) {
6927 i = new_memo_size;
6928 while (--i >= 0) {
6929 Py_XDECREF(new_memo[i]);
6930 }
6931 PyMem_FREE(new_memo);
6932 }
6933 return -1;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006934}
6935
6936static PyObject *
6937Unpickler_get_persload(UnpicklerObject *self)
6938{
6939 if (self->pers_func == NULL)
6940 PyErr_SetString(PyExc_AttributeError, "persistent_load");
6941 else
6942 Py_INCREF(self->pers_func);
6943 return self->pers_func;
6944}
6945
6946static int
6947Unpickler_set_persload(UnpicklerObject *self, PyObject *value)
6948{
6949 PyObject *tmp;
6950
6951 if (value == NULL) {
6952 PyErr_SetString(PyExc_TypeError,
6953 "attribute deletion is not supported");
6954 return -1;
6955 }
6956 if (!PyCallable_Check(value)) {
6957 PyErr_SetString(PyExc_TypeError,
6958 "persistent_load must be a callable taking "
6959 "one argument");
6960 return -1;
6961 }
6962
6963 tmp = self->pers_func;
6964 Py_INCREF(value);
6965 self->pers_func = value;
6966 Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */
6967
6968 return 0;
6969}
6970
6971static PyGetSetDef Unpickler_getsets[] = {
6972 {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo},
6973 {"persistent_load", (getter)Unpickler_get_persload,
6974 (setter)Unpickler_set_persload},
6975 {NULL}
6976};
6977
6978static PyTypeObject Unpickler_Type = {
6979 PyVarObject_HEAD_INIT(NULL, 0)
6980 "_pickle.Unpickler", /*tp_name*/
6981 sizeof(UnpicklerObject), /*tp_basicsize*/
6982 0, /*tp_itemsize*/
6983 (destructor)Unpickler_dealloc, /*tp_dealloc*/
6984 0, /*tp_print*/
6985 0, /*tp_getattr*/
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00006986 0, /*tp_setattr*/
Mark Dickinsone94c6792009-02-02 20:36:42 +00006987 0, /*tp_reserved*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00006988 0, /*tp_repr*/
6989 0, /*tp_as_number*/
6990 0, /*tp_as_sequence*/
6991 0, /*tp_as_mapping*/
6992 0, /*tp_hash*/
6993 0, /*tp_call*/
6994 0, /*tp_str*/
6995 0, /*tp_getattro*/
6996 0, /*tp_setattro*/
6997 0, /*tp_as_buffer*/
6998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08006999 _pickle_Unpickler___init____doc__, /*tp_doc*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007000 (traverseproc)Unpickler_traverse, /*tp_traverse*/
7001 (inquiry)Unpickler_clear, /*tp_clear*/
7002 0, /*tp_richcompare*/
7003 0, /*tp_weaklistoffset*/
7004 0, /*tp_iter*/
7005 0, /*tp_iternext*/
7006 Unpickler_methods, /*tp_methods*/
7007 0, /*tp_members*/
7008 Unpickler_getsets, /*tp_getset*/
7009 0, /*tp_base*/
7010 0, /*tp_dict*/
7011 0, /*tp_descr_get*/
7012 0, /*tp_descr_set*/
7013 0, /*tp_dictoffset*/
Larry Hastingsb7ccb202014-01-18 23:50:21 -08007014 _pickle_Unpickler___init__, /*tp_init*/
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007015 PyType_GenericAlloc, /*tp_alloc*/
7016 PyType_GenericNew, /*tp_new*/
7017 PyObject_GC_Del, /*tp_free*/
7018 0, /*tp_is_gc*/
7019};
7020
Larry Hastings61272b72014-01-07 12:41:53 -08007021/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007022
7023_pickle.dump
7024
7025 obj: object
7026 file: object
7027 protocol: object = NULL
7028 *
7029 fix_imports: bool = True
7030
7031Write a pickled representation of obj to the open file object file.
7032
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007033This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
7034be more efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007035
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007036The optional *protocol* argument tells the pickler to use the given
7037protocol supported protocols are 0, 1, 2, 3 and 4. The default
7038protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007039
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007040Specifying a negative protocol version selects the highest protocol
7041version supported. The higher the protocol used, the more recent the
7042version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007043
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007044The *file* argument must have a write() method that accepts a single
7045bytes argument. It can thus be a file object opened for binary
Martin Panter7462b6492015-11-02 03:37:02 +00007046writing, an io.BytesIO instance, or any other custom object that meets
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007047this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007048
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007049If *fix_imports* is True and protocol is less than 3, pickle will try
7050to map the new Python 3 names to the old module names used in Python
70512, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007052[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007053
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007054static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007055_pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file,
7056 PyObject *protocol, int fix_imports)
Martin Panter2eb819f2015-11-02 04:04:57 +00007057/*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007058{
7059 PicklerObject *pickler = _Pickler_New();
7060
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007061 if (pickler == NULL)
7062 return NULL;
7063
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007064 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007065 goto error;
7066
7067 if (_Pickler_SetOutputStream(pickler, file) < 0)
7068 goto error;
7069
7070 if (dump(pickler, obj) < 0)
7071 goto error;
7072
7073 if (_Pickler_FlushToFile(pickler) < 0)
7074 goto error;
7075
7076 Py_DECREF(pickler);
7077 Py_RETURN_NONE;
7078
7079 error:
7080 Py_XDECREF(pickler);
7081 return NULL;
7082}
7083
Larry Hastings61272b72014-01-07 12:41:53 -08007084/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007085
7086_pickle.dumps
7087
7088 obj: object
7089 protocol: object = NULL
7090 *
7091 fix_imports: bool = True
7092
7093Return the pickled representation of the object as a bytes object.
7094
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007095The optional *protocol* argument tells the pickler to use the given
7096protocol; supported protocols are 0, 1, 2, 3 and 4. The default
7097protocol is 3; a backward-incompatible protocol designed for Python 3.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007098
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007099Specifying a negative protocol version selects the highest protocol
7100version supported. The higher the protocol used, the more recent the
7101version of Python needed to read the pickle produced.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007102
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007103If *fix_imports* is True and *protocol* is less than 3, pickle will
7104try to map the new Python 3 names to the old module names used in
7105Python 2, so that the pickle data stream is readable with Python 2.
Larry Hastings61272b72014-01-07 12:41:53 -08007106[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007107
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007108static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007109_pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol,
7110 int fix_imports)
7111/*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007112{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007113 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007114 PicklerObject *pickler = _Pickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007115
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007116 if (pickler == NULL)
7117 return NULL;
7118
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007119 if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007120 goto error;
7121
7122 if (dump(pickler, obj) < 0)
7123 goto error;
7124
7125 result = _Pickler_GetString(pickler);
7126 Py_DECREF(pickler);
7127 return result;
7128
7129 error:
7130 Py_XDECREF(pickler);
7131 return NULL;
7132}
7133
Larry Hastings61272b72014-01-07 12:41:53 -08007134/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007135
7136_pickle.load
7137
7138 file: object
7139 *
7140 fix_imports: bool = True
7141 encoding: str = 'ASCII'
7142 errors: str = 'strict'
7143
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007144Read and return an object from the pickle data stored in a file.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007145
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007146This is equivalent to ``Unpickler(file).load()``, but may be more
7147efficient.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007148
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007149The protocol version of the pickle is detected automatically, so no
7150protocol argument is needed. Bytes past the pickled object's
7151representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007152
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007153The argument *file* must have two methods, a read() method that takes
7154an integer argument, and a readline() method that requires no
7155arguments. Both methods should return bytes. Thus *file* can be a
Martin Panter7462b6492015-11-02 03:37:02 +00007156binary file object opened for reading, an io.BytesIO object, or any
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007157other custom object that meets this interface.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007158
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007159Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7160which are used to control compatiblity support for pickle stream
7161generated by Python 2. If *fix_imports* is True, pickle will try to
7162map the old Python 2 names to the new names used in Python 3. The
7163*encoding* and *errors* tell pickle how to decode 8-bit string
7164instances pickled by Python 2; these default to 'ASCII' and 'strict',
7165respectively. The *encoding* can be 'bytes' to read these 8-bit
7166string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007167[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007168
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007169static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007170_pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports,
7171 const char *encoding, const char *errors)
Martin Panter2eb819f2015-11-02 04:04:57 +00007172/*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007173{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007174 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007175 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007176
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007177 if (unpickler == NULL)
7178 return NULL;
7179
7180 if (_Unpickler_SetInputStream(unpickler, file) < 0)
7181 goto error;
7182
7183 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7184 goto error;
7185
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007186 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007187
7188 result = load(unpickler);
7189 Py_DECREF(unpickler);
7190 return result;
7191
7192 error:
7193 Py_XDECREF(unpickler);
7194 return NULL;
7195}
7196
Larry Hastings61272b72014-01-07 12:41:53 -08007197/*[clinic input]
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007198
7199_pickle.loads
7200
7201 data: object
7202 *
7203 fix_imports: bool = True
7204 encoding: str = 'ASCII'
7205 errors: str = 'strict'
7206
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007207Read and return an object from the given pickle data.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007208
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007209The protocol version of the pickle is detected automatically, so no
7210protocol argument is needed. Bytes past the pickled object's
7211representation are ignored.
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007212
Alexandre Vassalottid05c9ff2013-12-07 01:09:27 -08007213Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
7214which are used to control compatiblity support for pickle stream
7215generated by Python 2. If *fix_imports* is True, pickle will try to
7216map the old Python 2 names to the new names used in Python 3. The
7217*encoding* and *errors* tell pickle how to decode 8-bit string
7218instances pickled by Python 2; these default to 'ASCII' and 'strict',
7219respectively. The *encoding* can be 'bytes' to read these 8-bit
7220string instances as bytes objects.
Larry Hastings61272b72014-01-07 12:41:53 -08007221[clinic start generated code]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007222
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007223static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -04007224_pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports,
7225 const char *encoding, const char *errors)
7226/*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007227{
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007228 PyObject *result;
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007229 UnpicklerObject *unpickler = _Unpickler_New();
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007230
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007231 if (unpickler == NULL)
7232 return NULL;
7233
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007234 if (_Unpickler_SetStringInput(unpickler, data) < 0)
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007235 goto error;
7236
7237 if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
7238 goto error;
7239
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007240 unpickler->fix_imports = fix_imports;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007241
7242 result = load(unpickler);
7243 Py_DECREF(unpickler);
7244 return result;
7245
7246 error:
7247 Py_XDECREF(unpickler);
7248 return NULL;
7249}
7250
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007251static struct PyMethodDef pickle_methods[] = {
Alexandre Vassalottied8c9062013-11-24 12:25:48 -08007252 _PICKLE_DUMP_METHODDEF
7253 _PICKLE_DUMPS_METHODDEF
7254 _PICKLE_LOAD_METHODDEF
7255 _PICKLE_LOADS_METHODDEF
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007256 {NULL, NULL} /* sentinel */
7257};
7258
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007259static int
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007260pickle_clear(PyObject *m)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007261{
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007262 _Pickle_ClearState(_Pickle_GetState(m));
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007263 return 0;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007264}
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007265
Stefan Krahf483b0f2013-12-14 13:43:10 +01007266static void
7267pickle_free(PyObject *m)
7268{
7269 _Pickle_ClearState(_Pickle_GetState(m));
7270}
7271
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007272static int
7273pickle_traverse(PyObject *m, visitproc visit, void *arg)
7274{
7275 PickleState *st = _Pickle_GetState(m);
7276 Py_VISIT(st->PickleError);
7277 Py_VISIT(st->PicklingError);
7278 Py_VISIT(st->UnpicklingError);
7279 Py_VISIT(st->dispatch_table);
7280 Py_VISIT(st->extension_registry);
7281 Py_VISIT(st->extension_cache);
7282 Py_VISIT(st->inverted_registry);
7283 Py_VISIT(st->name_mapping_2to3);
7284 Py_VISIT(st->import_mapping_2to3);
7285 Py_VISIT(st->name_mapping_3to2);
7286 Py_VISIT(st->import_mapping_3to2);
7287 Py_VISIT(st->codecs_encode);
Serhiy Storchaka58e41342015-03-31 14:07:24 +03007288 Py_VISIT(st->getattr);
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007289 return 0;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007290}
7291
7292static struct PyModuleDef _picklemodule = {
7293 PyModuleDef_HEAD_INIT,
Stefan Krahf483b0f2013-12-14 13:43:10 +01007294 "_pickle", /* m_name */
7295 pickle_module_doc, /* m_doc */
7296 sizeof(PickleState), /* m_size */
7297 pickle_methods, /* m_methods */
7298 NULL, /* m_reload */
7299 pickle_traverse, /* m_traverse */
7300 pickle_clear, /* m_clear */
7301 (freefunc)pickle_free /* m_free */
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007302};
7303
7304PyMODINIT_FUNC
7305PyInit__pickle(void)
7306{
7307 PyObject *m;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007308 PickleState *st;
7309
7310 m = PyState_FindModule(&_picklemodule);
7311 if (m) {
7312 Py_INCREF(m);
7313 return m;
7314 }
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007315
7316 if (PyType_Ready(&Unpickler_Type) < 0)
7317 return NULL;
7318 if (PyType_Ready(&Pickler_Type) < 0)
7319 return NULL;
7320 if (PyType_Ready(&Pdata_Type) < 0)
7321 return NULL;
Antoine Pitrouea99c5c2010-09-09 18:33:21 +00007322 if (PyType_Ready(&PicklerMemoProxyType) < 0)
7323 return NULL;
7324 if (PyType_Ready(&UnpicklerMemoProxyType) < 0)
7325 return NULL;
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007326
7327 /* Create the module and add the functions. */
7328 m = PyModule_Create(&_picklemodule);
7329 if (m == NULL)
7330 return NULL;
7331
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007332 Py_INCREF(&Pickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007333 if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0)
7334 return NULL;
Antoine Pitrou8391cf42011-07-15 21:01:21 +02007335 Py_INCREF(&Unpickler_Type);
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007336 if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0)
7337 return NULL;
7338
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007339 st = _Pickle_GetState(m);
7340
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007341 /* Initialize the exceptions. */
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007342 st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL);
7343 if (st->PickleError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007344 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007345 st->PicklingError = \
7346 PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL);
7347 if (st->PicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007348 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007349 st->UnpicklingError = \
7350 PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL);
7351 if (st->UnpicklingError == NULL)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007352 return NULL;
7353
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007354 Py_INCREF(st->PickleError);
7355 if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007356 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007357 Py_INCREF(st->PicklingError);
7358 if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007359 return NULL;
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007360 Py_INCREF(st->UnpicklingError);
7361 if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007362 return NULL;
7363
Alexandre Vassalotti23bdd832013-11-27 19:36:52 -08007364 if (_Pickle_InitState(st) < 0)
Alexandre Vassalottica2d6102008-06-12 18:26:05 +00007365 return NULL;
7366
7367 return m;
7368}